Project

General

Profile

1
package eu.dnetlib.uoaadmintoolslibrary.services;
2

    
3
import eu.dnetlib.uoaadmintoolslibrary.dao.PortalDAO;
4
import eu.dnetlib.uoaadmintoolslibrary.entities.DivId;
5
import eu.dnetlib.uoaadmintoolslibrary.entities.Entity;
6
import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
7
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
8
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.*;
9

    
10
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
11
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
12
import org.apache.log4j.Logger;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.stereotype.Service;
15

    
16
import java.util.*;
17

    
18
@Service
19
public class PortalService {
20
    private final Logger log = Logger.getLogger(this.getClass());
21

    
22
    @Autowired
23
    private PortalDAO portalDAO;
24

    
25
    @Autowired
26
    private PageService pageService;
27

    
28
    @Autowired
29
    private EntityService entityService;
30

    
31
    @Autowired
32
    private DivIdService divIdService;
33

    
34
    @Autowired
35
    private PageHelpContentService pageHelpContentService;
36

    
37
    @Autowired
38
    private DivHelpContentService divHelpContentService;
39

    
40
    public List<Portal> getAllPortals() {
41
        List<Portal> portals = portalDAO.findAll();
42

    
43
        return portals;
44
    }
45

    
46
    public List<Portal> getAllPortalsByType(String type) {
47
        List<Portal> portals = portalDAO.findByType(type);
48

    
49
        return portals;
50
    }
51

    
52
    public List<PortalResponse> getPortalsFull(List<Portal> portals) {
53
        List<PortalResponse> portalsResponse = new ArrayList<>();
54
        for(Portal portal : portals) {
55
            PortalResponse portalResponse = new PortalResponse(portal);
56

    
57
            setEnabledPagesForPortalByType(portal, portalResponse);
58
            setEnabledEntitiesForPortalByType(portal, portalResponse);
59

    
60
            //Layout layout = layoutDAO.findById(portal.getLayout());
61
            //portalResponse.setLayout(layout);
62

    
63
            portalsResponse.add(portalResponse);
64
        }
65
        return portalsResponse;
66
    }
67

    
68
    public List<PortalResponse> getAllPortalsFull() {
69
        List<Portal> portals = this.getAllPortals();
70
        return this.getPortalsFull(portals);
71
    }
72

    
73
    public List<PortalResponse> getAllPortalsFullByType(String type) {
74
        List<Portal> portals = this.getAllPortalsByType(type);
75
        return this.getPortalsFull(portals);
76
    }
77

    
78
    private void setEnabledPagesForPortalByType(Portal portal, PortalResponse portalResponse) {
79
        List<PortalPage> pages = this.getPagesForPortalByType(portal.getPid(), null, null, null, null);
80
//        log.debug("PAGES number="+pages.size());
81
        Iterator<PortalPage> iteratorPages = pages.iterator();
82
        while(iteratorPages.hasNext()) {
83
            PortalPage page = iteratorPages.next();
84
            if(!page.getIsEnabled()) {
85
                iteratorPages.remove();
86
            }
87
        }
88
        portalResponse.setPages(pages);
89
//        log.debug("PAGES set");
90
    }
91

    
92
    private void setEnabledEntitiesForPortalByType(Portal portal, PortalResponse portalResponse) {
93
        List<PortalEntity> entities = this.getEntitiesForPortal(portal.getPid(), null);
94
//        log.debug("ENTITIES number="+entities.size());
95
        Iterator<PortalEntity> iteratorEntities = entities.iterator();
96
        while(iteratorEntities.hasNext()) {
97
            PortalEntity entity = iteratorEntities.next();
98
            if(!entity.getIsEnabled()) {
99
                iteratorEntities.remove();
100
            }
101
        }
102
        portalResponse.setEntities(entities);
103
//        log.debug("ENTITIES set");
104
    }
105

    
106
    public PortalResponse getPortalFull(String pid) {
107
        Portal portal = portalDAO.findByPid(pid);
108
        if(pid != null && portal == null) {
109
            return null;
110
        }
111
        PortalResponse portalResponse = new PortalResponse(portal);
112

    
113
        setEnabledPagesForPortalByType(portal, portalResponse);
114
        setEnabledEntitiesForPortalByType(portal, portalResponse);
115

    
116
        //Layout layout = layoutDAO.findById(portal.getLayout());
117
        //portalResponse.setLayout(layout);
118

    
119
        return portalResponse;
120
    }
121

    
122
    public PortalResponse updatePortal(Portal portal) {
123
        Portal com = portalDAO.findById(portal.getId());
124

    
125
//        Statistics statistics = statisticsDAO.findByPid(com.getPid());
126
//        statistics.setPid(portal.getPid());
127
//        statisticsDAO.save(statistics);
128
//
129
//        PortalSubscribers portalSubscribers =  communitySubscribersDAO.findByPid(com.getPid());
130
//        portalSubscribers.setPid(portal.getPid());
131
//        communitySubscribersDAO.save(portalSubscribers);
132

    
133
        com.setName(portal.getName());
134
        com.setPid(portal.getPid());
135
        com.setPiwik(portal.getPiwik());
136

    
137
        portalDAO.save(com);
138
        PortalResponse portalResponse = this.getPortalFull(portal.getPid());
139

    
140
        return portalResponse;
141
    }
142

    
143
    public PortalResponse insertPortal(Portal portal) {
144
        List<PortalEntity> portalEntities = new ArrayList<>();
145
        List<PortalPage> portalPages = new ArrayList<>();
146
        Map<String, Boolean> entities = new HashMap<>();
147
        Map<String, Boolean> pages = new HashMap<>();
148

    
149
        for(Entity entity : entityService.getAllEntities()) {
150
            entities.put(entity.getId(), true);
151

    
152
            PortalEntity portalEntity = new PortalEntity(entity);
153
            portalEntity.setIsEnabled(true);
154
            portalEntities.add(portalEntity);
155
        }
156

    
157
        for(Page page : pageService.getPagesByPortalType(portal.getType())) {
158
            pages.put(page.getId(), true);
159

    
160
            PortalPage portalPage = new PortalPage(page);
161
            if(page.getRoute().equals("/curators") || page.getRoute().equals("/organizations")) {
162
                portalPage.setIsEnabled(false);
163
            } else {
164
                portalPage.setIsEnabled(true);
165
            }
166

    
167
            portalPages.add(portalPage);
168
        }
169

    
170
        portal.setEntities(entities);
171
        portal.setPages(pages);
172
//        Statistics statistics =  new Statistics(portal.getPid());
173
//        statisticsDAO.save(statistics);
174
//        PortalSubscribers portalSubscribers =  new PortalSubscribers(portal.getPid());
175
//        communitySubscribersDAO.save(portalSubscribers);
176
        Portal savedPortal = portalDAO.save(portal);
177
        PortalResponse portalResponse = this.getPortalFull(savedPortal.getPid());
178

    
179
//        log.debug("pid of saved portal: "+ savedPortal.getPid());
180

    
181
        String id = savedPortal.getId();
182

    
183
        divHelpContentService.addDivHelpContentsInPortal(id, savedPortal.getType());
184
        pageHelpContentService.addPageHelpContentsInPortal(id, savedPortal.getType());
185

    
186
        return portalResponse;
187
    }
188

    
189
    private Portal getPortalByPortalResponse(PortalResponse portalResponse) {
190
        Portal portal = new Portal();
191
        portal.setId(portalResponse.getId());
192
        portal.setName(portalResponse.getName());
193

    
194
        List<PortalEntity> fullEntities = portalResponse.getEntities();
195
        Map<String, Boolean> entities = new HashMap<String, Boolean>();
196
        for(PortalEntity entity : fullEntities) {
197
            entities.put(entity.getId(), true);
198
        }
199
        for(Entity entity : entityService.getAllEntities()) {
200
            if(!entities.containsKey(entity.getId())) {
201
                entities.put(entity.getId(), false);
202
            }
203
        }
204
        portal.setEntities(entities);
205

    
206
        List<PortalPage> fullPages = portalResponse.getPages();
207
        Map<String, Boolean> pages = new HashMap<String, Boolean>();
208
        for(PortalPage page : fullPages) {
209
            pages.put(page.getId(), true);
210
        }
211
        for(Page page : pageService.getAllPages(null, null, null)) {
212
            if(!pages.containsKey(page.getId())) {
213
                pages.put(page.getId(), false);
214
            }
215
        }
216
        portal.setPages(pages);
217
//        Layout layout = portalResponse.getLayout();
218
//        portal.setLayout(layout.getId());
219

    
220
        return portal;
221
    }
222

    
223
    public Boolean deletePortals(List<String> portals) throws Exception {
224
        for (String id: portals) {
225
            Portal portal = portalDAO.findById(id);
226
            String pid = portal.getPid();
227

    
228
            // delete div contents related to this portal
229
            List<DivHelpContentResponse> divHelpContentResponses = divHelpContentService.getDivHelpContents(pid, null, null, null);
230
            for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
231
                divHelpContentService.deleteDivHelpContent(divHelpContentResponse.getId());
232
            }
233

    
234
            // delete page contents related to this portal
235
            List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentService.getPageHelpContents(pid, null, null,null, null, null);
236
            for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
237
                pageHelpContentService.deletePageHelpContent(pageHelpContentResponse.getId());
238
            }
239

    
240
//            Statistics stats = statisticsDAO.findByPid(pid);
241
//            if(stats != null) {
242
//                statisticsDAO.delete(stats.getId());
243
//            }
244
//
245
//            PortalSubscribers portalSubscribers = communitySubscribersDAO.findByPid(pid);
246
//            if(portalSubscribers != null) {
247
//                communitySubscribersDAO.delete(portalSubscribers.getId());
248
//            }
249
//
250
//            Layout layout = layoutDAO.findById(portal.getLayout());
251
//            if(layout != null) {
252
//                layoutDAO.delete(layout.getId());
253
//            }
254

    
255
            portalDAO.delete(id);
256
        }
257

    
258
        return true;
259
    }
260

    
261
    public String deletePortal(String id) {
262
        Portal portal = portalDAO.findById(id);
263
        String pid = portal.getPid();
264

    
265
        // delete div contents related to this portal
266
        List<DivHelpContentResponse> divHelpContentResponses = divHelpContentService.getDivHelpContents(pid, null, null, null);
267
        for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
268
            divHelpContentService.deleteDivHelpContent(divHelpContentResponse.getId());
269
        }
270

    
271
        // delete page contents related to this portal
272
        List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentService.getPageHelpContents(pid, null, null,null, null, null);
273
        for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
274
            pageHelpContentService.deletePageHelpContent(pageHelpContentResponse.getId());
275
        }
276

    
277
        // if no other portlas with the same type, delete pages and divIds for this portal type
278
        List<Portal> portalsWithSameType = portalDAO.findByType(portal.getType());
279
        if(portalsWithSameType == null || portalsWithSameType.size() == 1) {
280
            // delete pages for this portal type
281
            for(String pageId : portal.getPages().keySet()) {
282
                pageService.deletePage(pageId);
283
            }
284

    
285
            // delete divIds for this portal type
286
            List<DivId> divIds = divIdService.getDivIdsByPortalType(portal.getType());
287
            for(DivId divId : divIds) {
288
                divIdService.deleteDivId(divId.getId());
289
            }
290
        }
291

    
292
        portalDAO.delete(id);
293
        return pid;
294
    }
295

    
296
    public Portal insertOrUpdatePortal(Portal portal) {
297
        return portalDAO.save(portal);
298
    }
299

    
300
    public Portal getPortalById(String id) {
301
//        log.debug("ID: "+ id);
302
        return portalDAO.findById(id);
303
    }
304

    
305
    public Portal getPortal(String pid) {
306
//        log.debug("PID: "+ pid);
307
        return portalDAO.findByPid(pid);
308
    }
309

    
310
    public void deletePortalId(String id) {
311
        portalDAO.delete(id);
312
    }
313

    
314
    public List<PortalPage> getPagesForPortalByType(String pid, String page_type, String page_route, String div, String with_positions) {
315
        List<PortalPage> return_pages = new ArrayList<PortalPage>();
316
        Portal portal = portalDAO.findByPid(pid);
317
        if(portal == null) {
318
            throw new ContentNotFoundException("Portal with pid: " + pid + " not found");
319
        }
320
        Map<String, Boolean> pages = portal.getPages();
321

    
322
        if(pages != null) {
323
            for (Map.Entry<String, Boolean> page : pages.entrySet()) {
324
                if(div != null && div.equals("true")) {
325
                    List<DivId> divIds = divIdService.getDivIds(page.getKey(), null, null);
326
                    Iterator<DivId> divIdIterator = divIds.iterator();
327

    
328
                    while (divIdIterator.hasNext()) {
329
                        DivId divId = divIdIterator.next();
330
                        if(!portal.getType().equals(divId.getPortalType())) {
331
                            divIdIterator.remove();
332
                        }
333
                    }
334

    
335
                    if(divIds.isEmpty()) {
336
                        continue;
337
                    }
338
                }
339

    
340
                Page p = pageService.getPage(page.getKey());
341

    
342
                if (with_positions != null) {
343
                    boolean at_least_one_position = Boolean.parseBoolean(with_positions);
344

    
345
                    if(at_least_one_position) {
346
                        if(!p.getTop() && !p.getBottom() && !p.getLeft() && !p.getRight()) {
347
                            continue;
348
                        }
349
                    } else {
350
                        if(p.getTop() || p.getBottom() || p.getLeft() || p.getRight()) {
351
                            continue;
352
                        }
353
                    }
354
                }
355

    
356
                if(portal.getType().equals(p.getPortalType())) {
357
                    if ((page_type == null && page_route == null) || (page_route == null && p.getType().equals(page_type))
358
                            || p.getRoute().equals(page_route)) {
359
                        PortalPage portalPage = new PortalPage(p);
360

    
361
                        List<Entity> entities = new ArrayList<>();
362
                        for (String entity : p.getEntities()) {
363
                            entities.add(entityService.getEntity(entity));
364
                        }
365
                        portalPage.setEntities(entities);
366
                        portalPage.setIsEnabled(page.getValue());
367

    
368
                        return_pages.add(portalPage);
369

    
370
                        if (page_route != null) {
371
                            break;
372
                        }
373
                    }
374
                }
375
            }
376
        }
377
        return_pages.sort(Comparator.comparing(PortalPage::getName));
378
        return return_pages;
379
    }
380

    
381
    public Portal insertOrUpdatePage(String id, PortalPage page) {
382
        Portal portal = portalDAO.findById(id);
383
        Map<String, Boolean> pages = portal.getPages();
384

    
385
        String name = page.getName();
386
        boolean isEnabled = page.getIsEnabled();
387

    
388
        pages.put(name, isEnabled);
389
        portal.setPages(pages);
390

    
391
        return portalDAO.save(portal);
392
    }
393

    
394
    public Portal togglePage(String pid, String portalType, List<String> pageIds, String status) throws Exception {
395
        Portal portal = portalDAO.findByPid(pid);
396
        checkPortalInfo(pid, portalType, portal, pid, "pid");
397
        Map<String, Boolean> pages = portal.getPages();
398

    
399
        for (String pageId: pageIds) {
400
            Page page = pageService.getPage(pageId);
401
            if(page == null) {
402
                throw new ContentNotFoundException("Page with id: " + pageId + " not found");
403
            }
404
            if(!page.getPortalType().equals(portalType)) {
405
                throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting page type: "+page.getPortalType());
406
            }
407
//            log.debug("Toggle portal page: " + pageId + " of portal: " + pid + " to " + status);
408
            pages.put(pageId, Boolean.parseBoolean(status));
409
        }
410

    
411
        portal.setPages(pages);
412
        return portalDAO.save(portal);
413
    }
414

    
415
    public Portal toggleEntity(String pid, List<String> entityIds, String status) throws Exception {
416
        Portal portal = portalDAO.findByPid(pid);
417
        Map<String, Boolean> entities = portal.getEntities();
418
        Map<String, Boolean> pages = portal.getPages();
419

    
420
        for (String entityId: entityIds) {
421
//            log.debug("Toggle portal entity: " + entityId + " of portal: " + pid + " to " + status);
422

    
423
            entities.put(entityId, Boolean.parseBoolean(status));
424

    
425
            if(pages != null) {
426
                for (Map.Entry<String, Boolean> pageEntry : pages.entrySet()) {
427
                    Page page = pageService.getPage(pageEntry.getKey());
428
                    if (page.getEntities().contains(entityId) && page.getType().equals("search")) {
429
                        pages.put(pageEntry.getKey(), Boolean.parseBoolean(status));
430
                    }
431
                }
432
            }
433
        }
434

    
435
        portal.setEntities(entities);
436
        return portalDAO.save(portal);
437
    }
438

    
439
    public List<PortalEntity> getEntitiesForPortal(String pid, String entity) {
440
        List<PortalEntity> return_entities = new ArrayList<PortalEntity>();
441
        Map<String, Boolean> entities = portalDAO.findByPid(pid).getEntities();
442

    
443
        //log.debug("/portal/"+pid+"/entities -- entity: "+entity);
444
        if (entity != null) {
445
            Entity _entity = entityService.getEntityByPid(entity);
446
            String entityId = _entity.getId();
447
            PortalEntity portalEntity = new PortalEntity(_entity);
448
            portalEntity.setIsEnabled(entities.get(entityId));
449
            return_entities.add(portalEntity);
450
        } else {
451
            if(entities != null) {
452
                for (Map.Entry<String, Boolean> _entity : entities.entrySet()) {
453
                    PortalEntity portalEntity = new PortalEntity(entityService.getEntity(_entity.getKey()));
454
                    portalEntity.setIsEnabled(_entity.getValue());
455
                    return_entities.add(portalEntity);
456
                }
457
            }
458
        }
459
        return return_entities;
460
    }
461

    
462
//    @RequestMapping(value = "/community/{pid}/layout", method = RequestMethod.GET)
463
//    public Layout getLayoutForCommunity(@PathVariable(value = "pid") String pid) {
464
//        Portal portal = portalDAO.findByPid(pid);
465
//        if(portal.getLayout() != null) {
466
//            return layoutDAO.findById(portal.getLayout());
467
//        } else {
468
//            return null;
469
//        }
470
//    }
471

    
472
//    @RequestMapping(value = "/community/{pid}/layout", method = RequestMethod.POST)
473
//    public Layout updateLayoutForCommunity(@PathVariable(value = "pid") String pid, @RequestBody Layout layout) {
474
//        Portal portal = portalDAO.findByPid(pid);
475
//        if(portal.getLayout() != null) {
476
//            layout.setId(portal.getLayout());
477
//            layout = layoutDAO.save(layout);
478
//        } else {
479
//            layout = layoutDAO.save(layout);
480
//            portal.setLayout(layout.getId());
481
//            portalDAO.save(portal);
482
//        }
483
//        return layout;
484
//    }
485

    
486

    
487
    public Map<String, List<PageHelpContentResponse>> getPageHelpContentsByPosition(String pid, String page, String active) {
488
        Map<String, List<PageHelpContentResponse>> pageHelpContentResponses = new HashMap<>();
489

    
490
        List<PageHelpContentResponse> pageHelpContents = null;
491
        pageHelpContents = pageHelpContentService.getPageHelpContents(pid, null, page, null, active, null);
492

    
493
        pageHelpContentResponses.put("top", new ArrayList<>());
494
        pageHelpContentResponses.put("bottom", new ArrayList<>());
495
        pageHelpContentResponses.put("left", new ArrayList<>());
496
        pageHelpContentResponses.put("right", new ArrayList<>());
497

    
498
        for (PageHelpContentResponse pageHelpContentResponse : pageHelpContents) {
499
            pageHelpContentResponses.get(pageHelpContentResponse.getPlacement()).add(pageHelpContentResponse);
500
        }
501

    
502

    
503
        return pageHelpContentResponses;
504
    }
505

    
506
    public Map<String, List<DivHelpContentResponse>> getDivHelpContentsByPosition(String pid, String page, String active) {
507
        Map<String, List<DivHelpContentResponse>> divHelpContentResponses = new HashMap<>();
508

    
509
        List<DivHelpContentResponse> divHelpContents = null;
510
        divHelpContents = divHelpContentService.getDivHelpContents(pid, page, null, active);
511

    
512
        for (DivHelpContentResponse divHelpContentResponse : divHelpContents) {
513
            if(!divHelpContentResponses.containsKey(divHelpContentResponse.getDivId())) {
514
                divHelpContentResponses.put(divHelpContentResponse.getDivId().getName(), new ArrayList<>());
515
            }
516
            divHelpContentResponses.get(divHelpContentResponse.getDivId().getName()).add(divHelpContentResponse);
517
        }
518

    
519

    
520
        return divHelpContentResponses;
521
    }
522

    
523
    public void checkPortalInfo(String pid, String portalType, Portal portal, String portalId, String getBy) {
524
        if(portal == null) {
525
            if(portalId != null) {
526
                throw new ContentNotFoundException("Portal with "+getBy+": " + portalId + " not found");
527
            }
528
        }
529
        if(!portal.getType().equals(portalType)) {
530
            throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting portal info: type: "+portal.getType());
531
        }
532
        if(!portal.getPid().equals(pid)) {
533
            throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting portal info: pid: "+portal.getPid());
534
        }
535
    }
536
}
(6-6/6)