2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 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.openecomp.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;
52 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.openecomp.portalsdk.core.service.DataAccessService;
54 import org.openecomp.portalapp.portal.domain.SharedContext;
55 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
58 * Implementation of the shared-context service that talks to the database.
60 @Service("sharedContextService")
62 @org.springframework.context.annotation.Configuration
63 @EnableAspectJAutoProxy
65 public class SharedContextServiceImpl implements SharedContextService {
68 private DataAccessService dataAccessService;
70 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SharedContextServiceImpl.class);
75 * @see org.openecomp.portalsdk.core.service.SharedContextService#
79 @SuppressWarnings("unchecked")
80 public List<SharedContext> getSharedContexts(String contextId) {
81 List<Criterion> restrictionsList = new ArrayList<Criterion>();
82 Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
83 restrictionsList.add(contextIdCrit);
84 List<SharedContext> contexts = (List<SharedContext>) getDataAccessService().getList(SharedContext.class, null,
85 restrictionsList, null);
93 * @see org.openecomp.portalsdk.core.service.SharedContextService#
94 * getSharedContext(java. lang.String, java.lang.String)
97 public SharedContext getSharedContext(String contextId, String key) {
98 SharedContext context = null;
99 List<Criterion> restrictionsList = new ArrayList<Criterion>();
100 Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
101 Criterion keyCrit = Restrictions.eq("ckey", key);
102 restrictionsList.add(contextIdCrit);
103 restrictionsList.add(keyCrit);
104 @SuppressWarnings("unchecked")
105 List<SharedContext> contexts = (List<SharedContext>) getDataAccessService().getList(SharedContext.class, null,
106 restrictionsList, null);
107 if (contexts != null && contexts.size() == 1)
108 context = contexts.get(0);
116 * @see org.openecomp.portalapp.portal.service.SharedContextService#
117 * addSharedContext(java.lang.String, java.lang.String, java.lang.String)
120 public void addSharedContext(String contextId, String key, String value) {
121 SharedContext context = new SharedContext(contextId, key, value);
122 saveSharedContext(context);
128 * @see org.openecomp.portalsdk.core.service.SharedContextService#
129 * saveSharedContext(org.openecomp.portalapp.portal.domain.SharedContext)
132 public void saveSharedContext(SharedContext context) {
133 getDataAccessService().saveDomainObject(context, null);
139 * @see org.openecomp.portalsdk.core.service.SharedContextService#
140 * deleteSharedContext(org.openecomp.portalapp.portal.domain.SharedContext)
143 public void deleteSharedContext(SharedContext context) {
144 getDataAccessService().deleteDomainObject(context, null);
150 * @see org.openecomp.portalapp.portal.service.SharedContextService#
151 * deleteSharedContexts(java.lang.String)
154 public int deleteSharedContexts(String contextId) {
155 // Uses an inefficient method to avoid a where clause
156 // that could be used to mount a SQL injection attack.
157 List<SharedContext> contexts = getSharedContexts(contextId);
158 if (contexts == null)
161 logger.debug(EELFLoggerDelegate.debugLogger, "deleteSharedContexts: count is " + contexts.size());
162 for (SharedContext sc : contexts)
163 deleteSharedContext(sc);
165 return contexts.size();
171 * @see org.openecomp.portalapp.portal.service.SharedContextService#
172 * expireSharedContexts(int)
175 public void expireSharedContexts(int ageInSeconds) {
176 // Specific to the MySQL database.
177 // final String whereClause = " where create_time < ADDDATE(NOW(),
178 // INTERVAL - " + ageInSeconds + " SECOND)";
179 final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
180 Date expiredDateTime = new Date(System.currentTimeMillis() - ageInSeconds * 1000);
181 logger.debug(EELFLoggerDelegate.debugLogger,
182 "expireSharedContexts: expire time is " + expiredDateTime.toString());
183 final String whereClause = " create_time < '" + dateFormat.format(expiredDateTime) + "'";
184 getDataAccessService().deleteDomainObjects(SharedContext.class, whereClause, null);
187 public DataAccessService getDataAccessService() {
188 return dataAccessService;
191 public void setDataAccessService(DataAccessService dataAccessService) {
192 this.dataAccessService = dataAccessService;