Project

General

Profile

1
package eu.dnetlib.uoaadmintools.controllers;
2

    
3
import com.sun.org.apache.xpath.internal.operations.Div;
4
import eu.dnetlib.uoaadmintools.dao.*;
5
import eu.dnetlib.uoaadmintools.entities.*;
6

    
7
import eu.dnetlib.uoaadmintools.entities.statistics.Statistics;
8
import org.apache.log4j.Logger;
9
import org.springframework.web.bind.annotation.*;
10
import org.springframework.beans.factory.annotation.Autowired;
11

    
12
import java.util.*;
13

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

    
19
    @Autowired
20
    private CommunityDAO communityDAO;
21

    
22
    @Autowired
23
    private PageDAO pageDAO;
24

    
25
    @Autowired
26
    private EntityDAO entityDAO;
27

    
28
    @Autowired
29
    private DivIdDAO divIdDAO;
30

    
31
    @Autowired
32
    private PageHelpContentController pageHelpContentController;
33

    
34
    @Autowired
35
    private DivHelpContentController divHelpContentController;
36

    
37
    @Autowired
38
    private DivIdController divIdController;
39

    
40
    @Autowired
41
    private StatisticsDAO statisticsDAO;
42
    @Autowired
43
    private CommunitySubscribersDAO  communitySubscribersDAO;
44

    
45
    @RequestMapping(value = "/community", method = RequestMethod.GET)
46
    public List<Community> getAllCommunities() {
47
        List<Community> communities = communityDAO.findAll();
48

    
49
        return communities;
50
    }
51

    
52
    @RequestMapping(value = "/communityFull", method = RequestMethod.GET)
53
    public List<CommunityResponse> getAllCommunitiesFull() {
54
        List<Community> communities = communityDAO.findAll();
55
        List<CommunityResponse> communitiesResponse = new ArrayList<>();
56
        for(Community community : communities) {
57
            CommunityResponse communityResponse = new CommunityResponse(community);
58

    
59
            List<CommunityPage> pages = this.getPagesForCommunityByType(community.getPid(), null, null, null);
60
            log.debug("PAGES number="+pages.size());
61
            Iterator<CommunityPage> iteratorPages = pages.iterator();
62
            while(iteratorPages.hasNext()) {
63
                CommunityPage page = iteratorPages.next();
64
                if(!page.getIsEnabled()) {
65
                    iteratorPages.remove();
66
                }
67
            }
68
            communityResponse.setPages(pages);
69
            log.debug("PAGES set");
70

    
71
            List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getPid(), null);
72
            log.debug("ENTITIES number="+entities.size());
73
            Iterator<CommunityEntity> iteratorEntities = entities.iterator();
74
            while(iteratorEntities.hasNext()) {
75
                CommunityEntity entity = iteratorEntities.next();
76
                if(!entity.getIsEnabled()) {
77
                    iteratorEntities.remove();
78
                }
79
            }
80
            communityResponse.setEntities(entities);
81

    
82
            communitiesResponse.add(communityResponse);
83
        }
84
        return communitiesResponse;
85
    }
86

    
87
    @RequestMapping(value = "/communityFull/{pid}", method = RequestMethod.GET)
88
    public CommunityResponse getCommunityFull(@PathVariable(value = "pid") String pid) {
89
        Community community = communityDAO.findByPid(pid);
90
        CommunityResponse communityResponse = new CommunityResponse(community);
91

    
92
        List<CommunityPage> pages = this.getPagesForCommunityByType(community.getPid(), null, null, null);
93
        Iterator<CommunityPage> iteratorPages = pages.iterator();
94
        while(iteratorPages.hasNext()) {
95
            CommunityPage page = iteratorPages.next();
96
            if(!page.getIsEnabled()) {
97
                iteratorPages.remove();
98
            }
99
        }
100
        communityResponse.setPages(pages);
101

    
102
        List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getPid(), null);
103
        Iterator<CommunityEntity> iteratorEntities = entities.iterator();
104
        while(iteratorEntities.hasNext()) {
105
            CommunityEntity entity = iteratorEntities.next();
106
            if(!entity.getIsEnabled()) {
107
                iteratorEntities.remove();
108
            }
109
        }
110
        communityResponse.setEntities(entities);
111
//        communityResponse.setPages(this.getPagesForCommunityByType(community.getId(), null));
112
//        communityResponse.setEntities(this.getEntitiesForCommunity(community.getId()));
113

    
114
        return communityResponse;
115
    }
116
/*
117

    
118
    @RequestMapping(value = "/communityFullByName/{name}", method = RequestMethod.GET)
119
    public CommunityResponse getCommunityFullByName(@PathVariable(value = "name") String name) {
120
        Community community = communityDAO.findByName(name);
121
        CommunityResponse communityResponse = new CommunityResponse(community);
122

    
123
        List<CommunityPage> pages = this.getPagesForCommunityByType(community.getId(), null, null);
124
        Iterator<CommunityPage> iteratorPages = pages.iterator();
125
        while(iteratorPages.hasNext()) {
126
            CommunityPage page = iteratorPages.next();
127
            if(!page.getIsEnabled()) {
128
                iteratorPages.remove();
129
            }
130
        }
131
        communityResponse.setPages(pages);
132

    
133
        List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getId(), null);
134
        Iterator<CommunityEntity> iteratorEntities = entities.iterator();
135
        while(iteratorEntities.hasNext()) {
136
            CommunityEntity entity = iteratorEntities.next();
137
            if(!entity.getIsEnabled()) {
138
                iteratorEntities.remove();
139
            }
140
        }
141
        communityResponse.setEntities(entities);
142

    
143
        return communityResponse;
144
    }
145
*/
146

    
147
    @RequestMapping(value = "/community/update", method = RequestMethod.POST)
148
    public CommunityResponse updateCommunity(@RequestBody Community community) {
149
        Community com = communityDAO.findById(community.getId());
150
        com.setName(community.getName());
151
        com.setPid(community.getPid());
152
        // = this.getCommunityByCommunityResponse(communityResponse);
153
        communityDAO.save(com);
154
        CommunityResponse communityResponse = this.getCommunityFull(community.getPid());
155

    
156
        return communityResponse;
157
    }
158

    
159
    @RequestMapping(value = "/community/save", method = RequestMethod.POST)
160
    public CommunityResponse insertCommunity(@RequestBody Community community) {
161
        //Community community = this.getCommunityByCommunityResponse(communityResponse);
162

    
163
        List<CommunityEntity> communityEntities = new ArrayList<>();
164
        List<CommunityPage> communityPages = new ArrayList<>();
165
        Map<String, Boolean> entities = new HashMap<>();
166
        Map<String, Boolean> pages = new HashMap<>();
167

    
168
        for(Entity entity : entityDAO.findAll()) {
169
            entities.put(entity.getId(), true);
170

    
171
            CommunityEntity communityEntity = new CommunityEntity(entity);
172
            communityEntity.setIsEnabled(true);
173
            communityEntities.add(communityEntity);
174
        }
175

    
176
        for(Page page : pageDAO.findAll()) {
177
            pages.put(page.getId(), true);
178

    
179
            CommunityPage communityPage = new CommunityPage(page);
180
            communityPage.setIsEnabled(true);
181

    
182
            communityPages.add(communityPage);
183
        }
184

    
185
        community.setEntities(entities);
186
        community.setPages(pages);
187
        Statistics statistics =  new Statistics(community.getPid());
188
        statisticsDAO.save(statistics);
189
        CommunitySubscribers communitySubscribers =  new CommunitySubscribers(community.getPid());
190
        communitySubscribersDAO.save(communitySubscribers);
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
            String pid = communityDAO.findById(id).getPid();
233

    
234
            // delete div contents related to this community
235
            List<DivHelpContentResponse> divHelpContentResponses = divHelpContentController.getDivHelpContents(pid, null, null, null);
236
            for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
237
                divHelpContentController.deleteDivHelpContent(divHelpContentResponse.getId());
238
            }
239

    
240
            // delete page contents related to this community
241
            List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentController.getPageHelpContents(pid, null, null, null, null);
242
            for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
243
                pageHelpContentController.deletePageHelpContent(pageHelpContentResponse.getId());
244
            }
245

    
246
            communityDAO.delete(id);
247
        }
248

    
249
        return true;
250
    }
251

    
252
//    @RequestMapping(value = "/community", method = RequestMethod.DELETE)
253
//    public void deleteAllCommunities() {
254
//        communityDAO.deleteAll();
255
//    }
256

    
257
    @RequestMapping(value = "/community", method = RequestMethod.POST)
258
    public Community insertOrUpdateCommunity(@RequestBody Community community) {
259
        return communityDAO.save(community);
260
    }
261

    
262
    @RequestMapping(value = "/community/{pid}", method = RequestMethod.GET)
263
    public Community getCommunity(@PathVariable(value = "pid") String pid) {
264
        log.debug("PID: "+ pid);
265
        return communityDAO.findByPid(pid);
266
    }
267

    
268
    @RequestMapping(value = "/community/{id}", method = RequestMethod.DELETE)
269
    public void deleteCommunity(@PathVariable(value = "id") String id) {
270
        communityDAO.delete(id);
271
    }
272

    
273
    @RequestMapping(value = "/community/{pid}/pages", method = RequestMethod.GET)
274
    public List<CommunityPage> getPagesForCommunityByType(@PathVariable(value = "pid") String pid,
275
                                                          @RequestParam(value="page_type", required=false) String page_type,
276
                                                          @RequestParam(value="page_route", required=false) String page_route,
277
                                                          @RequestParam(value="div", required = false) String div) {
278
        List<CommunityPage> return_pages = new ArrayList<CommunityPage>();
279
        Map<String, Boolean> pages = communityDAO.findByPid(pid).getPages();
280

    
281
        if(pages != null) {
282
            for (Map.Entry<String, Boolean> page : pages.entrySet()) {
283
                if(div != null && div.equals("true")) {
284
                    Community community = communityDAO.findByPid(pid);
285
                    List<DivId> divIds = divIdDAO.findByPagesContaining(page.getKey());
286
                    if(divIds.isEmpty()) {
287
                        continue;
288
                    }
289
                }
290

    
291
                Page p = pageDAO.findById(page.getKey());
292

    
293
                if((pid.equals("openaire") && p.getOpenaire()) ||(!pid.equals("openaire") && p.getConnect())) {
294
                    if ((page_type == null && page_route == null) || (page_route == null && p.getType().equals(page_type))
295
                            || p.getRoute().equals(page_route)) {
296
                        CommunityPage communityPage = new CommunityPage(p);
297

    
298
                        List<Entity> entities = new ArrayList<>();
299
                        for (String entity : p.getEntities()) {
300
                            entities.add(entityDAO.findById(entity));
301
                        }
302
                        communityPage.setEntities(entities);
303
                        communityPage.setIsEnabled(page.getValue());
304

    
305
                        return_pages.add(communityPage);
306

    
307
                        if (page_route != null) {
308
                            break;
309
                        }
310
                    }
311
                }
312
            }
313
        }
314
        return_pages.sort(Comparator.comparing(CommunityPage::getName));
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

    
(1-1/15)