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