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.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;
37 * Provides database access for the contact-us page controllers.
40 @org.springframework.context.annotation.Configuration
41 public class AppContactUsServiceImpl implements AppContactUsService {
43 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppContactUsServiceImpl.class);
46 private DataAccessService dataAccessService;
48 public DataAccessService getDataAccessService() {
49 return dataAccessService;
52 public void setDataAccessService(DataAccessService dataAccessService) {
53 this.dataAccessService = dataAccessService;
57 @SuppressWarnings("unchecked")
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;
67 @SuppressWarnings("unchecked")
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;
77 * Assists in sorting app-contact-us items by application name.
79 class AppContactUsItemCompare implements Comparator<AppContactUsItem> {
81 public int compare(AppContactUsItem o1, AppContactUsItem o2) {
82 return o1.getAppName().compareTo(o2.getAppName());
87 * Gets a table of category and function details for apps that participate
88 * in the functional menu.
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());
101 * Saves the list of contact-us objects to the database.
104 @Transactional(rollbackFor = Exception.class)
105 public String saveAppContactUs(List<AppContactUsItem> contactUsModelList) throws Exception {
107 for (AppContactUsItem contactUs : contactUsModelList) {
108 String status = saveAppContactUs(contactUs);
109 if (!status.equals("success"))
110 throw new Exception("saveAppContaatUsFailed: service returned " + status);
114 } catch (Exception e) {
115 logger.error(EELFLoggerDelegate.errorLogger, "", e);
116 throw new Exception(e);
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.
126 @Transactional(rollbackFor = Exception.class)
127 public String saveAppContactUs(AppContactUsItem contactUsModel) throws Exception {
129 HashMap<String, Object> map = new HashMap<String, Object>();
130 AppContactUs contactUs = null;
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();
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);
152 } catch (Exception e) {
153 logger.error(EELFLoggerDelegate.errorLogger, "saveAppContactUs failed", e);
160 * Deletes the row from the app contact us table with the specified ID.
163 public String deleteContactUs(Long id) throws Exception {
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);
171 } catch (Exception e) {
173 logger.info(EELFLoggerDelegate.errorLogger, "", e);