Project

General

Profile

1
package eu.dnetlib.uoaadmintools.controllers;
2

    
3
import eu.dnetlib.uoaadmintools.dao.*;
4
import eu.dnetlib.uoaadmintools.entities.*;
5

    
6
import org.apache.log4j.Logger;
7
import org.springframework.web.bind.annotation.*;
8
import org.springframework.beans.factory.annotation.Autowired;
9

    
10
import java.util.*;
11

    
12
@RestController
13
@CrossOrigin(origins = "*")
14
public class CommunityController {
15
    private final Logger log = Logger.getLogger(this.getClass());
16

    
17
    @Autowired
18
    private CommunityDAO communityDAO;
19

    
20
    @Autowired
21
    private PageDAO pageDAO;
22

    
23
    @Autowired
24
    private EntityDAO entityDAO;
25

    
26
    @Autowired
27
    private DivIdDAO divIdDAO;
28

    
29
    @Autowired
30
    private PageHelpContentController pageHelpContentController;
31

    
32
    @Autowired
33
    private DivHelpContentController divHelpContentController;
34

    
35
    @Autowired
36
    private DivIdController divIdController;
37

    
38
    @Autowired
39
    private StatisticsDAO statisticsDAO;
40
    @Autowired
41
    private CommunitySubscribersDAO  communitySubscribersDAO;
42

    
43
    @RequestMapping(value = "/community", method = RequestMethod.GET)
44
    public List<Community> getAllCommunities(@RequestParam(value="div", required = false) String div) {
45
        List<Community> communities = communityDAO.findAll();
46
        Iterator it = communities.iterator();
47
        while(it.hasNext()) {
48
            Community community = (Community)it.next();
49
            if(div != null && div.equals("true")) {
50
                List<DivId> divIds = divIdDAO.findByCommunity(community.getId());
51
                if(divIds.isEmpty()) {
52
                    it.remove();
53
                }
54
            }
55
        }
56
        return communities;
57
    }
58

    
59
    @RequestMapping(value = "/communityFull", method = RequestMethod.GET)
60
    public List<CommunityResponse> getAllCommunitiesFull() {
61
        List<Community> communities = communityDAO.findAll();
62
        List<CommunityResponse> communitiesResponse = new ArrayList<>();
63
        for(Community community : communities) {
64
            CommunityResponse communityResponse = new CommunityResponse(community);
65

    
66
            List<CommunityPage> pages = this.getPagesForCommunityByType(community.getPid(), null, null, null);
67
            log.debug("PAGES number="+pages.size());
68
            Iterator<CommunityPage> iteratorPages = pages.iterator();
69
            while(iteratorPages.hasNext()) {
70
                CommunityPage page = iteratorPages.next();
71
                if(!page.getIsEnabled()) {
72
                    iteratorPages.remove();
73
                }
74
            }
75
            communityResponse.setPages(pages);
76
            log.debug("PAGES set");
77

    
78
            List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getPid(), null);
79
            log.debug("ENTITIES number="+entities.size());
80
            Iterator<CommunityEntity> iteratorEntities = entities.iterator();
81
            while(iteratorEntities.hasNext()) {
82
                CommunityEntity entity = iteratorEntities.next();
83
                if(!entity.getIsEnabled()) {
84
                    iteratorEntities.remove();
85
                }
86
            }
87
            communityResponse.setEntities(entities);
88

    
89
            communitiesResponse.add(communityResponse);
90
        }
91
        return communitiesResponse;
92
    }
93

    
94
    @RequestMapping(value = "/communityFull/{pid}", method = RequestMethod.GET)
95
    public CommunityResponse getCommunityFull(@PathVariable(value = "pid") String pid) {
96
        Community community = communityDAO.findByPid(pid);
97
        CommunityResponse communityResponse = new CommunityResponse(community);
98

    
99
        List<CommunityPage> pages = this.getPagesForCommunityByType(community.getPid(), null, null, null);
100
        Iterator<CommunityPage> iteratorPages = pages.iterator();
101
        while(iteratorPages.hasNext()) {
102
            CommunityPage page = iteratorPages.next();
103
            if(!page.getIsEnabled()) {
104
                iteratorPages.remove();
105
            }
106
        }
107
        communityResponse.setPages(pages);
108

    
109
        List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getPid(), null);
110
        Iterator<CommunityEntity> iteratorEntities = entities.iterator();
111
        while(iteratorEntities.hasNext()) {
112
            CommunityEntity entity = iteratorEntities.next();
113
            if(!entity.getIsEnabled()) {
114
                iteratorEntities.remove();
115
            }
116
        }
117
        communityResponse.setEntities(entities);
118
//        communityResponse.setPages(this.getPagesForCommunityByType(community.getId(), null));
119
//        communityResponse.setEntities(this.getEntitiesForCommunity(community.getId()));
120

    
121
        return communityResponse;
122
    }
123
/*
124

    
125
    @RequestMapping(value = "/communityFullByName/{name}", method = RequestMethod.GET)
126
    public CommunityResponse getCommunityFullByName(@PathVariable(value = "name") String name) {
127
        Community community = communityDAO.findByName(name);
128
        CommunityResponse communityResponse = new CommunityResponse(community);
129

    
130
        List<CommunityPage> pages = this.getPagesForCommunityByType(community.getId(), null, null);
131
        Iterator<CommunityPage> iteratorPages = pages.iterator();
132
        while(iteratorPages.hasNext()) {
133
            CommunityPage page = iteratorPages.next();
134
            if(!page.getIsEnabled()) {
135
                iteratorPages.remove();
136
            }
137
        }
138
        communityResponse.setPages(pages);
139

    
140
        List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getId(), null);
141
        Iterator<CommunityEntity> iteratorEntities = entities.iterator();
142
        while(iteratorEntities.hasNext()) {
143
            CommunityEntity entity = iteratorEntities.next();
144
            if(!entity.getIsEnabled()) {
145
                iteratorEntities.remove();
146
            }
147
        }
148
        communityResponse.setEntities(entities);
149

    
150
        return communityResponse;
151
    }
152
*/
153

    
154
    @RequestMapping(value = "/community/update", method = RequestMethod.POST)
155
    public CommunityResponse updateCommunity(@RequestBody Community community) {
156
        Community com = communityDAO.findById(community.getId());
157
        com.setName(community.getName());
158
        com.setPid(community.getPid());
159
        // = this.getCommunityByCommunityResponse(communityResponse);
160
        communityDAO.save(com);
161
        CommunityResponse communityResponse = this.getCommunityFull(community.getPid());
162

    
163
        return communityResponse;
164
    }
165

    
166
    @RequestMapping(value = "/community/save", method = RequestMethod.POST)
167
    public CommunityResponse insertCommunity(@RequestBody Community community) {
168
        //Community community = this.getCommunityByCommunityResponse(communityResponse);
169

    
170
        List<CommunityEntity> communityEntities = new ArrayList<>();
171
        List<CommunityPage> communityPages = new ArrayList<>();
172
        Map<String, Boolean> pages = new HashMap<>();
173
        Map<String, Boolean> entities = new HashMap<>();
174
        for(Entity entity : entityDAO.findAll()) {
175
            entities.put(entity.getId(), true);
176

    
177
            CommunityEntity communityEntity = new CommunityEntity(entity);
178
            communityEntity.setIsEnabled(true);
179
            communityEntities.add(communityEntity);
180
        }
181
        for(Page page : pageDAO.findAll()) {
182
            pages.put(page.getId(), true);
183

    
184
            CommunityPage communityPage = new CommunityPage(page);
185
            communityPage.setIsEnabled(true);
186

    
187
            communityPages.add(communityPage);
188
        }
189

    
190
        community.setEntities(entities);
191
        community.setPages(pages);
192
        Statistics statistics =  new Statistics(community.getPid());
193
        statisticsDAO.save(statistics);
194
        CommunitySubscribers communitySubscribers =  new CommunitySubscribers(community.getPid());
195
        communitySubscribersDAO.save(communitySubscribers);
196
        Community savedCommunity = communityDAO.save(community);
197
        CommunityResponse communityResponse = this.getCommunityFull(savedCommunity.getPid());
198

    
199
        return communityResponse;
200
    }
201

    
202
    private Community getCommunityByCommunityResponse(CommunityResponse communityResponse) {
203
        Community community = new Community();
204
        community.setId(communityResponse.getId());
205
        community.setName(communityResponse.getName());
206

    
207
        List<CommunityEntity> fullEntities = communityResponse.getEntities();
208
        Map<String, Boolean> entities = new HashMap<String, Boolean>();
209
        for(CommunityEntity entity : fullEntities) {
210
            entities.put(entity.getId(), true);
211
        }
212
        for(Entity entity : entityDAO.findAll()) {
213
            if(!entities.containsKey(entity.getId())) {
214
                entities.put(entity.getId(), false);
215
            }
216
        }
217
        community.setEntities(entities);
218

    
219
        List<CommunityPage> fullPages = communityResponse.getPages();
220
        Map<String, Boolean> pages = new HashMap<String, Boolean>();
221
        for(CommunityPage page : fullPages) {
222
            pages.put(page.getId(), true);
223
        }
224
        for(Page page : pageDAO.findAll()) {
225
            if(!pages.containsKey(page.getId())) {
226
                pages.put(page.getId(), false);
227
            }
228
        }
229
        community.setPages(pages);
230

    
231
        return community;
232
    }
233

    
234
    @RequestMapping(value = "/community/delete", method = RequestMethod.POST)
235
    public Boolean deleteCommunities(@RequestBody List<String> communities) throws Exception {
236
        for (String id: communities) {
237
            communityDAO.delete(id);
238

    
239
            // delete divIds related to this page from all communities
240
            List<DivId> divIds = divIdController.getDivIds(id, null, null);
241
            for(DivId divId : divIds) {
242
                //divIdController.deleteDivId(divId.getId());
243
            }
244

    
245
            // delete div contents related to this page from all communities
246
            List<DivHelpContentResponse> divHelpContentResponses = divHelpContentController.getDivHelpContents(null, id, null, null);
247
            for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
248
                divHelpContentController.deleteDivHelpContent(divHelpContentResponse.getId());
249
            }
250

    
251
            // delete contents related to this community
252
            List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentController.getPageHelpContents(id, null, null, null, null);
253
            for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
254
                pageHelpContentController.deletePageHelpContent(pageHelpContentResponse.getId());
255
            }
256
        }
257

    
258
        return true;
259
    }
260

    
261
//    @RequestMapping(value = "/community", method = RequestMethod.DELETE)
262
//    public void deleteAllCommunities() {
263
//        communityDAO.deleteAll();
264
//    }
265

    
266
    @RequestMapping(value = "/community", method = RequestMethod.POST)
267
    public Community insertOrUpdateCommunity(@RequestBody Community community) {
268
        return communityDAO.save(community);
269
    }
270

    
271
    @RequestMapping(value = "/community/{pid}", method = RequestMethod.GET)
272
    public Community getCommunity(@PathVariable(value = "pid") String pid) {
273
        log.debug("PID: "+ pid);
274
        return communityDAO.findByPid(pid);
275
    }
276

    
277
    @RequestMapping(value = "/community/{id}", method = RequestMethod.DELETE)
278
    public void deleteCommunity(@PathVariable(value = "id") String id) {
279
        communityDAO.delete(id);
280
    }
281

    
282
    @RequestMapping(value = "/community/{pid}/pages", method = RequestMethod.GET)
283
    public List<CommunityPage> getPagesForCommunityByType(@PathVariable(value = "pid") String pid,
284
                                                          @RequestParam(value="page_type", required=false) String page_type,
285
                                                          @RequestParam(value="page_route", required=false) String page_route,
286
                                                          @RequestParam(value="div", required = false) String div) {
287
        List<CommunityPage> return_pages = new ArrayList<CommunityPage>();
288
        Map<String, Boolean> pages = communityDAO.findByPid(pid).getPages();
289

    
290
        if(pages != null) {
291
            for (Map.Entry<String, Boolean> page : pages.entrySet()) {
292
                if(div != null && div.equals("true")) {
293
                    Community community = communityDAO.findByPid(pid);
294
                    List<DivId> divIds = divIdDAO.findByCommunityAndPagesContaining(community.getId(), page.getKey());
295
                    if(divIds.isEmpty()) {
296
                        continue;
297
                    }
298
                }
299

    
300
                Page p = pageDAO.findById(page.getKey());
301
                if ((page_type == null && page_route == null) || (page_route == null && p.getType().equals(page_type))
302
                        || p.getRoute().equals(page_route)) {
303
                    CommunityPage communityPage = new CommunityPage(p);
304

    
305
                    List<Entity> entities = new ArrayList<>();
306
                    for (String entity : p.getEntities()) {
307
                        entities.add(entityDAO.findById(entity));
308
                    }
309
                    communityPage.setEntities(entities);
310
                    communityPage.setIsEnabled(page.getValue());
311

    
312
                    return_pages.add(communityPage);
313

    
314
                    if (page_route != null) {
315
                        break;
316
                    }
317
                }
318
            }
319
        }
320
        return return_pages;
321
    }
322

    
323
    @RequestMapping(value = "/community/{id}/page", method = RequestMethod.POST)
324
    public Community insertOrUpdatePage(@PathVariable(value = "id") String id, @RequestBody CommunityPage page) {
325
        Community community = communityDAO.findById(id);
326
        Map<String, Boolean> pages = community.getPages();
327

    
328
        String name = page.getName();
329
        boolean isEnabled = page.getIsEnabled();
330

    
331
        pages.put(name, isEnabled);
332
        community.setPages(pages);
333

    
334
        return communityDAO.save(community);
335
    }
336

    
337
    @RequestMapping(value = "community/{pid}/page/toggle", method = RequestMethod.POST)
338
    public Community togglePage(@PathVariable(value = "pid") String pid, @RequestBody List<String> pageIds, @RequestParam String status) throws Exception {
339
        Community community = communityDAO.findByPid(pid);
340
        Map<String, Boolean> pages = community.getPages();
341

    
342
        for (String pageId: pageIds) {
343
            log.debug("Toggle community page: " + pageId + " of community: " + pid + " to " + status);
344
            pages.put(pageId, Boolean.parseBoolean(status));
345
        }
346

    
347
        community.setPages(pages);
348
        return communityDAO.save(community);
349
    }
350

    
351
    @RequestMapping(value = "community/{pid}/entity/toggle", method = RequestMethod.POST)
352
    public Community toggleEntity(@PathVariable(value = "pid") String pid, @RequestBody List<String> entityIds, @RequestParam String status) throws Exception {
353
        Community community = communityDAO.findByPid(pid);
354
        Map<String, Boolean> entities = community.getEntities();
355
        Map<String, Boolean> pages = community.getPages();
356

    
357
        for (String entityId: entityIds) {
358
            log.debug("Toggle community entity: " + entityId + " of community: " + pid + " to " + status);
359

    
360
            entities.put(entityId, Boolean.parseBoolean(status));
361

    
362
            if(pages != null) {
363
                for (Map.Entry<String, Boolean> pageEntry : pages.entrySet()) {
364
                    Page page = pageDAO.findById(pageEntry.getKey());
365
                    if (page.getEntities().contains(entityId) && page.getType().equals("search")) {
366
                        pages.put(pageEntry.getKey(), Boolean.parseBoolean(status));
367
                    }
368
                }
369
            }
370
        }
371

    
372
        community.setEntities(entities);
373
        return communityDAO.save(community);
374
    }
375

    
376
    @RequestMapping(value = "/community/{pid}/entities", method = RequestMethod.GET)
377
    public List<CommunityEntity> getEntitiesForCommunity(@PathVariable(value = "pid") String pid, @RequestParam(value="entity", required=false) String entity) {
378
        List<CommunityEntity> return_entities = new ArrayList<CommunityEntity>();
379
        Map<String, Boolean> entities = communityDAO.findByPid(pid).getEntities();
380

    
381
        log.debug("/community/"+pid+"/entities -- entity: "+entity);
382
        if (entity != null) {
383
            String entityId = entityDAO.findByPid(entity).getId();
384
            CommunityEntity communityEntity = new CommunityEntity(entityDAO.findById(entityId));
385
            communityEntity.setIsEnabled(entities.get(entityId));
386
            return_entities.add(communityEntity);
387
        } else {
388
            if(entities != null) {
389
                for (Map.Entry<String, Boolean> _entity : entities.entrySet()) {
390
                    CommunityEntity communityEntity = new CommunityEntity(entityDAO.findById(_entity.getKey()));
391
                    communityEntity.setIsEnabled(_entity.getValue());
392
                    return_entities.add(communityEntity);
393
                }
394
            }
395
        }
396
        return return_entities;
397
    }
398
}
399

    
(1-1/11)