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