SharedContextRestController up
[portal.git] / portal-BE / src / main / java / org / onap / portal / service / sharedContext / FnSharedContextService.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  * 
37  */
38 package org.onap.portal.service.sharedContext;
39
40 import java.text.SimpleDateFormat;
41 import java.time.Instant;
42 import java.time.LocalDateTime;
43 import java.time.ZoneId;
44 import java.util.ArrayList;
45 import java.util.Date;
46 import java.util.List;
47 import java.util.Optional;
48 import org.onap.portal.domain.db.fn.FnSharedContext;
49 import org.onap.portal.logging.aop.EPMetricsLog;
50 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.context.annotation.EnableAspectJAutoProxy;
53 import org.springframework.stereotype.Service;
54 import org.springframework.transaction.annotation.Transactional;
55
56
57 @Service
58 @Transactional
59 @EnableAspectJAutoProxy
60 @EPMetricsLog
61 public class FnSharedContextService {
62
63         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FnSharedContextService.class);
64
65          private final FnSharedContextDao fnSharedContextDao;
66
67          @Autowired
68         public FnSharedContextService(final FnSharedContextDao fnSharedContextDao) {
69                 this.fnSharedContextDao = fnSharedContextDao;
70         }
71
72         public List<FnSharedContext> getSharedContexts(String contextId) {
73                 return fnSharedContextDao.getByContextId(contextId).orElse(new ArrayList<>());
74         }
75
76         public FnSharedContext getFnSharedContext(String contextId, String ckey) {
77                 FnSharedContext context = null;
78                 Optional<List<FnSharedContext>> contexts = fnSharedContextDao.getByContextIdAndCkey(contextId, ckey);
79                 if (contexts.isPresent() && contexts.get().size() == 1) {
80                         context = contexts.get().get(0);
81                 }
82                 return context;
83         }
84
85         public FnSharedContext addFnSharedContext(String contextId, String key, String value) {
86                 return this.save(new FnSharedContext(contextId, key, value));
87         }
88
89
90         public FnSharedContext save(FnSharedContext context) {
91                 return this.fnSharedContextDao.saveAndFlush(context);
92         }
93
94         public List<FnSharedContext> saveAll(List<FnSharedContext> fnSharedContexts){
95                 return fnSharedContextDao.saveAll(fnSharedContexts);
96         }
97
98         public void delete(FnSharedContext context) {
99                 this.fnSharedContextDao.delete(context);
100         }
101
102         public int deleteSharedContexts(String contextId) {
103                 // Uses an inefficient method to avoid a where clause
104                 // that could be used to mount a SQL injection attack.
105                 List<FnSharedContext> contexts = getSharedContexts(contextId);
106                 if (contexts == null)
107                         return 0;
108
109                 logger.debug(EELFLoggerDelegate.debugLogger, "deleteFnSharedContexts: count is " + contexts.size());
110                 for (FnSharedContext sc : contexts)
111                         this.delete(sc);
112
113                 return contexts.size();
114         }
115
116         public void expireFnSharedContexts(int ageInSeconds) {
117                 Date expiredDateTime = new Date(System.currentTimeMillis() - ageInSeconds * 1000);
118                 logger.debug(EELFLoggerDelegate.debugLogger,
119                                 "expireFnSharedContexts: expire time is " + expiredDateTime.toString());
120                 this.fnSharedContextDao.deleteByCreated(convertToLocalDateTimeViaInstant(expiredDateTime));
121         }
122
123         private LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) {
124                 return dateToConvert.toInstant()
125                         .atZone(ZoneId.systemDefault())
126                         .toLocalDateTime();
127         }
128 }