Update license; improve coverage; add docs dir
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / AppContactUsServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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.openecomp.portalapp.portal.service;
39
40 import java.util.Collections;
41 import java.util.Comparator;
42 import java.util.HashMap;
43 import java.util.List;
44
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.transaction.annotation.Transactional;
47
48 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
49 import org.openecomp.portalsdk.core.service.DataAccessService;
50 import org.openecomp.portalapp.portal.domain.AppContactUs;
51 import org.openecomp.portalapp.portal.domain.EPApp;
52 import org.openecomp.portalapp.portal.ecomp.model.AppCategoryFunctionsItem;
53 import org.openecomp.portalapp.portal.ecomp.model.AppContactUsItem;
54
55 /**
56  * Provides database access for the contact-us page controllers.
57  */
58 @Transactional
59 @org.springframework.context.annotation.Configuration
60 public class AppContactUsServiceImpl implements AppContactUsService {
61
62         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppContactUsServiceImpl.class);
63
64         @Autowired
65         private DataAccessService dataAccessService;
66
67         public DataAccessService getDataAccessService() {
68                 return dataAccessService;
69         }
70
71         public void setDataAccessService(DataAccessService dataAccessService) {
72                 this.dataAccessService = dataAccessService;
73         }
74
75         /*
76          * (non-Javadoc)
77          * 
78          * @see org.openecomp.portalapp.portal.service.AppContactUsService#
79          * getAppContactUs()
80          */
81         @SuppressWarnings("unchecked")
82         @Override
83         public List<AppContactUsItem> getAppContactUs() throws Exception {
84                 List<AppContactUsItem> contactUsItemList = (List<AppContactUsItem>) getDataAccessService()
85                                 .executeNamedQuery("getAppContactUsItems", null, null);
86                 Collections.sort(contactUsItemList, new AppContactUsItemCompare());
87                 return contactUsItemList;
88         }
89
90         /*
91          * (non-Javadoc)
92          * 
93          * @see org.openecomp.portalapp.portal.service.AppContactUsService#
94          * getAllAppsAndContacts()
95          */
96         @SuppressWarnings("unchecked")
97         @Override
98         public List<AppContactUsItem> getAppsAndContacts() throws Exception {
99                 List<AppContactUsItem> contactUsItemList = (List<AppContactUsItem>) getDataAccessService()
100                                 .executeNamedQuery("getAppsAndContacts", null, null);
101                 Collections.sort(contactUsItemList, new AppContactUsItemCompare());
102                 return contactUsItemList;
103         }
104
105         /**
106          * Assists in sorting app-contact-us items by application name.
107          */
108         class AppContactUsItemCompare implements Comparator<AppContactUsItem> {
109                 @Override
110                 public int compare(AppContactUsItem o1, AppContactUsItem o2) {
111                         return o1.getAppName().compareTo(o2.getAppName());
112                 }
113         }
114
115         /**
116          * Gets a table of category and function details for apps that participate
117          * in the functional menu.
118          */
119         @Override
120         public List<AppCategoryFunctionsItem> getAppCategoryFunctions() throws Exception {
121                 @SuppressWarnings("unchecked")
122                 // This named query requires no parameters.
123                 List<AppCategoryFunctionsItem> list = (List<AppCategoryFunctionsItem>) dataAccessService
124                                 .executeNamedQuery("getAppCategoryFunctions", null, null);
125                 logger.debug(EELFLoggerDelegate.debugLogger, "getAppCategoryFunctions: result list size is " + list.size());
126                 return list;
127         }
128
129         /**
130          * Saves the list of contact-us objects to the database.
131          */
132         @Override
133         @Transactional(rollbackFor = Exception.class)
134         public String saveAppContactUs(List<AppContactUsItem> contactUsModelList) throws Exception {
135                 try {
136                         for (AppContactUsItem contactUs : contactUsModelList) {
137                                 String status = saveAppContactUs(contactUs);
138                                 if (!status.equals("success"))
139                                         throw new Exception("saveAppContaatUsFailed: service returned " + status);
140                         }
141                         return "success";
142
143                 } catch (Exception e) {
144                         logger.error(EELFLoggerDelegate.errorLogger, "", e);
145                         throw new Exception(e);
146                 }
147
148         }
149
150         /**
151          * Saves a single contact-us object to the database, either creating a new
152          * row or updating if the argument has the ID of an existing row.
153          */
154         @Override
155         @Transactional(rollbackFor = Exception.class)
156         public String saveAppContactUs(AppContactUsItem contactUsModel) throws Exception {
157                 try {
158                         HashMap<String, Object> map = new HashMap<String, Object>();
159                         AppContactUs contactUs = null;
160                         try {
161                                 contactUs = (AppContactUs) getDataAccessService().getDomainObject(AppContactUs.class,
162                                                 contactUsModel.getAppId(), map);
163                         } catch (Exception e) {
164                                 logger.debug(EELFLoggerDelegate.debugLogger, "saveAppContactUs: not found for App {}, adding new entry",
165                                                 contactUsModel.getAppName());
166                                 contactUs = new AppContactUs();
167                         }
168
169                         // Populate the AppContactUs model for the database.
170                         EPApp app = (EPApp) getDataAccessService().getDomainObject(EPApp.class, contactUsModel.getAppId(), map);
171                         if (app == null || app.getId() == null)
172                                 throw new Exception("saveAppContactus: App not found for Id " + contactUsModel.getAppId());
173                         contactUs.setApp(app);
174                         contactUs.setDescription(contactUsModel.getDescription());
175                         contactUs.setContactName(contactUsModel.getContactName());
176                         contactUs.setContactEmail(contactUsModel.getContactEmail());
177                         contactUs.setActiveYN(contactUsModel.getActiveYN());
178                         contactUs.setUrl(contactUsModel.getUrl());
179                         getDataAccessService().saveDomainObject(contactUs, map);
180                         return "success";
181                 } catch (Exception e) {
182                         logger.error(EELFLoggerDelegate.errorLogger, "saveAppContactUs failed", e);
183                         throw e;
184                         // return "failure";
185                 }
186         }
187
188         /**
189          * Deletes the row from the app contact us table with the specified ID.
190          */
191         @Override
192         public String deleteContactUs(Long id) throws Exception {
193                 try {
194                         HashMap<String, Object> map = new HashMap<String, Object>();
195                         AppContactUs contactUs = (AppContactUs) getDataAccessService().getDomainObject(AppContactUs.class, id, map);
196                         if (contactUs.getApp() == null)
197                                 throw new Exception("Delete unsuccessful for Id " + id);
198                         getDataAccessService().deleteDomainObject(contactUs, map);
199                         return "success";
200                 } catch (Exception e) {
201
202                         logger.info(EELFLoggerDelegate.errorLogger, "", e);
203                         throw e;
204                 }
205         }
206
207 }