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