[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / AppCatalogController.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.controller;
21
22 import java.io.IOException;
23 import java.util.List;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
29 import org.openecomp.portalapp.portal.domain.EPApp;
30 import org.openecomp.portalapp.portal.domain.EPUser;
31 import org.openecomp.portalapp.portal.ecomp.model.AppCatalogItem;
32 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
33 import org.openecomp.portalapp.portal.service.AdminRolesService;
34 import org.openecomp.portalapp.portal.service.EPAppService;
35 import org.openecomp.portalapp.portal.service.PersUserAppService;
36 import org.openecomp.portalapp.portal.transport.AppCatalogPersonalization;
37 import org.openecomp.portalapp.portal.transport.FieldsValidator;
38 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
39 import org.openecomp.portalapp.util.EPUserUtils;
40 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.context.annotation.EnableAspectJAutoProxy;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestMethod;
46 import org.springframework.web.bind.annotation.RestController;
47
48 @RestController
49 @org.springframework.context.annotation.Configuration
50 @EnableAspectJAutoProxy
51 @EPAuditLog
52 public class AppCatalogController extends EPRestrictedBaseController {
53
54         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppCatalogController.class);
55
56         @Autowired
57         private AdminRolesService adminRolesService;
58         @Autowired
59         private EPAppService appService;
60         @Autowired
61         private PersUserAppService persUserAppService;
62
63         /**
64          * RESTful service method to fetch all enabled applications, with details
65          * about which are accessible to the current user, selected by the current
66          * user.
67          * 
68          * @param request
69          *            HttpServletRequest
70          * @param response
71          *            HttpServletResponse
72          * @throws IOException If sendError fails
73          * @return List of items suitable for display
74          */
75         @RequestMapping(value = { "/portalApi/appCatalog" }, method = RequestMethod.GET, produces = "application/json")
76         public List<AppCatalogItem> getAppCatalog(HttpServletRequest request, HttpServletResponse response)
77                         throws IOException {
78                 EPUser user = EPUserUtils.getUserSession(request);
79                 List<AppCatalogItem> appCatalog = null;
80                 try {
81                         if (user == null) {
82                                 EcompPortalUtils.setBadPermissions(user, response, "getAppCatalog");
83                         } else {
84                                 if (adminRolesService.isSuperAdmin(user))
85                                         appCatalog = appService.getAdminAppCatalog(user);
86                                 else
87                                         appCatalog = appService.getUserAppCatalog(user);
88                                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/getAppCatalog", "GET result =", appCatalog);
89                         }
90                 } catch (Exception e) {
91                         logger.error(EELFLoggerDelegate.errorLogger, "getAppCatalog failed", e);
92                         response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
93                 }
94                 return appCatalog;
95         }
96
97         /**
98          * RESTful service to accept a user's action made on the application
99          * catalog.
100          * 
101          * @param request
102          *            HttpServletRequest
103          * @param persRequest
104          *            JSON with data including application ID
105          * @param response
106          *            HttpServletResponse
107          * @return FieldsValidator
108          * @throws IOException If sendError fails
109          */
110         @RequestMapping(value = { "/portalApi/appCatalog" }, method = RequestMethod.PUT, produces = "application/json")
111         public FieldsValidator putAppCatalogSelection(HttpServletRequest request,
112                         @RequestBody AppCatalogPersonalization persRequest, HttpServletResponse response) throws IOException {
113                 FieldsValidator result = new FieldsValidator();
114                 EPApp app = appService.getApp(persRequest.getAppId());
115                 EPUser user = EPUserUtils.getUserSession(request);
116                 try {
117                         if (app == null || user == null) {
118                                 EcompPortalUtils.setBadPermissions(user, response, "putAppCatalogSelection");
119                         } else {
120                                 persUserAppService.setPersUserAppValue(user, app, persRequest.getSelect(), persRequest.getPending());
121                         }
122                 } catch (Exception e) {
123                         logger.error(EELFLoggerDelegate.errorLogger, "putAppCatalogSelection failed", e);
124                         response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
125                 }
126                 result.httpStatusCode = new Long(HttpServletResponse.SC_OK);
127                 return result;
128         }
129
130 }