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