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