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============================================
 
  36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  38 package org.onap.portalapp.portal.service;
 
  40 import java.text.SimpleDateFormat;
 
  41 import java.util.ArrayList;
 
  42 import java.util.Date;
 
  43 import java.util.List;
 
  45 import org.hibernate.criterion.Criterion;
 
  46 import org.hibernate.criterion.Restrictions;
 
  47 import org.springframework.beans.factory.annotation.Autowired;
 
  48 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
  49 import org.springframework.stereotype.Service;
 
  50 import org.springframework.transaction.annotation.Transactional;
 
  51 import org.onap.portalapp.portal.domain.SharedContext;
 
  52 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
 
  53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
 
  54 import org.onap.portalsdk.core.service.DataAccessService;
 
  57  * Implementation of the shared-context service that talks to the database.
 
  59 @Service("sharedContextService")
 
  61 @org.springframework.context.annotation.Configuration
 
  62 @EnableAspectJAutoProxy
 
  64 public class SharedContextServiceImpl implements SharedContextService {
 
  67         private DataAccessService dataAccessService;
 
  69         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SharedContextServiceImpl.class);
 
  74          * @see org.onap.portalsdk.core.service.SharedContextService#
 
  78         @SuppressWarnings("unchecked")
 
  79         public List<SharedContext> getSharedContexts(String contextId) {
 
  80                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
 
  81                 Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
 
  82                 restrictionsList.add(contextIdCrit);
 
  83                 List<SharedContext> contexts = (List<SharedContext>) getDataAccessService().getList(SharedContext.class, null,
 
  84                                 restrictionsList, null);
 
  92          * @see org.onap.portalsdk.core.service.SharedContextService#
 
  93          * getSharedContext(java. lang.String, java.lang.String)
 
  96         public SharedContext getSharedContext(String contextId, String key) {
 
  97                 SharedContext context = null;
 
  98                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
 
  99                 Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
 
 100                 Criterion keyCrit = Restrictions.eq("ckey", key);
 
 101                 restrictionsList.add(contextIdCrit);
 
 102                 restrictionsList.add(keyCrit);
 
 103                 @SuppressWarnings("unchecked")
 
 104                 List<SharedContext> contexts = (List<SharedContext>) getDataAccessService().getList(SharedContext.class, null,
 
 105                                 restrictionsList, null);
 
 106                 if (contexts != null && contexts.size() == 1)
 
 107                         context = contexts.get(0);
 
 115          * @see org.onap.portalapp.portal.service.SharedContextService#
 
 116          * addSharedContext(java.lang.String, java.lang.String, java.lang.String)
 
 119         public void addSharedContext(String contextId, String key, String value) {
 
 120                 SharedContext context = new SharedContext(contextId, key, value);
 
 121                 saveSharedContext(context);
 
 127          * @see org.onap.portalsdk.core.service.SharedContextService#
 
 128          * saveSharedContext(org.onap.portalapp.portal.domain.SharedContext)
 
 131         public void saveSharedContext(SharedContext context) {
 
 132                 getDataAccessService().saveDomainObject(context, null);
 
 138          * @see org.onap.portalsdk.core.service.SharedContextService#
 
 139          * deleteSharedContext(org.onap.portalapp.portal.domain.SharedContext)
 
 142         public void deleteSharedContext(SharedContext context) {
 
 143                 getDataAccessService().deleteDomainObject(context, null);
 
 149          * @see org.onap.portalapp.portal.service.SharedContextService#
 
 150          * deleteSharedContexts(java.lang.String)
 
 153         public int deleteSharedContexts(String contextId) {
 
 154                 // Uses an inefficient method to avoid a where clause
 
 155                 // that could be used to mount a SQL injection attack.
 
 156                 List<SharedContext> contexts = getSharedContexts(contextId);
 
 157                 if (contexts == null)
 
 160                 logger.debug(EELFLoggerDelegate.debugLogger, "deleteSharedContexts: count is " + contexts.size());
 
 161                 for (SharedContext sc : contexts)
 
 162                         deleteSharedContext(sc);
 
 164                 return contexts.size();
 
 170          * @see org.onap.portalapp.portal.service.SharedContextService#
 
 171          * expireSharedContexts(int)
 
 174         public void expireSharedContexts(int ageInSeconds) {
 
 175                 // Specific to the MySQL database.
 
 176                 // final String whereClause = " where create_time < ADDDATE(NOW(),
 
 177                 // INTERVAL - " + ageInSeconds + " SECOND)";
 
 178                 final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
 179                 Date expiredDateTime = new Date(System.currentTimeMillis() - ageInSeconds * 1000);
 
 180                 logger.debug(EELFLoggerDelegate.debugLogger,
 
 181                                 "expireSharedContexts: expire time is " + expiredDateTime.toString());
 
 182                 final String whereClause = " create_time < '" + dateFormat.format(expiredDateTime) + "'";
 
 183                 getDataAccessService().deleteDomainObjects(SharedContext.class, whereClause, null);
 
 186         public DataAccessService getDataAccessService() {
 
 187                 return dataAccessService;
 
 190         public void setDataAccessService(DataAccessService dataAccessService) {
 
 191                 this.dataAccessService = dataAccessService;