2 * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.akraino.validation.ui.daoimpl;
18 import java.util.List;
20 import javax.persistence.EntityManager;
21 import javax.persistence.criteria.CriteriaBuilder;
22 import javax.persistence.criteria.CriteriaQuery;
23 import javax.persistence.criteria.Root;
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;
35 public class BlueprintDAOImpl implements BlueprintDAO {
37 private static final Logger LOGGER = Logger.getLogger(BlueprintDAOImpl.class);
40 private SessionFactory sessionFactory;
42 protected Session getSession() {
43 return sessionFactory.getCurrentSession();
47 public List<Blueprint> getBlueprints() {
49 CriteriaBuilder builder = getSession().getCriteriaBuilder();
50 CriteriaQuery<Blueprint> criteria = builder.createQuery(Blueprint.class);
52 Root<Blueprint> root = criteria.from(Blueprint.class);
53 criteria.select(root);
55 Query<Blueprint> query = getSession().createQuery(criteria);
57 return query.getResultList();
62 public Blueprint getBlueprint(Integer blueprintId) {
64 EntityManager entityManager = getSession().getEntityManagerFactory().createEntityManager();
66 return entityManager.find(Blueprint.class, blueprintId);
70 public void saveOrUpdate(Blueprint blueprint) {
71 getSession().saveOrUpdate(blueprint);
76 public void merge(Blueprint blueprint) {
77 getSession().merge(blueprint);
82 public void deleteBlueprint(Blueprint blueprint) {
83 getSession().delete(blueprint);
88 public void deleteAll() {
90 Query<?> query = getSession().createQuery("delete from Blueprint");
92 int result = query.executeUpdate();
95 LOGGER.info("All blueprint entries are cleaned up");