2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.portal.service;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashMap;
25 import java.util.List;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.transaction.annotation.Transactional;
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;
38 * Provides database access for the contact-us page controllers.
41 @org.springframework.context.annotation.Configuration
42 public class AppContactUsServiceImpl implements AppContactUsService {
44 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppContactUsServiceImpl.class);
47 private DataAccessService dataAccessService;
49 public DataAccessService getDataAccessService() {
50 return dataAccessService;
53 public void setDataAccessService(DataAccessService dataAccessService) {
54 this.dataAccessService = dataAccessService;
60 * @see org.openecomp.portalapp.portal.service.AppContactUsService#
63 @SuppressWarnings("unchecked")
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;
75 * @see org.openecomp.portalapp.portal.service.AppContactUsService#
76 * getAllAppsAndContacts()
78 @SuppressWarnings("unchecked")
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;
88 * Assists in sorting app-contact-us items by application name.
90 class AppContactUsItemCompare implements Comparator<AppContactUsItem> {
92 public int compare(AppContactUsItem o1, AppContactUsItem o2) {
93 return o1.getAppName().compareTo(o2.getAppName());
98 * Gets a table of category and function details for apps that participate
99 * in the functional menu.
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());
112 * Saves the list of contact-us objects to the database.
115 @Transactional(rollbackFor = Exception.class)
116 public String saveAppContactUs(List<AppContactUsItem> contactUsModelList) throws Exception {
118 for (AppContactUsItem contactUs : contactUsModelList) {
119 String status = saveAppContactUs(contactUs);
120 if (!status.equals("success"))
121 throw new Exception("saveAppContaatUsFailed: service returned " + status);
125 } catch (Exception e) {
126 logger.error(EELFLoggerDelegate.errorLogger, "", e);
127 throw new Exception(e);
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.
137 @Transactional(rollbackFor = Exception.class)
138 public String saveAppContactUs(AppContactUsItem contactUsModel) throws Exception {
140 HashMap<String, Object> map = new HashMap<String, Object>();
141 AppContactUs contactUs = null;
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();
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);
163 } catch (Exception e) {
164 logger.error(EELFLoggerDelegate.errorLogger, "saveAppContactUs failed", e);
171 * Deletes the row from the app contact us table with the specified ID.
174 public String deleteContactUs(Long id) throws Exception {
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);
182 } catch (Exception e) {
184 logger.info(EELFLoggerDelegate.errorLogger, "", e);