49513525a76a3d54f73d869c7c2b7d9f797c194e
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / SharedContextServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.service;
39
40 import java.text.SimpleDateFormat;
41 import java.util.ArrayList;
42 import java.util.Date;
43 import java.util.List;
44
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;
55
56 /**
57  * Implementation of the shared-context service that talks to the database.
58  */
59 @Service("sharedContextService")
60 @Transactional
61 @org.springframework.context.annotation.Configuration
62 @EnableAspectJAutoProxy
63 @EPMetricsLog
64 public class SharedContextServiceImpl implements SharedContextService {
65
66         @Autowired
67         private DataAccessService dataAccessService;
68
69         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SharedContextServiceImpl.class);
70
71         /*
72          * (non-Javadoc)
73          * 
74          * @see org.onap.portalsdk.core.service.SharedContextService#
75          * getSharedContexts()
76          */
77         @Override
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);
85
86                 return contexts;
87         }
88
89         /*
90          * (non-Javadoc)
91          * 
92          * @see org.onap.portalsdk.core.service.SharedContextService#
93          * getSharedContext(java. lang.String, java.lang.String)
94          */
95         @Override
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);
108
109                 return context;
110         }
111
112         /*
113          * (non-Javadoc)
114          * 
115          * @see org.onap.portalapp.portal.service.SharedContextService#
116          * addSharedContext(java.lang.String, java.lang.String, java.lang.String)
117          */
118         @Override
119         public void addSharedContext(String contextId, String key, String value) {
120                 SharedContext context = new SharedContext(contextId, key, value);
121                 saveSharedContext(context);
122         }
123
124         /*
125          * (non-Javadoc)
126          * 
127          * @see org.onap.portalsdk.core.service.SharedContextService#
128          * saveSharedContext(org.onap.portalapp.portal.domain.SharedContext)
129          */
130         @Override
131         public void saveSharedContext(SharedContext context) {
132                 getDataAccessService().saveDomainObject(context, null);
133         }
134
135         /*
136          * (non-Javadoc)
137          * 
138          * @see org.onap.portalsdk.core.service.SharedContextService#
139          * deleteSharedContext(org.onap.portalapp.portal.domain.SharedContext)
140          */
141         @Override
142         public void deleteSharedContext(SharedContext context) {
143                 getDataAccessService().deleteDomainObject(context, null);
144         }
145
146         /*
147          * (non-Javadoc)
148          * 
149          * @see org.onap.portalapp.portal.service.SharedContextService#
150          * deleteSharedContexts(java.lang.String)
151          */
152         @Override
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)
158                         return 0;
159
160                 logger.debug(EELFLoggerDelegate.debugLogger, "deleteSharedContexts: count is " + contexts.size());
161                 for (SharedContext sc : contexts)
162                         deleteSharedContext(sc);
163
164                 return contexts.size();
165         }
166
167         /*
168          * (non-Javadoc)
169          * 
170          * @see org.onap.portalapp.portal.service.SharedContextService#
171          * expireSharedContexts(int)
172          */
173         @Override
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);
184         }
185
186         public DataAccessService getDataAccessService() {
187                 return dataAccessService;
188         }
189
190         public void setDataAccessService(DataAccessService dataAccessService) {
191                 this.dataAccessService = dataAccessService;
192         }
193
194 }