Project

General

Profile

« Previous | Next » 

Revision 58031

[Trunk - Admin tools library Service]:
1. Created services for all entities: PortalService.java, PageService.java, EntityService.java, DivIdService.java, PageHelpContentService.java, DivHelpContentService.java.
2. PortalController.java: Deleted from library. Each service will have its own portal controllers, that will use methods from PortalService.java.
3. In all controllers use methods only from services.

View differences:

modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/services/DivIdService.java
1
package eu.dnetlib.uoaadmintoolslibrary.services;
2

  
3
import eu.dnetlib.uoaadmintoolslibrary.controllers.DivHelpContentController;
4
import eu.dnetlib.uoaadmintoolslibrary.dao.DivIdDAO;
5
import eu.dnetlib.uoaadmintoolslibrary.entities.DivId;
6
import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
7
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
8
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.DivHelpContentResponse;
9
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.DivIdResponse;
10

  
11
import org.apache.log4j.Logger;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.stereotype.Service;
14

  
15
import java.util.*;
16

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

  
21
    @Autowired
22
    private DivIdDAO divIdDAO;
23

  
24
    @Autowired
25
    private PageService pageService;
26

  
27
    @Autowired
28
    private PortalService portalService;
29

  
30
    @Autowired
31
    private DivHelpContentController divHelpContentController;
32

  
33
    public List<DivId> getDivIds(String page, String name, String pid) {
34
        Portal portal = portalService.getPortal(pid);
35
        String dashboardAlias = portal.getAlias();
36

  
37
        List<DivId> divIds = null;
38

  
39
        if(page != null && name != null) {
40
            DivId divId = divIdDAO.findByPagesContainingAndName(page, name);
41
            if(divId != null) {
42
                divIds = new ArrayList<>();
43
                divIds.add(divId);
44
            }
45
        } else if(page != null) {
46
            divIds = divIdDAO.findByPagesContaining(page);
47
        } else if(name != null) {
48
            DivId divId = divIdDAO.findByName(name);
49
            if(divId != null) {
50
                divIds = new ArrayList<>();
51
                divIds.add(divId);
52
            }
53
        } else {
54
            divIds = divIdDAO.findAll();
55
        }
56

  
57
        if(dashboardAlias != null) {
58
            Iterator<DivId> iteratorDivIds = divIds.iterator();
59
            while(iteratorDivIds.hasNext()) {
60
                DivId divId = iteratorDivIds.next();
61
                if (!dashboardAlias.equals(divId.getDashboardAlias())) {
62
                    iteratorDivIds.remove();
63
                }
64
            }
65
        }
66

  
67
        return divIds;
68
    }
69

  
70
    public List<DivIdResponse> getDivIdsFull(String page, String name, String pid) {
71

  
72
        List<DivId> divIds = this.getDivIds(page, name, pid);
73

  
74
        List<DivIdResponse> divIdResponses = new ArrayList<>();
75
        for(DivId divId : divIds) {
76
            DivIdResponse divIdResponse = new DivIdResponse(divId);
77
            List<Page> pages = new ArrayList<>();
78
            for(String pageId : divId.getPages()) {
79
                pages.add(pageService.getPage(pageId));
80
            }
81
            divIdResponse.setPages(pages);
82

  
83
            divIdResponses.add(divIdResponse);
84
        }
85

  
86
        return divIdResponses;
87
    }
88

  
89
    public DivId getDivIdByName(String name) {
90
        return divIdDAO.findByName(name);
91
    }
92

  
93
    public DivId getDivId(String id) {
94
        return divIdDAO.findById(id);
95
    }
96

  
97
    public DivIdResponse getDivIdFull(String id) {
98
        DivId divId = divIdDAO.findById(id);
99
        return divIdResponseFromDivId(divId);
100
    }
101

  
102
    public DivIdResponse divIdResponseFromDivId(DivId divId) {
103
        DivIdResponse divIdResponse = new DivIdResponse(divId);
104
        List<Page> pages = new ArrayList<>();
105
        for(String pageId : divId.getPages()) {
106
            pages.add(pageService.getPage(pageId));
107
        }
108
        divIdResponse.setPages(pages);
109
        return divIdResponse;
110
    }
111

  
112
    public void deleteAllDivIds() {
113
        divIdDAO.deleteAll();
114
    }
115

  
116
    public DivIdResponse insertDivId(DivIdResponse divIdResponse) {
117
        DivId divId = this.getDivIdByDivIdResponse(divIdResponse);
118

  
119
        DivId savedDivId = divIdDAO.save(divId);
120
        divIdResponse.setId(savedDivId.getId());
121

  
122
        return this.getDivIdFull(divId.getId());
123
    }
124

  
125
    private DivId getDivIdByDivIdResponse(DivIdResponse divIdResponse) {
126
        DivId divId = new DivId();
127
        divId.setId(divIdResponse.getId());
128
        divId.setName(divIdResponse.getName());
129
        divId.setDashboardAlias(divIdResponse.getDashboardAlias());
130

  
131
        List<Page> fullPages = divIdResponse.getPages();
132
        List<String> pages = new ArrayList<String>();
133
        for(Page page : fullPages) {
134
            pages.add(page.getId());
135
        }
136
        divId.setPages(pages);
137

  
138
        return divId;
139
    }
140

  
141
    public DivIdResponse updateDivId(DivIdResponse divIdResponse) {
142
//        DivId divIdOld = divIdDAO.findById(divIdResponse.getId());
143
//        DivId divId = this.getDivIdByDivIdResponse(divIdResponse);
144
//        divIdDAO.save(divId);
145
//
146
//        boolean openaireEnabled = divId.getOpenaire();
147
//        boolean connectEnabled = divId.getConnect();
148
//
149
//        if(divId.getCommunities() && !divIdOld.getCommunities()) {
150
//            List<Portal> communities = communityController.getAllPortals();
151
//            for( Portal community : communities ) {
152
//                if(!community.getPid().equals("openaire") && !community.getPid().equals("connect")) {
153
//                    divHelpContentController.addDivHelpContentsInPortal(community.getPid(), community.getId(), divId.getName());
154
//                }
155
//            }
156
//        }
157
//        if(openaireEnabled && !divIdOld.getOpenaire()) {
158
//            Portal community = communityController.getPortal("openaire");
159
//            divHelpContentController.addDivHelpContentsInPortal(community.getPid(), community.getId(), divId.getName());
160
//        }
161
//
162
//        if(connectEnabled && !divIdOld.getConnect()) {
163
//            Portal community = communityController.getPortal("connect");
164
//            divHelpContentController.addDivHelpContentsInPortal(community.getPid(), community.getId(), divId.getName());
165
//        }
166
//
167
//        if(!divId.getCommunities()) {
168
//            List<Portal> communities = communityController.getAllPortals();
169
//            for( Portal community : communities ) {
170
//                if(!community.getPid().equals("openaire") && !community.getPid().equals("connect")) {
171
//                    // delete div contents related to this divId
172
//                    List<DivHelpContentResponse> divHelpContentResponses = divHelpContentController.getDivHelpContents(community.getPid(), null, divId.getName(), null);
173
//                    for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
174
//                        divHelpContentController.deleteDivHelpContent(divHelpContentResponse.getId());
175
//                    }
176
//                }
177
//            }
178
//        }
179
//
180
//        if(!openaireEnabled) {
181
//            Portal community = communityController.getPortal("openaire");
182
//
183
//            // delete div contents related to this divId
184
//            List<DivHelpContentResponse> divHelpContentResponses = divHelpContentController.getDivHelpContents("openaire", null, divId.getName(), null);
185
//            for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
186
//                divHelpContentController.deleteDivHelpContent(divHelpContentResponse.getId());
187
//            }
188
//        }
189
//
190
//        if(!connectEnabled) {
191
//            Portal community = communityController.getPortal("connect");
192
//
193
//            // delete div contents related to this divId
194
//            List<DivHelpContentResponse> divHelpContentResponses = divHelpContentController.getDivHelpContents("connect", null, divId.getName(), null);
195
//            for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
196
//                divHelpContentController.deleteDivHelpContent(divHelpContentResponse.getId());
197
//            }
198
//        }
199

  
200
        DivId divId = this.getDivIdByDivIdResponse(divIdResponse);
201
        divIdDAO.save(divId);
202

  
203
        return divIdResponse;
204
    }
205

  
206
    public Boolean deleteDivIds(List<String> divIds) throws Exception {
207
        for (String id: divIds) {
208
            DivId divId = divIdDAO.findById(id);
209

  
210
            // delete div contents related to this divId
211
            List<DivHelpContentResponse> divHelpContentResponses = divHelpContentController.getDivHelpContents(null, null, divId.getName(), null);
212
            for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
213
                divHelpContentController.deleteDivHelpContent(divHelpContentResponse.getId());
214
            }
215

  
216
            divIdDAO.delete(id);
217
        }
218
        return true;
219
    }
220

  
221
    public void deleteDivId(String id) {
222
        divIdDAO.delete(id);
223
    }
224

  
225
    public Set<String> getDivIdsPages(String pid) {
226
        Portal portal = portalService.getPortal(pid);
227
        String dashboardAlias = portal.getAlias();
228

  
229
        List<DivId> divIds = null;
230
        Set<String> hasPortalPageDivIds = new HashSet<>();
231

  
232
        if(dashboardAlias != null) {
233
            divIds = divIdDAO.findByDashboardAlias(dashboardAlias);
234
        } else {
235
            divIds = divIdDAO.findAll();
236
        }
237

  
238
        for(DivId divId : divIds) {
239
            for(String pageId : divId.getPages()) {
240
                hasPortalPageDivIds.add(pageId);
241
            }
242
        }
243
        return hasPortalPageDivIds;
244
    }
245

  
246
}
modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/services/PageHelpContentService.java
1
package eu.dnetlib.uoaadmintoolslibrary.services;
2

  
3
import eu.dnetlib.uoaadmintoolslibrary.dao.PageHelpContentDAO;
4
import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
5
import eu.dnetlib.uoaadmintoolslibrary.entities.PageHelpContent;
6
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
7
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PageHelpContentResponse;
8

  
9
import org.apache.log4j.Logger;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.stereotype.Service;
12

  
13
import java.util.ArrayList;
14
import java.util.List;
15

  
16
@Service
17
public class PageHelpContentService {
18
    private final Logger log = Logger.getLogger(this.getClass());
19

  
20
    @Autowired
21
    private PageHelpContentDAO pageHelpContentDAO;
22

  
23
    @Autowired
24
    private PageService pageService;
25

  
26
    @Autowired
27
    private PortalService portalService;
28

  
29
    public List<PageHelpContentResponse> getPageHelpContents(String pid, String page, String position, String active, String before) {
30
        List<PageHelpContent> pageHelpContents = null;
31

  
32
        Portal _portal = null;
33
        String portalId = null;
34
        if(pid != null) {
35
            _portal = portalService.getPortal(pid);
36
            if(_portal != null) {
37
                portalId = _portal.getId();
38
            }
39
        }
40

  
41
        if(pid != null && position != null && active != null && before != null) {
42
            pageHelpContents = pageHelpContentDAO.findByPortalAndPlacementAndIsActiveAndIsPriorToOrderByOrderAsc(portalId, position, Boolean.parseBoolean(active), Boolean.parseBoolean(before));
43
        } else if(pid != null && position != null && active != null) {
44
            pageHelpContents = pageHelpContentDAO.findByPortalAndPlacementAndIsActiveOrderByOrderAsc(portalId, position, Boolean.parseBoolean(active));
45
        } else if(pid != null && position != null && before != null) {
46
            pageHelpContents = pageHelpContentDAO.findByPortalAndPlacementAndIsPriorToOrderByOrderAsc(portalId, position, Boolean.parseBoolean(before));
47
        } else if(pid != null && active != null && before != null) {
48
            pageHelpContents = pageHelpContentDAO.findByPortalAndIsActiveAndIsPriorToOrderByPlacementAscOrderAsc(portalId, Boolean.parseBoolean(active), Boolean.parseBoolean(before));
49
        } else if(position != null && active != null && before != null) {
50
            pageHelpContents = pageHelpContentDAO.findByPlacementAndIsActiveAndIsPriorToOrderByOrderAsc(position, Boolean.parseBoolean(active), Boolean.parseBoolean(before));
51
        } else if(pid != null && position != null ) {
52
            pageHelpContents = pageHelpContentDAO.findByPortalAndPlacementOrderByOrderAsc(portalId, position);
53
        } else if(pid != null && active != null ) {
54
            pageHelpContents = pageHelpContentDAO.findByPortalAndIsActiveOrderByPlacementAscOrderAsc(portalId, Boolean.parseBoolean(active));
55
        } else if(pid != null && before != null) {
56
            pageHelpContents = pageHelpContentDAO.findByPortalAndIsPriorToOrderByPlacementAscOrderAsc(portalId, Boolean.parseBoolean(before));
57
        } else if(position != null && active != null) {
58
            pageHelpContents = pageHelpContentDAO.findByPlacementAndIsActiveOrderByOrderAsc(position, Boolean.parseBoolean(active));
59
        } else if(position != null && before != null) {
60
            pageHelpContents = pageHelpContentDAO.findByPlacementAndIsPriorToOrderByOrderAsc(position,  Boolean.parseBoolean(before));
61
        } else if(active != null && before != null) {
62
            pageHelpContents = pageHelpContentDAO.findByIsActiveAndIsPriorToOrderByPlacementAscOrderAsc(Boolean.parseBoolean(active), Boolean.parseBoolean(before));
63
        } else if(pid != null) {
64
            pageHelpContents = pageHelpContentDAO.findByPortalOrderByPlacementAscOrderAsc(portalId);
65
        } else if(position != null) {
66
            pageHelpContents = pageHelpContentDAO.findByPlacementOrderByOrderAsc(position);
67
        } else if(active != null) {
68
            pageHelpContents = pageHelpContentDAO.findByIsActiveOrderByPlacementAscOrderAsc(Boolean.parseBoolean(active));
69
        } else if(before != null) {
70
            pageHelpContents = pageHelpContentDAO.findByIsPriorToOrderByPlacementAscOrderAsc(Boolean.parseBoolean(before));
71
        } else {
72
            pageHelpContents = pageHelpContentDAO.findAllByOrderByPlacementAscOrderAsc();
73
        }
74

  
75
        List<PageHelpContentResponse> pageHelpContentResponses = new ArrayList<>();
76
        for (PageHelpContent pageHelpContent : pageHelpContents) {
77
            Page _page = pageService.getPage(pageHelpContent.getPage());
78
            if(page == null || page.equals(_page.getRoute())) {
79
                PageHelpContentResponse pageHelpContentResponse = new PageHelpContentResponse(pageHelpContent);
80

  
81
                pageHelpContentResponse.setPage(_page);
82
                pageHelpContentResponse.setPortal(portalService.getPortalById(pageHelpContent.getPortal()));
83
                pageHelpContentResponses.add(pageHelpContentResponse);
84
            }
85
        }
86
        return pageHelpContentResponses;
87
    }
88

  
89
    public void deleteAllPageHelpContents() {
90
        pageHelpContentDAO.deleteAll();
91
    }
92

  
93
    public PageHelpContent insertPageHelpContent(PageHelpContent pageHelpContent) {
94
        String portalId = portalService.getPortal(pageHelpContent.getPortal()).getId();
95
        pageHelpContent.setPortal(portalId);
96
        return pageHelpContentDAO.save(pageHelpContent);
97
    }
98

  
99
    public PageHelpContent updatePageHelpContent(PageHelpContent pageHelpContent) {
100
        return pageHelpContentDAO.save(pageHelpContent);
101
    }
102

  
103
    public PageHelpContent getPageHelpContent(String id) {
104
        return pageHelpContentDAO.findById(id);
105
    }
106

  
107
    public List<String> togglePageHelpContent(List<String> pageHelpContents, String status) throws Exception {
108
        for (String id: pageHelpContents) {
109
            log.debug("Id of pageHelpContent: "+id);
110
            PageHelpContent pageHelpContent = pageHelpContentDAO.findById(id);
111
            pageHelpContent.setIsActive(Boolean.parseBoolean(status));
112
            pageHelpContentDAO.save(pageHelpContent);
113
        }
114
        return pageHelpContents;
115
    }
116

  
117
    public void deletePageHelpContent(String id) {
118
        pageHelpContentDAO.delete(id);
119
    }
120

  
121
    public Boolean deletePageHelpContents(List<String> pageHelpContents) throws Exception {
122
        for (String id: pageHelpContents) {
123
            pageHelpContentDAO.delete(id);
124
        }
125
        return true;
126
    }
127

  
128
    public void addPageHelpContentsInPortal(String pid, String portalId) {
129
        if (pid != "connect" && pid != "openaire") {
130
            String organizations_page_content = "<div> <p>Here you can write more details about the organizations related to your community.</p> </div>";
131
            Page organizationsPage = (pageService.getAllPages(null, "/organizations", null)).get(0);
132

  
133
            PageHelpContent organizations_pageHelpContent = new PageHelpContent(organizationsPage.getId(), pid, "top", 1, organizations_page_content, false, false);
134
            this.insertPageHelpContent(organizations_pageHelpContent);
135

  
136
            String depositLearnHow_page_content = "" +
137
                    "<div class=\"uk-width-3-5 uk-align-center\"> " +
138
                    " <div class=\"uk-text-bold\">How to comply with funder Open Access policies</div> " +
139
                    " <ul class=\"uk-list uk-list-bullet\"> " +
140
                    "   <li>Read the <a class=\"custom-external\" href=\"https://www.openaire.eu/how-to-comply-to-h2020-mandates-for-publications\" target=\"_blank\"> OpenAIRE guide to learn how to comply with EC H2020 Open Access policy on publications </a></li> " +
141
                    "   <li>Read the <a class=\"custom-external\" href=\"https://www.openaire.eu/how-to-comply-to-h2020-mandates-for-data\" target=\"_blank\"> OpenAIRE guide to learn how to comply with EC H2020 Open Access policy on research data </a></li> " +
142
                    "   <li>If you want to know about National Open Access policies, please check them out <a class=\"custom-external\" href=\"https://www.openaire.eu/frontpage/country-pages\" target=\"_blank\">here</a></li> " +
143
                    "   <li>All OpenAIRE guides can be found <a class=\"custom-external\" href=\"https://www.openaire.eu/guides\" target=\"_blank\">here</a></li> " +
144
                    " </ul> " +
145
                    "</div>";
146
            Page depositLearnHowPage = (pageService.getAllPages(null, "/participate/deposit/learn-how", null)).get(0);
147

  
148
            PageHelpContent depositLearnHow_pageHelpContent = new PageHelpContent(depositLearnHowPage.getId(), pid, "bottom", 1, depositLearnHow_page_content, true, false);
149
            this.insertPageHelpContent(depositLearnHow_pageHelpContent);
150
        }
151
    }
152
}
modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/services/PortalService.java
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 org.apache.log4j.Logger;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.stereotype.Service;
13

  
14
import java.util.*;
15

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

  
20
    @Autowired
21
    private PortalDAO portalDAO;
22

  
23
    @Autowired
24
    private PageService pageService;
25

  
26
    @Autowired
27
    private EntityService entityService;
28

  
29
    @Autowired
30
    private DivIdService divIdService;
31

  
32
    @Autowired
33
    private PageHelpContentService pageHelpContentService;
34

  
35
    @Autowired
36
    private DivHelpContentService divHelpContentService;
37

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

  
41
        return portals;
42
    }
43

  
44
    public List<PortalResponse> getAllPortalsFull() {
45
        List<Portal> portals = portalDAO.findAll();
46
        List<PortalResponse> portalsResponse = new ArrayList<>();
47
        for(Portal portal : portals) {
48
            PortalResponse portalResponse = new PortalResponse(portal);
49

  
50
            setEnabledPagesForPortalByType(portal, portalResponse);
51
            setEnabledEntitiesForPortalByType(portal, portalResponse);
52

  
53
            //Layout layout = layoutDAO.findById(portal.getLayout());
54
            //portalResponse.setLayout(layout);
55

  
56
            portalsResponse.add(portalResponse);
57
        }
58
        return portalsResponse;
59
    }
60

  
61
    private void setEnabledPagesForPortalByType(Portal portal, PortalResponse portalResponse) {
62
        List<PortalPage> pages = this.getPagesForPortalByType(portal.getPid(), null, null, null);
63
        log.debug("PAGES number="+pages.size());
64
        Iterator<PortalPage> iteratorPages = pages.iterator();
65
        while(iteratorPages.hasNext()) {
66
            PortalPage page = iteratorPages.next();
67
            if(!page.getIsEnabled()) {
68
                iteratorPages.remove();
69
            }
70
        }
71
        portalResponse.setPages(pages);
72
        log.debug("PAGES set");
73
    }
74

  
75
    private void setEnabledEntitiesForPortalByType(Portal portal, PortalResponse portalResponse) {
76
        List<PortalEntity> entities = this.getEntitiesForPortal(portal.getPid(), null);
77
        log.debug("ENTITIES number="+entities.size());
78
        Iterator<PortalEntity> iteratorEntities = entities.iterator();
79
        while(iteratorEntities.hasNext()) {
80
            PortalEntity entity = iteratorEntities.next();
81
            if(!entity.getIsEnabled()) {
82
                iteratorEntities.remove();
83
            }
84
        }
85
        portalResponse.setEntities(entities);
86
        log.debug("ENTITIES set");
87
    }
88

  
89
    public PortalResponse getPortalFull(String pid) {
90
        Portal portal = portalDAO.findByPid(pid);
91
        PortalResponse portalResponse = new PortalResponse(portal);
92

  
93
        setEnabledPagesForPortalByType(portal, portalResponse);
94
        setEnabledEntitiesForPortalByType(portal, portalResponse);
95

  
96
        //Layout layout = layoutDAO.findById(portal.getLayout());
97
        //portalResponse.setLayout(layout);
98

  
99
        return portalResponse;
100
    }
101

  
102
    public PortalResponse updatePortal(Portal portal) {
103
        Portal com = portalDAO.findById(portal.getId());
104

  
105
//        Statistics statistics = statisticsDAO.findByPid(com.getPid());
106
//        statistics.setPid(portal.getPid());
107
//        statisticsDAO.save(statistics);
108
//
109
//        PortalSubscribers portalSubscribers =  communitySubscribersDAO.findByPid(com.getPid());
110
//        portalSubscribers.setPid(portal.getPid());
111
//        communitySubscribersDAO.save(portalSubscribers);
112

  
113
        com.setName(portal.getName());
114
        com.setPid(portal.getPid());
115

  
116
        portalDAO.save(com);
117
        PortalResponse portalResponse = this.getPortalFull(portal.getPid());
118

  
119
        return portalResponse;
120
    }
121

  
122
    public PortalResponse insertPortal(Portal portal) {
123
        List<PortalEntity> portalEntities = new ArrayList<>();
124
        List<PortalPage> portalPages = new ArrayList<>();
125
        Map<String, Boolean> entities = new HashMap<>();
126
        Map<String, Boolean> pages = new HashMap<>();
127

  
128
        for(Entity entity : entityService.getAllEntities()) {
129
            entities.put(entity.getId(), true);
130

  
131
            PortalEntity portalEntity = new PortalEntity(entity);
132
            portalEntity.setIsEnabled(true);
133
            portalEntities.add(portalEntity);
134
        }
135

  
136
        for(Page page : pageService.getAllPages(null, null, null)) {
137
            pages.put(page.getId(), true);
138

  
139
            PortalPage portalPage = new PortalPage(page);
140
            if(page.getRoute().equals("/curators") || page.getRoute().equals("/organizations")) {
141
                portalPage.setIsEnabled(false);
142
            } else {
143
                portalPage.setIsEnabled(true);
144
            }
145

  
146
            portalPages.add(portalPage);
147
        }
148

  
149
        portal.setEntities(entities);
150
        portal.setPages(pages);
151
//        Statistics statistics =  new Statistics(portal.getPid());
152
//        statisticsDAO.save(statistics);
153
//        PortalSubscribers portalSubscribers =  new PortalSubscribers(portal.getPid());
154
//        communitySubscribersDAO.save(portalSubscribers);
155
        Portal savedPortal = portalDAO.save(portal);
156
        PortalResponse portalResponse = this.getPortalFull(savedPortal.getPid());
157

  
158
        log.debug("pid of saved portal: "+ savedPortal.getPid());
159

  
160
        String id = savedPortal.getId();
161

  
162
        divHelpContentService.addDivHelpContentsInPortal(savedPortal.getPid(), id, null);
163
        pageHelpContentService.addPageHelpContentsInPortal(savedPortal.getPid(), id);
164

  
165
        return portalResponse;
166
    }
167

  
168
    private Portal getPortalByPortalResponse(PortalResponse portalResponse) {
169
        Portal portal = new Portal();
170
        portal.setId(portalResponse.getId());
171
        portal.setName(portalResponse.getName());
172

  
173
        List<PortalEntity> fullEntities = portalResponse.getEntities();
174
        Map<String, Boolean> entities = new HashMap<String, Boolean>();
175
        for(PortalEntity entity : fullEntities) {
176
            entities.put(entity.getId(), true);
177
        }
178
        for(Entity entity : entityService.getAllEntities()) {
179
            if(!entities.containsKey(entity.getId())) {
180
                entities.put(entity.getId(), false);
181
            }
182
        }
183
        portal.setEntities(entities);
184

  
185
        List<PortalPage> fullPages = portalResponse.getPages();
186
        Map<String, Boolean> pages = new HashMap<String, Boolean>();
187
        for(PortalPage page : fullPages) {
188
            pages.put(page.getId(), true);
189
        }
190
        for(Page page : pageService.getAllPages(null, null, null)) {
191
            if(!pages.containsKey(page.getId())) {
192
                pages.put(page.getId(), false);
193
            }
194
        }
195
        portal.setPages(pages);
196
//        Layout layout = portalResponse.getLayout();
197
//        portal.setLayout(layout.getId());
198

  
199
        return portal;
200
    }
201

  
202
    public Boolean deletePortals(List<String> portals) throws Exception {
203
        for (String id: portals) {
204
            Portal portal = portalDAO.findById(id);
205
            String pid = portal.getPid();
206

  
207
            // delete div contents related to this portal
208
            List<DivHelpContentResponse> divHelpContentResponses = divHelpContentService.getDivHelpContents(pid, null, null, null);
209
            for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
210
                divHelpContentService.deleteDivHelpContent(divHelpContentResponse.getId());
211
            }
212

  
213
            // delete page contents related to this portal
214
            List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentService.getPageHelpContents(pid, null,null, null, null);
215
            for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
216
                pageHelpContentService.deletePageHelpContent(pageHelpContentResponse.getId());
217
            }
218

  
219
//            Statistics stats = statisticsDAO.findByPid(pid);
220
//            if(stats != null) {
221
//                statisticsDAO.delete(stats.getId());
222
//            }
223
//
224
//            PortalSubscribers portalSubscribers = communitySubscribersDAO.findByPid(pid);
225
//            if(portalSubscribers != null) {
226
//                communitySubscribersDAO.delete(portalSubscribers.getId());
227
//            }
228
//
229
//            Layout layout = layoutDAO.findById(portal.getLayout());
230
//            if(layout != null) {
231
//                layoutDAO.delete(layout.getId());
232
//            }
233

  
234
            portalDAO.delete(id);
235
        }
236

  
237
        return true;
238
    }
239

  
240
    public Portal insertOrUpdatePortal(Portal portal) {
241
        return portalDAO.save(portal);
242
    }
243

  
244
    public Portal getPortalById(String id) {
245
        log.debug("ID: "+ id);
246
        return portalDAO.findById(id);
247
    }
248

  
249
    public Portal getPortal(String pid) {
250
        log.debug("PID: "+ pid);
251
        return portalDAO.findByPid(pid);
252
    }
253

  
254
    public void deletePortal(String id) {
255
        portalDAO.delete(id);
256
    }
257

  
258
    public List<PortalPage> getPagesForPortalByType(String pid, String page_type, String page_route, String div) {
259
        List<PortalPage> return_pages = new ArrayList<PortalPage>();
260
        Portal portal = portalDAO.findByPid(pid);
261
        Map<String, Boolean> pages = portal.getPages();
262

  
263
        if(pages != null) {
264
            for (Map.Entry<String, Boolean> page : pages.entrySet()) {
265
                if(div != null && div.equals("true")) {
266
                    List<DivId> divIds = divIdService.getDivIds(page.getKey(), null, null);
267
                    Iterator<DivId> divIdIterator = divIds.iterator();
268

  
269
                    while (divIdIterator.hasNext()) {
270
                        DivId divId = divIdIterator.next();
271
                        if(!portal.getAlias().equals(divId.getDashboardAlias())) {
272
                            divIdIterator.remove();
273
                        }
274
                    }
275

  
276
                    if(divIds.isEmpty()) {
277
                        continue;
278
                    }
279
                }
280

  
281
                Page p = pageService.getPage(page.getKey());
282

  
283
                if(portal.getAlias().equals(p.getDashboardAlias())) {
284
                    if ((page_type == null && page_route == null) || (page_route == null && p.getType().equals(page_type))
285
                            || p.getRoute().equals(page_route)) {
286
                        PortalPage portalPage = new PortalPage(p);
287

  
288
                        List<Entity> entities = new ArrayList<>();
289
                        for (String entity : p.getEntities()) {
290
                            entities.add(entityService.getEntity(entity));
291
                        }
292
                        portalPage.setEntities(entities);
293
                        portalPage.setIsEnabled(page.getValue());
294

  
295
                        return_pages.add(portalPage);
296

  
297
                        if (page_route != null) {
298
                            break;
299
                        }
300
                    }
301
                }
302
            }
303
        }
304
        return_pages.sort(Comparator.comparing(PortalPage::getName));
305
        return return_pages;
306
    }
307

  
308
    public Portal insertOrUpdatePage(String id, PortalPage page) {
309
        Portal portal = portalDAO.findById(id);
310
        Map<String, Boolean> pages = portal.getPages();
311

  
312
        String name = page.getName();
313
        boolean isEnabled = page.getIsEnabled();
314

  
315
        pages.put(name, isEnabled);
316
        portal.setPages(pages);
317

  
318
        return portalDAO.save(portal);
319
    }
320

  
321
    public Portal togglePage(String pid, List<String> pageIds, String status) throws Exception {
322
        Portal portal = portalDAO.findByPid(pid);
323
        Map<String, Boolean> pages = portal.getPages();
324

  
325
        for (String pageId: pageIds) {
326
            log.debug("Toggle portal page: " + pageId + " of portal: " + pid + " to " + status);
327
            pages.put(pageId, Boolean.parseBoolean(status));
328
        }
329

  
330
        portal.setPages(pages);
331
        return portalDAO.save(portal);
332
    }
333

  
334
    public Portal toggleEntity(String pid, List<String> entityIds, String status) throws Exception {
335
        Portal portal = portalDAO.findByPid(pid);
336
        Map<String, Boolean> entities = portal.getEntities();
337
        Map<String, Boolean> pages = portal.getPages();
338

  
339
        for (String entityId: entityIds) {
340
            log.debug("Toggle portal entity: " + entityId + " of portal: " + pid + " to " + status);
341

  
342
            entities.put(entityId, Boolean.parseBoolean(status));
343

  
344
            if(pages != null) {
345
                for (Map.Entry<String, Boolean> pageEntry : pages.entrySet()) {
346
                    Page page = pageService.getPage(pageEntry.getKey());
347
                    if (page.getEntities().contains(entityId) && page.getType().equals("search")) {
348
                        pages.put(pageEntry.getKey(), Boolean.parseBoolean(status));
349
                    }
350
                }
351
            }
352
        }
353

  
354
        portal.setEntities(entities);
355
        return portalDAO.save(portal);
356
    }
357

  
358
    public List<PortalEntity> getEntitiesForPortal(String pid, String entity) {
359
        List<PortalEntity> return_entities = new ArrayList<PortalEntity>();
360
        Map<String, Boolean> entities = portalDAO.findByPid(pid).getEntities();
361

  
362
        //log.debug("/portal/"+pid+"/entities -- entity: "+entity);
363
        if (entity != null) {
364
            Entity _entity = entityService.getEntityByPid(entity);
365
            String entityId = _entity.getId();
366
            PortalEntity portalEntity = new PortalEntity(_entity);
367
            portalEntity.setIsEnabled(entities.get(entityId));
368
            return_entities.add(portalEntity);
369
        } else {
370
            if(entities != null) {
371
                for (Map.Entry<String, Boolean> _entity : entities.entrySet()) {
372
                    PortalEntity portalEntity = new PortalEntity(entityService.getEntity(_entity.getKey()));
373
                    portalEntity.setIsEnabled(_entity.getValue());
374
                    return_entities.add(portalEntity);
375
                }
376
            }
377
        }
378
        return return_entities;
379
    }
380

  
381
//    @RequestMapping(value = "/community/{pid}/layout", method = RequestMethod.GET)
382
//    public Layout getLayoutForCommunity(@PathVariable(value = "pid") String pid) {
383
//        Portal portal = portalDAO.findByPid(pid);
384
//        if(portal.getLayout() != null) {
385
//            return layoutDAO.findById(portal.getLayout());
386
//        } else {
387
//            return null;
388
//        }
389
//    }
390

  
391
//    @RequestMapping(value = "/community/{pid}/layout", method = RequestMethod.POST)
392
//    public Layout updateLayoutForCommunity(@PathVariable(value = "pid") String pid, @RequestBody Layout layout) {
393
//        Portal portal = portalDAO.findByPid(pid);
394
//        if(portal.getLayout() != null) {
395
//            layout.setId(portal.getLayout());
396
//            layout = layoutDAO.save(layout);
397
//        } else {
398
//            layout = layoutDAO.save(layout);
399
//            portal.setLayout(layout.getId());
400
//            portalDAO.save(portal);
401
//        }
402
//        return layout;
403
//    }
404

  
405

  
406
    public Map<String, List<PageHelpContentResponse>> getPageHelpContentsByPosition(String pid, String page, String active) {
407
        Map<String, List<PageHelpContentResponse>> pageHelpContentResponses = new HashMap<>();
408

  
409
        List<PageHelpContentResponse> pageHelpContents = null;
410
        pageHelpContents = pageHelpContentService.getPageHelpContents(pid, page, null, active, null);
411

  
412
        pageHelpContentResponses.put("top", new ArrayList<>());
413
        pageHelpContentResponses.put("bottom", new ArrayList<>());
414
        pageHelpContentResponses.put("left", new ArrayList<>());
415
        pageHelpContentResponses.put("right", new ArrayList<>());
416

  
417
        for (PageHelpContentResponse pageHelpContentResponse : pageHelpContents) {
418
            pageHelpContentResponses.get(pageHelpContentResponse.getPlacement()).add(pageHelpContentResponse);
419
        }
420

  
421

  
422
        return pageHelpContentResponses;
423
    }
424

  
425
    public Map<String, List<DivHelpContentResponse>> getDivHelpContentsByPosition(String pid, String page, String active) {
426
        Map<String, List<DivHelpContentResponse>> divHelpContentResponses = new HashMap<>();
427

  
428
        List<DivHelpContentResponse> divHelpContents = null;
429
        divHelpContents = divHelpContentService.getDivHelpContents(pid, page, null, active);
430

  
431
        for (DivHelpContentResponse divHelpContentResponse : divHelpContents) {
432
            if(!divHelpContentResponses.containsKey(divHelpContentResponse.getDivId())) {
433
                divHelpContentResponses.put(divHelpContentResponse.getDivId().getName(), new ArrayList<>());
434
            }
435
            divHelpContentResponses.get(divHelpContentResponse.getDivId().getName()).add(divHelpContentResponse);
436
        }
437

  
438

  
439
        return divHelpContentResponses;
440
    }
441

  
442
}
modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/services/DivHelpContentService.java
1
package eu.dnetlib.uoaadmintoolslibrary.services;
2

  
3
import eu.dnetlib.uoaadmintoolslibrary.dao.DivHelpContentDAO;
4
import eu.dnetlib.uoaadmintoolslibrary.entities.DivHelpContent;
5
import eu.dnetlib.uoaadmintoolslibrary.entities.DivId;
6
import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
7
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
8
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.DivHelpContentResponse;
9
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.DivIdResponse;
10

  
11
import org.apache.log4j.Logger;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.stereotype.Service;
14

  
15
import java.util.ArrayList;
16
import java.util.List;
17

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

  
22
    @Autowired
23
    private DivHelpContentDAO divHelpContentDAO;
24

  
25
    @Autowired
26
    DivIdService divIdService;
27

  
28
    @Autowired
29
    private PortalService portalService;
30

  
31
    public List<DivHelpContentResponse> getDivHelpContents(String pid, String page, String div, String active) {
32

  
33
        Portal _portal = portalService.getPortal(pid);
34
        String portalId = null;
35
        if(_portal != null) {
36
            portalId = _portal.getId();
37
        }
38

  
39
        DivId divId = divIdService.getDivIdByName(div);
40
        String divIdId = null;
41
        if(divId != null) {
42
            divIdId = divId.getId();
43
        }
44

  
45
        List<DivHelpContentResponse> divHelpContentResponses = new ArrayList<>();
46

  
47
        List<DivHelpContent> divHelpContents = null;
48
        if(pid != null && div != null && active != null) {
49
            divHelpContents = divHelpContentDAO.findByPortalAndDivIdAndIsActive(portalId, divIdId, Boolean.parseBoolean(active));
50
        } else if(pid != null && div != null) {
51
            divHelpContents = divHelpContentDAO.findByPortalAndDivId(portalId, divIdId);
52
        } else if(pid != null && active != null) {
53
            divHelpContents = divHelpContentDAO.findByPortalAndIsActive(portalId, Boolean.parseBoolean(active));
54
        } else if(div != null && active != null) {
55
            divHelpContents = divHelpContentDAO.findByDivIdAndIsActive(divIdId, Boolean.parseBoolean(active));
56
        } else if(pid != null) {
57
            divHelpContents = divHelpContentDAO.findByPortal(portalId);
58
        } else if(div != null) {
59
            divHelpContents = divHelpContentDAO.findByDivId(divIdId);
60
        } else if(active != null){
61
            divHelpContents = divHelpContentDAO.findByIsActive(Boolean.parseBoolean(active));
62
        } else {
63
            divHelpContents = divHelpContentDAO.findAll();
64
        }
65

  
66
        for (DivHelpContent divHelpContent : divHelpContents) {
67
            DivIdResponse divIdResponse = null;
68
            if(div == null) {
69
                divId = divIdService.getDivId(divHelpContent.getDivId());
70
            }
71
            divIdResponse = divIdService.divIdResponseFromDivId(divId);
72

  
73
            if(pid == null) {
74
                _portal = portalService.getPortalById(divHelpContent.getPortal());
75
            }
76

  
77
            for(Page p : divIdResponse.getPages()) {
78
                if (page == null || p.getRoute().equals(page)) {
79
                    DivHelpContentResponse divHelpContentResponse = new DivHelpContentResponse(divHelpContent);
80
                    divHelpContentResponse.setDivId(divIdResponse);
81
                    divHelpContentResponse.setPortal(_portal);
82
                    divHelpContentResponses.add(divHelpContentResponse);
83
                    break;
84
                }
85
            }
86
        }
87

  
88
        return divHelpContentResponses;
89
    }
90

  
91
    public DivHelpContent getDivHelpContent(String id) {
92
        return divHelpContentDAO.findById(id);
93
    }
94

  
95
    public DivHelpContent insertOrUpdateDivHelpContent(DivHelpContent divHelpContent) {
96
        return divHelpContentDAO.save(divHelpContent);
97
    }
98

  
99
    public void deleteDivHelpContent(String id) {
100
        divHelpContentDAO.delete(id);
101
    }
102

  
103
    public Boolean deleteDivHelpContents(List<String> divHelpContents) throws Exception {
104
        for (String id: divHelpContents) {
105
            divHelpContentDAO.delete(id);
106
        }
107
        return true;
108
    }
109

  
110
    public List<String> toggleDivHelpContent(List<String> divHelpContents, String status) throws Exception {
111
        for (String id: divHelpContents) {
112
            log.debug("Id of divHelpContent: "+id);
113
            DivHelpContent divHelpContent = divHelpContentDAO.findById(id);
114
            divHelpContent.setIsActive(Boolean.parseBoolean(status));
115
            divHelpContentDAO.save(divHelpContent);
116
        }
117
        return divHelpContents;
118
    }
119

  
120
    public void addDivHelpContentsInPortal(String pid, String portalId, String divIdName) {
121
        Portal portal = portalService.getPortal(pid);
122
        String dashboardAlias = portal.getAlias();
123

  
124
        //String organizations_class_content = "<div> <p>Here you can write more details about the organizations related to your portal.</p> </div>";
125
        String link_context_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Select a research portal and/or a category and search for a portal concept, or browse to the portal tree through the categories</div> </div>";
126
        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>";
127
        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>";
128
        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>";
129
        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>";
130

  
131
        List<DivId> divIds = divIdService.getDivIds(null, divIdName, null);
132

  
133
        for( DivId div : divIds ) {
134
            if (div != null && div.getDashboardAlias().equals(dashboardAlias) ) {
135
//                    (div.getOpenaire() && pid.equals("openaire")) ||
136
//                            (div.getConnect() && pid.equals("connect")) ||
137
//                            (div.getCommunities() && !pid.equals("openaire") && !pid.equals("connect"))
138
//            )) {
139
                /*
140
                if (div.getName().equals("organizations")) {
141
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, organizations_class_content, false));
142
                } else
143
                */
144
                if (div.getName().equals("link-context-form")) {
145
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_context_form_content, false));
146
                } else if (div.getName().equals("link-project-form")) {
147
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_project_form_content, false));
148
                } else if (div.getName().equals("link-result-form")) {
149
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_result_form_content, false));
150
                } else if (div.getName().equals("link-result-bulk")) {
151
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_result_bulk_content, false));
152
                } else if (div.getName().equals("link-metadata")) {
153
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_metadata_content, false));
154
                }
155
            }
156
        }
157
    }
158
}
modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/services/EntityService.java
1
package eu.dnetlib.uoaadmintoolslibrary.services;
2

  
3
import eu.dnetlib.uoaadmintoolslibrary.dao.EntityDAO;
4
import eu.dnetlib.uoaadmintoolslibrary.entities.Entity;
5
import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
6
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
7
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalEntity;
8

  
9
import org.apache.log4j.Logger;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.stereotype.Service;
12

  
13
import java.util.List;
14
import java.util.Map;
15

  
16
@Service
17
public class EntityService {
18
    private final Logger log = Logger.getLogger(this.getClass());
19

  
20
    @Autowired
21
    private EntityDAO entityDAO;
22

  
23
    @Autowired
24
    private PageService pageService;
25

  
26
    @Autowired
27
    private PortalService portalService;
28

  
29
    public List<Entity> getAllEntities() {
30
        return entityDAO.findAll();
31
    }
32

  
33
    public void deleteAllEntities() {
34
        entityDAO.deleteAll();
35
    }
36

  
37
    public Entity insertOrUpdateEntity(Entity entity) {
38
        return entityDAO.save(entity);
39
    }
40

  
41
    public Entity getEntity(String id) {
42
        return entityDAO.findById(id);
43
    }
44

  
45
    public Entity getEntityByPid(String pid) {
46
        return entityDAO.findByPid(pid);
47
    }
48

  
49
    public void deleteEntity(String id) {
50
        entityDAO.delete(id);
51
    }
52

  
53
    public PortalEntity updateEntity(PortalEntity portalEntity) {
54
        Entity entity = this.getEntityByPortalEntity(portalEntity);
55
        entityDAO.save(entity);
56

  
57
        return portalEntity;
58
    }
59

  
60
    public PortalEntity insertEntity(Entity entity) {
61
        Entity savedEntity = entityDAO.save(entity);
62
        PortalEntity portalEntity = new PortalEntity(savedEntity);
63

  
64
        // add entity in portals
65
        List<Portal> portals = portalService.getAllPortals();
66
        for( Portal portal : portals ) {
67
            Map<String, Boolean> entities = portal.getEntities();
68
            entities.put(entity.getId(), true);
69
            portal.setEntities(entities);
70
            portalService.insertOrUpdatePortal(portal);
71
        }
72

  
73
        return portalEntity;
74
    }
75

  
76
    private Entity getEntityByPortalEntity(PortalEntity portalEntity) {
77
        Entity entity = new Entity();
78
        entity.setId(portalEntity.getId());
79
        entity.setPid(portalEntity.getPid());
80
        entity.setName(portalEntity.getName());
81

  
82
        return entity;
83
    }
84

  
85
    public Boolean deleteEntities(List<String> entities) throws Exception {
86
        for (String id: entities) {
87
            entityDAO.delete(id);
88

  
89
            // delete entity from portals
90
            List<Portal> portals = portalService.getAllPortals();
91
            for( Portal portal : portals ) {
92
                Map<String, Boolean> portalEntities = portal.getEntities();
93
                portalEntities.remove(id);
94
                portal.setEntities(portalEntities);
95
                portalService.insertOrUpdatePortal(portal);
96
            }
97

  
98
            // delete entity from pages
99
            List<Page> pages = pageService.getAllPages(null, null, null);
100
            for( Page page : pages ) {
101
                List<String> pageEntities = page.getEntities();
102
                int index = 0;
103
                for(String pageEntity : pageEntities) {
104
                    if(pageEntity == id) {
105
                        pageEntities.remove(index);
106
                        break;
107
                    }
108
                    index++;
109
                }
110
                page.setEntities(pageEntities);
111
                pageService.insertOrUpdatePage(page);
112
            }
113
        }
114

  
115
        return true;
116
    }
117
}
modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/services/PageService.java
1
package eu.dnetlib.uoaadmintoolslibrary.services;
2

  
3
import eu.dnetlib.uoaadmintoolslibrary.controllers.DivHelpContentController;
4
import eu.dnetlib.uoaadmintoolslibrary.controllers.DivIdController;
5
import eu.dnetlib.uoaadmintoolslibrary.controllers.EntityController;
6
import eu.dnetlib.uoaadmintoolslibrary.controllers.PageHelpContentController;
7
import eu.dnetlib.uoaadmintoolslibrary.dao.DivIdDAO;
8
import eu.dnetlib.uoaadmintoolslibrary.dao.PageDAO;
9
import eu.dnetlib.uoaadmintoolslibrary.entities.DivId;
10
import eu.dnetlib.uoaadmintoolslibrary.entities.Entity;
11
import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
12
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
13
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.DivHelpContentResponse;
14
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PageHelpContentResponse;
15
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalPage;
16

  
17
import org.apache.log4j.Logger;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.stereotype.Service;
20

  
21
import java.util.*;
22

  
23
@Service
24
public class PageService {
25
    private final Logger log = Logger.getLogger(this.getClass());
26

  
27
    @Autowired
28
    private PageDAO pageDAO;
29

  
30
    @Autowired
31
    private PortalService portalService;
32

  
33
    @Autowired
34
    private DivIdDAO divIdDAO;
35

  
36
    @Autowired
37
    private PageHelpContentController pageHelpContentController;
38

  
39
    @Autowired
40
    private DivHelpContentController divHelpContentController;
41

  
42
    @Autowired
43
    private DivIdController divIdController;
44

  
45
    @Autowired
46
    private EntityController entityController;
47

  
48

  
49
    public List<PortalPage> getPagesFull(String pid, String page_route) {
50

  
51
        List<Page> pages = this.getAllPages(pid, page_route, null);
52

  
53
        List<PortalPage> portalPages = new ArrayList<>();
54
        for(Page page : pages) {
55
            PortalPage portalPage = new PortalPage(page);
56
            List<Entity> entities = new ArrayList<>();
57
            for(String entityId : page.getEntities()) {
58
                entities.add(entityController.getEntity(entityId));
59
            }
60
            portalPage.setEntities(entities);
61

  
62
            portalPages.add(portalPage);
63
        }
64

  
65
        return portalPages;
66
    }
67

  
68
    public List<Page> getAllPages(String pid, String page_route, String with_positions) {
69
        List<Page> pages;
70
        if (pid != null) {
71
            Portal portal = portalService.getPortal(pid);
72
            String dashboardAlias = portal.getAlias();
73

  
74
            if (page_route != null) {
75
                pages = new ArrayList<Page>();
76
                pages.add(pageDAO.findByDashboardAliasAndRoute(dashboardAlias, page_route));
77
            } else {
78
                pages = pageDAO.findByDashboardAlias(dashboardAlias);
79
            }
80
        } else if (page_route != null) {
81
            pages = pageDAO.findByRoute(page_route);
82
        } else {
83
            pages = pageDAO.findAll();
84
        }
85

  
86
        if (with_positions != null) {
87
            boolean at_least_one_position = Boolean.parseBoolean(with_positions);
88

  
89
            Iterator<Page> iteratorPages = pages.iterator();
90
            while(iteratorPages.hasNext()) {
91
                Page page = iteratorPages.next();
92

  
93
                if(at_least_one_position) {
94
                    if(!page.getTop() && !page.getBottom() && !page.getLeft() && !page.getRight()) {
95
                        iteratorPages.remove();
96
                    }
97
                } else {
98
                    if(page.getTop() || page.getBottom() || page.getLeft() || page.getRight()) {
99
                        iteratorPages.remove();
100
                    }
101
                }
102
            }
103
        }
104
        pages.sort(Comparator.comparing(Page::getName));
105
        return pages;
106
    }
107

  
108
    public void deleteAllPages() {
109
        pageDAO.deleteAll();
110
    }
111

  
112
    public Page insertOrUpdatePage(Page page) {
113
        return pageDAO.save(page);
114
    }
115

  
116
    public PortalPage updatePage(PortalPage portalPage) {
117
        this.deletePageHelpContentsForPositionsIfDisabled(portalPage);
118
        Page page = this.getPageByPortalPage(portalPage);
119
        pageDAO.save(page);
120
        return portalPage;
121
    }
122

  
123
    public PortalPage insertPage(PortalPage portalPage) {
124
        Page page = this.getPageByPortalPage(portalPage);
125
        Page savedPage = pageDAO.save(page);
126
        portalPage.setId(savedPage.getId());
127

  
128
        // add page in portals
129
        List<Portal> portals = portalService.getAllPortals();
130
        for( Portal portal : portals ) {
131
            Map<String, Boolean> pages = portal.getPages();
132
            pages.put(page.getId(), true);
133
            portal.setPages(pages);
134
            portalService.insertOrUpdatePortal(portal);
135
        }
136

  
137
        return portalPage;
138
    }
139

  
140
    private Page getPageByPortalPage(PortalPage portalPage) {
141
        Page page = new Page();
142
        page.setId(portalPage.getId());
143
        page.setRoute(portalPage.getRoute());
144
        page.setName(portalPage.getName());
145
        page.setType(portalPage.getType());
146
        page.setDashboardAlias(portalPage.getDashboardAlias());
147
        page.setTop(portalPage.getTop());
148
        page.setBottom(portalPage.getBottom());
149
        page.setLeft(portalPage.getLeft());
150
        page.setRight(portalPage.getRight());
151

  
152
        List<Entity> fullEntities = portalPage.getEntities();
153
        List<String> entities = new ArrayList<String>();
154
        for(Entity entity : fullEntities) {
155
            entities.add(entity.getId());
156
        }
157
        page.setEntities(entities);
158

  
159
        return page;
160
    }
161

  
162
    private void deletePageHelpContentsForPositionsIfDisabled(PortalPage portalPage) {
163

  
164
        if(!portalPage.getTop()) {
165
            // delete page contents with position "top" related to this page from all portals
166
            List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentController.getPageHelpContents(null, portalPage.getRoute(), "top", null, null);
167
            for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
168
                pageHelpContentController.deletePageHelpContent(pageHelpContentResponse.getId());
169
            }
170
        }
171

  
172
        if(!portalPage.getBottom()) {
173
            // delete page contents with position "bottom" related to this page from all portals
174
            List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentController.getPageHelpContents(null, portalPage.getRoute(), "bottom", null, null);
175
            for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
176
                pageHelpContentController.deletePageHelpContent(pageHelpContentResponse.getId());
177
            }
178
        }
179

  
180
        if(!portalPage.getLeft()) {
181
            // delete page contents with position "left" related to this page from all portals
182
            List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentController.getPageHelpContents(null, portalPage.getRoute(), "left", null, null);
183
            for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
184
                pageHelpContentController.deletePageHelpContent(pageHelpContentResponse.getId());
185
            }
186
        }
187

  
188
        if(!portalPage.getRight()) {
189
            // delete page contents with position "right" related to this page from all portals
190
            List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentController.getPageHelpContents(null, portalPage.getRoute(), "right", null, null);
191
            for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
192
                pageHelpContentController.deletePageHelpContent(pageHelpContentResponse.getId());
193
            }
194
        }
195
    }
196

  
197
    public Boolean deletePages(List<String> pages) throws Exception {
198
        for (String id: pages) {
199
            pageDAO.delete(id);
200

  
201
            // delete divIds related only to this page from all portals, otherwise remove this page from divIds
202
            List<DivId> divIds = divIdController.getDivIds(id, null, null);
203
            for(DivId divId : divIds) {
204
                if(divId.getPages().size() == 1) {
205
                    divIdController.deleteDivId(divId.getId());
206

  
207
                    // delete div contents related to this page from all portals
208
                    List<DivHelpContentResponse> divHelpContentResponses = divHelpContentController.getDivHelpContents(null, id, divId.getId(), null);
209
                    for (DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
210
                        divHelpContentController.deleteDivHelpContent(divHelpContentResponse.getId());
211
                    }
212
                } else {
213
                    List<String> divIdPages = divId.getPages();
214
                    divIdPages.remove(id);
215
                    divId.setPages(divIdPages);
216
                    divIdDAO.save(divId);
217
                }
218
            }
219

  
220

  
221
            // delete page contents related to this page from all portals
222
            List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentController.getPageHelpContents(null, id, null, null, null);
223
            for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
224
                pageHelpContentController.deletePageHelpContent(pageHelpContentResponse.getId());
225
            }
226

  
227
            // delete page from portals
228
            List<Portal> portals = portalService.getAllPortals();
229
            for( Portal portal : portals ) {
230
                Map<String, Boolean> portalPages = portal.getPages();
231
                portalPages.remove(id);
232
                portal.setPages(portalPages);
233
                portalService.insertOrUpdatePortal(portal);
234
            }
235
        }
236

  
237
        return true;
238
    }
239

  
240
    public Page getPage(String id) {
241
        return pageDAO.findById(id);
242
    }
243

  
244
    public void deletePage(String id) {
245
        pageDAO.delete(id);
246
    }
247

  
248
    public List<String> getPageEntities(String id) {
249
        return pageDAO.findById(id).getEntities();
250
    }
251

  
252
    public Page togglePageEntity(String id, String entityId, String status) throws Exception {
253
        log.debug("Toggle entity : "+entityId +" of page: "+id+" to "+status);
254
        Page page = pageDAO.findById(id);
255
        List<String> entities = page.getEntities();
256
        if(Boolean.parseBoolean(status) && !entities.contains(entityId)) {
257
            entities.add(entityId);
258
        } else if (!Boolean.parseBoolean(status)) {
259
            entities.remove(entityId);
260
        }
261
        page.setEntities(entities);
262
        return pageDAO.save(page);
263
    }
264
}
modules/uoa-admin-tools-library/trunk/src/main/java/eu/dnetlib/uoaadmintoolslibrary/controllers/PortalController.java
1
package eu.dnetlib.uoaadmintoolslibrary.controllers;
2

  
3
import eu.dnetlib.uoaadmintoolslibrary.dao.PortalDAO;
4
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
5
import org.apache.log4j.Logger;
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.web.bind.annotation.*;
8

  
9
import java.util.*;
10

  
11
import eu.dnetlib.uoaadmintoolslibrary.dao.DivIdDAO;
12
import eu.dnetlib.uoaadmintoolslibrary.dao.EntityDAO;
13
import eu.dnetlib.uoaadmintoolslibrary.dao.PageDAO;
14
import eu.dnetlib.uoaadmintoolslibrary.entities.DivId;
15
import eu.dnetlib.uoaadmintoolslibrary.entities.Entity;
16
import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
17
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.*;
18

  
19
@RestController
20
@CrossOrigin(origins = "*")
21
public class PortalController {
22
    private final Logger log = Logger.getLogger(this.getClass());
23

  
24
    @Autowired
25
    private PortalDAO portalDAO;
26

  
27
    @Autowired
28
    private PageDAO pageDAO;
29

  
30
    @Autowired
31
    private EntityDAO entityDAO;
32

  
33
    @Autowired
34
    private DivIdDAO divIdDAO;
35

  
36
    @Autowired
37
    private PageHelpContentController pageHelpContentController;
38

  
39
    @Autowired
40
    private DivHelpContentController divHelpContentController;
41

  
42
    @RequestMapping(value = "/community", method = RequestMethod.GET)
43
    public List<Portal> getAllPortals() {
44
        List<Portal> portals = portalDAO.findAll();
45

  
46
        return portals;
47
    }
48

  
49
    @RequestMapping(value = "/communityFull", method = RequestMethod.GET)
50
    public List<PortalResponse> getAllPortalsFull() {
51
        List<Portal> portals = portalDAO.findAll();
52
        List<PortalResponse> portalResponses = new ArrayList<>();
53
        for(Portal portal : portals) {
54
            PortalResponse portalResponse = new PortalResponse(portal);
55

  
56
            List<PortalPage> pages = this.getPagesForPortalByType(portal.getPid(), null, null, null);
57
            log.debug("PAGES number="+pages.size());
58
            Iterator<PortalPage> iteratorPages = pages.iterator();
59
            while(iteratorPages.hasNext()) {
60
                PortalPage page = iteratorPages.next();
61
                if(!page.getIsEnabled()) {
62
                    iteratorPages.remove();
63
                }
64
            }
65
            portalResponse.setPages(pages);
66
            log.debug("PAGES set");
67

  
68
            List<PortalEntity> entities = this.getEntitiesForPortal(portal.getPid(), null);
69
            log.debug("ENTITIES number="+entities.size());
70
            Iterator<PortalEntity> iteratorEntities = entities.iterator();
71
            while(iteratorEntities.hasNext()) {
72
                PortalEntity entity = iteratorEntities.next();
73
                if(!entity.getIsEnabled()) {
74
                    iteratorEntities.remove();
75
                }
76
            }
77
            portalResponse.setEntities(entities);
78
//            Layout layout = layoutDAO.findById(portal.getLayout());
79
//            portalResponse.setLayout(layout);
80
            portalResponses.add(portalResponse);
81
        }
82
        return portalResponses;
83
    }
84

  
85
    @RequestMapping(value = "/communityFull/{pid}", method = RequestMethod.GET)
86
    public PortalResponse getPortalFull(@PathVariable(value = "pid") String pid) {
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff