Project

General

Profile

1
package eu.dnetlib.uoaadmintoolslibrary.controllers;
2

    
3
import eu.dnetlib.uoaadmintoolslibrary.entities.*;
4
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.*;
5
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
6
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
7
import eu.dnetlib.uoaadmintoolslibrary.responses.SingleValueWrapperResponse;
8
import eu.dnetlib.uoaadmintoolslibrary.services.*;
9
import org.apache.log4j.Logger;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.web.bind.annotation.*;
12
import org.springframework.security.access.prepost.PreAuthorize;
13

    
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18

    
19
@RestController
20
@RequestMapping(value = "{portalType}")
21
@CrossOrigin(origins = "*")
22
public class AdminPortalRelationsController {
23
    private final Logger log = Logger.getLogger(this.getClass());
24

    
25
    @Autowired
26
    private PortalService portalService;
27

    
28
    @Autowired
29
    private DivHelpContentService divHelpContentService;
30

    
31
    @Autowired
32
    private DivIdService divIdService;
33

    
34
    @Autowired
35
    private PageService pageService;
36

    
37
    @Autowired
38
    private PageHelpContentService pageHelpContentService;
39

    
40
    // ENTITIES
41

    
42
    @RequestMapping(value = {"/{pid}/entities"}, method = RequestMethod.GET)
43
    public List<PortalEntity> getEntitiesForCommunity(@PathVariable PortalType portalType,
44
                                                      @PathVariable(value = "pid") String pid) {
45
        //@RequestParam(value="entity", required=false) String entity) {
46
        Portal portal = portalService.getPortal(pid);
47
        portalService.checkPortalInfo(pid, portalType.name(), portal, pid, "pid");
48
        return portalService.getEntitiesForPortal(pid, null);
49
    }
50

    
51
    @PreAuthorize("hasAnyAuthority(" +
52
            "@AuthorizationService.PORTAL_ADMIN, " +
53
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
54
    @RequestMapping(value = {"/{pid}/entity/toggle"}, method = RequestMethod.POST)
55
    public Portal toggleEntity(@PathVariable PortalType portalType,
56
                               @PathVariable(value = "pid") String pid,
57
                               @RequestBody List<String> entityIds, @RequestParam String status) throws Exception {
58
        return portalService.toggleEntity(pid, entityIds, status);
59
    }
60

    
61
    // DIV HELP CONTENTS
62

    
63
    // not used by portals
64
//    //    @RequestMapping(value = {"/community/{pid}/divhelpcontent", "/explore/{pid}/divhelpcontent", "/connect/{pid}/divhelpcontent"}, method = RequestMethod.GET)
65
//    @RequestMapping(value = {"/{pid}/divhelpcontent/test"}, method = RequestMethod.GET)
66
//    public Map<String, List<DivHelpContentResponse>> getDivHelpContentsByPositionAdmin(@PathVariable PortalType portalType,
67
//                                                                                       @PathVariable(value = "pid") String pid,
68
//                                                                                       @RequestParam(required=false) String page,
69
//                                                                                       @RequestParam(required=false) String active) {
70
//        return portalService.getDivHelpContentsByPosition(pid, page, active);
71
//    }
72

    
73
    // not used by portals
74
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
75
    @RequestMapping(value = "/{pid}/divhelpcontent", method = RequestMethod.GET)
76
    public List<DivHelpContentResponse> getDivHelpContents(@PathVariable PortalType portalType,
77
                                                           @PathVariable(value = "pid") String pid) {
78
        return divHelpContentService.getDivHelpContents(pid, null, null, null);
79
    }
80

    
81
    @RequestMapping(value = "/{pid}/divhelpcontent/{id}", method = RequestMethod.GET)
82
    public DivHelpContent getDivHelpContent(@PathVariable PortalType portalType,
83
                                            @PathVariable(value = "pid") String pid,
84
                                            @PathVariable(value = "id") String id) {
85
        DivHelpContent divHelpContent = divHelpContentService.getDivHelpContent(id);
86
        if(divHelpContent == null) {
87
            throw new ContentNotFoundException("DivHelpContent with id: "+id+" not found");
88
        }
89

    
90
        Portal portal = portalService.getPortalById(divHelpContent.getPortal());
91
        portalService.checkPortalInfo(pid, portalType.name(), portal, divHelpContent.getPortal(), "id");
92
        return divHelpContent;
93
    }
94

    
95
//    @RequestMapping(value = "/{pid}/{pageId}/divhelpcontent", method = RequestMethod.GET)
96
//    public List<DivHelpContent> getDivHelpContentsByPageId(@PathVariable PortalType portalType,
97
//                                                             @PathVariable(value = "pid") String pid,
98
//                                                             @PathVariable(value =  "pageId") String pageId) {
99
//        return divHelpContentService.getDivHelpContentsBasic(pid, portalType.name(), pageId);
100
//    }
101

    
102
    @RequestMapping(value = "/{pid}/{pageId}/divhelpcontent", method = RequestMethod.GET)
103
    public List<DivHelpContentResponse> getDivHelpContentsByPageId(@PathVariable PortalType portalType,
104
                                                           @PathVariable(value = "pid") String pid,
105
                                                           @PathVariable(value =  "pageId") String pageId) {
106
        Page page = pageService.getPage(pageId);
107
        if(page == null) {
108
            throw new ContentNotFoundException("Page with id: "+pageId+" not found");
109
        }
110
        return divHelpContentService.getDivHelpContents(pid, page.getRoute(), null, null);
111
    }
112

    
113
    @RequestMapping(value = "/{pid}/divhelpcontent/page/count", method = RequestMethod.GET)
114
    public Map<String, Integer> countDivHelpContentsForPages(@PathVariable PortalType portalType,
115
                                                              @PathVariable(value = "pid") String pid) {
116
        Map<String, Integer> mapCount = new HashMap<String, Integer>();
117

    
118
        List<Page> pages = pageService.getAllPages(pid, null, null);
119
        for(Page page : pages){
120
            List<DivHelpContent> divHelpContents = divHelpContentService.getDivHelpContentsBasic(pid, portalType.name(), page.getId());
121
            if (divHelpContents == null) {
122
                mapCount.put(page.getId(), 0);
123
            } else {
124
                mapCount.put(page.getId(), divHelpContents.size());
125
            }
126
        }
127
        return mapCount;
128
    }
129

    
130
    @PreAuthorize("hasAnyAuthority(" +
131
            "@AuthorizationService.PORTAL_ADMIN, " +
132
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
133
    @RequestMapping(value = "/{pid}/divhelpcontent/save", method = RequestMethod.POST)
134
    public DivHelpContent saveDivHelpContent(@PathVariable PortalType portalType,
135
                                             @PathVariable(value = "pid") String pid,
136
                                             @RequestBody DivHelpContent divHelpContent) {
137
        Portal portal = portalService.getPortal(divHelpContent.getPortal());
138
        portalService.checkPortalInfo(pid, portalType.name(), portal, divHelpContent.getPortal(), "pid");
139
        divHelpContent.setPortal(portal.getId());
140
        return divHelpContentService.insertOrUpdateDivHelpContent(divHelpContent);
141
    }
142

    
143
        @PreAuthorize("hasAnyAuthority(" +
144
            "@AuthorizationService.PORTAL_ADMIN, " +
145
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
146
    @RequestMapping(value = "/{pid}/divhelpcontent/update", method = RequestMethod.POST)
147
    public DivHelpContent updateDivHelpContent(@PathVariable PortalType portalType,
148
                                               @PathVariable(value = "pid") String pid,
149
                                               @RequestBody DivHelpContent divHelpContent) {
150
        Portal portal = portalService.getPortalById(divHelpContent.getPortal());
151
        portalService.checkPortalInfo(pid, portalType.name(), portal, divHelpContent.getPortal(), "id");
152
        return divHelpContentService.insertOrUpdateDivHelpContent(divHelpContent);
153
    }
154

    
155
    // cannot handle MismatchingContent
156
    @PreAuthorize("hasAnyAuthority(" +
157
            "@AuthorizationService.PORTAL_ADMIN, " +
158
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
159
    @RequestMapping(value = "/{pid}/divhelpcontent/delete", method = RequestMethod.POST)
160
    public Boolean deleteDivHelpContents(@PathVariable PortalType portalType,
161
                                         @PathVariable(value = "pid") String pid,
162
                                         @RequestBody List<String> divHelpContents) throws Exception {
163
        return divHelpContentService.deleteDivHelpContents(divHelpContents, pid, portalType);
164
    }
165

    
166
    // cannot handle MismatchingContent
167
    @PreAuthorize("hasAnyAuthority(" +
168
            "@AuthorizationService.PORTAL_ADMIN, " +
169
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
170
    @RequestMapping(value = "/{pid}/divhelpcontent/toggle", method = RequestMethod.POST)
171
    public List<String> toggleDivHelpContent(@PathVariable PortalType portalType,
172
                                             @PathVariable(value = "pid") String pid,
173
                                             @RequestBody List<String> divHelpContents, @RequestParam String status) throws Exception {
174
        return divHelpContentService.toggleDivHelpContent(divHelpContents, status, pid, portalType);
175
    }
176

    
177

    
178
    // DIVIDS
179

    
180
    @RequestMapping(value = "/{pid}/div/full", method = RequestMethod.GET)
181
    public List<DivIdResponse> getDivIdsFull(@PathVariable PortalType portalType,
182
                                             @PathVariable(value = "pid") String pid,
183
                                             @RequestParam(required = false) String page) {
184
        return divIdService.getDivIdsFull(page, null, pid);
185
    }
186

    
187
    @RequestMapping(value = "/{pid}/div/{id}/full", method = RequestMethod.GET)
188
    public DivIdResponse getDivIdFull(@PathVariable PortalType portalType,
189
                                      @PathVariable(value = "pid") String pid,
190
                                      @PathVariable(value = "id") String id) {
191
        DivIdResponse divIdResponse = divIdService.getDivIdFull(id);
192
        if(divIdResponse == null) {
193
            throw new ContentNotFoundException("DivId with id: "+id+" not found");
194
        }
195
        if(!divIdResponse.getPortalType().equals(portalType.name())) {
196
            throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting portal info: type: "+divIdResponse.getPortalType());
197
        }
198
        return divIdResponse;
199
    }
200

    
201
    @RequestMapping(value = "/{pid}/div/pages", method = RequestMethod.GET)
202
    public Set<String> getDivIdsPages(@PathVariable PortalType portalType,
203
                                      @PathVariable(value = "pid") String pid) {
204
        return divIdService.getDivIdsPages(pid);
205
    }
206

    
207
    // PAGES
208

    
209
    // used
210
    @RequestMapping(value = {"/{pid}/pages"}, method = RequestMethod.GET)
211
    public List<PortalPage> getPagesForPortalByType(@PathVariable PortalType portalType,
212
                                                    @PathVariable(value = "pid") String pid,
213
                                                    @RequestParam(value="page_type", required=false) String page_type,
214
                                                    @RequestParam(value="page_route", required=false) String page_route,
215
                                                    @RequestParam(value="div", required = false) String div,
216
                                                    @RequestParam(value="with_positions", required = false) String with_positions) {
217
        return portalService.getPagesForPortalByType(pid, page_type, page_route, div, with_positions);
218
    }
219

    
220
    // not used by portals
221
//    @RequestMapping(value = "/{pid}/page", method = RequestMethod.GET)
222
//    public List<Page> getAllPages(@PathVariable PortalType portalType,
223
//                                  @PathVariable(value = "pid") String pid,
224
//                                  @RequestParam(value="page_route", required=false) String page_route,
225
//                                  @RequestParam(value="with_positions", required=false) String with_positions) {
226
//        return pageService.getAllPages(pid, page_route, with_positions);
227
//    }
228

    
229
//    // not used by portals
230
//    @RequestMapping(value = {"/{id}/page"}, method = RequestMethod.POST)
231
//    public Portal insertOrUpdatePage(@PathVariable PortalType portalType,
232
//                                     @PathVariable(value = "id") String id, @RequestBody PortalPage page) {
233
//        return portalService.insertOrUpdatePage(id, page);
234
//    }
235

    
236
    // used
237
    @PreAuthorize("hasAnyAuthority(" +
238
            "@AuthorizationService.PORTAL_ADMIN, " +
239
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
240
    @RequestMapping(value = {"/{pid}/page/toggle"}, method = RequestMethod.POST)
241
    public Portal togglePage(@PathVariable PortalType portalType,
242
                             @PathVariable(value = "pid") String pid,
243
                             @RequestBody List<String> pageIds, @RequestParam String status) throws Exception {
244
        return portalService.togglePage(pid, portalType.name(), pageIds, status);
245
    }
246

    
247
    @RequestMapping(value = "/{pid}/page", method = RequestMethod.GET)
248
    public Page getPageByRoute(@PathVariable PortalType portalType,
249
                               @PathVariable(value = "pid") String pid,
250
                               @RequestParam(value="page_route", required=true) String page_route) {
251
        List<Page> pageInArray = pageService.getAllPages(pid, page_route, null);
252
        if(pageInArray == null || pageInArray.size() == 0) {
253
            throw new ContentNotFoundException("No page with route: "+page_route + " found for portal with pid: "+pid);
254
        }
255
        return pageInArray.get(0);
256
    }
257

    
258
    @RequestMapping(value = "/{pid}/page/{id}", method = RequestMethod.GET)
259
    public Page getPage(@PathVariable PortalType portalType,
260
                        @PathVariable(value = "pid") String pid,
261
                        @PathVariable(value = "id") String id) {
262
        Page page = pageService.getPage(id);
263
        if(page == null) {
264
            throw new ContentNotFoundException("Page with id: "+id+" not found");
265
        }
266
        if(!page.getPortalType().equals(portalType.name())) {
267
            throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting portal info: type: "+page.getPortalType());
268
        }
269
        return page;
270
    }
271

    
272
    // PAGE HELP CONTENTS
273

    
274
    // not used by portals
275
//    @RequestMapping(value = {"/{pid}/pagehelpcontent/test"}, method = RequestMethod.GET)
276
//    public Map<String, List<PageHelpContentResponse>> getPageHelpContentsByPositionAdmin(@PathVariable PortalType portalType,
277
//                                                                                         @PathVariable(value = "pid") String pid,
278
//                                                                                         @RequestParam(required=false) String page,
279
//                                                                                         @RequestParam(required=false) String active) {
280
//        return portalService.getPageHelpContentsByPosition(pid, page, active);
281
//    }
282

    
283
    // not used by portals
284
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
285
    @RequestMapping(value = "/{pid}/pagehelpcontent", method = RequestMethod.GET)
286
    public List<PageHelpContentResponse> getPageHelpContents(@PathVariable PortalType portalType,
287
                                                             @PathVariable(value = "pid") String pid) {
288
        return pageHelpContentService.getPageHelpContents(pid, null, null, null, null, null);
289
    }
290

    
291
    // used
292
    @RequestMapping(value = "/{pid}/{pageId}/pagehelpcontent", method = RequestMethod.GET)
293
    public List<PageHelpContent> getPageHelpContentsByPageId(@PathVariable PortalType portalType,
294
                                                             @PathVariable(value = "pid") String pid,
295
                                                             @PathVariable(value =  "pageId") String pageId) {
296
        return pageHelpContentService.getPageHelpContentsBasic(pid, portalType.name(), pageId);
297
    }
298

    
299
    // used
300
    @RequestMapping(value = "/{pid}/pagehelpcontent/page/count", method = RequestMethod.GET)
301
    public Map<String, Integer> countPageHelpContentsForPages(@PathVariable PortalType portalType,
302
                                                              @PathVariable(value = "pid") String pid) {
303
        Map<String, Integer> mapCount = new HashMap<String, Integer>();
304

    
305
        List<Page> pages = pageService.getAllPages(pid, null, null);
306
        for(Page page : pages){
307
            List<PageHelpContent> pageHelpContents = pageHelpContentService.getPageHelpContentsBasic(pid, portalType.name(), page.getId());
308
            if (pageHelpContents == null) {
309
                mapCount.put(page.getId(), 0);
310
            } else {
311
                mapCount.put(page.getId(), pageHelpContents.size());
312
            }
313
        }
314
        return mapCount;
315
    }
316

    
317
//    @RequestMapping(value = "/{pid}/pagehelpcontent/{id}", method = RequestMethod.GET)
318
//    public PageHelpContent getPageHelpContent(@PathVariable PortalType portalType,
319
//                                              @PathVariable(value = "pid") String pid,
320
//                                              @PathVariable(value = "id") String id) {
321
//        PageHelpContent pageHelpContent = pageHelpContentService.getPageHelpContent(id);
322
//        if(pageHelpContent == null) {
323
//            throw new ContentNotFoundException("PageHelpContent with id: "+id+" not found");
324
//        }
325
//
326
//        Portal portal = portalService.getPortalById(pageHelpContent.getPortal());
327
//        portalService.checkPortalInfo(pid, portalType.name(), portal, pageHelpContent.getPortal(), "id");
328
//        return pageHelpContent;
329
//    }
330

    
331
    @PreAuthorize("hasAnyAuthority(" +
332
            "@AuthorizationService.PORTAL_ADMIN, " +
333
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
334
    @RequestMapping(value = "/{pid}/pagehelpcontent/save", method = RequestMethod.POST)
335
    public PageHelpContent insertPageHelpContent(@PathVariable PortalType portalType,
336
                                                 @PathVariable(value = "pid") String pid,
337
                                                 @RequestBody PageHelpContent pageHelpContent) {
338
        Portal portal = portalService.getPortal(pageHelpContent.getPortal());
339
        portalService.checkPortalInfo(pid, portalType.name(), portal, pageHelpContent.getPortal(), "pid");
340
        pageHelpContent.setPortal(portal.getId());
341
        return pageHelpContentService.insertOrUpdatePageHelpContent(pageHelpContent);
342
    }
343

    
344
    @PreAuthorize("hasAnyAuthority(" +
345
            "@AuthorizationService.PORTAL_ADMIN, " +
346
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
347
    @RequestMapping(value = "/{pid}/pagehelpcontent/update", method = RequestMethod.POST)
348
    public PageHelpContent updatePageHelpContent(@PathVariable PortalType portalType,
349
                                                 @PathVariable(value = "pid") String pid,
350
                                                 @RequestBody PageHelpContent pageHelpContent) {
351
        Portal portal = portalService.getPortalById(pageHelpContent.getPortal());
352
        portalService.checkPortalInfo(pid, portalType.name(), portal, pageHelpContent.getPortal(), "id");
353
        return pageHelpContentService.insertOrUpdatePageHelpContent(pageHelpContent);
354
    }
355

    
356
    // cannot handle MismatchingContent
357
    @PreAuthorize("hasAnyAuthority(" +
358
            "@AuthorizationService.PORTAL_ADMIN, " +
359
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
360
    @RequestMapping(value = "/{pid}/pagehelpcontent/delete", method = RequestMethod.POST)
361
    public Boolean deletePageHelpContents(@PathVariable PortalType portalType,
362
                                          @PathVariable(value = "pid") String pid,
363
                                          @RequestBody List<String> pageHelpContents) throws Exception {
364
        return pageHelpContentService.deletePageHelpContents(pageHelpContents, pid, portalType);
365
    }
366

    
367
    // cannot handle MismatchingContent
368
    @PreAuthorize("hasAnyAuthority(" +
369
            "@AuthorizationService.PORTAL_ADMIN, " +
370
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
371
    @RequestMapping(value = "/{pid}/pagehelpcontent/toggle", method = RequestMethod.POST)
372
    public List<String> togglePageHelpContent(@PathVariable PortalType portalType,
373
                                              @PathVariable(value = "pid") String pid,
374
                                              @RequestBody List<String> pageHelpContents, @RequestParam String status) throws Exception {
375
        return pageHelpContentService.togglePageHelpContent(pageHelpContents, status, pid, portalType);
376
    }
377

    
378
}
(1-1/10)