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 javax.swing.text.html.HTML;
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 HtmlPageContentController htmlPageContentController;
40

    
41
    @Autowired
42
    private DivIdController divIdController;
43

    
44
    @Autowired
45
    private StatisticsDAO statisticsDAO;
46
    @Autowired
47
    private CommunitySubscribersDAO  communitySubscribersDAO;
48

    
49
    @RequestMapping(value = "/community", method = RequestMethod.GET)
50
    public List<Community> getAllCommunities() {
51
        List<Community> communities = communityDAO.findAll();
52

    
53
        return communities;
54
    }
55

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

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

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

    
86
            communitiesResponse.add(communityResponse);
87
        }
88
        return communitiesResponse;
89
    }
90

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

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

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

    
118
        return communityResponse;
119
    }
120
/*
121

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

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

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

    
147
        return communityResponse;
148
    }
149
*/
150

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

    
160
        return communityResponse;
161
    }
162

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

    
167
        List<CommunityEntity> communityEntities = new ArrayList<>();
168
        List<CommunityPage> communityPages = new ArrayList<>();
169
        Map<String, Boolean> entities = new HashMap<>();
170
        Map<String, Boolean> pages = new HashMap<>();
171

    
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

    
180
        for(Page page : pageDAO.findAll()) {
181
            pages.put(page.getId(), true);
182

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

    
186
            communityPages.add(communityPage);
187
        }
188

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

    
198
        log.debug("pid of saved community: "+savedCommunity.getPid());
199

    
200
        String id = savedCommunity.getId();
201

    
202
        String link_context_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Select a research community and/or a category and search for a community concept, or browse to the community tree through the categories</div> </div>";
203
        String link_project_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Search for projects using project name or grant id. Limit results filtering by funder.</div> </div>";
204
        String link_result_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span></div> Search for research results in OpenAIRE information space, Datacite, CrossRef or ORCID. <div class=\"uk-text-small\">Use keywords, DOI (more than one - space separated), author&#39;s ORCID</div> </div>";
205
        String link_result_bulk_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Upload a csv file containing a list of DOIs. For each DOI found in the file, metadata will be fetched from CrossRef or Datacite and will be added to your selected research results.</div> <div class=\"uk-margin-top uk-text-small\"><span class=\"uk-text-bold\">CSV format:</span> <ul class=\"uk-list\"> <li>The format of CSV file should be &quot;DOI&quot;,&quot;ACCESS_MODE&quot;,&quot;DATE&quot;.</li> <li>The value &quot;DOI&quot; is required</li> <li>Access mode column should have values: &quot;OPEN&quot;,&quot;CLOSED&quot; or &quot;EMBARGO&quot;.</li> <li>Date column valid format is YYYY-MM-DD and is required when access mode has value EMBARGO.</li> <li>In case access mode is not available default value is &quot;OPEN&quot;.</li> </ul> </div> </div>";
206
        String link_metadata_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Manage access mode &amp; type of selected research results. For OpenAIRE this functionality isn&#39;t available.</div> </div>";
207

    
208
        DivId div = null;
209
        div=divIdDAO.findByName("link-context-form");
210
        if(div != null) {
211
            String link_context_form = div.getId();
212
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_context_form, id, link_context_form_content, false));
213
        }
214

    
215
        div=divIdDAO.findByName("link-project-form");
216
        if(div != null) {
217
            String link_project_form = div.getId();
218
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_project_form, id, link_project_form_content, false));
219
        }
220

    
221
        div=divIdDAO.findByName("link-result-form");
222
        if(div != null) {
223
            String link_result_form = div.getId();
224
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_result_form, id, link_result_form_content, true));
225
        }
226

    
227
        div=divIdDAO.findByName("link-result-bulk");
228
        if(div != null) {
229
            String link_result_bulk = div.getId();
230
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_result_bulk, id, link_result_bulk_content, true));
231
        }
232

    
233
        div=divIdDAO.findByName("link-metadata");
234
        if(div != null) {
235
            String link_metadata = div.getId();
236
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_metadata, id, link_metadata_content, false));
237
        }
238

    
239
        Page page = null;
240
        page = pageDAO.findByRoute("/about" );
241
        if(page != null) {
242
            String htmlContent = "<div><div class=\"uk-article-title custom-article-title\"> About the community </div> <p> This is an introductory text. To be updated... </p> </div>";
243
            HtmlPageContent htmlPageContent = new HtmlPageContent(page.getId(), id, htmlContent);
244
            htmlPageContentController.updateHtmlPageContent(htmlPageContent);
245
        }
246

    
247
        page = pageDAO.findByRoute("/organizations");
248
        if(page != null) {
249
            String htmlContent = "<div><div class=\"uk-article-title custom-article-title\"> Organizations related to the community </div> <p> This is an introductory text. Here follows the list of organizations... </p> <div class=\"uk-child-width-1-3@m uk-text-center uk-grid-match \" uk-grid > <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"https://upload.wikimedia.org/wikipedia/el/2/2b/Logo_uoa_blue.png\" alt=\"\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"https://www.uoa.gr/\">University of Athens</a> </h3> </div> </div> <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"https://pbs.twimg.com/profile_images/631127495933165569/ElbqhHK0_400x400.jpg\" alt=\"\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"https://www.athena-innovation.gr/en\">Athena Research & Innovation center</a> </h3> </div> </div> <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"\" alt=\"Logo 1\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"\">Organization 1</a> </h3> </div> </div> <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"\" alt=\"Logo 2\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"\">Organization 2</a> </h3> </div> </div> <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"\" alt=\"Logo 3\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"\">Organization 3</a> </h3> </div> </div> </div></div>";
250
            HtmlPageContent htmlPageContent = new HtmlPageContent(page.getId(), id, htmlContent);
251
            htmlPageContentController.updateHtmlPageContent(htmlPageContent);
252
        }
253

    
254
        return communityResponse;
255
    }
256

    
257
    private Community getCommunityByCommunityResponse(CommunityResponse communityResponse) {
258
        Community community = new Community();
259
        community.setId(communityResponse.getId());
260
        community.setName(communityResponse.getName());
261

    
262
        List<CommunityEntity> fullEntities = communityResponse.getEntities();
263
        Map<String, Boolean> entities = new HashMap<String, Boolean>();
264
        for(CommunityEntity entity : fullEntities) {
265
            entities.put(entity.getId(), true);
266
        }
267
        for(Entity entity : entityDAO.findAll()) {
268
            if(!entities.containsKey(entity.getId())) {
269
                entities.put(entity.getId(), false);
270
            }
271
        }
272
        community.setEntities(entities);
273

    
274
        List<CommunityPage> fullPages = communityResponse.getPages();
275
        Map<String, Boolean> pages = new HashMap<String, Boolean>();
276
        for(CommunityPage page : fullPages) {
277
            pages.put(page.getId(), true);
278
        }
279
        for(Page page : pageDAO.findAll()) {
280
            if(!pages.containsKey(page.getId())) {
281
                pages.put(page.getId(), false);
282
            }
283
        }
284
        community.setPages(pages);
285

    
286
        return community;
287
    }
288

    
289
    @RequestMapping(value = "/community/delete", method = RequestMethod.POST)
290
    public Boolean deleteCommunities(@RequestBody List<String> communities) throws Exception {
291
        for (String id: communities) {
292
            String pid = communityDAO.findById(id).getPid();
293

    
294
            // delete div contents related to this community
295
            List<DivHelpContentResponse> divHelpContentResponses = divHelpContentController.getDivHelpContents(pid, null, null, null);
296
            for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
297
                divHelpContentController.deleteDivHelpContent(divHelpContentResponse.getId());
298
            }
299

    
300
            // delete page contents related to this community
301
            List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentController.getPageHelpContents(pid, null, null, null, null);
302
            for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
303
                pageHelpContentController.deletePageHelpContent(pageHelpContentResponse.getId());
304
            }
305

    
306
            List<HtmlPageContent> htmlPageContents = htmlPageContentController.getHtmlPageContents(pid, null);
307
            for(HtmlPageContent htmlPageContent : htmlPageContents) {
308
                htmlPageContentController.deleteHtmlPageContent(htmlPageContent.getId());
309
            }
310

    
311
            Statistics stats = statisticsDAO.findByPid(pid);
312
            if(stats != null) {
313
                statisticsDAO.delete(stats.getId());
314
            }
315

    
316
            CommunitySubscribers communitySubscribers = communitySubscribersDAO.findByPid(pid);
317
            if(communitySubscribers != null) {
318
                communitySubscribersDAO.delete(communitySubscribers.getId());
319
            }
320

    
321
            communityDAO.delete(id);
322
        }
323

    
324
        return true;
325
    }
326

    
327
//    @RequestMapping(value = "/community", method = RequestMethod.DELETE)
328
//    public void deleteAllCommunities() {
329
//        communityDAO.deleteAll();
330
//    }
331

    
332
    @RequestMapping(value = "/community", method = RequestMethod.POST)
333
    public Community insertOrUpdateCommunity(@RequestBody Community community) {
334
        return communityDAO.save(community);
335
    }
336

    
337
    @RequestMapping(value = "/community/{pid}", method = RequestMethod.GET)
338
    public Community getCommunity(@PathVariable(value = "pid") String pid) {
339
        log.debug("PID: "+ pid);
340
        return communityDAO.findByPid(pid);
341
    }
342

    
343
    @RequestMapping(value = "/community/{id}", method = RequestMethod.DELETE)
344
    public void deleteCommunity(@PathVariable(value = "id") String id) {
345
        communityDAO.delete(id);
346
    }
347

    
348
    @RequestMapping(value = "/community/{pid}/pages", method = RequestMethod.GET)
349
    public List<CommunityPage> getPagesForCommunityByType(@PathVariable(value = "pid") String pid,
350
                                                          @RequestParam(value="page_type", required=false) String page_type,
351
                                                          @RequestParam(value="page_route", required=false) String page_route,
352
                                                          @RequestParam(value="div", required = false) String div) {
353
        List<CommunityPage> return_pages = new ArrayList<CommunityPage>();
354
        Map<String, Boolean> pages = communityDAO.findByPid(pid).getPages();
355

    
356
        if(pages != null) {
357
            for (Map.Entry<String, Boolean> page : pages.entrySet()) {
358
                if(div != null && div.equals("true")) {
359
                    Community community = communityDAO.findByPid(pid);
360
                    List<DivId> divIds = divIdDAO.findByPagesContaining(page.getKey());
361
                    if(divIds.isEmpty()) {
362
                        continue;
363
                    }
364
                }
365

    
366
                Page p = pageDAO.findById(page.getKey());
367

    
368
                if((pid.equals("openaire") && p.getOpenaire()) ||(!pid.equals("openaire") && p.getConnect())) {
369
                    if ((page_type == null && page_route == null) || (page_route == null && p.getType().equals(page_type))
370
                            || p.getRoute().equals(page_route)) {
371
                        CommunityPage communityPage = new CommunityPage(p);
372

    
373
                        List<Entity> entities = new ArrayList<>();
374
                        for (String entity : p.getEntities()) {
375
                            entities.add(entityDAO.findById(entity));
376
                        }
377
                        communityPage.setEntities(entities);
378
                        communityPage.setIsEnabled(page.getValue());
379

    
380
                        return_pages.add(communityPage);
381

    
382
                        if (page_route != null) {
383
                            break;
384
                        }
385
                    }
386
                }
387
            }
388
        }
389
        return_pages.sort(Comparator.comparing(CommunityPage::getName));
390
        return return_pages;
391
    }
392

    
393
    @RequestMapping(value = "/community/{id}/page", method = RequestMethod.POST)
394
    public Community insertOrUpdatePage(@PathVariable(value = "id") String id, @RequestBody CommunityPage page) {
395
        Community community = communityDAO.findById(id);
396
        Map<String, Boolean> pages = community.getPages();
397

    
398
        String name = page.getName();
399
        boolean isEnabled = page.getIsEnabled();
400

    
401
        pages.put(name, isEnabled);
402
        community.setPages(pages);
403

    
404
        return communityDAO.save(community);
405
    }
406

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

    
412
        for (String pageId: pageIds) {
413
            log.debug("Toggle community page: " + pageId + " of community: " + pid + " to " + status);
414
            pages.put(pageId, Boolean.parseBoolean(status));
415
        }
416

    
417
        community.setPages(pages);
418
        return communityDAO.save(community);
419
    }
420

    
421
    @RequestMapping(value = "community/{pid}/entity/toggle", method = RequestMethod.POST)
422
    public Community toggleEntity(@PathVariable(value = "pid") String pid, @RequestBody List<String> entityIds, @RequestParam String status) throws Exception {
423
        Community community = communityDAO.findByPid(pid);
424
        Map<String, Boolean> entities = community.getEntities();
425
        Map<String, Boolean> pages = community.getPages();
426

    
427
        for (String entityId: entityIds) {
428
            log.debug("Toggle community entity: " + entityId + " of community: " + pid + " to " + status);
429

    
430
            entities.put(entityId, Boolean.parseBoolean(status));
431

    
432
            if(pages != null) {
433
                for (Map.Entry<String, Boolean> pageEntry : pages.entrySet()) {
434
                    Page page = pageDAO.findById(pageEntry.getKey());
435
                    if (page.getEntities().contains(entityId) && page.getType().equals("search")) {
436
                        pages.put(pageEntry.getKey(), Boolean.parseBoolean(status));
437
                    }
438
                }
439
            }
440
        }
441

    
442
        community.setEntities(entities);
443
        return communityDAO.save(community);
444
    }
445

    
446
    @RequestMapping(value = "/community/{pid}/entities", method = RequestMethod.GET)
447
    public List<CommunityEntity> getEntitiesForCommunity(@PathVariable(value = "pid") String pid, @RequestParam(value="entity", required=false) String entity) {
448
        List<CommunityEntity> return_entities = new ArrayList<CommunityEntity>();
449
        Map<String, Boolean> entities = communityDAO.findByPid(pid).getEntities();
450

    
451
        log.debug("/community/"+pid+"/entities -- entity: "+entity);
452
        if (entity != null) {
453
            String entityId = entityDAO.findByPid(entity).getId();
454
            CommunityEntity communityEntity = new CommunityEntity(entityDAO.findById(entityId));
455
            communityEntity.setIsEnabled(entities.get(entityId));
456
            return_entities.add(communityEntity);
457
        } else {
458
            if(entities != null) {
459
                for (Map.Entry<String, Boolean> _entity : entities.entrySet()) {
460
                    CommunityEntity communityEntity = new CommunityEntity(entityDAO.findById(_entity.getKey()));
461
                    communityEntity.setIsEnabled(_entity.getValue());
462
                    return_entities.add(communityEntity);
463
                }
464
            }
465
        }
466
        return return_entities;
467
    }
468
}
469

    
(1-1/15)