3
3
import com .softwarelab .dataextractor .core .persistence .entities .CommitEntity ;
4
4
import com .softwarelab .dataextractor .core .persistence .entities .LibraryEntity ;
5
5
import com .softwarelab .dataextractor .core .persistence .entities .ProjectEntity ;
6
+ import com .softwarelab .dataextractor .core .persistence .models .PagedData ;
6
7
import com .softwarelab .dataextractor .core .persistence .models .dtos .CommitModel ;
7
8
import com .softwarelab .dataextractor .core .persistence .repositories .CommitRepository ;
8
9
import com .softwarelab .dataextractor .core .persistence .repositories .LibraryRepository ;
10
+ import com .softwarelab .dataextractor .core .persistence .repositories .ProjectRepository ;
9
11
import com .softwarelab .dataextractor .core .services .usecases .CommitService ;
10
12
import com .softwarelab .dataextractor .core .utilities .DateTimeUtil ;
11
13
import lombok .AccessLevel ;
12
14
import lombok .AllArgsConstructor ;
13
15
import lombok .experimental .FieldDefaults ;
16
+ import org .springframework .data .domain .Page ;
17
+ import org .springframework .data .domain .PageRequest ;
18
+ import org .springframework .data .domain .Pageable ;
14
19
import org .springframework .stereotype .Service ;
15
20
16
- import java .util .ArrayList ;
17
- import java .util .List ;
21
+ import java .util .*;
18
22
import java .util .stream .Collectors ;
19
23
20
24
/**
@@ -28,6 +32,7 @@ public class CommitServiceImpl implements CommitService {
28
32
29
33
CommitRepository commitRepository ;
30
34
LibraryRepository libraryRepository ;
35
+ ProjectRepository projectRepository ;
31
36
32
37
@ Override
33
38
public CommitEntity saveCommit (CommitModel commitModel ,ProjectEntity projectEntity ) {
@@ -54,12 +59,93 @@ public void saveCommitAll(List<CommitModel> commitModels, ProjectEntity projectE
54
59
55
60
final CommitEntity commitEntity = saveCommit (commitModel ,projectEntity );
56
61
57
- libs .addAll (commitModel .getLibraries ().stream ()
62
+ Map <String ,Boolean > devLibs = getDeveloperLibs (commitModel .getDeveloperName (),projectEntity );
63
+
64
+ //exclude all libs the dev already have
65
+ libs .addAll (commitModel .getLibraries ()
66
+ .stream ()
67
+ .filter (lib -> devLibs .getOrDefault (lib ,true ))
58
68
.map (lib ->LibraryEntity .builder ()
59
69
.commit (commitEntity )
60
70
.library (lib )
61
71
.build ()).collect (Collectors .toList ()));
62
72
}
63
- libraryRepository .saveAll (libs );
73
+ if (!libs .isEmpty ())
74
+ libraryRepository .saveAll (libs );
75
+ }
76
+
77
+ @ Override
78
+ public PagedData <CommitModel > getCommits (Long projectId , int page , int size ) {
79
+ Optional <ProjectEntity > optionalProjectEntity = projectRepository .findById (projectId );
80
+ if (optionalProjectEntity .isEmpty ()){
81
+ return new PagedData <>(Collections .emptyList (),0 ,0 );
82
+ }
83
+ Pageable pageable = PageRequest .of (page ,size );
84
+ Page <CommitEntity > commitEntityPage = commitRepository .findAllByProject (optionalProjectEntity .get (),pageable );
85
+ List <CommitModel > commitModels = commitEntityPage .stream ().map (this ::convertEntityToModel ).collect (Collectors .toList ());
86
+ return new PagedData <>(commitModels ,commitEntityPage .getTotalElements (),commitEntityPage .getTotalPages ());
87
+ }
88
+
89
+ @ Override
90
+ public List <String > getDevelopers (Long projectId ) {
91
+ var optionalProjectEntity = projectRepository .findById (projectId );
92
+ if (optionalProjectEntity .isEmpty ())
93
+ return Collections .emptyList ();
94
+
95
+ return commitRepository .getDistinctDevelopers (optionalProjectEntity .get ());
96
+ }
97
+
98
+ private Map <String ,Boolean > getDeveloperLibs (String developerName , ProjectEntity project ){
99
+ Map <String ,Boolean > res = new HashMap <>();
100
+ List <CommitEntity > commitEntityList = commitRepository .findAllByDeveloperNameAndProject (developerName , project );
101
+ for (CommitEntity commitEntity : commitEntityList ) {
102
+ List <LibraryEntity > libraryEntities = libraryRepository .findAllByCommit (commitEntity );
103
+ for (LibraryEntity lib : libraryEntities ) {
104
+ res .put (lib .getLibrary (),false );
105
+ }
106
+ }
107
+ return res ;
64
108
}
109
+
110
+ @ Override
111
+ public Optional <String > getDeveloperLibraries (String developerName , Long projectId ) {
112
+ var optionalProjectEntity = projectRepository .findById (projectId );
113
+ if (optionalProjectEntity .isEmpty ())
114
+ return Optional .empty ();
115
+
116
+ StringBuilder sb = null ;
117
+ List <CommitEntity > commitEntityList = commitRepository .findAllByDeveloperNameAndProject (developerName ,optionalProjectEntity .get ());
118
+ for (CommitEntity commitEntity : commitEntityList ){
119
+ List <LibraryEntity > libraryEntities = libraryRepository .findAllByCommit (commitEntity );
120
+ for (LibraryEntity lib : libraryEntities ){
121
+ if (sb == null ){
122
+ sb = new StringBuilder ();
123
+ sb .append (lib .getLibrary ());
124
+ }else {
125
+ sb .append ("," );
126
+ sb .append (lib .getLibrary ());
127
+ }
128
+ }
129
+ }
130
+ if (sb == null )return Optional .empty ();
131
+ return Optional .of (sb .toString ());
132
+ }
133
+
134
+ private CommitModel convertEntityToModel (CommitEntity commitEntity ){
135
+ return CommitModel .builder ()
136
+ .commitId (commitEntity .getCommitId ())
137
+ .commitDate (DateTimeUtil .isoDateTime (commitEntity .getCommitDate ()))
138
+ .developerEmail (commitEntity .getDeveloperEmail ())
139
+ .developerName (commitEntity .getDeveloperName ())
140
+ .fileUrl (commitEntity .getFileUrl ())
141
+ .libraries (getLibraries (commitEntity ))
142
+ .build ();
143
+ }
144
+
145
+ private Set <String > getLibraries (CommitEntity commitEntity ){
146
+ return libraryRepository .findAllByCommit (commitEntity ).stream ().map (LibraryEntity ::getLibrary )
147
+ .collect (Collectors .toSet ());
148
+ }
149
+
150
+
65
151
}
0 commit comments