[UI] Common class for results
[validation.git] / ui / src / main / java / org / akraino / validation / ui / daoimpl / BlueprintInstanceDAOImpl.java
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License. You may obtain
6  * a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 package org.akraino.validation.ui.daoimpl;
17
18 import java.util.List;
19
20 import javax.annotation.Nonnull;
21
22 import org.akraino.validation.ui.dao.BlueprintInstanceDAO;
23 import org.akraino.validation.ui.entity.Blueprint;
24 import org.akraino.validation.ui.entity.BlueprintInstance;
25 import org.hibernate.Criteria;
26 import org.hibernate.Session;
27 import org.hibernate.SessionFactory;
28 import org.hibernate.criterion.Restrictions;
29 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Repository;
32
33 @Repository
34 public class BlueprintInstanceDAOImpl implements BlueprintInstanceDAO {
35
36     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(BlueprintInstanceDAOImpl.class);
37
38     @Autowired
39     private SessionFactory sessionFactory;
40
41     protected Session getSession() {
42         return sessionFactory.getCurrentSession();
43     }
44
45     @Override
46     public List<BlueprintInstance> getBlueprintInstances() {
47         Criteria criteria = getSession().createCriteria(BlueprintInstance.class);
48         criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
49         return criteria.list();
50     }
51
52     @Override
53     public BlueprintInstance getBlueprintInstance(@Nonnull Integer instId) {
54         Criteria criteria = getSession().createCriteria(BlueprintInstance.class);
55         criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
56         criteria.add(Restrictions.eq("id", instId));
57         return criteria.list() == null || criteria.list().size() < 1 ? null
58                 : (BlueprintInstance) criteria.list().get(0);
59     }
60
61     @Override
62     public BlueprintInstance getBlueprintInstance(Blueprint blueprint, String version) {
63         Criteria criteria = getSession().createCriteria(BlueprintInstance.class);
64         criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
65         if (blueprint != null) {
66             criteria.add(Restrictions.eq("blueprint", blueprint));
67         }
68         if (version != null) {
69             criteria.add(Restrictions.eq("version", version));
70         }
71         return criteria.list() == null || criteria.list().size() < 1 ? null
72                 : (BlueprintInstance) criteria.list().get(0);
73     }
74
75     @Override
76     public void saveOrUpdate(@Nonnull BlueprintInstance blueprintInst) {
77         getSession().saveOrUpdate(blueprintInst);
78         getSession().flush();
79     }
80
81     @Override
82     public void merge(@Nonnull BlueprintInstance blueprintInst) {
83         getSession().merge(blueprintInst);
84         getSession().flush();
85     }
86
87     @Override
88     public void deleteBlueprintInstance(@Nonnull BlueprintInstance blueprintInst) {
89         getSession().delete(blueprintInst);
90         getSession().flush();
91     }
92
93     @Override
94     public void deleteAll() {
95         if (getSession().createQuery("delete from BlueprintInstance").executeUpdate() > 0) {
96             LOGGER.info(EELFLoggerDelegate.applicationLogger, "All blueprint instances entries are cleaned up");
97             getSession().flush();
98         }
99     }
100
101 }