Implement cancelled Trips in range (1.2.3 b)
This commit is contained in:
parent
b38b24f018
commit
0887b15614
@ -2,12 +2,20 @@ package dst.ass1.jpa.dao.impl;
|
|||||||
|
|
||||||
import dst.ass1.jpa.dao.ITripDAO;
|
import dst.ass1.jpa.dao.ITripDAO;
|
||||||
import dst.ass1.jpa.model.ITrip;
|
import dst.ass1.jpa.model.ITrip;
|
||||||
|
import dst.ass1.jpa.model.TripState;
|
||||||
import dst.ass1.jpa.model.impl.Trip;
|
import dst.ass1.jpa.model.impl.Trip;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.Predicate;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import static dst.ass1.jpa.util.Constants.M_TRIP_CREATED;
|
||||||
|
import static dst.ass1.jpa.util.Constants.M_TRIP_STATE;
|
||||||
|
|
||||||
public class TripDAO extends GenericDAO<ITrip> implements ITripDAO {
|
public class TripDAO extends GenericDAO<ITrip> implements ITripDAO {
|
||||||
public TripDAO(EntityManager entityManager) {
|
public TripDAO(EntityManager entityManager) {
|
||||||
super(entityManager, Trip.class);
|
super(entityManager, Trip.class);
|
||||||
@ -15,6 +23,26 @@ public class TripDAO extends GenericDAO<ITrip> implements ITripDAO {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<ITrip> findCancelledTrips(Date start, Date end) {
|
public Collection<ITrip> findCancelledTrips(Date start, Date end) {
|
||||||
return null;
|
CriteriaBuilder criteriaBuilder = super.entityManager.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<ITrip> criteriaQuery = criteriaBuilder.createQuery(ITrip.class);
|
||||||
|
|
||||||
|
Root<Trip> root = criteriaQuery.from(Trip.class);
|
||||||
|
criteriaQuery.select(root);
|
||||||
|
|
||||||
|
Predicate cancelled = criteriaBuilder.equal(root.get(M_TRIP_STATE), TripState.CANCELLED);
|
||||||
|
Predicate startRange = criteriaBuilder.greaterThan(root.get(M_TRIP_CREATED), start);
|
||||||
|
Predicate endRange = criteriaBuilder.lessThan(root.get(M_TRIP_CREATED), end);
|
||||||
|
|
||||||
|
if (start == null && end == null) {
|
||||||
|
criteriaQuery.where(cancelled);
|
||||||
|
} else if (start == null) {
|
||||||
|
criteriaQuery.where(criteriaBuilder.and(cancelled, endRange));
|
||||||
|
} else if (end == null) {
|
||||||
|
criteriaQuery.where(criteriaBuilder.and(cancelled, startRange));
|
||||||
|
} else {
|
||||||
|
criteriaQuery.where(criteriaBuilder.and(cancelled, startRange, endRange));
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.entityManager.createQuery(criteriaQuery).getResultList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user