Sonar fix in ecomp-portal-BE-common
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / AppContactUsController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  *
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import java.util.Comparator;
41 import java.util.HashMap;
42 import java.util.List;
43
44 import javax.servlet.http.HttpServletRequest;
45
46 import org.json.JSONObject;
47 import org.onap.portalapp.controller.EPRestrictedBaseController;
48 import org.onap.portalapp.portal.ecomp.model.AppCategoryFunctionsItem;
49 import org.onap.portalapp.portal.ecomp.model.AppContactUsItem;
50 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
51 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
52 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
53 import org.onap.portalapp.portal.service.AppContactUsService;
54 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
55 import org.onap.portalapp.validation.DataValidator;
56 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
57 import org.onap.portalsdk.core.util.SystemProperties;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.context.annotation.Configuration;
60 import org.springframework.context.annotation.EnableAspectJAutoProxy;
61 import org.springframework.web.bind.annotation.PathVariable;
62 import org.springframework.web.bind.annotation.RequestBody;
63 import org.springframework.web.bind.annotation.RequestMapping;
64 import org.springframework.web.bind.annotation.RequestMethod;
65 import org.springframework.web.bind.annotation.RestController;
66
67 @RestController
68 @RequestMapping("/portalApi/contactus")
69 @Configuration
70 @EnableAspectJAutoProxy
71 @EPAuditLog
72 public class AppContactUsController extends EPRestrictedBaseController {
73
74         private static final String FAILURE = "failure";
75         private static final String SUCCESS= "success";
76
77         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppContactUsController.class);
78         private static final DataValidator dataValidator = new DataValidator();
79         private final Comparator<AppCategoryFunctionsItem> appCategoryFunctionsItemComparator = Comparator
80                 .comparing(AppCategoryFunctionsItem::getCategory);
81
82         private AppContactUsService contactUsService;
83
84         @Autowired
85         public AppContactUsController(AppContactUsService contactUsService) {
86                 this.contactUsService = contactUsService;
87         }
88
89
90         /**
91          * Answers a JSON object with three items from the system.properties file:
92          * user self-help ticket URL, email for feedback, and Portal info link.
93          *
94          * @param request HttpServletRequest
95          * @return PortalRestResponse
96          */
97         @RequestMapping(value = "/feedback", method = RequestMethod.GET, produces = "application/json")
98         public PortalRestResponse<String> getPortalDetails(HttpServletRequest request) {
99                 PortalRestResponse<String> portalRestResponse;
100                 try {
101                         final String ticketUrl = SystemProperties.getProperty(EPCommonSystemProperties.USH_TICKET_URL);
102                         final String portalInfoUrl = SystemProperties.getProperty(EPCommonSystemProperties.PORTAL_INFO_URL);
103                         final String feedbackEmail = SystemProperties.getProperty(EPCommonSystemProperties.FEEDBACK_EMAIL_ADDRESS);
104                         HashMap<String, String> map = new HashMap<>();
105                         map.put(EPCommonSystemProperties.USH_TICKET_URL, ticketUrl);
106                         map.put(EPCommonSystemProperties.PORTAL_INFO_URL, portalInfoUrl);
107                         map.put(EPCommonSystemProperties.FEEDBACK_EMAIL_ADDRESS, feedbackEmail);
108                         JSONObject j = new JSONObject(map);
109                         String contactUsPortalResponse = j.toString();
110                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS,
111                                 contactUsPortalResponse);
112                 } catch (Exception e) {
113             logger.error(EELFLoggerDelegate.errorLogger, "getPortalDetails failed", e);
114                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
115                 }
116                 return portalRestResponse;
117         }
118
119         /**
120          * Answers the contents of the contact-us table, extended with the
121          * application name.
122          *
123          * @param request HttpServletRequest
124          * @return PortalRestResponse<List<AppContactUsItem>>
125          */
126         @RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
127         public PortalRestResponse<List<AppContactUsItem>> getAppContactUsList(HttpServletRequest request) {
128                 PortalRestResponse<List<AppContactUsItem>> portalRestResponse;
129                 try {
130                         List<AppContactUsItem> contents = contactUsService.getAppContactUs();
131                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS,
132                                 contents);
133                 } catch (Exception e) {
134                         logger.error(EELFLoggerDelegate.errorLogger, "getAppContactUsList failed", e);
135                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
136                                 e.getMessage(), null);
137                 }
138                 return portalRestResponse;
139         }
140
141         /**
142          * Answers a list of objects, one per application, extended with available
143          * data on how to contact that app's organization (possibly none).
144          *
145          * @param request HttpServletRequest
146          * @return PortalRestResponse<List<AppContactUsItem>>
147          */
148         @RequestMapping(value = "/allapps", method = RequestMethod.GET, produces = "application/json")
149         public PortalRestResponse<List<AppContactUsItem>> getAppsAndContacts(HttpServletRequest request) {
150                 PortalRestResponse<List<AppContactUsItem>> portalRestResponse;
151                 try {
152                         List<AppContactUsItem> contents = contactUsService.getAppsAndContacts();
153                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS,
154                                 contents);
155                 } catch (Exception e) {
156                         logger.error(EELFLoggerDelegate.errorLogger, "getAllAppsAndContacts failed", e);
157                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
158                                 e.getMessage(), null);
159                 }
160                 return portalRestResponse;
161         }
162
163         /**
164          * Answers a list of objects with category-application-function details. Not
165          * all applications participate in the functional menu.
166          *
167          * @param request HttpServletRequest
168          * @return PortalRestResponse<List<AppCategoryFunctionsItem>>
169          */
170         @RequestMapping(value = "/functions", method = RequestMethod.GET, produces = "application/json")
171         public PortalRestResponse<List<AppCategoryFunctionsItem>> getAppCategoryFunctions(HttpServletRequest request) {
172                 PortalRestResponse<List<AppCategoryFunctionsItem>> portalRestResponse;
173                 try {
174                         List<AppCategoryFunctionsItem> contents = contactUsService.getAppCategoryFunctions();
175                         contents.sort(appCategoryFunctionsItemComparator);
176                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.OK,
177                                 SUCCESS, contents);
178                 } catch (Exception e) {
179                         logger.error(EELFLoggerDelegate.errorLogger, "getAppCategoryFunctions failed", e);
180                         // TODO build JSON error
181                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
182                                 e.getMessage(), null);
183                 }
184                 return portalRestResponse;
185         }
186
187         /**
188          * Accepts a new application's contact us details.
189          *
190          * @param contactUs AppContactUsItem
191          * @return PortalRestResponse<String>
192          */
193         @RequestMapping(value = "/save", method = RequestMethod.POST, produces = "application/json")
194         public PortalRestResponse<String> save(@RequestBody AppContactUsItem contactUs) {
195
196                 if (contactUs == null || contactUs.getAppName() == null) {
197                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
198                                 "AppName cannot be null or empty");
199                 }else if(!dataValidator.isValid(contactUs)){
200                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, "AppName is not valid.");
201                 }
202
203                 String saveAppContactUs = FAILURE;
204                 try {
205                         saveAppContactUs = contactUsService.saveAppContactUs(contactUs);
206                 } catch (Exception e) {
207                         logger.error(EELFLoggerDelegate.errorLogger, "save failed", e);
208                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, saveAppContactUs, e.getMessage());
209                 }
210                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, saveAppContactUs, "");
211         }
212
213         @RequestMapping(value = "/saveAll", method = RequestMethod.POST, produces = "application/json")
214         public PortalRestResponse<String> save(@RequestBody List<AppContactUsItem> contactUsList) {
215
216                 if (contactUsList == null) {
217                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
218                                 "AppNameList cannot be null or empty");
219                 }else if(!dataValidator.isValid(contactUsList)){
220                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, "AppNameList is not valid.");
221                 }
222
223                 String saveAppContactUs = FAILURE;
224                 try {
225                         saveAppContactUs = contactUsService.saveAppContactUs(contactUsList);
226                 } catch (Exception e) {
227                         logger.error(EELFLoggerDelegate.errorLogger, "save failed", e);
228                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, saveAppContactUs, e.getMessage());
229                 }
230                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, saveAppContactUs, "");
231         }
232
233         /**
234          * Deletes the specified application's contact-us details entry from the
235          * table.
236          *
237          * @param id app ID
238          * @return PortalRestResponse<String>
239          */
240         @RequestMapping(value = "/delete/{appid}", method = RequestMethod.POST, produces = "application/json")
241         public PortalRestResponse<String> delete(@PathVariable("appid") Long id) {
242
243                 String saveAppContactUs = FAILURE;
244                 try {
245                         saveAppContactUs = contactUsService.deleteContactUs(id);
246                 } catch (Exception e) {
247                         logger.error(EELFLoggerDelegate.errorLogger, "delete failed", e);
248                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, saveAppContactUs, e.getMessage());
249                 }
250                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, saveAppContactUs, "");
251         }
252
253 }