2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.service;
40 import java.util.Collections;
41 import java.util.Comparator;
42 import java.util.HashMap;
43 import java.util.List;
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;
55 * Provides database access for the contact-us page controllers.
58 @org.springframework.context.annotation.Configuration
59 public class AppContactUsServiceImpl implements AppContactUsService {
61 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppContactUsServiceImpl.class);
64 private DataAccessService dataAccessService;
66 public DataAccessService getDataAccessService() {
67 return dataAccessService;
70 public void setDataAccessService(DataAccessService dataAccessService) {
71 this.dataAccessService = dataAccessService;
77 * @see org.onap.portalapp.portal.service.AppContactUsService#
80 @SuppressWarnings("unchecked")
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;
92 * @see org.onap.portalapp.portal.service.AppContactUsService#
93 * getAllAppsAndContacts()
95 @SuppressWarnings("unchecked")
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;
105 * Assists in sorting app-contact-us items by application name.
107 class AppContactUsItemCompare implements Comparator<AppContactUsItem> {
109 public int compare(AppContactUsItem o1, AppContactUsItem o2) {
110 return o1.getAppName().compareTo(o2.getAppName());
115 * Gets a table of category and function details for apps that participate
116 * in the functional menu.
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());
129 * Saves the list of contact-us objects to the database.
132 @Transactional(rollbackFor = Exception.class)
133 public String saveAppContactUs(List<AppContactUsItem> contactUsModelList) throws Exception {
135 for (AppContactUsItem contactUs : contactUsModelList) {
136 String status = saveAppContactUs(contactUs);
137 if (!status.equals("success"))
138 throw new Exception("saveAppContaatUsFailed: service returned " + status);
142 } catch (Exception e) {
143 logger.error(EELFLoggerDelegate.errorLogger, "", e);
144 throw new Exception(e);
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.
154 @Transactional(rollbackFor = Exception.class)
155 public String saveAppContactUs(AppContactUsItem contactUsModel) throws Exception {
157 HashMap<String, Object> map = new HashMap<String, Object>();
158 AppContactUs contactUs = null;
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();
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);
180 } catch (Exception e) {
181 logger.error(EELFLoggerDelegate.errorLogger, "saveAppContactUs failed", e);
188 * Deletes the row from the app contact us table with the specified ID.
191 public String deleteContactUs(Long id) throws Exception {
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);
199 } catch (Exception e) {
201 logger.info(EELFLoggerDelegate.errorLogger, "", e);