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.openecomp.portalapp.portal.domain.SharedContext;
30 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
31 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
32 import org.openecomp.portalsdk.core.service.DataAccessService;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.context.annotation.EnableAspectJAutoProxy;
35 import org.springframework.stereotype.Service;
36 import org.springframework.transaction.annotation.Transactional;
39 * Implementation of the shared-context service that talks to the database.
41 @Service("sharedContextService")
43 @org.springframework.context.annotation.Configuration
44 @EnableAspectJAutoProxy
46 public class SharedContextServiceImpl implements SharedContextService {
49 private DataAccessService dataAccessService;
51 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SharedContextServiceImpl.class);
56 * @see org.openecomp.portalsdk.core.service.SharedContextService#getSharedContexts()
59 @SuppressWarnings("unchecked")
60 public List<SharedContext> getSharedContexts(String contextId) {
61 List<Criterion> restrictionsList = new ArrayList<Criterion>();
62 Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
63 restrictionsList.add(contextIdCrit);
64 List<SharedContext> contexts = (List<SharedContext>) getDataAccessService().getList(SharedContext.class, null,
65 restrictionsList, null);
74 * org.openecomp.portalsdk.core.service.SharedContextService#getSharedContext(java.
75 * lang.String, java.lang.String)
78 public SharedContext getSharedContext(String contextId, String key) {
79 SharedContext context = null;
80 List<Criterion> restrictionsList = new ArrayList<Criterion>();
81 Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
82 Criterion keyCrit = Restrictions.eq("ckey", key);
83 restrictionsList.add(contextIdCrit);
84 restrictionsList.add(keyCrit);
85 @SuppressWarnings("unchecked")
86 List<SharedContext> contexts = (List<SharedContext>) getDataAccessService().getList(SharedContext.class, null,
87 restrictionsList, null);
88 if (contexts != null && contexts.size() == 1)
89 context = contexts.get(0);
97 * @see org.openecomp.portalapp.portal.service.SharedContextService#
98 * addSharedContext(java.lang.String, java.lang.String, java.lang.String)
101 public void addSharedContext(String contextId, String key, String value) {
102 SharedContext context = new SharedContext(contextId, key, value);
103 saveSharedContext(context);
110 * org.openecomp.portalsdk.core.service.SharedContextService#saveSharedContext
113 public void saveSharedContext(SharedContext context) {
114 getDataAccessService().saveDomainObject(context, null);
121 * org.openecomp.portalsdk.core.service.SharedContextService#deleteSharedContext(org.
122 * openecomp.portalapp.portal.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 for (SharedContext sc : contexts)
144 deleteSharedContext(sc);
146 return contexts.size();
152 * @see org.openecomp.portalapp.portal.service.SharedContextService#
153 * expireSharedContexts(int)
156 public void expireSharedContexts(int ageInSeconds) {
157 // Specific to the MySQL database.
158 //final String whereClause = " where create_time < ADDDATE(NOW(), INTERVAL - " + ageInSeconds + " SECOND)";
159 final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
160 Date expiredDateTime = new Date(System.currentTimeMillis() - ageInSeconds*1000);
161 final String whereClause = " create_time < '" + dateFormat.format(expiredDateTime) + "'";
162 getDataAccessService().deleteDomainObjects(SharedContext.class, whereClause, null);
165 public DataAccessService getDataAccessService() {
166 return dataAccessService;
169 public void setDataAccessService(DataAccessService dataAccessService) {
170 this.dataAccessService = dataAccessService;