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