protected Response internalExecute(HttpServletRequest request, Session databaseSession) {
Response response = null;
try {
Event oldEvent = null;
Date fromDate = DateOperator.stringToDate(from);
Date toDate = DateOperator.stringToDate(to);
if( null == fromDate || null == toDate ) {
throw new IllegalArgumentException("Dates parsing problem" );
}
if( !DateOperator.date1IsBeforeDate2(fromDate, toDate) ) {
throw new IllegalArgumentException("Start date need to be before end date");
}
if (oldFrom != null && oldTo != null) { // if we need to exclude old event, get it.
EventType oldEventTypeObj = (EventType) databaseSession.get(EventType.class, oldEventType);
User oldOwnerObj = (User) databaseSession.get(User.class, oldOwner);
oldEvent = (Event) databaseSession.createCriteria(Event.class)
.add(Restrictions.eq("id.from", DateOperator.stringToDate(this.oldFrom) ))
.add(Restrictions.eq("id.to", DateOperator.stringToDate(this.oldTo)))
.add(Restrictions.eq("id.type", oldEventTypeObj))
.add(Restrictions.eq("id.owner", oldOwnerObj))
.uniqueResult();
}
RepetitionType noRepeat = (RepetitionType) databaseSession.get(RepetitionType.class, new Long(1));
// Get not repeated event that overlap
Criteria notRepeatedOverlapCriteria = databaseSession.createCriteria(Event.class)
.add(Restrictions.conjunction() // AND
.add(Restrictions.eq("repetition", noRepeat))
.add(Restrictions.disjunction() // OR
.add(Restrictions.conjunction()
.add(Restrictions.between("id.from", fromDate, toDate))
.add(Restrictions.between("id.to", fromDate, toDate))
)
.add(Restrictions.conjunction() // AND
.add(Restrictions.le("id.from", fromDate))
.add(Restrictions.ge("id.to", toDate))
)
.add(Restrictions.conjunction() // AND
.add(Restrictions.ge("id.from", fromDate))
.add(Restrictions.le("id.to", toDate))
)
)
);
if (oldEvent != null) { // skip old event in overlap check
notRepeatedOverlapCriteria.add(Restrictions.not(Restrictions.idEq(oldEvent.getId())));
}
List<Event> notRepeatedOverlap = notRepeatedOverlapCriteria.list();
List<Event> repeatedEvents = databaseSession.createCriteria(Event.class)