1
|
package eu.dnetlib.uoaadmintoolslibrary.services;
|
2
|
|
3
|
import eu.dnetlib.uoaadmintoolslibrary.dao.DivIdDAO;
|
4
|
import eu.dnetlib.uoaadmintoolslibrary.entities.DivId;
|
5
|
import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
|
6
|
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
|
7
|
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.DivHelpContentResponse;
|
8
|
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.DivIdResponse;
|
9
|
|
10
|
import org.apache.log4j.Logger;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.stereotype.Service;
|
13
|
|
14
|
import java.util.*;
|
15
|
|
16
|
@Service
|
17
|
public class DivIdService {
|
18
|
private final Logger log = Logger.getLogger(this.getClass());
|
19
|
|
20
|
@Autowired
|
21
|
private DivIdDAO divIdDAO;
|
22
|
|
23
|
@Autowired
|
24
|
private PageService pageService;
|
25
|
|
26
|
@Autowired
|
27
|
private PortalService portalService;
|
28
|
|
29
|
@Autowired
|
30
|
private DivHelpContentService divHelpContentService;
|
31
|
|
32
|
public List<DivId> getDivIds(String page, String name, String pid) {
|
33
|
Portal portal = portalService.getPortal(pid);
|
34
|
String portalType = portal.getType();
|
35
|
|
36
|
List<DivId> divIds = null;
|
37
|
|
38
|
if(page != null && name != null) {
|
39
|
DivId divId = divIdDAO.findByPagesContainingAndName(page, name);
|
40
|
if(divId != null) {
|
41
|
divIds = new ArrayList<>();
|
42
|
divIds.add(divId);
|
43
|
}
|
44
|
} else if(page != null) {
|
45
|
divIds = divIdDAO.findByPagesContaining(page);
|
46
|
} else if(name != null) {
|
47
|
DivId divId = divIdDAO.findByName(name);
|
48
|
if(divId != null) {
|
49
|
divIds = new ArrayList<>();
|
50
|
divIds.add(divId);
|
51
|
}
|
52
|
} else {
|
53
|
divIds = divIdDAO.findAll();
|
54
|
}
|
55
|
|
56
|
if(portalType != null) {
|
57
|
Iterator<DivId> iteratorDivIds = divIds.iterator();
|
58
|
while(iteratorDivIds.hasNext()) {
|
59
|
DivId divId = iteratorDivIds.next();
|
60
|
if (!portalType.equals(divId.getPortalType())) {
|
61
|
iteratorDivIds.remove();
|
62
|
}
|
63
|
}
|
64
|
}
|
65
|
|
66
|
return divIds;
|
67
|
}
|
68
|
|
69
|
public List<DivIdResponse> getDivIdsFull(String page, String name, String pid) {
|
70
|
|
71
|
List<DivId> divIds = this.getDivIds(page, name, pid);
|
72
|
|
73
|
List<DivIdResponse> divIdResponses = new ArrayList<>();
|
74
|
for(DivId divId : divIds) {
|
75
|
DivIdResponse divIdResponse = new DivIdResponse(divId);
|
76
|
List<Page> pages = new ArrayList<>();
|
77
|
for(String pageId : divId.getPages()) {
|
78
|
pages.add(pageService.getPage(pageId));
|
79
|
}
|
80
|
divIdResponse.setPages(pages);
|
81
|
|
82
|
divIdResponses.add(divIdResponse);
|
83
|
}
|
84
|
|
85
|
return divIdResponses;
|
86
|
}
|
87
|
|
88
|
public DivId getDivIdByName(String name) {
|
89
|
return divIdDAO.findByName(name);
|
90
|
}
|
91
|
|
92
|
public DivId getDivId(String id) {
|
93
|
return divIdDAO.findById(id);
|
94
|
}
|
95
|
|
96
|
public DivIdResponse getDivIdFull(String id) {
|
97
|
DivId divId = divIdDAO.findById(id);
|
98
|
return divIdResponseFromDivId(divId);
|
99
|
}
|
100
|
|
101
|
public DivIdResponse divIdResponseFromDivId(DivId divId) {
|
102
|
DivIdResponse divIdResponse = new DivIdResponse(divId);
|
103
|
List<Page> pages = new ArrayList<>();
|
104
|
for(String pageId : divId.getPages()) {
|
105
|
pages.add(pageService.getPage(pageId));
|
106
|
}
|
107
|
divIdResponse.setPages(pages);
|
108
|
return divIdResponse;
|
109
|
}
|
110
|
|
111
|
public void deleteAllDivIds() {
|
112
|
divIdDAO.deleteAll();
|
113
|
}
|
114
|
|
115
|
public DivId insertOrUpdateDivId(DivId divId) {
|
116
|
return divIdDAO.save(divId);
|
117
|
}
|
118
|
|
119
|
public DivIdResponse insertDivId(DivIdResponse divIdResponse) {
|
120
|
DivId divId = this.getDivIdByDivIdResponse(divIdResponse);
|
121
|
|
122
|
DivId savedDivId = divIdDAO.save(divId);
|
123
|
divIdResponse.setId(savedDivId.getId());
|
124
|
|
125
|
return this.getDivIdFull(divId.getId());
|
126
|
}
|
127
|
|
128
|
private DivId getDivIdByDivIdResponse(DivIdResponse divIdResponse) {
|
129
|
DivId divId = new DivId();
|
130
|
divId.setId(divIdResponse.getId());
|
131
|
divId.setName(divIdResponse.getName());
|
132
|
divId.setPortalType(divIdResponse.getPortalType());
|
133
|
|
134
|
List<Page> fullPages = divIdResponse.getPages();
|
135
|
List<String> pages = new ArrayList<String>();
|
136
|
for(Page page : fullPages) {
|
137
|
pages.add(page.getId());
|
138
|
}
|
139
|
divId.setPages(pages);
|
140
|
|
141
|
return divId;
|
142
|
}
|
143
|
|
144
|
public DivIdResponse updateDivId(DivIdResponse divIdResponse) {
|
145
|
// DivId divIdOld = divIdDAO.findById(divIdResponse.getId());
|
146
|
// DivId divId = this.getDivIdByDivIdResponse(divIdResponse);
|
147
|
// divIdDAO.save(divId);
|
148
|
//
|
149
|
// boolean openaireEnabled = divId.getOpenaire();
|
150
|
// boolean connectEnabled = divId.getConnect();
|
151
|
//
|
152
|
// if(divId.getCommunities() && !divIdOld.getCommunities()) {
|
153
|
// List<Portal> communities = communityController.getAllPortals();
|
154
|
// for( Portal community : communities ) {
|
155
|
// if(!community.getPid().equals("openaire") && !community.getPid().equals("connect")) {
|
156
|
// divHelpContentService.addDivHelpContentsInPortal(community.getPid(), community.getId(), divId.getName());
|
157
|
// }
|
158
|
// }
|
159
|
// }
|
160
|
// if(openaireEnabled && !divIdOld.getOpenaire()) {
|
161
|
// Portal community = communityController.getPortal("openaire");
|
162
|
// divHelpContentService.addDivHelpContentsInPortal(community.getPid(), community.getId(), divId.getName());
|
163
|
// }
|
164
|
//
|
165
|
// if(connectEnabled && !divIdOld.getConnect()) {
|
166
|
// Portal community = communityController.getPortal("connect");
|
167
|
// divHelpContentService.addDivHelpContentsInPortal(community.getPid(), community.getId(), divId.getName());
|
168
|
// }
|
169
|
//
|
170
|
// if(!divId.getCommunities()) {
|
171
|
// List<Portal> communities = communityController.getAllPortals();
|
172
|
// for( Portal community : communities ) {
|
173
|
// if(!community.getPid().equals("openaire") && !community.getPid().equals("connect")) {
|
174
|
// // delete div contents related to this divId
|
175
|
// List<DivHelpContentResponse> divHelpContentResponses = divHelpContentService.getDivHelpContents(community.getPid(), null, divId.getName(), null);
|
176
|
// for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
|
177
|
// divHelpContentService.deleteDivHelpContent(divHelpContentResponse.getId());
|
178
|
// }
|
179
|
// }
|
180
|
// }
|
181
|
// }
|
182
|
//
|
183
|
// if(!openaireEnabled) {
|
184
|
// Portal community = communityController.getPortal("openaire");
|
185
|
//
|
186
|
// // delete div contents related to this divId
|
187
|
// List<DivHelpContentResponse> divHelpContentResponses = divHelpContentService.getDivHelpContents("openaire", null, divId.getName(), null);
|
188
|
// for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
|
189
|
// divHelpContentService.deleteDivHelpContent(divHelpContentResponse.getId());
|
190
|
// }
|
191
|
// }
|
192
|
//
|
193
|
// if(!connectEnabled) {
|
194
|
// Portal community = communityController.getPortal("connect");
|
195
|
//
|
196
|
// // delete div contents related to this divId
|
197
|
// List<DivHelpContentResponse> divHelpContentResponses = divHelpContentService.getDivHelpContents("connect", null, divId.getName(), null);
|
198
|
// for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
|
199
|
// divHelpContentService.deleteDivHelpContent(divHelpContentResponse.getId());
|
200
|
// }
|
201
|
// }
|
202
|
|
203
|
DivId divId = this.getDivIdByDivIdResponse(divIdResponse);
|
204
|
divIdDAO.save(divId);
|
205
|
|
206
|
return divIdResponse;
|
207
|
}
|
208
|
|
209
|
public Boolean deleteDivIds(List<String> divIds) throws Exception {
|
210
|
for (String id: divIds) {
|
211
|
DivId divId = divIdDAO.findById(id);
|
212
|
|
213
|
// delete div contents related to this divId
|
214
|
List<DivHelpContentResponse> divHelpContentResponses = divHelpContentService.getDivHelpContents(null, null, divId.getName(), null);
|
215
|
for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
|
216
|
divHelpContentService.deleteDivHelpContent(divHelpContentResponse.getId());
|
217
|
}
|
218
|
|
219
|
divIdDAO.delete(id);
|
220
|
}
|
221
|
return true;
|
222
|
}
|
223
|
|
224
|
public void deleteDivId(String id) {
|
225
|
divIdDAO.delete(id);
|
226
|
}
|
227
|
|
228
|
public Set<String> getDivIdsPages(String pid) {
|
229
|
Portal portal = portalService.getPortal(pid);
|
230
|
String portalType = portal.getType();
|
231
|
|
232
|
List<DivId> divIds = null;
|
233
|
Set<String> hasPortalPageDivIds = new HashSet<>();
|
234
|
|
235
|
if(portalType != null) {
|
236
|
divIds = divIdDAO.findByPortalType(portalType);
|
237
|
} else {
|
238
|
divIds = divIdDAO.findAll();
|
239
|
}
|
240
|
|
241
|
for(DivId divId : divIds) {
|
242
|
for(String pageId : divId.getPages()) {
|
243
|
hasPortalPageDivIds.add(pageId);
|
244
|
}
|
245
|
}
|
246
|
return hasPortalPageDivIds;
|
247
|
}
|
248
|
|
249
|
}
|