lower code smells
[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.RestController;
65 import org.springframework.web.bind.annotation.GetMapping;
66 import org.springframework.web.bind.annotation.PostMapping;
67
68 @RestController
69 @RequestMapping("/portalApi/contactus")
70 @Configuration
71 @EnableAspectJAutoProxy
72 @EPAuditLog
73 public class AppContactUsController extends EPRestrictedBaseController {
74
75         private static final String FAILURE = "failure";
76         private static final String SUCCESS= "success";
77
78         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppContactUsController.class);
79         private static final DataValidator dataValidator = new DataValidator();
80         private final Comparator<AppCategoryFunctionsItem> appCategoryFunctionsItemComparator = Comparator
81                 .comparing(AppCategoryFunctionsItem::getCategory);
82
83         private AppContactUsService contactUsService;
84
85         @Autowired
86         public AppContactUsController(AppContactUsService contactUsService) {
87                 this.contactUsService = contactUsService;
88         }
89
90
91         /**
92          * Answers a JSON object with three items from the system.properties file:
93          * user self-help ticket URL, email for feedback, and Portal info link.
94          *
95          * @param request HttpServletRequest
96          * @return PortalRestResponse
97          */
98         @GetMapping(value = "/feedback", produces = "application/json")
99         public PortalRestResponse<String> getPortalDetails(HttpServletRequest request) {
100                 PortalRestResponse<String> portalRestResponse;
101                 try {
102                         final String ticketUrl = SystemProperties.getProperty(EPCommonSystemProperties.USH_TICKET_URL);
103                         final String portalInfoUrl = SystemProperties.getProperty(EPCommonSystemProperties.PORTAL_INFO_URL);
104                         final String feedbackEmail = SystemProperties.getProperty(EPCommonSystemProperties.FEEDBACK_EMAIL_ADDRESS);
105                         HashMap<String, String> map = new HashMap<>();
106                         map.put(EPCommonSystemProperties.USH_TICKET_URL, ticketUrl);
107                         map.put(EPCommonSystemProperties.PORTAL_INFO_URL, portalInfoUrl);
108                         map.put(EPCommonSystemProperties.FEEDBACK_EMAIL_ADDRESS, feedbackEmail);
109                         JSONObject j = new JSONObject(map);
110                         String contactUsPortalResponse = j.toString();
111                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS,
112                                 contactUsPortalResponse);
113                 } catch (Exception e) {
114             logger.error(EELFLoggerDelegate.errorLogger, "getPortalDetails failed", e);
115                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
116                 }
117                 return portalRestResponse;
118         }
119
120         /**
121          * Answers the contents of the contact-us table, extended with the
122          * application name.
123          *
124          * @param request HttpServletRequest
125          * @return PortalRestResponse<List<AppContactUsItem>>
126          */
127         @GetMapping(value = "/list", produces = "application/json")
128         public PortalRestResponse<List<AppContactUsItem>> getAppContactUsList(HttpServletRequest request) {
129                 PortalRestResponse<List<AppContactUsItem>> portalRestResponse;
130                 try {
131                         List<AppContactUsItem> contents = contactUsService.getAppContactUs();
132                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS,
133                                 contents);
134                 } catch (Exception e) {
135                         logger.error(EELFLoggerDelegate.errorLogger, "getAppContactUsList failed", e);
136                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
137                                 e.getMessage(), null);
138                 }
139                 return portalRestResponse;
140         }
141
142         /**
143          * Answers a list of objects, one per application, extended with available
144          * data on how to contact that app's organization (possibly none).
145          *
146          * @param request HttpServletRequest
147          * @return PortalRestResponse<List<AppContactUsItem>>
148          */
149         @GetMapping(value = "/allapps", produces = "application/json")
150         public PortalRestResponse<List<AppContactUsItem>> getAppsAndContacts(HttpServletRequest request) {
151                 PortalRestResponse<List<AppContactUsItem>> portalRestResponse;
152                 try {
153                         List<AppContactUsItem> contents = contactUsService.getAppsAndContacts();
154                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS,
155                                 contents);
156                 } catch (Exception e) {
157                         logger.error(EELFLoggerDelegate.errorLogger, "getAllAppsAndContacts failed", e);
158                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
159                                 e.getMessage(), null);
160                 }
161                 return portalRestResponse;
162         }
163
164         /**
165          * Answers a list of objects with category-application-function details. Not
166          * all applications participate in the functional menu.
167          *
168          * @param request HttpServletRequest
169          * @return PortalRestResponse<List<AppCategoryFunctionsItem>>
170          */
171         @GetMapping(value = "/functions", produces = "application/json")
172         public PortalRestResponse<List<AppCategoryFunctionsItem>> getAppCategoryFunctions(HttpServletRequest request) {
173                 PortalRestResponse<List<AppCategoryFunctionsItem>> portalRestResponse;
174                 try {
175                         List<AppCategoryFunctionsItem> contents = contactUsService.getAppCategoryFunctions();
176                         contents.sort(appCategoryFunctionsItemComparator);
177                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.OK,
178                                 SUCCESS, contents);
179                 } catch (Exception e) {
180                         logger.error(EELFLoggerDelegate.errorLogger, "getAppCategoryFunctions failed", e);
181                         // TODO build JSON error
182                         portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
183                                 e.getMessage(), null);
184                 }
185                 return portalRestResponse;
186         }
187
188         /**
189          * Accepts a new application's contact us details.
190          *
191          * @param contactUs AppContactUsItem
192          * @return PortalRestResponse<String>
193          */
194         @PostMapping(value = "/save", produces = "application/json")
195         public PortalRestResponse<String> save(@RequestBody AppContactUsItem contactUs) {
196
197                 if (contactUs == null || contactUs.getAppName() == null) {
198                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
199                                 "AppName cannot be null or empty");
200                 }else if(!dataValidator.isValid(contactUs)){
201                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, "AppName is not valid.");
202                 }
203
204                 String saveAppContactUs = FAILURE;
205                 try {
206                         saveAppContactUs = contactUsService.saveAppContactUs(contactUs);
207                 } catch (Exception e) {
208                         logger.error(EELFLoggerDelegate.errorLogger, "save failed", e);
209                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, saveAppContactUs, e.getMessage());
210                 }
211                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, saveAppContactUs, "");
212         }
213
214         @PostMapping(value = "/saveAll", produces = "application/json")
215         public PortalRestResponse<String> save(@RequestBody List<AppContactUsItem> contactUsList) {
216
217                 if (contactUsList == null) {
218                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
219                                 "AppNameList cannot be null or empty");
220                 }else if(!dataValidator.isValid(contactUsList)){
221                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, "AppNameList is not valid.");
222                 }
223
224                 String saveAppContactUs = FAILURE;
225                 try {
226                         saveAppContactUs = contactUsService.saveAppContactUs(contactUsList);
227                 } catch (Exception e) {
228                         logger.error(EELFLoggerDelegate.errorLogger, "save failed", e);
229                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, saveAppContactUs, e.getMessage());
230                 }
231                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, saveAppContactUs, "");
232         }
233
234         /**
235          * Deletes the specified application's contact-us details entry from the
236          * table.
237          *
238          * @param id app ID
239          * @return PortalRestResponse<String>
240          */
241         @PostMapping(value = "/delete/{appid}", produces = "application/json")
242         public PortalRestResponse<String> delete(@PathVariable("appid") Long id) {
243
244                 String saveAppContactUs = FAILURE;
245                 try {
246                         saveAppContactUs = contactUsService.deleteContactUs(id);
247                 } catch (Exception e) {
248                         logger.error(EELFLoggerDelegate.errorLogger, "delete failed", e);
249                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, saveAppContactUs, e.getMessage());
250                 }
251                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, saveAppContactUs, "");
252         }
253
254 }