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}/divhelpcontent/page/count", method = RequestMethod.GET)
103
    public Map<String, Integer> countDivHelpContentsForPages(@PathVariable PortalType portalType,
104
                                                              @PathVariable(value = "pid") String pid) {
105
        Map<String, Integer> mapCount = new HashMap<String, Integer>();
106

    
107
        List<Page> pages = pageService.getAllPages(pid, null, null);
108
        for(Page page : pages){
109
            List<DivHelpContent> divHelpContents = divHelpContentService.getDivHelpContentsBasic(pid, portalType.name(), page.getId());
110
            if (divHelpContents == null) {
111
                mapCount.put(page.getId(), 0);
112
            } else {
113
                mapCount.put(page.getId(), divHelpContents.size());
114
            }
115
        }
116
        return mapCount;
117
    }
118

    
119
    @PreAuthorize("hasAnyAuthority(" +
120
            "@AuthorizationService.PORTAL_ADMIN, " +
121
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
122
    @RequestMapping(value = "/{pid}/divhelpcontent/save", method = RequestMethod.POST)
123
    public DivHelpContent saveDivHelpContent(@PathVariable PortalType portalType,
124
                                             @PathVariable(value = "pid") String pid,
125
                                             @RequestBody DivHelpContent divHelpContent) {
126
        Portal portal = portalService.getPortal(divHelpContent.getPortal());
127
        portalService.checkPortalInfo(pid, portalType.name(), portal, divHelpContent.getPortal(), "pid");
128
        divHelpContent.setPortal(portal.getId());
129
        return divHelpContentService.insertOrUpdateDivHelpContent(divHelpContent);
130
    }
131

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

    
144
    // cannot handle MismatchingContent
145
    @PreAuthorize("hasAnyAuthority(" +
146
            "@AuthorizationService.PORTAL_ADMIN, " +
147
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
148
    @RequestMapping(value = "/{pid}/divhelpcontent/delete", method = RequestMethod.POST)
149
    public Boolean deleteDivHelpContents(@PathVariable PortalType portalType,
150
                                         @PathVariable(value = "pid") String pid,
151
                                         @RequestBody List<String> divHelpContents) throws Exception {
152
        return divHelpContentService.deleteDivHelpContents(divHelpContents, pid, portalType);
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/toggle", method = RequestMethod.POST)
160
    public List<String> toggleDivHelpContent(@PathVariable PortalType portalType,
161
                                             @PathVariable(value = "pid") String pid,
162
                                             @RequestBody List<String> divHelpContents, @RequestParam String status) throws Exception {
163
        return divHelpContentService.toggleDivHelpContent(divHelpContents, status, pid, portalType);
164
    }
165

    
166

    
167
    // DIVIDS
168

    
169
    @RequestMapping(value = "/{pid}/div/full", method = RequestMethod.GET)
170
    public List<DivIdResponse> getDivIdsFull(@PathVariable PortalType portalType,
171
                                             @PathVariable(value = "pid") String pid,
172
                                             @RequestParam(required = false) String page) {
173
        return divIdService.getDivIdsFull(page, null, pid);
174
    }
175

    
176
    @RequestMapping(value = "/{pid}/div/{id}/full", method = RequestMethod.GET)
177
    public DivIdResponse getDivIdFull(@PathVariable PortalType portalType,
178
                                      @PathVariable(value = "pid") String pid,
179
                                      @PathVariable(value = "id") String id) {
180
        DivIdResponse divIdResponse = divIdService.getDivIdFull(id);
181
        if(divIdResponse == null) {
182
            throw new ContentNotFoundException("DivId with id: "+id+" not found");
183
        }
184
        if(!divIdResponse.getPortalType().equals(portalType.name())) {
185
            throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting portal info: type: "+divIdResponse.getPortalType());
186
        }
187
        return divIdResponse;
188
    }
189

    
190
    @RequestMapping(value = "/{pid}/div/pages", method = RequestMethod.GET)
191
    public Set<String> getDivIdsPages(@PathVariable PortalType portalType,
192
                                      @PathVariable(value = "pid") String pid) {
193
        return divIdService.getDivIdsPages(pid);
194
    }
195

    
196
    // PAGES
197

    
198
    // used
199
    @RequestMapping(value = {"/{pid}/pages"}, method = RequestMethod.GET)
200
    public List<PortalPage> getPagesForPortalByType(@PathVariable PortalType portalType,
201
                                                    @PathVariable(value = "pid") String pid,
202
                                                    @RequestParam(value="page_type", required=false) String page_type,
203
                                                    //@RequestParam(value="page_route", required=false) String page_route,
204
                                                    @RequestParam(value="div", required = false) String div,
205
                                                    @RequestParam(value="with_positions", required = false) String with_positions) {
206
        return portalService.getPagesForPortalByType(pid, page_type, null, div, with_positions);
207
    }
208

    
209
    // not used by portals
210
//    @RequestMapping(value = "/{pid}/page", method = RequestMethod.GET)
211
//    public List<Page> getAllPages(@PathVariable PortalType portalType,
212
//                                  @PathVariable(value = "pid") String pid,
213
//                                  @RequestParam(value="page_route", required=false) String page_route,
214
//                                  @RequestParam(value="with_positions", required=false) String with_positions) {
215
//        return pageService.getAllPages(pid, page_route, with_positions);
216
//    }
217

    
218
//    // not used by portals
219
//    @RequestMapping(value = {"/{id}/page"}, method = RequestMethod.POST)
220
//    public Portal insertOrUpdatePage(@PathVariable PortalType portalType,
221
//                                     @PathVariable(value = "id") String id, @RequestBody PortalPage page) {
222
//        return portalService.insertOrUpdatePage(id, page);
223
//    }
224

    
225
    // used
226
    @PreAuthorize("hasAnyAuthority(" +
227
            "@AuthorizationService.PORTAL_ADMIN, " +
228
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
229
    @RequestMapping(value = {"/{pid}/page/toggle"}, method = RequestMethod.POST)
230
    public Portal togglePage(@PathVariable PortalType portalType,
231
                             @PathVariable(value = "pid") String pid,
232
                             @RequestBody List<String> pageIds, @RequestParam String status) throws Exception {
233
        return portalService.togglePage(pid, portalType.name(), pageIds, status);
234
    }
235

    
236
    @RequestMapping(value = "/{pid}/page", method = RequestMethod.GET)
237
    public Page getPageByRoute(@PathVariable PortalType portalType,
238
                               @PathVariable(value = "pid") String pid,
239
                               @RequestParam(value="page_route", required=true) String page_route) {
240
        List<Page> pageInArray = pageService.getAllPages(pid, page_route, null);
241
        if(pageInArray == null || pageInArray.size() == 0) {
242
            throw new ContentNotFoundException("No page with route: "+page_route + " found for portal with pid: "+pid);
243
        }
244
        return pageInArray.get(0);
245
    }
246

    
247
    @RequestMapping(value = "/{pid}/page/{id}", method = RequestMethod.GET)
248
    public Page getPage(@PathVariable PortalType portalType,
249
                        @PathVariable(value = "pid") String pid,
250
                        @PathVariable(value = "id") String id) {
251
        Page page = pageService.getPage(id);
252
        if(page == null) {
253
            throw new ContentNotFoundException("Page with id: "+id+" not found");
254
        }
255
        if(!page.getPortalType().equals(portalType.name())) {
256
            throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting portal info: type: "+page.getPortalType());
257
        }
258
        return page;
259
    }
260

    
261
    // PAGE HELP CONTENTS
262

    
263
    // not used by portals
264
//    @RequestMapping(value = {"/{pid}/pagehelpcontent/test"}, method = RequestMethod.GET)
265
//    public Map<String, List<PageHelpContentResponse>> getPageHelpContentsByPositionAdmin(@PathVariable PortalType portalType,
266
//                                                                                         @PathVariable(value = "pid") String pid,
267
//                                                                                         @RequestParam(required=false) String page,
268
//                                                                                         @RequestParam(required=false) String active) {
269
//        return portalService.getPageHelpContentsByPosition(pid, page, active);
270
//    }
271

    
272
    // not used by portals
273
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
274
    @RequestMapping(value = "/{pid}/pagehelpcontent", method = RequestMethod.GET)
275
    public List<PageHelpContentResponse> getPageHelpContents(@PathVariable PortalType portalType,
276
                                                             @PathVariable(value = "pid") String pid) {
277
        return pageHelpContentService.getPageHelpContents(pid, null, null, null, null, null);
278
    }
279

    
280
    // used
281
    @RequestMapping(value = "/{pid}/{pageId}/pagehelpcontent", method = RequestMethod.GET)
282
    public List<PageHelpContent> getPageHelpContentsByPageId(@PathVariable PortalType portalType,
283
                                                             @PathVariable(value = "pid") String pid,
284
                                                             @PathVariable(value =  "pageId") String pageId) {
285
        return pageHelpContentService.getPageHelpContentsBasic(pid, portalType.name(), pageId);
286
    }
287

    
288
    // used
289
    @RequestMapping(value = "/{pid}/pagehelpcontent/page/count", method = RequestMethod.GET)
290
    public Map<String, Integer> countPageHelpContentsForPages(@PathVariable PortalType portalType,
291
                                                              @PathVariable(value = "pid") String pid) {
292
        Map<String, Integer> mapCount = new HashMap<String, Integer>();
293

    
294
        List<Page> pages = pageService.getAllPages(pid, null, null);
295
        for(Page page : pages){
296
            List<PageHelpContent> pageHelpContents = pageHelpContentService.getPageHelpContentsBasic(pid, portalType.name(), page.getId());
297
            if (pageHelpContents == null) {
298
                mapCount.put(page.getId(), 0);
299
            } else {
300
                mapCount.put(page.getId(), pageHelpContents.size());
301
            }
302
        }
303
        return mapCount;
304
    }
305

    
306
//    @RequestMapping(value = "/{pid}/pagehelpcontent/{id}", method = RequestMethod.GET)
307
//    public PageHelpContent getPageHelpContent(@PathVariable PortalType portalType,
308
//                                              @PathVariable(value = "pid") String pid,
309
//                                              @PathVariable(value = "id") String id) {
310
//        PageHelpContent pageHelpContent = pageHelpContentService.getPageHelpContent(id);
311
//        if(pageHelpContent == null) {
312
//            throw new ContentNotFoundException("PageHelpContent with id: "+id+" not found");
313
//        }
314
//
315
//        Portal portal = portalService.getPortalById(pageHelpContent.getPortal());
316
//        portalService.checkPortalInfo(pid, portalType.name(), portal, pageHelpContent.getPortal(), "id");
317
//        return pageHelpContent;
318
//    }
319

    
320
    @PreAuthorize("hasAnyAuthority(" +
321
            "@AuthorizationService.PORTAL_ADMIN, " +
322
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
323
    @RequestMapping(value = "/{pid}/pagehelpcontent/save", method = RequestMethod.POST)
324
    public PageHelpContent insertPageHelpContent(@PathVariable PortalType portalType,
325
                                                 @PathVariable(value = "pid") String pid,
326
                                                 @RequestBody PageHelpContent pageHelpContent) {
327
        Portal portal = portalService.getPortal(pageHelpContent.getPortal());
328
        portalService.checkPortalInfo(pid, portalType.name(), portal, pageHelpContent.getPortal(), "pid");
329
        pageHelpContent.setPortal(portal.getId());
330
        return pageHelpContentService.insertOrUpdatePageHelpContent(pageHelpContent);
331
    }
332

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

    
345
    // cannot handle MismatchingContent
346
    @PreAuthorize("hasAnyAuthority(" +
347
            "@AuthorizationService.PORTAL_ADMIN, " +
348
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
349
    @RequestMapping(value = "/{pid}/pagehelpcontent/delete", method = RequestMethod.POST)
350
    public Boolean deletePageHelpContents(@PathVariable PortalType portalType,
351
                                          @PathVariable(value = "pid") String pid,
352
                                          @RequestBody List<String> pageHelpContents) throws Exception {
353
        return pageHelpContentService.deletePageHelpContents(pageHelpContents, pid, portalType);
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/toggle", method = RequestMethod.POST)
361
    public List<String> togglePageHelpContent(@PathVariable PortalType portalType,
362
                                              @PathVariable(value = "pid") String pid,
363
                                              @RequestBody List<String> pageHelpContents, @RequestParam String status) throws Exception {
364
        return pageHelpContentService.togglePageHelpContent(pageHelpContents, status, pid, portalType);
365
    }
366

    
367
}
(1-1/9)