use of net.fortuna.ical4j.model.Date in project bw-calendar-engine by Bedework.
the class BwDateTime method addDuration.
private BwDateTime addDuration(final Dur val) throws CalFacadeException {
DtEnd dtEnd;
java.util.Date endDt = val.getTime(makeDate());
DtStart dtStart = makeDtStart(Timezones.getTzRegistry());
if (getDateType()) {
dtEnd = new DtEnd(new Date(endDt));
addIcalParameter(dtEnd, Value.DATE);
} else {
DateTime d = new DateTime(endDt);
Parameter tzid = getIcalParameter(dtStart, "TZID");
if (tzid != null) {
DateTime sd = (DateTime) dtStart.getDate();
d.setTimeZone(sd.getTimeZone());
}
// dtEnd = new DtEnd(d, dtStart.isUtc());
dtEnd = new DtEnd(d);
if (tzid != null) {
addIcalParameter(dtEnd, tzid);
} else if (dtStart.isUtc()) {
dtEnd.setUtc(true);
}
}
return makeBwDateTime(dtEnd);
}
use of net.fortuna.ical4j.model.Date in project bw-calendar-engine by Bedework.
the class RecurRuleComponents method fromEventXrules.
private static Collection<RecurRuleComponents> fromEventXrules(BwEvent ev, Collection<String> rules) throws CalFacadeException {
Collection<RecurRuleComponents> rrcs = new ArrayList<RecurRuleComponents>();
if (!ev.isRecurringEntity()) {
return rrcs;
}
if (rules == null) {
return rrcs;
}
try {
for (String rule : rules) {
RecurRuleComponents rrc = new RecurRuleComponents();
Recur recur = new Recur(rule);
rrc.setRule(rule);
rrc.setFreq(Freq.valueOf(recur.getFrequency()));
Date until = recur.getUntil();
if (until != null) {
rrc.setUntil(until);
} else {
rrc.setCount(recur.getCount());
}
rrc.setInterval(recur.getInterval());
rrc.setBySecond(checkNumList(recur.getSecondList()));
rrc.setByMinute(checkNumList(recur.getMinuteList()));
rrc.setByHour(checkNumList(recur.getHourList()));
/* Group by position */
Collection wds = recur.getDayList();
if (wds != null) {
HashMap<Integer, Collection<String>> hm = new HashMap<Integer, Collection<String>>();
for (Object o : wds) {
WeekDay wd = (WeekDay) o;
Collection<String> c = hm.get(wd.getOffset());
if (c == null) {
c = new ArrayList<>();
hm.put(wd.getOffset(), c);
}
c.add(wd.getDay().name());
}
final Collection<PosDays> pds = new ArrayList<>();
final Set<Integer> poss = hm.keySet();
for (final Integer pos : poss) {
pds.add(new PosDays(pos, hm.get(pos)));
}
rrc.setByDay(pds);
}
rrc.setByMonthDay(checkNumList(recur.getMonthDayList()));
rrc.setByYearDay(checkNumList(recur.getYearDayList()));
rrc.setByWeekNo(checkNumList(recur.getWeekNoList()));
rrc.setByMonth(checkNumList(recur.getMonthList()));
rrc.setBySetPos(checkNumList(recur.getSetPosList()));
if (recur.getWeekStartDay() != null) {
rrc.setWkst(recur.getWeekStartDay().name());
}
rrcs.add(rrc);
}
} catch (Throwable t) {
throw new CalFacadeException(t);
}
return rrcs;
}
use of net.fortuna.ical4j.model.Date in project bw-calendar-engine by Bedework.
the class RecurUtil method getLatestRecurrenceDate.
/**
* Return the absolute latest end date for this event. Note that
* exclusions may mean the actual latest date is earlier.
*
* @param evprops
* @param vstart
* @param enddt
* @param d
* @param maxRangeEnd
* @return date
* @throws CalFacadeException
*/
private static Date getLatestRecurrenceDate(final PropertyList evprops, final DtStart vstart, final DtEnd enddt, final Duration d, final Date maxRangeEnd) throws CalFacadeException {
boolean debug = getLog().isDebugEnabled();
try {
Date start = vstart.getDate();
Date until = null;
/* Get the duration of the event to get us past the end
*/
Dur dur;
if (d != null) {
dur = d.getDuration();
} else {
Date evend;
if (enddt == null) {
evend = start;
} else {
evend = enddt.getDate();
}
dur = new Dur(start, evend);
}
/* Make a new duration incremented a little to avoid any boundary
conditions */
if (dur.getWeeks() != 0) {
dur = new Dur(dur.getWeeks() + 1);
} else {
dur = new Dur(dur.getDays() + 1, dur.getHours(), dur.getMinutes(), dur.getSeconds());
}
PropertyList rrules = evprops.getProperties(Property.RRULE);
PropertyList rdts = evprops.getProperties(Property.RDATE);
if ((rrules == null) && (rdts == null)) {
// Not a recurring event
return null;
}
if (rrules != null) {
Iterator rit = rrules.iterator();
while (rit.hasNext()) {
RRule r = (RRule) rit.next();
Date nextUntil = getLastDate(r.getRecur(), start, maxRangeEnd);
if (nextUntil == null) {
/* We have a rule without an end date so it's infinite.
*/
return null;
}
if (debug) {
debugMsg("Last date for recur=" + nextUntil);
}
if ((until == null) || (nextUntil.after(until))) {
until = nextUntil;
}
}
/* We have some rules - none have an end date so it's infinite.
*/
if (until == null) {
// infinite
return null;
}
}
if (rdts != null) {
// Get the latest date from each
// XXX are these sorted?
Iterator rit = rdts.iterator();
while (rit.hasNext()) {
RDate r = (RDate) rit.next();
if (Value.PERIOD.equals(r.getParameter(Parameter.VALUE))) {
PeriodList pl = r.getPeriods();
Iterator it = pl.iterator();
while (it.hasNext()) {
Period p = (Period) it.next();
// Not sure if a single date gives a null end
Date nextUntil = p.getEnd();
if (nextUntil == null) {
nextUntil = p.getStart();
}
if ((until == null) || (nextUntil.after(until))) {
until = nextUntil;
}
}
} else {
// date or datetime
DateList startDates = r.getDates();
for (int j = 0; j < startDates.size(); j++) {
Date startDate = (Date) startDates.get(j);
Date endDate = new Date(dur.getTime(startDate));
if ((until == null) || (endDate.after(until))) {
until = endDate;
}
}
}
}
}
if (debug) {
debugMsg("Last date before fix=" + until);
}
/* Now add the duration of the event to get us past the end
*/
if (until instanceof DateTime) {
until = new DateTime(dur.getTime(until));
((DateTime) until).setUtc(true);
} else {
until = new Date(dur.getTime(until));
}
if (debug) {
debugMsg("Last date after fix=" + until);
}
return until;
} catch (Throwable t) {
throw new CalFacadeException(t);
}
}
use of net.fortuna.ical4j.model.Date in project bw-calendar-engine by Bedework.
the class ToXEvent method doRecurring.
/**
* Build recurring properties from event.
*
* @param pattern
* @param compCl - component class for pattern matching
* @param val
* @param pl
* @throws CalFacadeException
*/
public static void doRecurring(final BaseComponentType pattern, final Class compCl, final BwEvent val, final List<JAXBElement<? extends BasePropertyType>> pl) throws CalFacadeException {
try {
if (emit(pattern, compCl, RrulePropType.class) && val.hasRrules()) {
for (String s : val.getRrules()) {
RRule rule = new RRule();
rule.setValue(s);
Recur r = rule.getRecur();
RecurType rt = new RecurType();
rt.setFreq(FreqRecurType.fromValue(r.getFrequency()));
if (r.getCount() > 0) {
rt.setCount(BigInteger.valueOf(r.getCount()));
}
Date until = r.getUntil();
if (until != null) {
UntilRecurType u = new UntilRecurType();
/*
if (until instanceof DateTime) {
u.setDateTime(until.toString());
} else {
u.setDate(until.toString());
}
*/
XcalUtil.initUntilRecur(u, until.toString());
}
if (r.getInterval() > 0) {
rt.setInterval(String.valueOf(r.getInterval()));
}
listFromNumberList(rt.getBysecond(), r.getSecondList());
listFromNumberList(rt.getByminute(), r.getMinuteList());
listFromNumberList(rt.getByhour(), r.getHourList());
if (r.getDayList() != null) {
List<String> l = rt.getByday();
for (WeekDay wd : r.getDayList()) {
l.add(wd.getDay().name());
}
}
listFromNumberList(rt.getByyearday(), r.getYearDayList());
intlistFromNumberList(rt.getBymonthday(), r.getMonthDayList());
listFromNumberList(rt.getByweekno(), r.getWeekNoList());
intlistFromNumberList(rt.getBymonth(), r.getMonthList());
bigintlistFromNumberList(rt.getBysetpos(), r.getSetPosList());
RrulePropType rrp = new RrulePropType();
rrp.setRecur(rt);
pl.add(of.createRrule(rrp));
}
}
/*
if (emit(pattern, compCl, ExrulePropType.class) &&
val.hasExrules()) {
for(String s: val.getExrules()) {
ExRule rule = new ExRule();
rule.setValue(s);
pl.add(rule);
}
}
if (emit(pattern, compCl, RdatePropType.class) {
makeDlp(false, val.getRdates(), pl);
}
if (emit(pattern, compCl, ExdatePropType.class) {
makeDlp(true, val.getExdates(), pl);
}
*/
// } catch (CalFacadeException cfe) {
// throw cfe;
} catch (Throwable t) {
throw new CalFacadeException(t);
}
}
use of net.fortuna.ical4j.model.Date 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