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.services.*;
8
import org.apache.log4j.Logger;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.web.bind.annotation.*;
11
import org.springframework.security.access.prepost.PreAuthorize;
12

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

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

    
22
    @Autowired
23
    private PortalService portalService;
24

    
25
    @Autowired
26
    private DivHelpContentService divHelpContentService;
27

    
28
    @Autowired
29
    private DivIdService divIdService;
30

    
31
    @Autowired
32
    private PageService pageService;
33

    
34
    @Autowired
35
    private PageHelpContentService pageHelpContentService;
36

    
37
    // ENTITIES
38

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

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

    
58
    // DIV HELP CONTENTS
59

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

    
70
    @RequestMapping(value = "/{pid}/divhelpcontent", method = RequestMethod.GET)
71
    public List<DivHelpContentResponse> getDivHelpContents(@PathVariable PortalType portalType,
72
                                                           @PathVariable(value = "pid") String pid) {
73
        return divHelpContentService.getDivHelpContents(pid, null, null, null);
74
    }
75

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

    
85
        Portal portal = portalService.getPortalById(divHelpContent.getPortal());
86
        portalService.checkPortalInfo(pid, portalType.name(), portal, divHelpContent.getPortal(), "id");
87
        return divHelpContent;
88
    }
89

    
90
    @PreAuthorize("hasAnyAuthority(" +
91
            "@AuthorizationService.PORTAL_ADMIN, " +
92
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
93
    @RequestMapping(value = "/{pid}/divhelpcontent/save", method = RequestMethod.POST)
94
    public DivHelpContent saveDivHelpContent(@PathVariable PortalType portalType,
95
                                             @PathVariable(value = "pid") String pid,
96
                                             @RequestBody DivHelpContent divHelpContent) {
97
        Portal portal = portalService.getPortal(divHelpContent.getPortal());
98
        portalService.checkPortalInfo(pid, portalType.name(), portal, divHelpContent.getPortal(), "pid");
99
        divHelpContent.setPortal(portal.getId());
100
        return divHelpContentService.insertOrUpdateDivHelpContent(divHelpContent);
101
    }
102

    
103
        @PreAuthorize("hasAnyAuthority(" +
104
            "@AuthorizationService.PORTAL_ADMIN, " +
105
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
106
    @RequestMapping(value = "/{pid}/divhelpcontent/update", method = RequestMethod.POST)
107
    public DivHelpContent updateDivHelpContent(@PathVariable PortalType portalType,
108
                                               @PathVariable(value = "pid") String pid,
109
                                               @RequestBody DivHelpContent divHelpContent) {
110
        Portal portal = portalService.getPortalById(divHelpContent.getPortal());
111
        portalService.checkPortalInfo(pid, portalType.name(), portal, divHelpContent.getPortal(), "id");
112
        return divHelpContentService.insertOrUpdateDivHelpContent(divHelpContent);
113
    }
114

    
115
    // cannot handle MismatchingContent
116
    @PreAuthorize("hasAnyAuthority(" +
117
            "@AuthorizationService.PORTAL_ADMIN, " +
118
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
119
    @RequestMapping(value = "/{pid}/divhelpcontent/delete", method = RequestMethod.POST)
120
    public Boolean deleteDivHelpContents(@PathVariable PortalType portalType,
121
                                         @PathVariable(value = "pid") String pid,
122
                                         @RequestBody List<String> divHelpContents) throws Exception {
123
        return divHelpContentService.deleteDivHelpContents(divHelpContents, pid, portalType);
124
    }
125

    
126
    // cannot handle MismatchingContent
127
    @PreAuthorize("hasAnyAuthority(" +
128
            "@AuthorizationService.PORTAL_ADMIN, " +
129
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
130
    @RequestMapping(value = "/{pid}/divhelpcontent/toggle", method = RequestMethod.POST)
131
    public List<String> toggleDivHelpContent(@PathVariable PortalType portalType,
132
                                             @PathVariable(value = "pid") String pid,
133
                                             @RequestBody List<String> divHelpContents, @RequestParam String status) throws Exception {
134
        return divHelpContentService.toggleDivHelpContent(divHelpContents, status, pid, portalType);
135
    }
136

    
137

    
138
    // DIVIDS
139

    
140
    @RequestMapping(value = "/{pid}/div/full", method = RequestMethod.GET)
141
    public List<DivIdResponse> getDivIdsFull(@PathVariable PortalType portalType,
142
                                             @PathVariable(value = "pid") String pid,
143
                                             @RequestParam(required = false) String page) {
144
        return divIdService.getDivIdsFull(page, null, pid);
145
    }
146

    
147
    @RequestMapping(value = "/{pid}/div/{id}/full", method = RequestMethod.GET)
148
    public DivIdResponse getDivIdFull(@PathVariable PortalType portalType,
149
                                      @PathVariable(value = "pid") String pid,
150
                                      @PathVariable(value = "id") String id) {
151
        DivIdResponse divIdResponse = divIdService.getDivIdFull(id);
152
        if(divIdResponse == null) {
153
            throw new ContentNotFoundException("DivId with id: "+id+" not found");
154
        }
155
        if(!divIdResponse.getPortalType().equals(portalType.name())) {
156
            throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting portal info: type: "+divIdResponse.getPortalType());
157
        }
158
        return divIdResponse;
159
    }
160

    
161
    @RequestMapping(value = "/{pid}/div/pages", method = RequestMethod.GET)
162
    public Set<String> getDivIdsPages(@PathVariable PortalType portalType,
163
                                      @PathVariable(value = "pid") String pid) {
164
        return divIdService.getDivIdsPages(pid);
165
    }
166

    
167
    // PAGES
168

    
169
    // used
170
    @RequestMapping(value = {"/{pid}/pages"}, method = RequestMethod.GET)
171
    public List<PortalPage> getPagesForPortalByType(@PathVariable PortalType portalType,
172
                                                    @PathVariable(value = "pid") String pid,
173
                                                    @RequestParam(value="page_type", required=false) String page_type,
174
                                                    //@RequestParam(value="page_route", required=false) String page_route,
175
                                                    @RequestParam(value="div", required = false) String div,
176
                                                    @RequestParam(value="with_positions", required = false) String with_positions) {
177
        return portalService.getPagesForPortalByType(pid, page_type, null, div, with_positions);
178
    }
179

    
180
    // not used by portals
181
//    @RequestMapping(value = "/{pid}/page", method = RequestMethod.GET)
182
//    public List<Page> getAllPages(@PathVariable PortalType portalType,
183
//                                  @PathVariable(value = "pid") String pid,
184
//                                  @RequestParam(value="page_route", required=false) String page_route,
185
//                                  @RequestParam(value="with_positions", required=false) String with_positions) {
186
//        return pageService.getAllPages(pid, page_route, with_positions);
187
//    }
188

    
189
//    // not used by portals
190
//    @RequestMapping(value = {"/{id}/page"}, method = RequestMethod.POST)
191
//    public Portal insertOrUpdatePage(@PathVariable PortalType portalType,
192
//                                     @PathVariable(value = "id") String id, @RequestBody PortalPage page) {
193
//        return portalService.insertOrUpdatePage(id, page);
194
//    }
195

    
196
    // used
197
    @PreAuthorize("hasAnyAuthority(" +
198
            "@AuthorizationService.PORTAL_ADMIN, " +
199
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
200
    @RequestMapping(value = {"/{pid}/page/toggle"}, method = RequestMethod.POST)
201
    public Portal togglePage(@PathVariable PortalType portalType,
202
                             @PathVariable(value = "pid") String pid,
203
                             @RequestBody List<String> pageIds, @RequestParam String status) throws Exception {
204
        return portalService.togglePage(pid, portalType.name(), pageIds, status);
205
    }
206

    
207
    @RequestMapping(value = "/{pid}/page", method = RequestMethod.GET)
208
    public Page getPageByRoute(@PathVariable PortalType portalType,
209
                               @PathVariable(value = "pid") String pid,
210
                               @RequestParam(value="page_route", required=true) String page_route) {
211
        List<Page> pageInArray = pageService.getAllPages(pid, page_route, null);
212
        if(pageInArray == null || pageInArray.size() == 0) {
213
            throw new ContentNotFoundException("No page with route: "+page_route + " found for portal with pid: "+pid);
214
        }
215
        return pageInArray.get(0);
216
    }
217

    
218
    @RequestMapping(value = "/{pid}/page/{id}", method = RequestMethod.GET)
219
    public Page getPage(@PathVariable PortalType portalType,
220
                        @PathVariable(value = "pid") String pid,
221
                        @PathVariable(value = "id") String id) {
222
        Page page = pageService.getPage(id);
223
        if(page == null) {
224
            throw new ContentNotFoundException("Page with id: "+id+" not found");
225
        }
226
        if(!page.getPortalType().equals(portalType.name())) {
227
            throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting portal info: type: "+page.getPortalType());
228
        }
229
        return page;
230
    }
231

    
232
    // PAGE HELP CONTENTS
233

    
234
    // not used by portals
235
//    @RequestMapping(value = {"/{pid}/pagehelpcontent/test"}, method = RequestMethod.GET)
236
//    public Map<String, List<PageHelpContentResponse>> getPageHelpContentsByPositionAdmin(@PathVariable PortalType portalType,
237
//                                                                                         @PathVariable(value = "pid") String pid,
238
//                                                                                         @RequestParam(required=false) String page,
239
//                                                                                         @RequestParam(required=false) String active) {
240
//        return portalService.getPageHelpContentsByPosition(pid, page, active);
241
//    }
242

    
243
    @RequestMapping(value = "/{pid}/pagehelpcontent", method = RequestMethod.GET)
244
    public List<PageHelpContentResponse> getPageHelpContents(@PathVariable PortalType portalType,
245
                                                             @PathVariable(value = "pid") String pid) {
246
        return pageHelpContentService.getPageHelpContents(pid, null, null, null, null, null);
247
    }
248

    
249
    @RequestMapping(value = "/{pid}/pagehelpcontent/{id}", method = RequestMethod.GET)
250
    public PageHelpContent getPageHelpContent(@PathVariable PortalType portalType,
251
                                              @PathVariable(value = "pid") String pid,
252
                                              @PathVariable(value = "id") String id) {
253
        PageHelpContent pageHelpContent = pageHelpContentService.getPageHelpContent(id);
254
        if(pageHelpContent == null) {
255
            throw new ContentNotFoundException("PageHelpContent with id: "+id+" not found");
256
        }
257

    
258
        Portal portal = portalService.getPortalById(pageHelpContent.getPortal());
259
        portalService.checkPortalInfo(pid, portalType.name(), portal, pageHelpContent.getPortal(), "id");
260
        return pageHelpContent;
261
    }
262

    
263
    @PreAuthorize("hasAnyAuthority(" +
264
            "@AuthorizationService.PORTAL_ADMIN, " +
265
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
266
    @RequestMapping(value = "/{pid}/pagehelpcontent/save", method = RequestMethod.POST)
267
    public PageHelpContent insertPageHelpContent(@PathVariable PortalType portalType,
268
                                                 @PathVariable(value = "pid") String pid,
269
                                                 @RequestBody PageHelpContent pageHelpContent) {
270
        Portal portal = portalService.getPortal(pageHelpContent.getPortal());
271
        portalService.checkPortalInfo(pid, portalType.name(), portal, pageHelpContent.getPortal(), "pid");
272
        pageHelpContent.setPortal(portal.getId());
273
        return pageHelpContentService.insertOrUpdatePageHelpContent(pageHelpContent);
274
    }
275

    
276
    @PreAuthorize("hasAnyAuthority(" +
277
            "@AuthorizationService.PORTAL_ADMIN, " +
278
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
279
    @RequestMapping(value = "/{pid}/pagehelpcontent/update", method = RequestMethod.POST)
280
    public PageHelpContent updatePageHelpContent(@PathVariable PortalType portalType,
281
                                                 @PathVariable(value = "pid") String pid,
282
                                                 @RequestBody PageHelpContent pageHelpContent) {
283
        Portal portal = portalService.getPortalById(pageHelpContent.getPortal());
284
        portalService.checkPortalInfo(pid, portalType.name(), portal, pageHelpContent.getPortal(), "id");
285
        return pageHelpContentService.insertOrUpdatePageHelpContent(pageHelpContent);
286
    }
287

    
288
    // cannot handle MismatchingContent
289
    @PreAuthorize("hasAnyAuthority(" +
290
            "@AuthorizationService.PORTAL_ADMIN, " +
291
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
292
    @RequestMapping(value = "/{pid}/pagehelpcontent/delete", method = RequestMethod.POST)
293
    public Boolean deletePageHelpContents(@PathVariable PortalType portalType,
294
                                          @PathVariable(value = "pid") String pid,
295
                                          @RequestBody List<String> pageHelpContents) throws Exception {
296
        return pageHelpContentService.deletePageHelpContents(pageHelpContents, pid, portalType);
297
    }
298

    
299
    // cannot handle MismatchingContent
300
    @PreAuthorize("hasAnyAuthority(" +
301
            "@AuthorizationService.PORTAL_ADMIN, " +
302
            "@AuthorizationService.curator(#portalType), @AuthorizationService.manager(#portalType, #pid))")
303
    @RequestMapping(value = "/{pid}/pagehelpcontent/toggle", method = RequestMethod.POST)
304
    public List<String> togglePageHelpContent(@PathVariable PortalType portalType,
305
                                              @PathVariable(value = "pid") String pid,
306
                                              @RequestBody List<String> pageHelpContents, @RequestParam String status) throws Exception {
307
        return pageHelpContentService.togglePageHelpContent(pageHelpContents, status, pid, portalType);
308
    }
309

    
310
}
(1-1/9)