Security/ Package Name changes
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import java.util.Collections;
41 import java.util.Comparator;
42 import java.util.HashMap;
43 import java.util.List;
44
45 import javax.servlet.http.HttpServletRequest;
46
47 import org.json.JSONObject;
48 import org.onap.portalapp.controller.EPRestrictedBaseController;
49 import org.onap.portalapp.portal.ecomp.model.AppCategoryFunctionsItem;
50 import org.onap.portalapp.portal.ecomp.model.AppContactUsItem;
51 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
52 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
53 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
54 import org.onap.portalapp.portal.service.AppContactUsService;
55 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
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.EnableAspectJAutoProxy;
60 import org.springframework.web.bind.annotation.PathVariable;
61 import org.springframework.web.bind.annotation.RequestBody;
62 import org.springframework.web.bind.annotation.RequestMapping;
63 import org.springframework.web.bind.annotation.RequestMethod;
64 import org.springframework.web.bind.annotation.RestController;
65
66 @RestController
67 @RequestMapping("/portalApi/contactus")
68 @org.springframework.context.annotation.Configuration
69 @EnableAspectJAutoProxy
70 @EPAuditLog
71 public class AppContactUsController extends EPRestrictedBaseController {
72
73         static final String FAILURE = "failure";
74
75         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppContactUsController.class);
76
77         @Autowired
78         private AppContactUsService contactUsService;
79
80         /**
81          * Answers a JSON object with three items from the system.properties file:
82          * user self-help ticket URL, email for feedback, and Portal info link.
83          * 
84          * @param request HttpServletRequest
85          * @return PortalRestResponse
86          */
87         @RequestMapping(value = "/feedback", method = RequestMethod.GET, produces = "application/json")
88         public PortalRestResponse<String> getPortalDetails(HttpServletRequest request) {
89                 PortalRestResponse<String> portalRestResponse = null;
90                 try {
91                         final String ticketUrl = SystemProperties.getProperty(EPCommonSystemProperties.USH_TICKET_URL);
92                         final String portalInfoUrl = SystemProperties.getProperty(EPCommonSystemProperties.PORTAL_INFO_URL);
93                         final String feedbackEmail = SystemProperties.getProperty(EPCommonSystemProperties.FEEDBACK_EMAIL_ADDRESS);
94                         HashMap<String, String> map = new HashMap<String, String>();
95                         map.put(EPCommonSystemProperties.USH_TICKET_URL, ticketUrl);
96                         map.put(EPCommonSystemProperties.PORTAL_INFO_URL, portalInfoUrl);
97                         map.put(EPCommonSystemProperties.FEEDBACK_EMAIL_ADDRESS, feedbackEmail);
98                         JSONObject j = new JSONObject(map);
99                         String contactUsPortalResponse = j.toString();
100                         portalRestResponse = new PortalRestResponse<String>(PortalRestStatusEnum.OK, "success",
101                                         contactUsPortalResponse);
102                 } catch (Exception e) {
103                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
104                 }
105                 return portalRestResponse;
106         }
107
108         /**
109          * Answers the contents of the contact-us table, extended with the
110          * application name.
111          * 
112          * @param request HttpServletRequest
113          * @return PortalRestResponse<List<AppContactUsItem>>
114          */
115         @RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
116         public PortalRestResponse<List<AppContactUsItem>> getAppContactUsList(HttpServletRequest request) {
117                 PortalRestResponse<List<AppContactUsItem>> portalRestResponse = null;
118                 try {
119                         List<AppContactUsItem> contents = contactUsService.getAppContactUs();
120                         portalRestResponse = new PortalRestResponse<List<AppContactUsItem>>(PortalRestStatusEnum.OK, "success",
121                                         contents);
122                 } catch (Exception e) {
123                         logger.error(EELFLoggerDelegate.errorLogger, "getAppContactUsList failed", e);
124                         portalRestResponse = new PortalRestResponse<List<AppContactUsItem>>(PortalRestStatusEnum.ERROR,
125                                         e.getMessage(), null);
126                 }
127                 return portalRestResponse;
128         }
129
130         /**
131          * Answers a list of objects, one per application, extended with available
132          * data on how to contact that app's organization (possibly none).
133          * 
134          * @param request HttpServletRequest
135          * @return PortalRestResponse<List<AppContactUsItem>>
136          */
137         @RequestMapping(value = "/allapps", method = RequestMethod.GET, produces = "application/json")
138         public PortalRestResponse<List<AppContactUsItem>> getAppsAndContacts(HttpServletRequest request) {
139                 PortalRestResponse<List<AppContactUsItem>> portalRestResponse = null;
140                 try {
141                         List<AppContactUsItem> contents = contactUsService.getAppsAndContacts();
142                         portalRestResponse = new PortalRestResponse<List<AppContactUsItem>>(PortalRestStatusEnum.OK, "success",
143                                         contents);
144                 } catch (Exception e) {
145                         logger.error(EELFLoggerDelegate.errorLogger, "getAllAppsAndContacts failed", e);
146                         portalRestResponse = new PortalRestResponse<List<AppContactUsItem>>(PortalRestStatusEnum.ERROR,
147                                         e.getMessage(), null);
148                 }
149                 return portalRestResponse;
150         }
151
152         /**
153          * Sorts by category name.
154          */
155         private Comparator<AppCategoryFunctionsItem> appCategoryFunctionsItemComparator = new Comparator<AppCategoryFunctionsItem>() {
156                 @Override
157                 public int compare(AppCategoryFunctionsItem o1, AppCategoryFunctionsItem o2) {
158                         return o1.getCategory().compareTo(o2.getCategory());
159                 }
160         };
161         
162         /**
163          * Answers a list of objects with category-application-function details. Not
164          * all applications participate in the functional menu.
165          * 
166          * @param request HttpServletRequest
167          * @return PortalRestResponse<List<AppCategoryFunctionsItem>>
168          */
169         @RequestMapping(value = "/functions", method = RequestMethod.GET, produces = "application/json")
170         public PortalRestResponse<List<AppCategoryFunctionsItem>> getAppCategoryFunctions(HttpServletRequest request) {
171                 PortalRestResponse<List<AppCategoryFunctionsItem>> portalRestResponse = null;
172                 try {
173                         List<AppCategoryFunctionsItem> contents = contactUsService.getAppCategoryFunctions();
174                         // logger.debug(EELFLoggerDelegate.debugLogger,
175                         // "getAppCategoryFunctions: result list size is " +
176                         // contents.size());
177                         Collections.sort(contents, appCategoryFunctionsItemComparator);
178                         portalRestResponse = new PortalRestResponse<List<AppCategoryFunctionsItem>>(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<List<AppCategoryFunctionsItem>>(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         @RequestMapping(value = "/save", method = RequestMethod.POST, produces = "application/json")
196         public PortalRestResponse<String> save(@RequestBody AppContactUsItem contactUs) {
197
198                 if (contactUs == null || contactUs.getAppName() == null)
199                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, FAILURE,
200                                         "AppName cannot be null or empty");
201
202                 String saveAppContactUs = FAILURE;
203                 try {
204                         saveAppContactUs = contactUsService.saveAppContactUs(contactUs);
205                 } catch (Exception e) {
206                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, saveAppContactUs, e.getMessage());
207                 }
208                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, saveAppContactUs, "");
209         }
210
211         @RequestMapping(value = "/saveAll", method = RequestMethod.POST, produces = "application/json")
212         public PortalRestResponse<String> save(@RequestBody List<AppContactUsItem> contactUsList) {
213
214                 String saveAppContactUs = FAILURE;
215                 try {
216                         saveAppContactUs = contactUsService.saveAppContactUs(contactUsList);
217                 } catch (Exception e) {
218                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, saveAppContactUs, e.getMessage());
219                 }
220                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, saveAppContactUs, "");
221         }
222
223         /**
224          * Deletes the specified application's contact-us details entry from the
225          * table.
226          * 
227          * @param id app ID
228          * @return PortalRestResponse<String>
229          */
230         @RequestMapping(value = "/delete/{appid}", method = RequestMethod.POST, produces = "application/json")
231         public PortalRestResponse<String> delete(@PathVariable("appid") Long id) {
232
233                 String saveAppContactUs = FAILURE;
234                 try {
235                         saveAppContactUs = contactUsService.deleteContactUs(id);
236                 } catch (Exception e) {
237                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, saveAppContactUs, e.getMessage());
238                 }
239                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, saveAppContactUs, "");
240         }
241
242 }