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.TimeslotDAO;
26 import org.akraino.validation.ui.entity.Timeslot;
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 TimeslotDAOImpl implements TimeslotDAO {
37 private static final Logger LOGGER = Logger.getLogger(TimeslotDAOImpl.class);
40 private SessionFactory sessionFactory;
42 protected Session getSession() {
43 return sessionFactory.getCurrentSession();
47 public List<Timeslot> getTimeslots() {
49 CriteriaBuilder builder = getSession().getCriteriaBuilder();
50 CriteriaQuery<Timeslot> criteria = builder.createQuery(Timeslot.class);
52 Root<Timeslot> root = criteria.from(Timeslot.class);
53 criteria.select(root);
55 Query<Timeslot> query = getSession().createQuery(criteria);
57 return query.getResultList();
62 public Timeslot getTimeslot(Integer timeslotId) {
64 EntityManager entityManager = getSession().getEntityManagerFactory().createEntityManager();
66 return entityManager.find(Timeslot.class, timeslotId);
70 public void saveOrUpdate(Timeslot timeslot) {
71 getSession().saveOrUpdate(timeslot);
76 public void merge(Timeslot timeslot) {
77 getSession().merge(timeslot);
82 public void deleteTimeslot(Timeslot timeslot) {
83 getSession().delete(timeslot);
88 public void deleteAll() {
90 Query<?> query = getSession().createQuery("delete from Timeslot");
92 int result = query.executeUpdate();
95 LOGGER.info("All timeslot entries are cleaned up");