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