1
1
package br .com .virtuallibrary .commons .services ;
2
2
3
3
import java .io .Serializable ;
4
+ import java .lang .reflect .Field ;
4
5
import java .util .List ;
5
6
import java .util .Map ;
7
+ import java .util .Optional ;
8
+
9
+ import javax .validation .ValidationException ;
10
+
11
+ import org .springframework .beans .BeanUtils ;
6
12
7
13
import br .com .virtuallibrary .commons .entities .BaseEntity ;
8
14
import br .com .virtuallibrary .commons .repositories .BaseRepository ;
11
17
public class BaseServiceImpl <E extends BaseEntity , ID extends Serializable , R extends BaseRepository <E , ID >>
12
18
implements BaseService <E , ID , R > {
13
19
20
+ public static final String A_ENTIDADE_NAO_PODE_SER_NULA = "A entidade não pode ser nula." ;
14
21
private final R repository ;
15
22
private final Class <E > entityClass ;
16
23
@@ -19,48 +26,69 @@ public BaseServiceImpl(R repository) {
19
26
this .entityClass = GenericsUtils .getGenericsInfo (this ).getType (0 );
20
27
}
21
28
22
- public R getRepository () {
29
+ protected R getRepository () {
23
30
return repository ;
24
31
}
25
32
26
- public Class <E > getEntityClass () {
33
+ protected Class <E > getEntityClass () {
27
34
return entityClass ;
28
35
}
29
36
30
37
@ Override
31
38
public List <E > findAll () {
32
- // TODO Auto-generated method stub
33
- return null ;
39
+ return repository .findAll ();
34
40
}
35
41
36
42
@ Override
37
- public E findById (ID id ) {
38
- // TODO Auto-generated method stub
39
- return null ;
43
+ public Optional <E > findById (ID id ) {
44
+ return repository .findById (id );
40
45
}
41
46
42
47
@ Override
43
- public E create (E object ) {
44
- // TODO Auto-generated method stub
45
- return null ;
48
+ public Optional < E > save (E object ) {
49
+ Optional < E > opt = Optional . ofNullable ( repository . save ( object ));
50
+ return opt ;
46
51
}
47
52
48
53
@ Override
49
54
public void delete (ID id ) {
50
- // TODO Auto-generated method stub
51
-
55
+ repository .deleteById (id );
52
56
}
53
-
57
+
54
58
@ Override
55
- public E update (Map <String , String > updates , ID id ) {
56
- // TODO Auto-generated method stub
57
- return null ;
59
+ public Optional <E > update (Map <String , String > updates , ID id ) throws ValidationException , SecurityException , IllegalArgumentException , IllegalAccessException {
60
+ Optional <E > opt = repository .findById (id );
61
+ if (opt .isEmpty ()) {
62
+ return Optional .empty ();
63
+ }
64
+ E entity = opt .get ();
65
+ for (String fieldUpdate : updates .keySet ()) {
66
+ Field declaredField ;
67
+ try {
68
+ declaredField = entityClass .getDeclaredField (fieldUpdate );
69
+ } catch (NoSuchFieldException e ) {
70
+ throw new ValidationException (String .format ("O campo %s não existe." , fieldUpdate ));
71
+ }
72
+ boolean accessible = declaredField .canAccess (entity );
73
+ declaredField .setAccessible (true );
74
+ declaredField .set (entity , updates .get (fieldUpdate ));
75
+ declaredField .setAccessible (accessible );
76
+ }
77
+ return Optional .of (repository .save (entity ));
58
78
}
59
79
60
80
@ Override
61
- public E update (E object , ID id ) {
62
- // TODO Auto-generated method stub
63
- return null ;
81
+ public Optional <E > update (E object , ID id ) {
82
+ if (object == null ) {
83
+ throw new IllegalArgumentException (A_ENTIDADE_NAO_PODE_SER_NULA );
84
+ }
85
+ Optional <E > opt = repository .findById (id );
86
+ if (opt .isEmpty ()) {
87
+ return Optional .empty ();
88
+ }
89
+ BeanUtils .copyProperties (opt .get (), object );
90
+
91
+ return Optional .of (repository .save (object ));
64
92
}
65
93
66
94
}
0 commit comments