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