Domain model change
[portal.git] / portal-BE / src / main / java / org / onap / portal / controller / WidgetsController.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.controller;
42
43 import java.io.IOException;
44 import java.security.Principal;
45 import java.util.List;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import org.onap.portal.domain.db.fn.FnUser;
49 import org.onap.portal.domain.dto.transport.FieldsValidator;
50 import org.onap.portal.domain.dto.transport.OnboardingWidget;
51 import org.onap.portal.domain.dto.transport.WidgetCatalogPersonalization;
52 import org.onap.portal.logging.aop.EPAuditLog;
53 import org.onap.portal.service.AdminRolesService;
54 import org.onap.portal.service.PersUserWidgetService;
55 import org.onap.portal.service.WidgetService;
56 import org.onap.portal.service.fn.FnUserService;
57 import org.onap.portal.utils.EcompPortalUtils;
58 import org.onap.portal.validation.DataValidator;
59 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.context.annotation.EnableAspectJAutoProxy;
62 import org.springframework.http.MediaType;
63 import org.springframework.security.access.prepost.PreAuthorize;
64 import org.springframework.web.bind.annotation.DeleteMapping;
65 import org.springframework.web.bind.annotation.GetMapping;
66 import org.springframework.web.bind.annotation.PathVariable;
67 import org.springframework.web.bind.annotation.PostMapping;
68 import org.springframework.web.bind.annotation.PutMapping;
69 import org.springframework.web.bind.annotation.RequestBody;
70 import org.springframework.web.bind.annotation.RestController;
71
72 @EPAuditLog
73 @RestController
74 @EnableAspectJAutoProxy
75 public class WidgetsController {
76
77        private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsController.class);
78
79        private final FnUserService fnUserService;
80        private final WidgetService widgetService;
81        private final PersUserWidgetService persUserWidgetService;
82
83        @Autowired
84        public WidgetsController(final FnUserService fnUserService, final WidgetService widgetService,
85                final PersUserWidgetService persUserWidgetService) {
86               this.fnUserService = fnUserService;
87               this.widgetService = widgetService;
88               this.persUserWidgetService = persUserWidgetService;
89        }
90
91        @GetMapping(value = {"/portalApi/widgets"}, produces = MediaType.APPLICATION_JSON_VALUE)
92        public List<OnboardingWidget> getOnboardingWidgets(Principal principal, HttpServletRequest request,
93                HttpServletResponse response) {
94               FnUser user = fnUserService.loadUserByUsername(principal.getName());
95               List<OnboardingWidget> onboardingWidgets = null;
96               if (user.getGuest()) {
97                      EcompPortalUtils.setBadPermissions(user, response, "getOnboardingWidgets");
98               } else {
99                      String getType = request.getHeader("X-Widgets-Type");
100                      if (!getType.isEmpty() && ("managed".equals(getType) || "all".equals(getType))) {
101                             onboardingWidgets = widgetService.getOnboardingWidgets(user, "managed".equals(getType));
102                      } else {
103                             logger.debug(EELFLoggerDelegate.debugLogger,
104                                     "WidgetsController.getOnboardingApps - request must contain header 'X-Widgets-Type' with 'all' or 'managed'");
105                             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
106                      }
107               }
108
109               EcompPortalUtils
110                       .logAndSerializeObject(logger, "/portalApi/widgets", "GET result =", response.getStatus());
111               return onboardingWidgets;
112        }
113
114        @PutMapping(value = {"/portalApi/widgets/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
115        @PreAuthorize("hasRole('System_Administrator')")
116        public FieldsValidator putOnboardingWidget(Principal principal, @PathVariable("widgetId") Long widgetId,
117                @RequestBody OnboardingWidget onboardingWidget, HttpServletResponse response) {
118               FnUser user = fnUserService.loadUserByUsername(principal.getName());
119               FieldsValidator fieldsValidator = null;
120
121               assert onboardingWidget != null;
122               onboardingWidget.setId(widgetId);
123               onboardingWidget.normalize();
124               try {
125                      fieldsValidator = widgetService.setOnboardingWidget(user.getId(), onboardingWidget);
126                      response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
127               } catch (IllegalArgumentException e) {
128                      fieldsValidator = new FieldsValidator();
129                      fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
130                      fieldsValidator.addProblematicFieldName(e.getMessage());
131                      return fieldsValidator;
132               }
133
134               EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "GET result =",
135                       response.getStatus());
136
137               return fieldsValidator;
138        }
139
140        @PostMapping(value = {"/portalApi/widgets"}, produces = MediaType.APPLICATION_JSON_VALUE)
141        @PreAuthorize("hasRole('System_Administrator') and hasRole('Account_Administrator')")
142        public FieldsValidator postOnboardingWidget(Principal principal, HttpServletResponse response,
143                @RequestBody OnboardingWidget onboardingWidget) {
144               FnUser user = fnUserService.loadUserByUsername(principal.getName());
145               FieldsValidator fieldsValidator;
146
147               onboardingWidget.setId(null);
148               onboardingWidget.normalize();
149
150               try {
151                      fieldsValidator = widgetService.setOnboardingWidget(user.getId(), onboardingWidget);
152               } catch (IllegalArgumentException e) {
153                      fieldsValidator = new FieldsValidator();
154                      fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
155                      fieldsValidator.addProblematicFieldName(e.getMessage());
156                      return fieldsValidator;
157               }
158               response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
159
160               EcompPortalUtils
161                       .logAndSerializeObject(logger, "/portalApi/widgets", "POST result =", response.getStatus());
162               return fieldsValidator;
163        }
164
165        @DeleteMapping(value = {"/portalApi/widgets/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
166        @PreAuthorize("hasRole('System_Administrator') and hasRole('Account_Administrator')")
167        public FieldsValidator deleteOnboardingWidget(Principal principal, HttpServletResponse response,
168                @PathVariable("widgetId") Long widgetId) {
169               FnUser user = fnUserService.loadUserByUsername(principal.getName());
170               FieldsValidator fieldsValidator;
171
172               fieldsValidator = widgetService.deleteOnboardingWidget(user, widgetId);
173               response.setStatus(fieldsValidator.getHttpStatusCode().intValue());
174
175               EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "DELETE result =",
176                       response.getStatus());
177               return fieldsValidator;
178        }
179
180        @PutMapping(value = {"portalApi/widgetCatalogSelection"}, produces = MediaType.APPLICATION_JSON_VALUE)
181        public FieldsValidator putWidgetCatalogSelection(Principal principal,
182                @RequestBody WidgetCatalogPersonalization persRequest, HttpServletResponse response) throws IOException {
183               FieldsValidator result = new FieldsValidator();
184               FnUser user = fnUserService.loadUserByUsername(principal.getName());
185
186               try {
187                      assert persRequest != null;
188                      persUserWidgetService
189                              .setPersUserAppValue(user, persRequest);
190               } catch (IllegalArgumentException iae) {
191                      logger.error(EELFLoggerDelegate.errorLogger, "Failed in putAppCatalogSelection", iae);
192                      response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, iae.getMessage());
193               } catch (Exception e) {
194                      logger.error(EELFLoggerDelegate.errorLogger, "Failed in putAppCatalogSelection", e);
195                      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
196               }
197               result.setHttpStatusCode((long) HttpServletResponse.SC_OK);
198               return result;
199        }
200 }