1
|
package eu.dnetlib.uoaadmintools.controllers;
|
2
|
|
3
|
import eu.dnetlib.uoaadmintools.dao.DivIdDAO;
|
4
|
import eu.dnetlib.uoaadmintools.dao.EntityDAO;
|
5
|
import eu.dnetlib.uoaadmintools.dao.PageDAO;
|
6
|
import eu.dnetlib.uoaadmintools.entities.*;
|
7
|
import eu.dnetlib.uoaadmintools.dao.CommunityDAO;
|
8
|
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.web.bind.annotation.*;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
|
13
|
import java.util.*;
|
14
|
|
15
|
@RestController
|
16
|
@CrossOrigin(origins = "*")
|
17
|
public class CommunityController {
|
18
|
private final Logger log = Logger.getLogger(this.getClass());
|
19
|
|
20
|
@Autowired
|
21
|
private CommunityDAO communityDAO;
|
22
|
|
23
|
@Autowired
|
24
|
private PageDAO pageDAO;
|
25
|
|
26
|
@Autowired
|
27
|
private EntityDAO entityDAO;
|
28
|
|
29
|
@Autowired
|
30
|
private DivIdDAO divIdDAO;
|
31
|
|
32
|
@Autowired
|
33
|
private PageHelpContentController pageHelpContentController;
|
34
|
|
35
|
@Autowired
|
36
|
private DivHelpContentController divHelpContentController;
|
37
|
|
38
|
@Autowired
|
39
|
private DivIdController divIdController;
|
40
|
|
41
|
@RequestMapping(value = "/community", method = RequestMethod.GET)
|
42
|
public List<Community> getAllCommunities(@RequestParam(value="div", required = false) String div) {
|
43
|
List<Community> communities = communityDAO.findAll();
|
44
|
Iterator it = communities.iterator();
|
45
|
while(it.hasNext()) {
|
46
|
Community community = (Community)it.next();
|
47
|
if(div != null && div.equals("true")) {
|
48
|
List<DivId> divIds = divIdDAO.findByCommunity(community.getId());
|
49
|
if(divIds.isEmpty()) {
|
50
|
it.remove();
|
51
|
}
|
52
|
}
|
53
|
}
|
54
|
return communities;
|
55
|
}
|
56
|
|
57
|
@RequestMapping(value = "/communityFull", method = RequestMethod.GET)
|
58
|
public List<CommunityResponse> getAllCommunitiesFull() {
|
59
|
List<Community> communities = communityDAO.findAll();
|
60
|
List<CommunityResponse> communitiesResponse = new ArrayList<>();
|
61
|
for(Community community : communities) {
|
62
|
CommunityResponse communityResponse = new CommunityResponse(community);
|
63
|
|
64
|
List<CommunityPage> pages = this.getPagesForCommunityByType(community.getPid(), null, null, null);
|
65
|
log.debug("PAGES number="+pages.size());
|
66
|
Iterator<CommunityPage> iteratorPages = pages.iterator();
|
67
|
while(iteratorPages.hasNext()) {
|
68
|
CommunityPage page = iteratorPages.next();
|
69
|
if(!page.getIsEnabled()) {
|
70
|
iteratorPages.remove();
|
71
|
}
|
72
|
}
|
73
|
communityResponse.setPages(pages);
|
74
|
log.debug("PAGES set");
|
75
|
|
76
|
List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getPid(), null);
|
77
|
log.debug("ENTITIES number="+entities.size());
|
78
|
Iterator<CommunityEntity> iteratorEntities = entities.iterator();
|
79
|
while(iteratorEntities.hasNext()) {
|
80
|
CommunityEntity entity = iteratorEntities.next();
|
81
|
if(!entity.getIsEnabled()) {
|
82
|
iteratorEntities.remove();
|
83
|
}
|
84
|
}
|
85
|
communityResponse.setEntities(entities);
|
86
|
|
87
|
communitiesResponse.add(communityResponse);
|
88
|
}
|
89
|
return communitiesResponse;
|
90
|
}
|
91
|
|
92
|
@RequestMapping(value = "/communityFull/{pid}", method = RequestMethod.GET)
|
93
|
public CommunityResponse getCommunityFull(@PathVariable(value = "pid") String pid) {
|
94
|
Community community = communityDAO.findByPid(pid);
|
95
|
CommunityResponse communityResponse = new CommunityResponse(community);
|
96
|
|
97
|
List<CommunityPage> pages = this.getPagesForCommunityByType(community.getPid(), null, null, null);
|
98
|
Iterator<CommunityPage> iteratorPages = pages.iterator();
|
99
|
while(iteratorPages.hasNext()) {
|
100
|
CommunityPage page = iteratorPages.next();
|
101
|
if(!page.getIsEnabled()) {
|
102
|
iteratorPages.remove();
|
103
|
}
|
104
|
}
|
105
|
communityResponse.setPages(pages);
|
106
|
|
107
|
List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getPid(), null);
|
108
|
Iterator<CommunityEntity> iteratorEntities = entities.iterator();
|
109
|
while(iteratorEntities.hasNext()) {
|
110
|
CommunityEntity entity = iteratorEntities.next();
|
111
|
if(!entity.getIsEnabled()) {
|
112
|
iteratorEntities.remove();
|
113
|
}
|
114
|
}
|
115
|
communityResponse.setEntities(entities);
|
116
|
// communityResponse.setPages(this.getPagesForCommunityByType(community.getId(), null));
|
117
|
// communityResponse.setEntities(this.getEntitiesForCommunity(community.getId()));
|
118
|
|
119
|
return communityResponse;
|
120
|
}
|
121
|
/*
|
122
|
|
123
|
@RequestMapping(value = "/communityFullByName/{name}", method = RequestMethod.GET)
|
124
|
public CommunityResponse getCommunityFullByName(@PathVariable(value = "name") String name) {
|
125
|
Community community = communityDAO.findByName(name);
|
126
|
CommunityResponse communityResponse = new CommunityResponse(community);
|
127
|
|
128
|
List<CommunityPage> pages = this.getPagesForCommunityByType(community.getId(), null, null);
|
129
|
Iterator<CommunityPage> iteratorPages = pages.iterator();
|
130
|
while(iteratorPages.hasNext()) {
|
131
|
CommunityPage page = iteratorPages.next();
|
132
|
if(!page.getIsEnabled()) {
|
133
|
iteratorPages.remove();
|
134
|
}
|
135
|
}
|
136
|
communityResponse.setPages(pages);
|
137
|
|
138
|
List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getId(), null);
|
139
|
Iterator<CommunityEntity> iteratorEntities = entities.iterator();
|
140
|
while(iteratorEntities.hasNext()) {
|
141
|
CommunityEntity entity = iteratorEntities.next();
|
142
|
if(!entity.getIsEnabled()) {
|
143
|
iteratorEntities.remove();
|
144
|
}
|
145
|
}
|
146
|
communityResponse.setEntities(entities);
|
147
|
|
148
|
return communityResponse;
|
149
|
}
|
150
|
*/
|
151
|
|
152
|
@RequestMapping(value = "/community/update", method = RequestMethod.POST)
|
153
|
public CommunityResponse updateCommunity(@RequestBody Community community) {
|
154
|
Community com = communityDAO.findById(community.getId());
|
155
|
com.setName(community.getName());
|
156
|
com.setPid(community.getPid());
|
157
|
// = this.getCommunityByCommunityResponse(communityResponse);
|
158
|
communityDAO.save(com);
|
159
|
CommunityResponse communityResponse = this.getCommunityFull(community.getPid());
|
160
|
|
161
|
return communityResponse;
|
162
|
}
|
163
|
|
164
|
@RequestMapping(value = "/community/save", method = RequestMethod.POST)
|
165
|
public CommunityResponse insertCommunity(@RequestBody Community community) {
|
166
|
//Community community = this.getCommunityByCommunityResponse(communityResponse);
|
167
|
|
168
|
List<CommunityEntity> communityEntities = new ArrayList<>();
|
169
|
List<CommunityPage> communityPages = new ArrayList<>();
|
170
|
Map<String, Boolean> pages = new HashMap<>();
|
171
|
Map<String, Boolean> entities = new HashMap<>();
|
172
|
for(Entity entity : entityDAO.findAll()) {
|
173
|
entities.put(entity.getId(), true);
|
174
|
|
175
|
CommunityEntity communityEntity = new CommunityEntity(entity);
|
176
|
communityEntity.setIsEnabled(true);
|
177
|
communityEntities.add(communityEntity);
|
178
|
}
|
179
|
for(Page page : pageDAO.findAll()) {
|
180
|
pages.put(page.getId(), true);
|
181
|
|
182
|
CommunityPage communityPage = new CommunityPage(page);
|
183
|
communityPage.setIsEnabled(true);
|
184
|
|
185
|
communityPages.add(communityPage);
|
186
|
}
|
187
|
|
188
|
community.setEntities(entities);
|
189
|
community.setPages(pages);
|
190
|
|
191
|
Community savedCommunity = communityDAO.save(community);
|
192
|
CommunityResponse communityResponse = this.getCommunityFull(savedCommunity.getPid());
|
193
|
|
194
|
return communityResponse;
|
195
|
}
|
196
|
|
197
|
private Community getCommunityByCommunityResponse(CommunityResponse communityResponse) {
|
198
|
Community community = new Community();
|
199
|
community.setId(communityResponse.getId());
|
200
|
community.setName(communityResponse.getName());
|
201
|
|
202
|
List<CommunityEntity> fullEntities = communityResponse.getEntities();
|
203
|
Map<String, Boolean> entities = new HashMap<String, Boolean>();
|
204
|
for(CommunityEntity entity : fullEntities) {
|
205
|
entities.put(entity.getId(), true);
|
206
|
}
|
207
|
for(Entity entity : entityDAO.findAll()) {
|
208
|
if(!entities.containsKey(entity.getId())) {
|
209
|
entities.put(entity.getId(), false);
|
210
|
}
|
211
|
}
|
212
|
community.setEntities(entities);
|
213
|
|
214
|
List<CommunityPage> fullPages = communityResponse.getPages();
|
215
|
Map<String, Boolean> pages = new HashMap<String, Boolean>();
|
216
|
for(CommunityPage page : fullPages) {
|
217
|
pages.put(page.getId(), true);
|
218
|
}
|
219
|
for(Page page : pageDAO.findAll()) {
|
220
|
if(!pages.containsKey(page.getId())) {
|
221
|
pages.put(page.getId(), false);
|
222
|
}
|
223
|
}
|
224
|
community.setPages(pages);
|
225
|
|
226
|
return community;
|
227
|
}
|
228
|
|
229
|
@RequestMapping(value = "/community/delete", method = RequestMethod.POST)
|
230
|
public Boolean deleteCommunities(@RequestBody List<String> communities) throws Exception {
|
231
|
for (String id: communities) {
|
232
|
communityDAO.delete(id);
|
233
|
|
234
|
// delete divIds related to this page from all communities
|
235
|
List<DivId> divIds = divIdController.getDivIds(id, null);
|
236
|
for(DivId divId : divIds) {
|
237
|
divIdController.deleteDivId(divId.getId());
|
238
|
}
|
239
|
|
240
|
// delete div contents related to this page from all communities
|
241
|
List<DivHelpContentResponse> divHelpContentResponses = divHelpContentController.getDivHelpContents(null, id, null);
|
242
|
for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
|
243
|
divHelpContentController.deleteDivHelpContent(divHelpContentResponse.getId());
|
244
|
}
|
245
|
|
246
|
// delete contents related to this community
|
247
|
List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentController.getPageHelpContents(id, null, null, null, null);
|
248
|
for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
|
249
|
pageHelpContentController.deletePageHelpContent(pageHelpContentResponse.getId());
|
250
|
}
|
251
|
}
|
252
|
|
253
|
return true;
|
254
|
}
|
255
|
|
256
|
// @RequestMapping(value = "/community", method = RequestMethod.DELETE)
|
257
|
// public void deleteAllCommunities() {
|
258
|
// communityDAO.deleteAll();
|
259
|
// }
|
260
|
|
261
|
@RequestMapping(value = "/community", method = RequestMethod.POST)
|
262
|
public Community insertOrUpdateCommunity(@RequestBody Community community) {
|
263
|
return communityDAO.save(community);
|
264
|
}
|
265
|
|
266
|
@RequestMapping(value = "/community/{pid}", method = RequestMethod.GET)
|
267
|
public Community getCommunity(@PathVariable(value = "pid") String pid) {
|
268
|
log.debug("PID: "+ pid);
|
269
|
return communityDAO.findByPid(pid);
|
270
|
}
|
271
|
|
272
|
@RequestMapping(value = "/community/{id}", method = RequestMethod.DELETE)
|
273
|
public void deleteCommunity(@PathVariable(value = "id") String id) {
|
274
|
communityDAO.delete(id);
|
275
|
}
|
276
|
|
277
|
@RequestMapping(value = "/community/{pid}/pages", method = RequestMethod.GET)
|
278
|
public List<CommunityPage> getPagesForCommunityByType(@PathVariable(value = "pid") String pid,
|
279
|
@RequestParam(value="page_type", required=false) String page_type,
|
280
|
@RequestParam(value="page_route", required=false) String page_route,
|
281
|
@RequestParam(value="div", required = false) String div) {
|
282
|
List<CommunityPage> return_pages = new ArrayList<CommunityPage>();
|
283
|
Map<String, Boolean> pages = communityDAO.findByPid(pid).getPages();
|
284
|
|
285
|
if(pages != null) {
|
286
|
for (Map.Entry<String, Boolean> page : pages.entrySet()) {
|
287
|
if(div != null && div.equals("true")) {
|
288
|
Community community = communityDAO.findByPid(pid);
|
289
|
List<DivId> divIds = divIdDAO.findByCommunityAndPage(community.getId(), page.getKey());
|
290
|
if(divIds.isEmpty()) {
|
291
|
continue;
|
292
|
}
|
293
|
}
|
294
|
|
295
|
Page p = pageDAO.findById(page.getKey());
|
296
|
if ((page_type == null && page_route == null) || (page_route == null && p.getType().equals(page_type))
|
297
|
|| p.getRoute().equals(page_route)) {
|
298
|
CommunityPage communityPage = new CommunityPage(p);
|
299
|
|
300
|
List<Entity> entities = new ArrayList<>();
|
301
|
for (String entity : p.getEntities()) {
|
302
|
entities.add(entityDAO.findById(entity));
|
303
|
}
|
304
|
communityPage.setEntities(entities);
|
305
|
communityPage.setIsEnabled(page.getValue());
|
306
|
|
307
|
return_pages.add(communityPage);
|
308
|
|
309
|
if (page_route != null) {
|
310
|
break;
|
311
|
}
|
312
|
}
|
313
|
}
|
314
|
}
|
315
|
return return_pages;
|
316
|
}
|
317
|
|
318
|
@RequestMapping(value = "/community/{id}/page", method = RequestMethod.POST)
|
319
|
public Community insertOrUpdatePage(@PathVariable(value = "id") String id, @RequestBody CommunityPage page) {
|
320
|
Community community = communityDAO.findById(id);
|
321
|
Map<String, Boolean> pages = community.getPages();
|
322
|
|
323
|
String name = page.getName();
|
324
|
boolean isEnabled = page.getIsEnabled();
|
325
|
|
326
|
pages.put(name, isEnabled);
|
327
|
community.setPages(pages);
|
328
|
|
329
|
return communityDAO.save(community);
|
330
|
}
|
331
|
|
332
|
@RequestMapping(value = "community/{pid}/page/toggle", method = RequestMethod.POST)
|
333
|
public Community togglePage(@PathVariable(value = "pid") String pid, @RequestBody List<String> pageIds, @RequestParam String status) throws Exception {
|
334
|
Community community = communityDAO.findByPid(pid);
|
335
|
Map<String, Boolean> pages = community.getPages();
|
336
|
|
337
|
for (String pageId: pageIds) {
|
338
|
log.debug("Toggle community page: " + pageId + " of community: " + pid + " to " + status);
|
339
|
pages.put(pageId, Boolean.parseBoolean(status));
|
340
|
}
|
341
|
|
342
|
community.setPages(pages);
|
343
|
return communityDAO.save(community);
|
344
|
}
|
345
|
|
346
|
@RequestMapping(value = "community/{pid}/entity/toggle", method = RequestMethod.POST)
|
347
|
public Community toggleEntity(@PathVariable(value = "pid") String pid, @RequestBody List<String> entityIds, @RequestParam String status) throws Exception {
|
348
|
Community community = communityDAO.findByPid(pid);
|
349
|
Map<String, Boolean> entities = community.getEntities();
|
350
|
Map<String, Boolean> pages = community.getPages();
|
351
|
|
352
|
for (String entityId: entityIds) {
|
353
|
log.debug("Toggle community entity: " + entityId + " of community: " + pid + " to " + status);
|
354
|
|
355
|
entities.put(entityId, Boolean.parseBoolean(status));
|
356
|
|
357
|
if(pages != null) {
|
358
|
for (Map.Entry<String, Boolean> pageEntry : pages.entrySet()) {
|
359
|
Page page = pageDAO.findById(pageEntry.getKey());
|
360
|
if (page.getEntities().contains(entityId) && page.getType().equals("search")) {
|
361
|
pages.put(pageEntry.getKey(), Boolean.parseBoolean(status));
|
362
|
}
|
363
|
}
|
364
|
}
|
365
|
}
|
366
|
|
367
|
community.setEntities(entities);
|
368
|
return communityDAO.save(community);
|
369
|
}
|
370
|
|
371
|
@RequestMapping(value = "/community/{pid}/entities", method = RequestMethod.GET)
|
372
|
public List<CommunityEntity> getEntitiesForCommunity(@PathVariable(value = "pid") String pid, @RequestParam(value="entity", required=false) String entity) {
|
373
|
List<CommunityEntity> return_entities = new ArrayList<CommunityEntity>();
|
374
|
Map<String, Boolean> entities = communityDAO.findByPid(pid).getEntities();
|
375
|
|
376
|
log.debug("/community/"+pid+"/entities -- entity: "+entity);
|
377
|
if (entity != null) {
|
378
|
String entityId = entityDAO.findByPid(entity).getId();
|
379
|
CommunityEntity communityEntity = new CommunityEntity(entityDAO.findById(entityId));
|
380
|
communityEntity.setIsEnabled(entities.get(entityId));
|
381
|
return_entities.add(communityEntity);
|
382
|
} else {
|
383
|
if(entities != null) {
|
384
|
for (Map.Entry<String, Boolean> _entity : entities.entrySet()) {
|
385
|
CommunityEntity communityEntity = new CommunityEntity(entityDAO.findById(_entity.getKey()));
|
386
|
communityEntity.setIsEnabled(_entity.getValue());
|
387
|
return_entities.add(communityEntity);
|
388
|
}
|
389
|
}
|
390
|
}
|
391
|
return return_entities;
|
392
|
}
|
393
|
}
|
394
|
|