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.text.SimpleDateFormat;
 
  23 import java.util.ArrayList;
 
  24 import java.util.Date;
 
  25 import java.util.List;
 
  27 import org.hibernate.criterion.Criterion;
 
  28 import org.hibernate.criterion.Restrictions;
 
  29 import org.springframework.beans.factory.annotation.Autowired;
 
  30 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
  31 import org.springframework.stereotype.Service;
 
  32 import org.springframework.transaction.annotation.Transactional;
 
  34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
 
  35 import org.openecomp.portalsdk.core.service.DataAccessService;
 
  36 import org.openecomp.portalapp.portal.domain.SharedContext;
 
  37 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
 
  40  * Implementation of the shared-context service that talks to the database.
 
  42 @Service("sharedContextService")
 
  44 @org.springframework.context.annotation.Configuration
 
  45 @EnableAspectJAutoProxy
 
  47 public class SharedContextServiceImpl implements SharedContextService {
 
  50         private DataAccessService dataAccessService;
 
  52         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SharedContextServiceImpl.class);
 
  57          * @see org.openecomp.portalsdk.core.service.SharedContextService#
 
  61         @SuppressWarnings("unchecked")
 
  62         public List<SharedContext> getSharedContexts(String contextId) {
 
  63                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
 
  64                 Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
 
  65                 restrictionsList.add(contextIdCrit);
 
  66                 List<SharedContext> contexts = (List<SharedContext>) getDataAccessService().getList(SharedContext.class, null,
 
  67                                 restrictionsList, null);
 
  75          * @see org.openecomp.portalsdk.core.service.SharedContextService#
 
  76          * getSharedContext(java. lang.String, java.lang.String)
 
  79         public SharedContext getSharedContext(String contextId, String key) {
 
  80                 SharedContext context = null;
 
  81                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
 
  82                 Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
 
  83                 Criterion keyCrit = Restrictions.eq("ckey", key);
 
  84                 restrictionsList.add(contextIdCrit);
 
  85                 restrictionsList.add(keyCrit);
 
  86                 @SuppressWarnings("unchecked")
 
  87                 List<SharedContext> contexts = (List<SharedContext>) getDataAccessService().getList(SharedContext.class, null,
 
  88                                 restrictionsList, null);
 
  89                 if (contexts != null && contexts.size() == 1)
 
  90                         context = contexts.get(0);
 
  98          * @see org.openecomp.portalapp.portal.service.SharedContextService#
 
  99          * addSharedContext(java.lang.String, java.lang.String, java.lang.String)
 
 102         public void addSharedContext(String contextId, String key, String value) {
 
 103                 SharedContext context = new SharedContext(contextId, key, value);
 
 104                 saveSharedContext(context);
 
 110          * @see org.openecomp.portalsdk.core.service.SharedContextService#
 
 111          * saveSharedContext(com. att.fusion.core.domain.SharedContext)
 
 114         public void saveSharedContext(SharedContext context) {
 
 115                 getDataAccessService().saveDomainObject(context, null);
 
 121          * @see org.openecomp.portalsdk.core.service.SharedContextService#
 
 122          * deleteSharedContext(com. att.fusion.core.domain.SharedContext)
 
 125         public void deleteSharedContext(SharedContext context) {
 
 126                 getDataAccessService().deleteDomainObject(context, null);
 
 132          * @see org.openecomp.portalapp.portal.service.SharedContextService#
 
 133          * deleteSharedContexts(java.lang.String)
 
 136         public int deleteSharedContexts(String contextId) {
 
 137                 // Uses an inefficient method to avoid a where clause
 
 138                 // that could be used to mount a SQL injection attack.
 
 139                 List<SharedContext> contexts = getSharedContexts(contextId);
 
 140                 if (contexts == null)
 
 143                 logger.debug(EELFLoggerDelegate.debugLogger, "deleteSharedContexts: count is " + contexts.size());
 
 144                 for (SharedContext sc : contexts)
 
 145                         deleteSharedContext(sc);
 
 147                 return contexts.size();
 
 153          * @see org.openecomp.portalapp.portal.service.SharedContextService#
 
 154          * expireSharedContexts(int)
 
 157         public void expireSharedContexts(int ageInSeconds) {
 
 158                 // Specific to the MySQL database.
 
 159                 // final String whereClause = " where create_time < ADDDATE(NOW(),
 
 160                 // INTERVAL - " + ageInSeconds + " SECOND)";
 
 161                 final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
 162                 Date expiredDateTime = new Date(System.currentTimeMillis() - ageInSeconds * 1000);
 
 163                 logger.debug(EELFLoggerDelegate.debugLogger,
 
 164                                 "expireSharedContexts: expire time is " + expiredDateTime.toString());
 
 165                 final String whereClause = " create_time < '" + dateFormat.format(expiredDateTime) + "'";
 
 166                 getDataAccessService().deleteDomainObjects(SharedContext.class, whereClause, null);
 
 169         public DataAccessService getDataAccessService() {
 
 170                 return dataAccessService;
 
 173         public void setDataAccessService(DataAccessService dataAccessService) {
 
 174                 this.dataAccessService = dataAccessService;