UI initial implementation.
[validation.git] / ui / src / main / java / org / akraino / validation / ui / daoimpl / BlueprintDAOImpl.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.BlueprintDAO;
26 import org.akraino.validation.ui.entity.Blueprint;
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 BlueprintDAOImpl implements BlueprintDAO {
36
37     private static final Logger LOGGER = Logger.getLogger(BlueprintDAOImpl.class);
38
39     @Autowired
40     private SessionFactory sessionFactory;
41
42     protected Session getSession() {
43         return sessionFactory.getCurrentSession();
44     }
45
46     @Override
47     public List<Blueprint> getBlueprints() {
48
49         CriteriaBuilder builder = getSession().getCriteriaBuilder();
50         CriteriaQuery<Blueprint> criteria = builder.createQuery(Blueprint.class);
51
52         Root<Blueprint> root = criteria.from(Blueprint.class);
53         criteria.select(root);
54
55         Query<Blueprint> query = getSession().createQuery(criteria);
56
57         return query.getResultList();
58
59     }
60
61     @Override
62     public Blueprint getBlueprint(Integer blueprintId) {
63
64         EntityManager entityManager = getSession().getEntityManagerFactory().createEntityManager();
65
66         return entityManager.find(Blueprint.class, blueprintId);
67     }
68
69     @Override
70     public void saveOrUpdate(Blueprint blueprint) {
71         getSession().saveOrUpdate(blueprint);
72
73     }
74
75     @Override
76     public void merge(Blueprint blueprint) {
77         getSession().merge(blueprint);
78
79     }
80
81     @Override
82     public void deleteBlueprint(Blueprint blueprint) {
83         getSession().delete(blueprint);
84
85     }
86
87     @Override
88     public void deleteAll() {
89
90         Query<?> query = getSession().createQuery("delete from Blueprint");
91
92         int result = query.executeUpdate();
93
94         if (result > 0) {
95             LOGGER.info("All blueprint entries are cleaned up");
96         }
97     }
98
99 }