19ad010543f2a381c9a7cfb8236431360360a465
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.persistence.jpa.hibernate;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27
28 import javax.persistence.EntityManager;
29
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
33 import org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
36
37 /**
38  * The Class HibernateApexDao is the Hibernate JPA implementation of the Apex DAO.
39  *
40  * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
41  */
42 public class HibernateApexDao extends DefaultApexDao {
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(HibernateApexDao.class);
44
45     private static final String FROM = "FROM ";
46     private static final String DELETE_FROM = "DELETE FROM ";
47     private static final String WHERE_KEY_NAME = " WHERE key.name='";
48     private static final String AND_KEY_VERSION = "' AND key.version='";
49     private static final String WHERE_KEY_PARENT_KEY_NAME = " WHERE key.parentKeyName='";
50     private static final String AND_KEY_PARENT_KEY_VERSION = "' AND key.parentKeyVersion='";
51     private static final String AND_KEY_LOCAL_NAME = "' AND key.localName='";
52
53     /*
54      * (non-Javadoc)
55      *
56      * @see org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao#delete(java.lang.Class,
57      * org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey)
58      */
59     @Override
60     public <T extends AxConcept> void delete(final Class<T> clazz, final AxArtifactKey key) {
61         if (key == null) {
62             return;
63         }
64         final EntityManager mg = getEntityManager();
65         try {
66             mg.getTransaction().begin();
67             mg.createQuery(DELETE_FROM + clazz.getSimpleName() + WHERE_KEY_NAME + key.getName() + AND_KEY_VERSION
68                             + key.getVersion() + "'").executeUpdate();
69             mg.getTransaction().commit();
70         } finally {
71             mg.close();
72         }
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao#delete(java.lang.Class,
79      * org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey)
80      */
81     @Override
82     public <T extends AxConcept> void delete(final Class<T> clazz, final AxReferenceKey key) {
83         if (key == null) {
84             return;
85         }
86         final EntityManager mg = getEntityManager();
87         try {
88             mg.getTransaction().begin();
89             mg.createQuery(DELETE_FROM + clazz.getSimpleName() + WHERE_KEY_PARENT_KEY_NAME + key.getParentKeyName()
90                             + AND_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion() + AND_KEY_LOCAL_NAME
91                             + key.getLocalName() + "'").executeUpdate();
92             mg.getTransaction().commit();
93         } finally {
94             mg.close();
95         }
96     }
97
98     /*
99      * (non-Javadoc)
100      *
101      * @see org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao#deleteByArtifactKey(java.lang.Class,
102      * java.util.Collection)
103      */
104     @Override
105     public <T extends AxConcept> int deleteByArtifactKey(final Class<T> clazz, final Collection<AxArtifactKey> keys) {
106         if (keys == null || keys.isEmpty()) {
107             return 0;
108         }
109         int deletedCount = 0;
110         final EntityManager mg = getEntityManager();
111         try {
112             mg.getTransaction().begin();
113             for (final AxArtifactKey key : keys) {
114                 deletedCount += mg.createQuery(DELETE_FROM + clazz.getSimpleName() + WHERE_KEY_NAME + key.getName()
115                                 + AND_KEY_VERSION + key.getVersion() + "'").executeUpdate();
116             }
117             mg.getTransaction().commit();
118         } finally {
119             mg.close();
120         }
121         return deletedCount;
122     }
123
124     /*
125      * (non-Javadoc)
126      *
127      * @see org.onap.policy.apex.core.model.dao.ApexDao#deleteByContextUsageKey(java.lang.Class, java.util.Collection)
128      */
129     @Override
130     public <T extends AxConcept> int deleteByReferenceKey(final Class<T> clazz, final Collection<AxReferenceKey> keys) {
131         if (keys == null || keys.isEmpty()) {
132             return 0;
133         }
134         int deletedCount = 0;
135         final EntityManager mg = getEntityManager();
136         try {
137             mg.getTransaction().begin();
138             for (final AxReferenceKey key : keys) {
139                 deletedCount += mg.createQuery(DELETE_FROM + clazz.getSimpleName() + WHERE_KEY_PARENT_KEY_NAME
140                                 + key.getParentKeyName() + AND_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion()
141                                 + AND_KEY_LOCAL_NAME + key.getLocalName() + "'").executeUpdate();
142             }
143             mg.getTransaction().commit();
144         } finally {
145             mg.close();
146         }
147         return deletedCount;
148     }
149
150     /*
151      * (non-Javadoc)
152      *
153      * @see org.onap.policy.apex.core.model.dao.ApexDao#deleteAll(java.lang.Class)
154      */
155     @Override
156     public <T extends AxConcept> void deleteAll(final Class<T> clazz) {
157         final EntityManager mg = getEntityManager();
158         try {
159             mg.getTransaction().begin();
160             mg.createQuery(DELETE_FROM + clazz.getSimpleName()).executeUpdate();
161             mg.getTransaction().commit();
162         } finally {
163             mg.close();
164         }
165     }
166
167     /*
168      * (non-Javadoc)
169      *
170      * @see org.onap.policy.apex.core.model.dao.ApexDao#getAll(java.lang.Class)
171      */
172     @Override
173     public <T extends AxConcept> List<T> getAll(final Class<T> clazz) {
174         if (clazz == null) {
175             return Collections.emptyList();
176         }
177         final EntityManager mg = getEntityManager();
178         try {
179             final List<T> result = mg.createQuery(FROM + clazz.getSimpleName(), clazz).getResultList();
180             final List<T> cloneResult = new ArrayList<>();
181             for (final T t : result) {
182                 try {
183                     final T clonedT = clazz.newInstance();
184                     t.copyTo(clonedT);
185                     cloneResult.add(clonedT);
186                 } catch (final Exception e) {
187                     LOGGER.warn("Could not clone object of class \"" + clazz.getCanonicalName() + "\"", e);
188                     return cloneResult;
189                 }
190             }
191             return cloneResult;
192         } finally {
193             mg.close();
194         }
195     }
196
197     /*
198      * (non-Javadoc)
199      *
200      * @see org.onap.policy.apex.core.model.dao.ApexDao#getAll(java.lang.Class,
201      * org.onap.policy.apex.core.model.concepts.AxArtifactKey)
202      */
203     @Override
204     public <T extends AxConcept> List<T> getAll(final Class<T> clazz, final AxArtifactKey parentKey) {
205         if (clazz == null) {
206             return Collections.emptyList();
207         }
208         final EntityManager mg = getEntityManager();
209         try {
210             return mg.createQuery(FROM + clazz.getSimpleName() + WHERE_KEY_PARENT_KEY_NAME + parentKey.getName()
211                             + AND_KEY_PARENT_KEY_VERSION + parentKey.getVersion() + "'", clazz).getResultList();
212         } finally {
213             mg.close();
214         }
215     }
216
217     /*
218      * (non-Javadoc)
219      *
220      * @see org.onap.policy.apex.core.model.dao.ApexDao#getArtifact(java.lang.Class,
221      * org.onap.policy.apex.core.model.concepts.AxArtifactKey)
222      */
223     @Override
224     public <T extends AxConcept> T getArtifact(final Class<T> clazz, final AxArtifactKey key) {
225         if (clazz == null || key == null) {
226             return null;
227         }
228         final EntityManager mg = getEntityManager();
229         List<T> ret;
230         try {
231             ret = mg.createQuery(FROM + clazz.getSimpleName() + WHERE_KEY_NAME + key.getName() + AND_KEY_VERSION
232                             + key.getVersion() + "'", clazz).getResultList();
233         } finally {
234             mg.close();
235         }
236         if (ret == null || ret.isEmpty()) {
237             return null;
238         }
239         if (ret.size() > 1) {
240             throw new IllegalArgumentException("More than one result was returned for search for " + clazz
241                             + " with key " + key.getId() + ": " + ret);
242         }
243         return ret.get(0);
244     }
245
246     /*
247      * (non-Javadoc)
248      *
249      * @see org.onap.policy.apex.core.model.dao.ApexDao#getArtifact(java.lang.Class,
250      * org.onap.policy.apex.core.model.concepts.AxReferenceKey)
251      */
252     @Override
253     public <T extends AxConcept> T getArtifact(final Class<T> clazz, final AxReferenceKey key) {
254         if (clazz == null || key == null) {
255             return null;
256         }
257         final EntityManager mg = getEntityManager();
258         List<T> ret;
259         try {
260             ret = mg.createQuery(FROM + clazz.getSimpleName() + WHERE_KEY_PARENT_KEY_NAME + key.getParentKeyName()
261                             + AND_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion() + AND_KEY_LOCAL_NAME
262                             + key.getLocalName() + "'", clazz).getResultList();
263         } finally {
264             mg.close();
265         }
266         if (ret == null || ret.isEmpty()) {
267             return null;
268         }
269         if (ret.size() > 1) {
270             throw new IllegalArgumentException("More than one result was returned for search for " + clazz
271                             + " with key " + key.getId() + ": " + ret);
272         }
273         return ret.get(0);
274     }
275 }