use of org.bedework.caldav.bwserver.stdupdaters.DateDatetimePropUpdater.DatesState in project bw-calendar-engine by Bedework.
the class BwUpdates method validateDates.
private UpdateResult validateDates(final EventInfo ei) throws WebdavException {
DatesState ds = (DatesState) state.get(DatesState.stateName);
if (ds == null) {
return UpdateResult.getOkResult();
}
BwEvent ev = ei.getEvent();
boolean task = ev.getEntityType() == IcalDefs.entityTypeTodo;
PropertyInfoIndex endPi;
ChangeTable chg = ei.getChangeset(userHref);
if (task) {
endPi = PropertyInfoIndex.DUE;
} else {
endPi = PropertyInfoIndex.DTEND;
}
try {
boolean scheduleReply = ev.getScheduleMethod() == ScheduleMethods.methodTypeReply;
if (ds.start == null) {
if (!scheduleReply && !task) {
return new UpdateResult("org.bedework.error.nostartdate");
}
/* A todo can have no date and time. set start to now, end to
* many years from now and the noStart flag.
*
* Such an entry has to appear only on the current day.
*/
if (ds.end != null) {
ds.start = ds.end;
} else {
Date now = new Date(new java.util.Date().getTime());
DtStart dtStart = new DtStart(now);
dtStart.getParameters().add(Value.DATE);
ds.start = BwDateTime.makeBwDateTime(dtStart);
}
if (!ev.getNoStart() || !CalFacadeUtil.eqObjval(ev.getDtstart(), ds.start)) {
chg.changed(PropertyInfoIndex.DTSTART, ev.getDtstart(), null);
ev.setDtstart(ds.start);
ev.setNoStart(true);
}
} else if (ev.getNoStart() || !ds.start.equals(ev.getDtstart())) {
chg.changed(PropertyInfoIndex.DTSTART, ev.getDtstart(), ds.start);
ev.setNoStart(false);
ev.setDtstart(ds.start);
}
char endType = StartEndComponent.endTypeNone;
if (ds.end != null) {
if ((ev.getEndType() != StartEndComponent.endTypeDate) || !CalFacadeUtil.eqObjval(ev.getDtend(), ds.end)) {
chg.changed(endPi, ev.getDtend(), ds.end);
endType = StartEndComponent.endTypeDate;
ev.setDtend(ds.end);
}
} else if (scheduleReply || task) {
// about 10 years
Dur years = new Dur(520);
Date now = new Date(new java.util.Date().getTime());
DtEnd dtEnd = new DtEnd(new Date(years.getTime(now)));
dtEnd.getParameters().add(Value.DATE);
ds.end = BwDateTime.makeBwDateTime(dtEnd);
if (!CalFacadeUtil.eqObjval(ev.getDtend(), ds.end)) {
chg.changed(endPi, ev.getDtend(), ds.end);
ev.setDtend(ds.end);
}
}
/**
* If we were given a duration store it in the event and calculate
* an end to the event - which we should not have been given.
*/
if (ds.duration != null) {
if (endType != StartEndComponent.endTypeNone) {
if (ev.getEntityType() == IcalDefs.entityTypeFreeAndBusy) {
// Apple is sending both - duration indicates the minimum
// freebusy duration. Ignore for now.
} else {
return new UpdateResult(CalFacadeException.endAndDuration);
}
}
endType = StartEndComponent.endTypeDuration;
if (!ds.duration.equals(ev.getDuration())) {
chg.changed(PropertyInfoIndex.DURATION, ev.getDuration(), ds.duration);
ev.setDuration(ds.duration);
}
ev.setDtend(BwDateTime.makeDateTime(ev.getDtstart().makeDtStart(), ev.getDtstart().getDateType(), new Dur(ds.duration)));
} else if (!scheduleReply && (endType == StartEndComponent.endTypeNone) && !task) {
/* No duration and no end specified.
* Set the end values to the start values + 1 for dates
*/
boolean dateOnly = ev.getDtstart().getDateType();
Dur dur;
if (dateOnly) {
// 1 day
dur = new Dur(1, 0, 0, 0);
} else {
// No duration
dur = new Dur(0, 0, 0, 0);
}
BwDateTime bwDtEnd = BwDateTime.makeDateTime(ev.getDtstart().makeDtStart(), dateOnly, dur);
if (!CalFacadeUtil.eqObjval(ev.getDtend(), bwDtEnd)) {
chg.changed(endPi, ev.getDtend(), bwDtEnd);
ev.setDtend(bwDtEnd);
}
}
if ((endType != StartEndComponent.endTypeDuration) && (ev.getDtstart() != null) && (ev.getDtend() != null)) {
// Calculate a duration
String durVal = BwDateTime.makeDuration(ev.getDtstart(), ev.getDtend()).toString();
if (!durVal.equals(ev.getDuration())) {
chg.changed(PropertyInfoIndex.DURATION, ev.getDuration(), durVal);
ev.setDuration(durVal);
}
}
ev.setEndType(endType);
return UpdateResult.getOkResult();
} catch (CalFacadeException cfe) {
throw new WebdavException(cfe);
}
}
Aggregations