cd76ae8de00666c96ce339f4c30645ce6d6d61b5
[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 public class JpaDroolsSessionConnector implements DroolsSessionConnector {
30
31         private static Logger logger = LoggerFactory.getLogger(JpaDroolsSessionConnector.class);
32
33         private final EntityManagerFactory emf;
34
35         public JpaDroolsSessionConnector(EntityManagerFactory emf) {
36                 this.emf = emf;
37         }
38
39         @Override
40         public DroolsSession get(String sessName) {
41
42                 EntityManager em = emf.createEntityManager();
43                 DroolsSessionEntity s = null;
44
45                 try (EntityMgrTrans trans = new EntityMgrTrans(em)) {
46
47                         s = em.find(DroolsSessionEntity.class, sessName);
48                         if (s != null) {
49                                 em.refresh(s);
50                         }
51
52                         trans.commit();
53                 }
54
55                 return s;
56         }
57
58         @Override
59         public void replace(DroolsSession sess) {
60                 String sessName = sess.getSessionName();
61
62                 logger.info("replace: Entering and manually updating session name= {}", sessName);
63
64                 EntityManager em = emf.createEntityManager();
65
66                 try (EntityMgrTrans trans = new EntityMgrTrans(em)) {
67
68                         if (!update(em, sess)) {
69                                 add(em, sess);
70                         }
71
72                         trans.commit();
73                 }
74
75                 logger.info("replace: Exiting");
76         }
77
78         /**
79          * Adds a session to the persistent store.
80          * 
81          * @param em
82          *            entity manager
83          * @param sess
84          *            session to be added
85          */
86         private void add(EntityManager em, DroolsSession sess) {
87                 logger.info("add: Inserting session id={}", sess.getSessionId());
88
89                 DroolsSessionEntity ent = new DroolsSessionEntity(sess.getSessionName(), sess.getSessionId());
90
91                 em.persist(ent);
92         }
93
94         /**
95          * Updates a session, if it exists within the persistent store.
96          * 
97          * @param em
98          *            entity manager
99          * @param sess
100          *            session data to be persisted
101          * @return {@code true} if a record was updated, {@code false} if it was not
102          *         found
103          */
104         private boolean update(EntityManager em, DroolsSession sess) {
105
106                 DroolsSessionEntity s = 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 }