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