[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / AppContactUsServiceImpl.java
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 package org.openecomp.portalapp.portal.service;\r
21 \r
22 import java.util.Collections;\r
23 import java.util.Comparator;\r
24 import java.util.HashMap;\r
25 import java.util.List;\r
26 \r
27 import org.springframework.beans.factory.annotation.Autowired;\r
28 import org.springframework.transaction.annotation.Transactional;\r
29 \r
30 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
31 import org.openecomp.portalsdk.core.service.DataAccessService;\r
32 import org.openecomp.portalapp.portal.domain.AppContactUs;\r
33 import org.openecomp.portalapp.portal.domain.EPApp;\r
34 import org.openecomp.portalapp.portal.ecomp.model.AppCategoryFunctionsItem;\r
35 import org.openecomp.portalapp.portal.ecomp.model.AppContactUsItem;\r
36 \r
37 /**\r
38  * Provides database access for the contact-us page controllers.\r
39  */\r
40 @Transactional\r
41 @org.springframework.context.annotation.Configuration\r
42 public class AppContactUsServiceImpl implements AppContactUsService {\r
43 \r
44         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppContactUsServiceImpl.class);\r
45 \r
46         @Autowired\r
47         private DataAccessService dataAccessService;\r
48 \r
49         public DataAccessService getDataAccessService() {\r
50                 return dataAccessService;\r
51         }\r
52 \r
53         public void setDataAccessService(DataAccessService dataAccessService) {\r
54                 this.dataAccessService = dataAccessService;\r
55         }\r
56 \r
57         /*\r
58          * (non-Javadoc)\r
59          * \r
60          * @see org.openecomp.portalapp.portal.service.AppContactUsService#\r
61          * getAppContactUs()\r
62          */\r
63         @SuppressWarnings("unchecked")\r
64         @Override\r
65         public List<AppContactUsItem> getAppContactUs() throws Exception {\r
66                 List<AppContactUsItem> contactUsItemList = (List<AppContactUsItem>) getDataAccessService()\r
67                                 .executeNamedQuery("getAppContactUsItems", null, null);\r
68                 Collections.sort(contactUsItemList, new AppContactUsItemCompare());\r
69                 return contactUsItemList;\r
70         }\r
71 \r
72         /*\r
73          * (non-Javadoc)\r
74          * \r
75          * @see org.openecomp.portalapp.portal.service.AppContactUsService#\r
76          * getAllAppsAndContacts()\r
77          */\r
78         @SuppressWarnings("unchecked")\r
79         @Override\r
80         public List<AppContactUsItem> getAppsAndContacts() throws Exception {\r
81                 List<AppContactUsItem> contactUsItemList = (List<AppContactUsItem>) getDataAccessService()\r
82                                 .executeNamedQuery("getAppsAndContacts", null, null);\r
83                 Collections.sort(contactUsItemList, new AppContactUsItemCompare());\r
84                 return contactUsItemList;\r
85         }\r
86 \r
87         /**\r
88          * Assists in sorting app-contact-us items by application name.\r
89          */\r
90         class AppContactUsItemCompare implements Comparator<AppContactUsItem> {\r
91                 @Override\r
92                 public int compare(AppContactUsItem o1, AppContactUsItem o2) {\r
93                         return o1.getAppName().compareTo(o2.getAppName());\r
94                 }\r
95         }\r
96 \r
97         /**\r
98          * Gets a table of category and function details for apps that participate\r
99          * in the functional menu.\r
100          */\r
101         @Override\r
102         public List<AppCategoryFunctionsItem> getAppCategoryFunctions() throws Exception {\r
103                 @SuppressWarnings("unchecked")\r
104                 // This named query requires no parameters.\r
105                 List<AppCategoryFunctionsItem> list = (List<AppCategoryFunctionsItem>) dataAccessService\r
106                                 .executeNamedQuery("getAppCategoryFunctions", null, null);\r
107                 logger.debug(EELFLoggerDelegate.debugLogger, "getAppCategoryFunctions: result list size is " + list.size());\r
108                 return list;\r
109         }\r
110 \r
111         /**\r
112          * Saves the list of contact-us objects to the database.\r
113          */\r
114         @Override\r
115         @Transactional(rollbackFor = Exception.class)\r
116         public String saveAppContactUs(List<AppContactUsItem> contactUsModelList) throws Exception {\r
117                 try {\r
118                         for (AppContactUsItem contactUs : contactUsModelList) {\r
119                                 String status = saveAppContactUs(contactUs);\r
120                                 if (!status.equals("success"))\r
121                                         throw new Exception("saveAppContaatUsFailed: service returned " + status);\r
122                         }\r
123                         return "success";\r
124 \r
125                 } catch (Exception e) {\r
126                         logger.error(EELFLoggerDelegate.errorLogger, "", e);\r
127                         throw new Exception(e);\r
128                 }\r
129 \r
130         }\r
131 \r
132         /**\r
133          * Saves a single contact-us object to the database, either creating a new\r
134          * row or updating if the argument has the ID of an existing row.\r
135          */\r
136         @Override\r
137         @Transactional(rollbackFor = Exception.class)\r
138         public String saveAppContactUs(AppContactUsItem contactUsModel) throws Exception {\r
139                 try {\r
140                         HashMap<String, Object> map = new HashMap<String, Object>();\r
141                         AppContactUs contactUs = null;\r
142                         try {\r
143                                 contactUs = (AppContactUs) getDataAccessService().getDomainObject(AppContactUs.class,\r
144                                                 contactUsModel.getAppId(), map);\r
145                         } catch (Exception e) {\r
146                                 logger.debug(EELFLoggerDelegate.debugLogger, "saveAppContactUs: not found for App {}, adding new entry",\r
147                                                 contactUsModel.getAppName());\r
148                                 contactUs = new AppContactUs();\r
149                         }\r
150 \r
151                         // Populate the AppContactUs model for the database.\r
152                         EPApp app = (EPApp) getDataAccessService().getDomainObject(EPApp.class, contactUsModel.getAppId(), map);\r
153                         if (app == null || app.getId() == null)\r
154                                 throw new Exception("saveAppContactus: App not found for Id " + contactUsModel.getAppId());\r
155                         contactUs.setApp(app);\r
156                         contactUs.setDescription(contactUsModel.getDescription());\r
157                         contactUs.setContactName(contactUsModel.getContactName());\r
158                         contactUs.setContactEmail(contactUsModel.getContactEmail());\r
159                         contactUs.setActiveYN(contactUsModel.getActiveYN());\r
160                         contactUs.setUrl(contactUsModel.getUrl());\r
161                         getDataAccessService().saveDomainObject(contactUs, map);\r
162                         return "success";\r
163                 } catch (Exception e) {\r
164                         logger.error(EELFLoggerDelegate.errorLogger, "saveAppContactUs failed", e);\r
165                         throw e;\r
166                         // return "failure";\r
167                 }\r
168         }\r
169 \r
170         /**\r
171          * Deletes the row from the app contact us table with the specified ID.\r
172          */\r
173         @Override\r
174         public String deleteContactUs(Long id) throws Exception {\r
175                 try {\r
176                         HashMap<String, Object> map = new HashMap<String, Object>();\r
177                         AppContactUs contactUs = (AppContactUs) getDataAccessService().getDomainObject(AppContactUs.class, id, map);\r
178                         if (contactUs.getApp() == null)\r
179                                 throw new Exception("Delete unsuccessful for Id " + id);\r
180                         getDataAccessService().deleteDomainObject(contactUs, map);\r
181                         return "success";\r
182                 } catch (Exception e) {\r
183 \r
184                         logger.info(EELFLoggerDelegate.errorLogger, "", e);\r
185                         throw e;\r
186                 }\r
187         }\r
188 \r
189 }\r