Project

General

Profile

1
package eu.dnetlib.uoaadmintoolslibrary.controllers;
2

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

    
10
import java.util.List;
11
import java.util.Map;
12

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

    
18
    @Autowired
19
    private PortalService portalService;
20

    
21
    @RequestMapping(value = {"/community", "/explore", "/connect"}, method = RequestMethod.GET)
22
    public List<Portal> getAllCommunities() {
23
        List<Portal> communities = portalService.getAllPortals();
24

    
25
        return communities;
26
    }
27

    
28
    @RequestMapping(value = {"/communityFull", "/exploreFull", "/connectFull"}, method = RequestMethod.GET)
29
    public List<PortalResponse> getAllCommunitiesFull() {
30
//        List<Community> communities = portalDAO.findAll();
31
//        List<CommunityResponse> communitiesResponse = new ArrayList<>();
32
//        for(Community community : communities) {
33
//            CommunityResponse communityResponse = new CommunityResponse(community);
34
//
35
//            List<CommunityPage> pages = this.getPagesForCommunityByType(community.getPid(), null, null, null);
36
//            log.debug("PAGES number="+pages.size());
37
//            Iterator<CommunityPage> iteratorPages = pages.iterator();
38
//            while(iteratorPages.hasNext()) {
39
//                CommunityPage page = iteratorPages.next();
40
//                if(!page.getIsEnabled()) {
41
//                    iteratorPages.remove();
42
//                }
43
//            }
44
//            communityResponse.setPages(pages);
45
//            log.debug("PAGES set");
46
//
47
//            List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getPid(), null);
48
//            log.debug("ENTITIES number="+entities.size());
49
//            Iterator<CommunityEntity> iteratorEntities = entities.iterator();
50
//            while(iteratorEntities.hasNext()) {
51
//                CommunityEntity entity = iteratorEntities.next();
52
//                if(!entity.getIsEnabled()) {
53
//                    iteratorEntities.remove();
54
//                }
55
//            }
56
//            communityResponse.setEntities(entities);
57
//            Layout layout = layoutDAO.findById(community.getLayout());
58
//            communityResponse.setLayout(layout);
59
//            communitiesResponse.add(communityResponse);
60
//        }
61
//        return communitiesResponse;
62
        // TODO check where is this used? no more layout in response!!
63
        return portalService.getAllPortalsFull();
64
    }
65

    
66
    @RequestMapping(value = {"/communityFull/{pid}", "/exploreFull/{pid}", "/connectFull/{pid}"}, method = RequestMethod.GET)
67
    public PortalResponse getCommunityFull(@PathVariable(value = "pid") String pid) {
68
//        Community community = portalDAO.findByPid(pid);
69
//        CommunityResponse communityResponse = new CommunityResponse(community);
70
//
71
//        List<CommunityPage> pages = this.getPagesForCommunityByType(community.getPid(), null, null, null);
72
//        Iterator<CommunityPage> iteratorPages = pages.iterator();
73
//        while(iteratorPages.hasNext()) {
74
//            CommunityPage page = iteratorPages.next();
75
//            if(!page.getIsEnabled()) {
76
//                iteratorPages.remove();
77
//            }
78
//        }
79
//        communityResponse.setPages(pages);
80
//
81
//        List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getPid(), null);
82
//        Iterator<CommunityEntity> iteratorEntities = entities.iterator();
83
//        while(iteratorEntities.hasNext()) {
84
//            CommunityEntity entity = iteratorEntities.next();
85
//            if(!entity.getIsEnabled()) {
86
//                iteratorEntities.remove();
87
//            }
88
//        }
89
//        communityResponse.setEntities(entities);
90
//        Layout layout = layoutDAO.findById(community.getLayout());
91
//        communityResponse.setLayout(layout);
92
//
93
//        return communityResponse;
94
        // TODO check where is this used? no more layout in response!!
95
        return portalService.getPortalFull(pid);
96
    }
97
/*
98

    
99
    @RequestMapping(value = "/communityFullByName/{name}", method = RequestMethod.GET)
100
    public CommunityResponse getCommunityFullByName(@PathVariable(value = "name") String name) {
101
        Community community = portalDAO.findByName(name);
102
        CommunityResponse communityResponse = new CommunityResponse(community);
103

    
104
        List<CommunityPage> pages = this.getPagesForCommunityByType(community.getId(), null, null);
105
        Iterator<CommunityPage> iteratorPages = pages.iterator();
106
        while(iteratorPages.hasNext()) {
107
            CommunityPage page = iteratorPages.next();
108
            if(!page.getIsEnabled()) {
109
                iteratorPages.remove();
110
            }
111
        }
112
        communityResponse.setPages(pages);
113

    
114
        List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getId(), null);
115
        Iterator<CommunityEntity> iteratorEntities = entities.iterator();
116
        while(iteratorEntities.hasNext()) {
117
            CommunityEntity entity = iteratorEntities.next();
118
            if(!entity.getIsEnabled()) {
119
                iteratorEntities.remove();
120
            }
121
        }
122
        communityResponse.setEntities(entities);
123

    
124
        return communityResponse;
125
    }
126
*/
127

    
128
//    @RequestMapping(value = "/community", method = RequestMethod.DELETE)
129
//    public void deleteAllCommunities() {
130
//        portalDAO.deleteAll();
131
//    }
132

    
133
    @RequestMapping(value = {"/community", "/explore", "/connect"}, method = RequestMethod.POST)
134
    public Portal insertOrUpdateCommunity(@RequestBody Portal portal) {
135
        return portalService.insertOrUpdatePortal(portal);
136
    }
137

    
138
    @RequestMapping(value = {"/community/{pid}", "/explore/{pid}", "/connect/{pid}"}, method = RequestMethod.GET)
139
    public Portal getCommunity(@PathVariable(value = "pid") String pid) {
140
        log.debug("PID: "+ pid);
141
        return portalService.getPortal(pid);
142
    }
143

    
144
    @RequestMapping(value = {"/community/{id}", "/explore/{id}", "/connect/{id}"}, method = RequestMethod.DELETE)
145
    public void deleteCommunity(@PathVariable(value = "id") String id) {
146
        portalService.deletePortalId(id);
147
    }
148

    
149
    @RequestMapping(value = {"/community/{pid}/pages", "/explore/{pid}/pages", "/connect/{pid}/pages"}, method = RequestMethod.GET)
150
    public List<PortalPage> getPagesForCommunityByType(@PathVariable(value = "pid") String pid,
151
                                                       @RequestParam(value="page_type", required=false) String page_type,
152
                                                       @RequestParam(value="page_route", required=false) String page_route,
153
                                                       @RequestParam(value="div", required = false) String div) {
154
//        List<CommunityPage> return_pages = new ArrayList<CommunityPage>();
155
//        Map<String, Boolean> pages = portalDAO.findByPid(pid).getPages();
156
//
157
//        if(pages != null) {
158
//            for (Map.Entry<String, Boolean> page : pages.entrySet()) {
159
//                if(div != null && div.equals("true")) {
160
//                    Community community = portalDAO.findByPid(pid);
161
//                    List<DivId> divIds = divIdDAO.findByPagesContaining(page.getKey());
162
//                    Iterator<DivId> divIdIterator = divIds.iterator();
163
//
164
//                    while (divIdIterator.hasNext()) {
165
//                        DivId divId = divIdIterator.next();
166
//                        if((pid.equals("openaire") && !divId.getOpenaire()) ||
167
//                                (pid.equals("connect") && !divId.getConnect()) ||
168
//                                (!pid.equals("openaire") && !pid.equals("connect") && !divId.getCommunities())) {
169
//                            divIdIterator.remove();
170
//                        }
171
//                    }
172
//
173
//                    if(divIds.isEmpty()) {
174
//                        continue;
175
//                    }
176
//                }
177
//
178
//                Page p = pageDAO.findById(page.getKey());
179
//
180
//                if((pid.equals("openaire") && p.getOpenaire()) || (pid.equals("connect") && p.getConnect()) ||(!pid.equals("openaire") && !pid.equals("connect") && p.getCommunities())) {
181
//                    if ((page_type == null && page_route == null) || (page_route == null && p.getType().equals(page_type))
182
//                            || p.getRoute().equals(page_route)) {
183
//                        CommunityPage communityPage = new CommunityPage(p);
184
//
185
//                        List<Entity> entities = new ArrayList<>();
186
//                        for (String entity : p.getEntities()) {
187
//                            entities.add(entityDAO.findById(entity));
188
//                        }
189
//                        communityPage.setEntities(entities);
190
//                        communityPage.setIsEnabled(page.getValue());
191
//
192
//                        return_pages.add(communityPage);
193
//
194
//                        if (page_route != null) {
195
//                            break;
196
//                        }
197
//                    }
198
//                }
199
//            }
200
//        }
201
//        return_pages.sort(Comparator.comparing(CommunityPage::getName));
202
//        return return_pages;
203
        return portalService.getPagesForPortalByType(pid, page_type, page_route, div);
204
    }
205

    
206
    @RequestMapping(value = {"/community/{id}/page", "/explore/{id}/page", "/connect/{id}/page"}, method = RequestMethod.POST)
207
    public Portal insertOrUpdatePage(@PathVariable(value = "id") String id, @RequestBody PortalPage page) {
208
//        Community community = portalDAO.findById(id);
209
//        Map<String, Boolean> pages = community.getPages();
210
//
211
//        String name = page.getName();
212
//        boolean isEnabled = page.getIsEnabled();
213
//
214
//        pages.put(name, isEnabled);
215
//        community.setPages(pages);
216
//
217
//        return portalDAO.save(community);
218
        return portalService.insertOrUpdatePage(id, page);
219
    }
220

    
221
    @RequestMapping(value = {"/community/{pid}/page/toggle", "/explore/{pid}/page/toggle", "/connect/{pid}/page/toggle"}, method = RequestMethod.POST)
222
    public Portal togglePage(@PathVariable(value = "pid") String pid, @RequestBody List<String> pageIds, @RequestParam String status) throws Exception {
223
//        Community community = portalDAO.findByPid(pid);
224
//        Map<String, Boolean> pages = community.getPages();
225
//
226
//        for (String pageId: pageIds) {
227
//            log.debug("Toggle community page: " + pageId + " of community: " + pid + " to " + status);
228
//            pages.put(pageId, Boolean.parseBoolean(status));
229
//        }
230
//
231
//        community.setPages(pages);
232
//        return portalDAO.save(community);
233
        return portalService.togglePage(pid, pageIds, status);
234
    }
235

    
236
    @RequestMapping(value = {"/community/{pid}/entity/toggle", "/explore/{pid}/entity/toggle", "/connect/{pid}/entity/toggle"}, method = RequestMethod.POST)
237
    public Portal toggleEntity(@PathVariable(value = "pid") String pid, @RequestBody List<String> entityIds, @RequestParam String status) throws Exception {
238
//        Community community = portalDAO.findByPid(pid);
239
//        Map<String, Boolean> entities = community.getEntities();
240
//        Map<String, Boolean> pages = community.getPages();
241
//
242
//        for (String entityId: entityIds) {
243
//            log.debug("Toggle community entity: " + entityId + " of community: " + pid + " to " + status);
244
//
245
//            entities.put(entityId, Boolean.parseBoolean(status));
246
//
247
//            if(pages != null) {
248
//                for (Map.Entry<String, Boolean> pageEntry : pages.entrySet()) {
249
//                    Page page = pageDAO.findById(pageEntry.getKey());
250
//                    if (page.getEntities().contains(entityId) && page.getType().equals("search")) {
251
//                        pages.put(pageEntry.getKey(), Boolean.parseBoolean(status));
252
//                    }
253
//                }
254
//            }
255
//        }
256
//
257
//        community.setEntities(entities);
258
//        return portalDAO.save(community);
259
        return portalService.toggleEntity(pid, entityIds, status);
260
    }
261

    
262
    @RequestMapping(value = {"/community/{pid}/entities", "/explore/{pid}/entities", "/connect/{pid}/entities"}, method = RequestMethod.GET)
263
    public List<PortalEntity> getEntitiesForCommunity(@PathVariable(value = "pid") String pid, @RequestParam(value="entity", required=false) String entity) {
264
//        List<CommunityEntity> return_entities = new ArrayList<CommunityEntity>();
265
//        Map<String, Boolean> entities = portalDAO.findByPid(pid).getEntities();
266
//
267
//        log.debug("/community/"+pid+"/entities -- entity: "+entity);
268
//        if (entity != null) {
269
//            String entityId = entityDAO.findByPid(entity).getId();
270
//            CommunityEntity communityEntity = new CommunityEntity(entityDAO.findById(entityId));
271
//            communityEntity.setIsEnabled(entities.get(entityId));
272
//            return_entities.add(communityEntity);
273
//        } else {
274
//            if(entities != null) {
275
//                for (Map.Entry<String, Boolean> _entity : entities.entrySet()) {
276
//                    CommunityEntity communityEntity = new CommunityEntity(entityDAO.findById(_entity.getKey()));
277
//                    communityEntity.setIsEnabled(_entity.getValue());
278
//                    return_entities.add(communityEntity);
279
//                }
280
//            }
281
//        }
282
//        return return_entities;
283
        return portalService.getEntitiesForPortal(pid, entity);
284
    }
285

    
286

    
287
    @RequestMapping(value = {"/community/{pid}/pagehelpcontent", "/explore/{pid}/pagehelpcontent", "/connect/{pid}/pagehelpcontent"}, method = RequestMethod.GET)
288
    public Map<String, List<PageHelpContentResponse>> getPageHelpContentsByPosition(@PathVariable(value = "pid") String pid,
289
                                                                                    @RequestParam(required=false) String page,
290
                                                                                    @RequestParam(required=false) String active) {
291
//        Map<String, List<PageHelpContentResponse>> pageHelpContentResponses = new HashMap<>();
292
//
293
//        List<PageHelpContentResponse> pageHelpContents = null;
294
//        pageHelpContents = pageHelpContentService.getPageHelpContents(pid, page, null, active, null);
295
//
296
//        pageHelpContentResponses.put("top", new ArrayList<>());
297
//        pageHelpContentResponses.put("bottom", new ArrayList<>());
298
//        pageHelpContentResponses.put("left", new ArrayList<>());
299
//        pageHelpContentResponses.put("right", new ArrayList<>());
300
//
301
//        for (PageHelpContentResponse pageHelpContentResponse : pageHelpContents) {
302
//            pageHelpContentResponses.get(pageHelpContentResponse.getPlacement()).add(pageHelpContentResponse);
303
//        }
304
//
305
//
306
//        return pageHelpContentResponses;
307
        return portalService.getPageHelpContentsByPosition(pid, page, active);
308
    }
309

    
310
    @RequestMapping(value = {"/community/{pid}/divhelpcontent", "/explore/{pid}/divhelpcontent", "/connect/{pid}/divhelpcontent"}, method = RequestMethod.GET)
311
    public Map<String, List<DivHelpContentResponse>> getDivHelpContentsByPosition(@PathVariable(value = "pid") String pid,
312
                                                                                  @RequestParam(required=false) String page,
313
                                                                                  @RequestParam(required=false) String active) {
314
//        Map<String, List<DivHelpContentResponse>> divHelpContentResponses = new HashMap<>();
315
//
316
//        List<DivHelpContentResponse> divHelpContents = null;
317
//        divHelpContents = divHelpContentService.getDivHelpContents(pid, page, null, active);
318
//
319
//        for (DivHelpContentResponse divHelpContentResponse : divHelpContents) {
320
//            if(!divHelpContentResponses.containsKey(divHelpContentResponse.getDivId())) {
321
//                divHelpContentResponses.put(divHelpContentResponse.getDivId().getName(), new ArrayList<>());
322
//            }
323
//            divHelpContentResponses.get(divHelpContentResponse.getDivId().getName()).add(divHelpContentResponse);
324
//        }
325
//
326
//
327
//        return divHelpContentResponses;
328
        return portalService.getDivHelpContentsByPosition(pid, page, active);
329
    }
330
}
(6-6/6)