087535d6b2a5d598f3a0cab56d0488875ebb2895
[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> aClass, 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 + aClass.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> aClass, 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 + aClass.getSimpleName() + WHERE_KEY_PARENT_KEY_NAME + key.getParentKeyName()
90                     + AND_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion() + AND_KEY_LOCAL_NAME + key.getLocalName()
91                     + "'").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> aClass, 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 + aClass.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> aClass,
131             final Collection<AxReferenceKey> keys) {
132         if (keys == null || keys.isEmpty()) {
133             return 0;
134         }
135         int deletedCount = 0;
136         final EntityManager mg = getEntityManager();
137         try {
138             mg.getTransaction().begin();
139             for (final AxReferenceKey key : keys) {
140                 deletedCount += mg.createQuery(DELETE_FROM + aClass.getSimpleName() + WHERE_KEY_PARENT_KEY_NAME
141                         + key.getParentKeyName() + AND_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion()
142                         + AND_KEY_LOCAL_NAME + key.getLocalName() + "'").executeUpdate();
143             }
144             mg.getTransaction().commit();
145         } finally {
146             mg.close();
147         }
148         return deletedCount;
149     }
150
151     /*
152      * (non-Javadoc)
153      *
154      * @see org.onap.policy.apex.core.model.dao.ApexDao#deleteAll(java.lang.Class)
155      */
156     @Override
157     public <T extends AxConcept> void deleteAll(final Class<T> aClass) {
158         final EntityManager mg = getEntityManager();
159         try {
160             mg.getTransaction().begin();
161             mg.createQuery(DELETE_FROM + aClass.getSimpleName()).executeUpdate();
162             mg.getTransaction().commit();
163         } finally {
164             mg.close();
165         }
166     }
167
168     /*
169      * (non-Javadoc)
170      *
171      * @see org.onap.policy.apex.core.model.dao.ApexDao#getAll(java.lang.Class)
172      */
173     @Override
174     public <T extends AxConcept> List<T> getAll(final Class<T> aClass) {
175         if (aClass == null) {
176             return Collections.emptyList();
177         }
178         final EntityManager mg = getEntityManager();
179         try {
180             final List<T> result = mg.createQuery(FROM + aClass.getSimpleName(), aClass).getResultList();
181             final List<T> cloneResult = new ArrayList<>();
182             for (final T t : result) {
183                 try {
184                     final T clonedT = aClass.newInstance();
185                     t.copyTo(clonedT);
186                     cloneResult.add(clonedT);
187                 } catch (final Exception e) {
188                     LOGGER.warn("Could not clone object of class \"" + aClass.getCanonicalName() + "\"", e);
189                     return cloneResult;
190                 }
191             }
192             return cloneResult;
193         } finally {
194             mg.close();
195         }
196     }
197
198     /*
199      * (non-Javadoc)
200      *
201      * @see org.onap.policy.apex.core.model.dao.ApexDao#getAll(java.lang.Class,
202      * org.onap.policy.apex.core.model.concepts.AxArtifactKey)
203      */
204     @Override
205     public <T extends AxConcept> List<T> getAll(final Class<T> aClass, final AxArtifactKey parentKey) {
206         if (aClass == null) {
207             return Collections.emptyList();
208         }
209         final EntityManager mg = getEntityManager();
210         try {
211             return mg.createQuery(FROM + aClass.getSimpleName() + WHERE_KEY_PARENT_KEY_NAME + parentKey.getName()
212                     + AND_KEY_PARENT_KEY_VERSION + parentKey.getVersion() + "'", aClass).getResultList();
213         } finally {
214             mg.close();
215         }
216     }
217
218     /*
219      * (non-Javadoc)
220      *
221      * @see org.onap.policy.apex.core.model.dao.ApexDao#getArtifact(java.lang.Class,
222      * org.onap.policy.apex.core.model.concepts.AxArtifactKey)
223      */
224     @Override
225     public <T extends AxConcept> T getArtifact(final Class<T> aClass, final AxArtifactKey key) {
226         if (aClass == null || key == null) {
227             return null;
228         }
229         final EntityManager mg = getEntityManager();
230         List<T> ret;
231         try {
232             ret = mg.createQuery(FROM + aClass.getSimpleName() + WHERE_KEY_NAME + key.getName() + AND_KEY_VERSION
233                     + key.getVersion() + "'", aClass).getResultList();
234         } finally {
235             mg.close();
236         }
237         if (ret == null || ret.isEmpty()) {
238             return null;
239         }
240         if (ret.size() > 1) {
241             throw new IllegalArgumentException("More than one result was returned for search for " + aClass
242                     + " with key " + key.getId() + ": " + ret);
243         }
244         return ret.get(0);
245     }
246
247     /*
248      * (non-Javadoc)
249      *
250      * @see org.onap.policy.apex.core.model.dao.ApexDao#getArtifact(java.lang.Class,
251      * org.onap.policy.apex.core.model.concepts.AxReferenceKey)
252      */
253     @Override
254     public <T extends AxConcept> T getArtifact(final Class<T> aClass, final AxReferenceKey key) {
255         if (aClass == null || key == null) {
256             return null;
257         }
258         final EntityManager mg = getEntityManager();
259         List<T> ret;
260         try {
261             ret = mg.createQuery(FROM + aClass.getSimpleName() + WHERE_KEY_PARENT_KEY_NAME + key.getParentKeyName()
262                     + AND_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion() + AND_KEY_LOCAL_NAME + key.getLocalName()
263                     + "'", aClass).getResultList();
264         } finally {
265             mg.close();
266         }
267         if (ret == null || ret.isEmpty()) {
268             return null;
269         }
270         if (ret.size() > 1) {
271             throw new IllegalArgumentException("More than one result was returned for search for " + aClass
272                     + " with key " + key.getId() + ": " + ret);
273         }
274         return ret.get(0);
275     }
276 }