[PORTAL-7] Rebase
[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.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.context.annotation.EnableAspectJAutoProxy;
30 import org.springframework.web.bind.annotation.RequestBody;
31 import org.springframework.web.bind.annotation.RequestMapping;
32 import org.springframework.web.bind.annotation.RequestMethod;
33 import org.springframework.web.bind.annotation.RestController;
34
35 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
36
37 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
38 import org.openecomp.portalapp.portal.domain.EPApp;
39 import org.openecomp.portalapp.portal.domain.EPUser;
40 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
41 import org.openecomp.portalapp.portal.service.AdminRolesService;
42 import org.openecomp.portalapp.portal.service.EPAppService;
43 import org.openecomp.portalapp.portal.service.PersUserAppService;
44 import org.openecomp.portalapp.portal.transport.AppCatalogPersonalization;
45 import org.openecomp.portalapp.portal.transport.FieldsValidator;
46 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
47 import org.openecomp.portalapp.portal.ecomp.model.AppCatalogItem;
48 import org.openecomp.portalapp.util.EPUserUtils;
49
50 @RestController
51 @org.springframework.context.annotation.Configuration
52 @EnableAspectJAutoProxy
53 @EPAuditLog
54 public class AppCatalogController extends EPRestrictedBaseController {
55         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppCatalogController.class);
56
57         @Autowired
58         AdminRolesService adminRolesService;
59         @Autowired
60         EPAppService appService;
61         @Autowired
62         PersUserAppService persUserAppService;
63
64         /**
65          * RESTful service method to fetch all enabled applications, with details
66          * about which are accessible to the current user, selected by the current
67          * user.
68          * 
69          * @return List of items suitable for display
70          */
71         @RequestMapping(value = { "/portalApi/appCatalog" }, method = RequestMethod.GET, produces = "application/json")
72         public List<AppCatalogItem> getAppCatalog(HttpServletRequest request, HttpServletResponse response)
73                         throws IOException {
74                 EPUser user = EPUserUtils.getUserSession(request);
75                 List<AppCatalogItem> appCatalog = null;
76                 try {
77                         if (user == null) {
78                                 EcompPortalUtils.setBadPermissions(user, response, "getAppCatalog");
79                         } else {
80                                 if (adminRolesService.isSuperAdmin(user))
81                                         appCatalog = appService.getAdminAppCatalog(user);
82                                 else
83                                         appCatalog = appService.getUserAppCatalog(user);
84                                 EcompPortalUtils.logAndSerializeObject(logger,"/portalApi/getAppCatalog", "GET result =", appCatalog);
85                         }
86                 } catch (Exception e) {
87                         logger.error(EELFLoggerDelegate.errorLogger, "Failed in getAppCatalog", e);
88                         response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
89                 }
90                 return appCatalog;
91         }
92
93         /**
94          * RESTful service to accept a user's action made on the application
95          * catalog.
96          * 
97          * @param request
98          * @param selectRequest
99          *            JSON with data including application ID
100          * @param response
101          * @return FieldsValidator
102          * @throws IOException
103          */
104         @RequestMapping(value = { "/portalApi/appCatalog" }, method = RequestMethod.PUT, produces = "application/json")
105         public FieldsValidator putAppCatalogSelection(HttpServletRequest request,
106                         @RequestBody AppCatalogPersonalization persRequest, HttpServletResponse response) throws IOException {
107                 FieldsValidator result = new FieldsValidator();
108                 EPApp app = appService.getApp(persRequest.getAppId());
109                 EPUser user = EPUserUtils.getUserSession(request);
110                 try {
111                         if (app == null || user == null) {
112                                 EcompPortalUtils.setBadPermissions(user, response, "putAppCatalogSelection");
113                         } else {
114                                 persUserAppService.setPersUserAppValue(user, app, persRequest.getSelect(), persRequest.getPending());
115                         }
116                 } catch (Exception e) {
117                         logger.error(EELFLoggerDelegate.errorLogger, "Failed in putAppCatalogSelection", e);
118                         response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
119                 }
120                 result.httpStatusCode = new Long(HttpServletResponse.SC_OK);
121                 return result;
122         }
123
124 }