76c096813452b82fb8df2d84dfe72a9daa32eb72
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * feature-session-persistence
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.persistence;
22
23 import javax.persistence.EntityManager;
24 import javax.persistence.EntityManagerFactory;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30 public class JpaDroolsSessionConnector implements DroolsSessionConnector {
31
32         private static Logger logger = LoggerFactory.getLogger(JpaDroolsSessionConnector.class);
33                 
34         private final EntityManagerFactory emf;
35         
36         
37         public JpaDroolsSessionConnector(EntityManagerFactory emf) {
38                 this.emf = emf;
39         }
40
41         @Override
42         public DroolsSession get(String sessName) {
43
44                 EntityManager em = emf.createEntityManager();
45                 DroolsSessionEntity s = null;
46                 
47                 try(EntityMgrTrans trans = new EntityMgrTrans(em)) {
48                         
49                         s = em.find(DroolsSessionEntity.class, sessName);
50                         if(s != null) {
51                                 em.refresh(s);
52                         }
53
54                         trans.commit();
55                 }
56
57                 return s;
58         }
59
60         @Override
61         public void replace(DroolsSession sess) {
62                 String sessName = sess.getSessionName();
63                 
64                 logger.info("replace: Entering and manually updating session name= {}", sessName);
65                 
66                 EntityManager em = emf.createEntityManager();
67                 
68                 try(EntityMgrTrans trans = new EntityMgrTrans(em)) {
69                         
70                         if( ! update(em, sess)) {
71                                 add(em, sess);
72                         }
73                 
74                         trans.commit();
75                 }
76                 
77                 logger.info("replace: Exiting");
78         }
79
80         /**
81          * Adds a session to the persistent store.
82          * @param em    entity manager
83          * @param sess  session to be added
84          */
85         private void add(EntityManager em, DroolsSession sess) {
86                 logger.info("add: Inserting session id={}", sess.getSessionId());
87
88                 DroolsSessionEntity ent =
89                                 new DroolsSessionEntity(
90                                                 sess.getSessionName(),
91                                                 sess.getSessionId());
92                 
93                 em.persist(ent);
94         }
95         
96         /**
97          * Updates a session, if it exists within the persistent store.
98          * @param em    entity manager
99          * @param sess  session data to be persisted
100          * @return {@code true} if a record was updated, {@code false} if it
101          *                      was not found
102          */
103         private boolean update(EntityManager em, DroolsSession sess) {
104                 
105                 DroolsSessionEntity s =
106                                 em.find(DroolsSessionEntity.class, sess.getSessionName());
107                 if(s == null) {
108                         return false;
109                 }
110
111                 logger.info("update: Updating session id to {}", sess.getSessionId());
112                 s.setSessionId( sess.getSessionId());
113                 
114                 return true;
115         }
116 }