1b1b6069f420ffe232cf249a4d3bcb512b1037d8
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / SharedContextServiceImpl.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.service;
21
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.List;
26
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;
33
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;
38
39 /**
40  * Implementation of the shared-context service that talks to the database.
41  */
42 @Service("sharedContextService")
43 @Transactional
44 @org.springframework.context.annotation.Configuration
45 @EnableAspectJAutoProxy
46 @EPMetricsLog
47 public class SharedContextServiceImpl implements SharedContextService {
48
49         @Autowired
50         private DataAccessService dataAccessService;
51
52         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SharedContextServiceImpl.class);
53
54         /*
55          * (non-Javadoc)
56          * 
57          * @see org.openecomp.portalsdk.core.service.SharedContextService#
58          * getSharedContexts()
59          */
60         @Override
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);
68
69                 return contexts;
70         }
71
72         /*
73          * (non-Javadoc)
74          * 
75          * @see org.openecomp.portalsdk.core.service.SharedContextService#
76          * getSharedContext(java. lang.String, java.lang.String)
77          */
78         @Override
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);
91
92                 return context;
93         }
94
95         /*
96          * (non-Javadoc)
97          * 
98          * @see org.openecomp.portalapp.portal.service.SharedContextService#
99          * addSharedContext(java.lang.String, java.lang.String, java.lang.String)
100          */
101         @Override
102         public void addSharedContext(String contextId, String key, String value) {
103                 SharedContext context = new SharedContext(contextId, key, value);
104                 saveSharedContext(context);
105         }
106
107         /*
108          * (non-Javadoc)
109          * 
110          * @see org.openecomp.portalsdk.core.service.SharedContextService#
111          * saveSharedContext(com. att.fusion.core.domain.SharedContext)
112          */
113         @Override
114         public void saveSharedContext(SharedContext context) {
115                 getDataAccessService().saveDomainObject(context, null);
116         }
117
118         /*
119          * (non-Javadoc)
120          * 
121          * @see org.openecomp.portalsdk.core.service.SharedContextService#
122          * deleteSharedContext(com. att.fusion.core.domain.SharedContext)
123          */
124         @Override
125         public void deleteSharedContext(SharedContext context) {
126                 getDataAccessService().deleteDomainObject(context, null);
127         }
128
129         /*
130          * (non-Javadoc)
131          * 
132          * @see org.openecomp.portalapp.portal.service.SharedContextService#
133          * deleteSharedContexts(java.lang.String)
134          */
135         @Override
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)
141                         return 0;
142
143                 logger.debug(EELFLoggerDelegate.debugLogger, "deleteSharedContexts: count is " + contexts.size());
144                 for (SharedContext sc : contexts)
145                         deleteSharedContext(sc);
146
147                 return contexts.size();
148         }
149
150         /*
151          * (non-Javadoc)
152          * 
153          * @see org.openecomp.portalapp.portal.service.SharedContextService#
154          * expireSharedContexts(int)
155          */
156         @Override
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);
167         }
168
169         public DataAccessService getDataAccessService() {
170                 return dataAccessService;
171         }
172
173         public void setDataAccessService(DataAccessService dataAccessService) {
174                 this.dataAccessService = dataAccessService;
175         }
176
177 }