Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.dalamar.model; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.TimeUnit; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.criterion.Conjunction; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Projection; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.hibernate.criterion.SimpleExpression; import org.springframework.stereotype.Repository; /** * * @author Dimitar */ public class LateTrainDaoImpl implements LateTrainDao { public ArrayList<Criterion> expressions = new ArrayList<Criterion>(); public ArrayList<Projection> projections = new ArrayList<Projection>(); private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public void save(LateTrain tr) { Session session = this.sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.persist(tr); tx.commit(); session.close(); } @Override public List<LateTrain> list() { Session session = this.sessionFactory.openSession(); List<LateTrain> trainList = session.createQuery("from LateTrain").list(); session.close(); return trainList; } public void update(LateTrain tr) { Session session = this.sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.update(tr); tx.commit(); session.close(); } public LateTrainDao station(String aStation) { SimpleExpression stationExpression = Restrictions.eq("toStation", aStation); expressions.add(stationExpression); return this; } public LateTrainDao betweenDates(Date begin, Date end) { Criterion datesExpression = createDateCriterion(begin, end); expressions.add(datesExpression); return this; } public LateTrainDao withDelay(boolean delay) { Criterion delayExpression = Restrictions.gt("delay", 0); if (delay) expressions.add(delayExpression); return this; } public LateTrainDao sum(String field) { Projection sum = Projections.sum(field); projections.add(sum); return this; } public LateTrainDao sumTotal(String field) { Projection sum = Projections.sum(field); projections.add(sum); expressions.add(Restrictions.gt("inSum", 0)); return this; } public LateTrainDao sumCumulative(String field) { Projection sum = Projections.sum(field); projections.add(sum); //expressions.add(Restrictions.eq("inSum", 0)); return this; } public LateTrainDao count(String field) { Projection cnt = Projections.count(field); projections.add(cnt); return this; } public LateTrainDao max(String field) { Projection max = Projections.max(field); projections.add(max); return this; } public LateTrainDao custom(Criterion criterion) { expressions.add(criterion); return this; } public LateTrainDao custom(Projection projection) { projections.add(projection); return this; } public LateTrainDao groupBy(String field) { Projection max = Projections.groupProperty(field); projections.add(max); return this; } public LateTrainDao clear() { expressions.clear(); projections.clear(); return this; } public List executeQuery() { Session session = this.sessionFactory.openSession(); Criteria c = session.createCriteria(LateTrain.class); for (Criterion cr : expressions) c.add(cr); for (Projection p : projections) c.setProjection(p); List<LateTrain> trainList = c.list(); session.close(); return trainList; } { } // // @Override // public List<LateTrain> listLateOnly() { // Session session = this.sessionFactory.openSession(); // Criteria c = session.createCriteria(LateTrain.class); // c.add(withDelay); // List<LateTrain> trainList = c.list(); // session.close(); // return trainList; // } // // @Override // public List<LateTrain> listForDate(Date d) { // Session session = this.sessionFactory.openSession(); // Criteria c = session.createCriteria(LateTrain.class); // c.add(createDateCriterion(d)); // List<LateTrain> trainList = c.list(); // session.close(); // return trainList; // } private Conjunction createDateCriterion(Date d) { d.setHours(0); d.setMinutes(0); d.setSeconds(0); Date beginDate = d; Date endDate = new Date(beginDate.getTime() + TimeUnit.DAYS.toMillis(1)); Conjunction andOperator = Restrictions.conjunction(); andOperator.add(Restrictions.ge("dateCaptured", beginDate)); andOperator.add(Restrictions.lt("dateCaptured", endDate)); return andOperator; } private Conjunction createDateCriterion(Date begin, Date end) { begin.setHours(0); begin.setMinutes(0); begin.setSeconds(0); end.setHours(0); end.setMinutes(0); end.setSeconds(0); Date beginDate = begin; Date endDate = new Date(end.getTime() + TimeUnit.DAYS.toMillis(1)); Conjunction andOperator = Restrictions.conjunction(); andOperator.add(Restrictions.ge("dateCaptured", beginDate)); andOperator.add(Restrictions.lt("dateCaptured", endDate)); return andOperator; } // @Override // public List<LateTrain> listForDateAndStation(Date d, String station) { // Session session = this.sessionFactory.openSession(); // Criteria c = session.createCriteria(LateTrain.class); // c.add(createDateCriterion(d)); // c.add(Restrictions.eq("toStation", station)); // List<LateTrain> trainList = c.list(); // session.close(); // return trainList; // } // // @Override // public List<LateTrain> listLateBetweenDates(Date begin, Date end) { // Session session = this.sessionFactory.openSession(); // Criteria c = session.createCriteria(LateTrain.class); // c.add(createDateCriterion(begin,end)); // c.add(withDelay); // List<LateTrain> trainList = c.list(); // session.close(); // return trainList; // } // // @Override // public List<LateTrain> listLateBetweenDatesForStation(Date begin, Date end, String station) { // Session session = this.sessionFactory.openSession(); // Criteria c = session.createCriteria(LateTrain.class); // c.add(createDateCriterion(begin,end)); // c.add(withDelay); // c.add(Restrictions.eq("toStation", station)); // List<LateTrain> trainList = c.list(); // session.close(); // return trainList; // } @Override public LateTrainDao saveTemplate() { LateTrainDaoImpl newDao = new LateTrainDaoImpl(); newDao.setSessionFactory(sessionFactory); newDao.projections.addAll(projections); newDao.expressions.addAll(expressions); return newDao; } }