Null check for ClientResponse in PolicyUril.java
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / SharedContextServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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.openecomp.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
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;
56
57 /**
58  * Implementation of the shared-context service that talks to the database.
59  */
60 @Service("sharedContextService")
61 @Transactional
62 @org.springframework.context.annotation.Configuration
63 @EnableAspectJAutoProxy
64 @EPMetricsLog
65 public class SharedContextServiceImpl implements SharedContextService {
66
67         @Autowired
68         private DataAccessService dataAccessService;
69
70         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SharedContextServiceImpl.class);
71
72         /*
73          * (non-Javadoc)
74          * 
75          * @see org.openecomp.portalsdk.core.service.SharedContextService#
76          * getSharedContexts()
77          */
78         @Override
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);
86
87                 return contexts;
88         }
89
90         /*
91          * (non-Javadoc)
92          * 
93          * @see org.openecomp.portalsdk.core.service.SharedContextService#
94          * getSharedContext(java. lang.String, java.lang.String)
95          */
96         @Override
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);
109
110                 return context;
111         }
112
113         /*
114          * (non-Javadoc)
115          * 
116          * @see org.openecomp.portalapp.portal.service.SharedContextService#
117          * addSharedContext(java.lang.String, java.lang.String, java.lang.String)
118          */
119         @Override
120         public void addSharedContext(String contextId, String key, String value) {
121                 SharedContext context = new SharedContext(contextId, key, value);
122                 saveSharedContext(context);
123         }
124
125         /*
126          * (non-Javadoc)
127          * 
128          * @see org.openecomp.portalsdk.core.service.SharedContextService#
129          * saveSharedContext(org.openecomp.portalapp.portal.domain.SharedContext)
130          */
131         @Override
132         public void saveSharedContext(SharedContext context) {
133                 getDataAccessService().saveDomainObject(context, null);
134         }
135
136         /*
137          * (non-Javadoc)
138          * 
139          * @see org.openecomp.portalsdk.core.service.SharedContextService#
140          * deleteSharedContext(org.openecomp.portalapp.portal.domain.SharedContext)
141          */
142         @Override
143         public void deleteSharedContext(SharedContext context) {
144                 getDataAccessService().deleteDomainObject(context, null);
145         }
146
147         /*
148          * (non-Javadoc)
149          * 
150          * @see org.openecomp.portalapp.portal.service.SharedContextService#
151          * deleteSharedContexts(java.lang.String)
152          */
153         @Override
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)
159                         return 0;
160
161                 logger.debug(EELFLoggerDelegate.debugLogger, "deleteSharedContexts: count is " + contexts.size());
162                 for (SharedContext sc : contexts)
163                         deleteSharedContext(sc);
164
165                 return contexts.size();
166         }
167
168         /*
169          * (non-Javadoc)
170          * 
171          * @see org.openecomp.portalapp.portal.service.SharedContextService#
172          * expireSharedContexts(int)
173          */
174         @Override
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);
185         }
186
187         public DataAccessService getDataAccessService() {
188                 return dataAccessService;
189         }
190
191         public void setDataAccessService(DataAccessService dataAccessService) {
192                 this.dataAccessService = dataAccessService;
193         }
194
195 }