UI initial implementation.
[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");
5  * you may not use this file except in compliance with the License.
6  * You may obtain 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 implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.akraino.validation.ui.daoimpl;
17
18 import java.util.List;
19
20 import javax.persistence.EntityManager;
21 import javax.persistence.criteria.CriteriaBuilder;
22 import javax.persistence.criteria.CriteriaQuery;
23 import javax.persistence.criteria.Root;
24
25 import org.akraino.validation.ui.dao.BlueprintInstanceDAO;
26 import org.akraino.validation.ui.entity.BlueprintInstance;
27 import org.apache.log4j.Logger;
28 import org.hibernate.Session;
29 import org.hibernate.SessionFactory;
30 import org.hibernate.query.Query;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Repository;
33
34 @Repository
35 public class BlueprintInstanceDAOImpl implements BlueprintInstanceDAO {
36
37     private static final Logger LOGGER = Logger.getLogger(BlueprintInstanceDAOImpl.class);
38
39     @Autowired
40     private SessionFactory sessionFactory;
41
42     protected Session getSession() {
43         return sessionFactory.getCurrentSession();
44     }
45
46     @Override
47     public List<BlueprintInstance> getBlueprintInstances() {
48
49         CriteriaBuilder builder = getSession().getCriteriaBuilder();
50         CriteriaQuery<BlueprintInstance> criteria = builder.createQuery(BlueprintInstance.class);
51
52         Root<BlueprintInstance> root = criteria.from(BlueprintInstance.class);
53         criteria.select(root);
54
55         Query<BlueprintInstance> query = getSession().createQuery(criteria);
56
57         return query.getResultList();
58
59     }
60
61     @Override
62     public BlueprintInstance getBlueprintInstance(Integer instId) {
63
64         EntityManager entityManager = getSession().getEntityManagerFactory().createEntityManager();
65
66         return entityManager.find(BlueprintInstance.class, instId);
67     }
68
69     @Override
70     public void saveOrUpdate(BlueprintInstance blueprintInstance) {
71         getSession().saveOrUpdate(blueprintInstance);
72
73     }
74
75     @Override
76     public void merge(BlueprintInstance blueprintInstance) {
77         getSession().merge(blueprintInstance);
78
79     }
80
81     @Override
82     public void deleteBlueprintInstance(BlueprintInstance blueprintInstance) {
83         getSession().delete(blueprintInstance);
84
85     }
86
87     @Override
88     public void deleteAll() {
89
90         Query<?> query = getSession().createQuery("delete from BlueprintInstance");
91
92         int result = query.executeUpdate();
93
94         if (result > 0) {
95             LOGGER.info("All blueprint instance entries are cleaned up");
96         }
97     }
98
99 }