nexus site path corrected
[portal.git] / ecomp-portal-BE / 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.openecomp.portalapp.portal.domain.SharedContext;
30 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
31 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
32 import org.openecomp.portalsdk.core.service.DataAccessService;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.context.annotation.EnableAspectJAutoProxy;
35 import org.springframework.stereotype.Service;
36 import org.springframework.transaction.annotation.Transactional;
37
38 /**
39  * Implementation of the shared-context service that talks to the database.
40  */
41 @Service("sharedContextService")
42 @Transactional
43 @org.springframework.context.annotation.Configuration
44 @EnableAspectJAutoProxy
45 @EPMetricsLog
46 public class SharedContextServiceImpl implements SharedContextService {
47
48         @Autowired
49         private DataAccessService dataAccessService;
50
51         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SharedContextServiceImpl.class);
52         
53         /*
54          * (non-Javadoc)
55          * 
56          * @see org.openecomp.portalsdk.core.service.SharedContextService#getSharedContexts()
57          */
58         @Override
59         @SuppressWarnings("unchecked")
60         public List<SharedContext> getSharedContexts(String contextId) {
61                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
62                 Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
63                 restrictionsList.add(contextIdCrit);
64                 List<SharedContext> contexts = (List<SharedContext>) getDataAccessService().getList(SharedContext.class, null,
65                                 restrictionsList, null);
66                 
67                 return contexts;
68         }
69
70         /*
71          * (non-Javadoc)
72          * 
73          * @see
74          * org.openecomp.portalsdk.core.service.SharedContextService#getSharedContext(java.
75          * lang.String, java.lang.String)
76          */
77         @Override
78         public SharedContext getSharedContext(String contextId, String key) {
79                 SharedContext context = null;
80                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
81                 Criterion contextIdCrit = Restrictions.eq("context_id", contextId);
82                 Criterion keyCrit = Restrictions.eq("ckey", key);
83                 restrictionsList.add(contextIdCrit);
84                 restrictionsList.add(keyCrit);
85                 @SuppressWarnings("unchecked")
86                 List<SharedContext> contexts = (List<SharedContext>) getDataAccessService().getList(SharedContext.class, null,
87                                 restrictionsList, null);
88                 if (contexts != null && contexts.size() == 1)
89                         context = contexts.get(0);
90                 
91                 return context;
92         }
93
94         /*
95          * (non-Javadoc)
96          * 
97          * @see org.openecomp.portalapp.portal.service.SharedContextService#
98          * addSharedContext(java.lang.String, java.lang.String, java.lang.String)
99          */
100         @Override
101         public void addSharedContext(String contextId, String key, String value) {
102                 SharedContext context = new SharedContext(contextId, key, value);
103                 saveSharedContext(context);
104         }
105
106         /*
107          * (non-Javadoc)
108          * 
109          * @see
110          * org.openecomp.portalsdk.core.service.SharedContextService#saveSharedContext
111          */
112         @Override
113         public void saveSharedContext(SharedContext context) {
114                 getDataAccessService().saveDomainObject(context, null);
115         }
116
117         /*
118          * (non-Javadoc)
119          * 
120          * @see
121          * org.openecomp.portalsdk.core.service.SharedContextService#deleteSharedContext(org.
122          * openecomp.portalapp.portal.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                 for (SharedContext sc : contexts)
144                         deleteSharedContext(sc);
145                 
146                 return contexts.size();
147         }
148
149         /*
150          * (non-Javadoc)
151          * 
152          * @see org.openecomp.portalapp.portal.service.SharedContextService#
153          * expireSharedContexts(int)
154          */
155         @Override
156         public void expireSharedContexts(int ageInSeconds) {
157                 // Specific to the MySQL database.
158                 //final String whereClause = " where create_time < ADDDATE(NOW(), INTERVAL - " + ageInSeconds + " SECOND)";
159                 final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
160                 Date expiredDateTime = new Date(System.currentTimeMillis() - ageInSeconds*1000);
161                 final String whereClause = " create_time < '" + dateFormat.format(expiredDateTime) + "'";
162                 getDataAccessService().deleteDomainObjects(SharedContext.class, whereClause, null);
163         }
164
165         public DataAccessService getDataAccessService() {
166                 return dataAccessService;
167         }
168
169         public void setDataAccessService(DataAccessService dataAccessService) {
170                 this.dataAccessService = dataAccessService;
171         }
172
173 }