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