2  * ================================================================================
\r 
   4  * ================================================================================
\r 
   5  * Copyright (C) 2017 AT&T Intellectual Property
\r 
   6  * ================================================================================
\r 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
\r 
   8  * you may not use this file except in compliance with the License.
\r 
   9  * You may obtain a copy of the License at
\r 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
\r 
  13  * Unless required by applicable law or agreed to in writing, software
\r 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
\r 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  16  * See the License for the specific language governing permissions and
\r 
  17  * limitations under the License.
\r 
  18  * ================================================================================
\r 
  20 package org.openecomp.portalapp.portal.service;
\r 
  22 import java.util.Collections;
\r 
  23 import java.util.Comparator;
\r 
  24 import java.util.HashMap;
\r 
  25 import java.util.List;
\r 
  27 import org.springframework.beans.factory.annotation.Autowired;
\r 
  28 import org.springframework.transaction.annotation.Transactional;
\r 
  30 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r 
  31 import org.openecomp.portalsdk.core.service.DataAccessService;
\r 
  32 import org.openecomp.portalapp.portal.domain.AppContactUs;
\r 
  33 import org.openecomp.portalapp.portal.domain.EPApp;
\r 
  34 import org.openecomp.portalapp.portal.ecomp.model.AppCategoryFunctionsItem;
\r 
  35 import org.openecomp.portalapp.portal.ecomp.model.AppContactUsItem;
\r 
  38  * Provides database access for the contact-us page controllers.
\r 
  41 @org.springframework.context.annotation.Configuration
\r 
  42 public class AppContactUsServiceImpl implements AppContactUsService {
\r 
  44         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppContactUsServiceImpl.class);
\r 
  47         private DataAccessService dataAccessService;
\r 
  49         public DataAccessService getDataAccessService() {
\r 
  50                 return dataAccessService;
\r 
  53         public void setDataAccessService(DataAccessService dataAccessService) {
\r 
  54                 this.dataAccessService = dataAccessService;
\r 
  60          * @see org.openecomp.portalapp.portal.service.AppContactUsService#
\r 
  63         @SuppressWarnings("unchecked")
\r 
  65         public List<AppContactUsItem> getAppContactUs() throws Exception {
\r 
  66                 List<AppContactUsItem> contactUsItemList = (List<AppContactUsItem>) getDataAccessService()
\r 
  67                                 .executeNamedQuery("getAppContactUsItems", null, null);
\r 
  68                 Collections.sort(contactUsItemList, new AppContactUsItemCompare());
\r 
  69                 return contactUsItemList;
\r 
  75          * @see org.openecomp.portalapp.portal.service.AppContactUsService#
\r 
  76          * getAllAppsAndContacts()
\r 
  78         @SuppressWarnings("unchecked")
\r 
  80         public List<AppContactUsItem> getAppsAndContacts() throws Exception {
\r 
  81                 List<AppContactUsItem> contactUsItemList = (List<AppContactUsItem>) getDataAccessService()
\r 
  82                                 .executeNamedQuery("getAppsAndContacts", null, null);
\r 
  83                 Collections.sort(contactUsItemList, new AppContactUsItemCompare());
\r 
  84                 return contactUsItemList;
\r 
  88          * Assists in sorting app-contact-us items by application name.
\r 
  90         class AppContactUsItemCompare implements Comparator<AppContactUsItem> {
\r 
  92                 public int compare(AppContactUsItem o1, AppContactUsItem o2) {
\r 
  93                         return o1.getAppName().compareTo(o2.getAppName());
\r 
  98          * Gets a table of category and function details for apps that participate
\r 
  99          * in the functional menu.
\r 
 102         public List<AppCategoryFunctionsItem> getAppCategoryFunctions() throws Exception {
\r 
 103                 @SuppressWarnings("unchecked")
\r 
 104                 // This named query requires no parameters.
\r 
 105                 List<AppCategoryFunctionsItem> list = (List<AppCategoryFunctionsItem>) dataAccessService
\r 
 106                                 .executeNamedQuery("getAppCategoryFunctions", null, null);
\r 
 107                 logger.debug(EELFLoggerDelegate.debugLogger, "getAppCategoryFunctions: result list size is " + list.size());
\r 
 112          * Saves the list of contact-us objects to the database.
\r 
 115         @Transactional(rollbackFor = Exception.class)
\r 
 116         public String saveAppContactUs(List<AppContactUsItem> contactUsModelList) throws Exception {
\r 
 118                         for (AppContactUsItem contactUs : contactUsModelList) {
\r 
 119                                 String status = saveAppContactUs(contactUs);
\r 
 120                                 if (!status.equals("success"))
\r 
 121                                         throw new Exception("saveAppContaatUsFailed: service returned " + status);
\r 
 125                 } catch (Exception e) {
\r 
 126                         logger.error(EELFLoggerDelegate.errorLogger, "", e);
\r 
 127                         throw new Exception(e);
\r 
 133          * Saves a single contact-us object to the database, either creating a new
\r 
 134          * row or updating if the argument has the ID of an existing row.
\r 
 137         @Transactional(rollbackFor = Exception.class)
\r 
 138         public String saveAppContactUs(AppContactUsItem contactUsModel) throws Exception {
\r 
 140                         HashMap<String, Object> map = new HashMap<String, Object>();
\r 
 141                         AppContactUs contactUs = null;
\r 
 143                                 contactUs = (AppContactUs) getDataAccessService().getDomainObject(AppContactUs.class,
\r 
 144                                                 contactUsModel.getAppId(), map);
\r 
 145                         } catch (Exception e) {
\r 
 146                                 logger.debug(EELFLoggerDelegate.debugLogger, "saveAppContactUs: not found for App {}, adding new entry",
\r 
 147                                                 contactUsModel.getAppName());
\r 
 148                                 contactUs = new AppContactUs();
\r 
 151                         // Populate the AppContactUs model for the database.
\r 
 152                         EPApp app = (EPApp) getDataAccessService().getDomainObject(EPApp.class, contactUsModel.getAppId(), map);
\r 
 153                         if (app == null || app.getId() == null)
\r 
 154                                 throw new Exception("saveAppContactus: App not found for Id " + contactUsModel.getAppId());
\r 
 155                         contactUs.setApp(app);
\r 
 156                         contactUs.setDescription(contactUsModel.getDescription());
\r 
 157                         contactUs.setContactName(contactUsModel.getContactName());
\r 
 158                         contactUs.setContactEmail(contactUsModel.getContactEmail());
\r 
 159                         contactUs.setActiveYN(contactUsModel.getActiveYN());
\r 
 160                         contactUs.setUrl(contactUsModel.getUrl());
\r 
 161                         getDataAccessService().saveDomainObject(contactUs, map);
\r 
 163                 } catch (Exception e) {
\r 
 164                         logger.error(EELFLoggerDelegate.errorLogger, "saveAppContactUs failed", e);
\r 
 166                         // return "failure";
\r 
 171          * Deletes the row from the app contact us table with the specified ID.
\r 
 174         public String deleteContactUs(Long id) throws Exception {
\r 
 176                         HashMap<String, Object> map = new HashMap<String, Object>();
\r 
 177                         AppContactUs contactUs = (AppContactUs) getDataAccessService().getDomainObject(AppContactUs.class, id, map);
\r 
 178                         if (contactUs.getApp() == null)
\r 
 179                                 throw new Exception("Delete unsuccessful for Id " + id);
\r 
 180                         getDataAccessService().deleteDomainObject(contactUs, map);
\r 
 182                 } catch (Exception e) {
\r 
 184                         logger.info(EELFLoggerDelegate.errorLogger, "", e);
\r