/*- * ============LICENSE_START========================================== * ONAP Portal * =================================================================== * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * =================================================================== * * Unless otherwise specified, all software contained herein is licensed * under the Apache License, Version 2.0 (the "License"); * you may not use this software except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Unless otherwise specified, all documentation contained herein is licensed * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); * you may not use this documentation except in compliance with the License. * You may obtain a copy of the License at * * https://creativecommons.org/licenses/by/4.0/ * * Unless required by applicable law or agreed to in writing, documentation * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ============LICENSE_END============================================ * * */ package org.onap.portalapp.portal.service; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.onap.portalapp.portal.domain.AppContactUs; import org.onap.portalapp.portal.domain.EPApp; import org.onap.portalapp.portal.ecomp.model.AppCategoryFunctionsItem; import org.onap.portalapp.portal.ecomp.model.AppContactUsItem; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.service.DataAccessService; /** * Provides database access for the contact-us page controllers. */ @Transactional @org.springframework.context.annotation.Configuration public class AppContactUsServiceImpl implements AppContactUsService { private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppContactUsServiceImpl.class); @Autowired private DataAccessService dataAccessService; public DataAccessService getDataAccessService() { return dataAccessService; } public void setDataAccessService(DataAccessService dataAccessService) { this.dataAccessService = dataAccessService; } /* * (non-Javadoc) * * @see org.onap.portalapp.portal.service.AppContactUsService# * getAppContactUs() */ @SuppressWarnings("unchecked") @Override public List getAppContactUs() throws Exception { List contactUsItemList = (List) getDataAccessService() .executeNamedQuery("getAppContactUsItems", null, null); Collections.sort(contactUsItemList, new AppContactUsItemCompare()); return contactUsItemList; } /* * (non-Javadoc) * * @see org.onap.portalapp.portal.service.AppContactUsService# * getAllAppsAndContacts() */ @SuppressWarnings("unchecked") @Override public List getAppsAndContacts() throws Exception { List contactUsItemList = (List) getDataAccessService() .executeNamedQuery("getAppsAndContacts", null, null); Collections.sort(contactUsItemList, new AppContactUsItemCompare()); return contactUsItemList; } /** * Assists in sorting app-contact-us items by application name. */ class AppContactUsItemCompare implements Comparator { @Override public int compare(AppContactUsItem o1, AppContactUsItem o2) { return o1.getAppName().compareTo(o2.getAppName()); } } /** * Gets a table of category and function details for apps that participate * in the functional menu. */ @Override public List getAppCategoryFunctions() throws Exception { @SuppressWarnings("unchecked") // This named query requires no parameters. List list = (List) dataAccessService .executeNamedQuery("getAppCategoryFunctions", null, null); logger.debug(EELFLoggerDelegate.debugLogger, "getAppCategoryFunctions: result list size is " + list.size()); return list; } /** * Saves the list of contact-us objects to the database. */ @Override @Transactional(rollbackFor = Exception.class) public String saveAppContactUs(List contactUsModelList) throws Exception { try { for (AppContactUsItem contactUs : contactUsModelList) { String status = saveAppContactUs(contactUs); if (!status.equals("success")) throw new Exception("saveAppContaatUsFailed: service returned " + status); } return "success"; } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "", e); throw new Exception(e); } } /** * Saves a single contact-us object to the database, either creating a new * row or updating if the argument has the ID of an existing row. */ @Override @Transactional(rollbackFor = Exception.class) public String saveAppContactUs(AppContactUsItem contactUsModel) throws Exception { try { HashMap map = new HashMap(); AppContactUs contactUs = null; try { contactUs = (AppContactUs) getDataAccessService().getDomainObject(AppContactUs.class, contactUsModel.getAppId(), map); } catch (Exception e) { logger.debug(EELFLoggerDelegate.debugLogger, "saveAppContactUs: not found for App {}, adding new entry", contactUsModel.getAppName()); contactUs = new AppContactUs(); } // Populate the AppContactUs model for the database. EPApp app = (EPApp) getDataAccessService().getDomainObject(EPApp.class, contactUsModel.getAppId(), map); if (app == null || app.getId() == null) throw new Exception("saveAppContactus: App not found for Id " + contactUsModel.getAppId()); contactUs.setApp(app); contactUs.setDescription(contactUsModel.getDescription()); contactUs.setContactName(contactUsModel.getContactName()); contactUs.setContactEmail(contactUsModel.getContactEmail()); contactUs.setActiveYN(contactUsModel.getActiveYN()); contactUs.setUrl(contactUsModel.getUrl()); getDataAccessService().saveDomainObject(contactUs, map); return "success"; } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "saveAppContactUs failed", e); throw e; // return "failure"; } } /** * Deletes the row from the app contact us table with the specified ID. */ @Override public String deleteContactUs(Long id) throws Exception { try { HashMap map = new HashMap(); AppContactUs contactUs = (AppContactUs) getDataAccessService().getDomainObject(AppContactUs.class, id, map); if (contactUs.getApp() == null) throw new Exception("Delete unsuccessful for Id " + id); getDataAccessService().deleteDomainObject(contactUs, map); return "success"; } catch (Exception e) { logger.info(EELFLoggerDelegate.errorLogger, "", e); throw e; } } }