Search in sources :

Example 6 with DtStamp

use of net.fortuna.ical4j.model.property.DtStamp in project zm-mailbox by Zimbra.

the class DefaultTnefToICalendar method convert.

/* (non-Javadoc)
     * @see com.zimbra.cs.util.tnef.TnefToICalendar#convert(java.io.InputStream, net.fortuna.ical4j.data.ContentHandler)
     */
public boolean convert(MimeMessage mimeMsg, InputStream tnefInput, ContentHandler icalOutput) throws ServiceException {
    boolean conversionSuccessful = false;
    recurDef = null;
    TNEFInputStream tnefStream = null;
    SchedulingViewOfTnef schedView = null;
    Integer sequenceNum = 0;
    icalType = ICALENDAR_TYPE.VEVENT;
    try {
        tnefStream = new TNEFInputStream(tnefInput);
        schedView = new SchedulingViewOfTnef(tnefStream);
        String msgClass = schedView.getMessageClass();
        if (msgClass == null) {
            sLog.debug("Unable to determine Class of TNEF - cannot generate ICALENDER equivalent");
            // throw TNEFtoIcalendarServiceException.NON_CALENDARING_CLASS(msgClass);
            return false;
        }
        icalType = schedView.getIcalType();
        method = null;
        PartStat partstat = null;
        boolean replyWanted = schedView.getResponseRequested();
        Boolean isCounterProposal = schedView.isCounterProposal();
        if (msgClass != null) {
            // just in case.
            if (msgClass.startsWith("IPM.Microsoft Schedule.MtgReq")) {
                method = Method.REQUEST;
                partstat = PartStat.NEEDS_ACTION;
            } else if (msgClass.startsWith("IPM.Microsoft Schedule.MtgRespP")) {
                method = Method.REPLY;
                partstat = PartStat.ACCEPTED;
                replyWanted = false;
            } else if (msgClass.startsWith("IPM.Microsoft Schedule.MtgRespN")) {
                method = Method.REPLY;
                partstat = PartStat.DECLINED;
                replyWanted = false;
            } else if (msgClass.startsWith("IPM.Microsoft Schedule.MtgRespA")) {
                if ((isCounterProposal != null) && isCounterProposal) {
                    method = Method.COUNTER;
                } else {
                    method = Method.REPLY;
                }
                partstat = PartStat.TENTATIVE;
                replyWanted = false;
            } else if (msgClass.startsWith("IPM.Microsoft Schedule.MtgCncl")) {
                method = Method.CANCEL;
                replyWanted = false;
            } else if (msgClass.startsWith("IPM.TaskRequest.Accept")) {
                method = Method.REPLY;
                partstat = PartStat.ACCEPTED;
                replyWanted = false;
            } else if (msgClass.startsWith("IPM.TaskRequest.Decline")) {
                method = Method.REPLY;
                partstat = PartStat.DECLINED;
                replyWanted = false;
            } else if (msgClass.startsWith("IPM.TaskRequest.Update")) {
                method = Method.REPLY;
                // May be overridden?
                partstat = PartStat.IN_PROCESS;
                replyWanted = false;
            } else if (msgClass.startsWith("IPM.TaskRequest")) {
                method = Method.REQUEST;
                partstat = PartStat.NEEDS_ACTION;
                replyWanted = true;
            }
        }
        if (method == null) {
            sLog.debug("Unable to map class %s to ICALENDER", msgClass);
            return false;
        // throw TNEFtoIcalendarServiceException.NON_CALENDARING_CLASS(msgClass);
        }
        if (icalType == ICALENDAR_TYPE.VTODO) {
            List<?> attaches = (List<?>) schedView.getAttachments();
            if (attaches == null) {
                sLog.debug("Unable to map class %s to ICALENDER - no attachments", msgClass);
                return false;
            }
            schedView = null;
            for (Object obj : attaches) {
                if (obj instanceof Attachment) {
                    Attachment currAttach = (Attachment) obj;
                    MAPIProps attachMPs = currAttach.getMAPIProps();
                    if (attachMPs != null) {
                        MAPIProp attachData = attachMPs.getProp(MAPIProp.PR_ATTACH_DATA_OBJ);
                        if (attachData != null) {
                            Object theVal = attachData.getValue();
                            if ((theVal != null) && (theVal instanceof TNEFInputStream)) {
                                TNEFInputStream tnefSubStream = (TNEFInputStream) theVal;
                                schedView = new SchedulingViewOfTnef(tnefSubStream);
                                break;
                            }
                        }
                    }
                }
            }
            if (schedView == null) {
                sLog.debug("Unable to map class %s to ICALENDER - no properties found for sub-msg", msgClass);
                return false;
            }
        }
        uid = schedView.getIcalUID();
        sequenceNum = schedView.getSequenceNumber();
        boolean reminderSet = schedView.getReminderSet();
        String location = schedView.getLocation();
        Boolean isAllDayEvent = schedView.isAllDayEvent();
        Integer importance = schedView.getMapiImportance();
        Clazz icalClass = schedView.getIcalClass();
        Integer ownerApptId = schedView.getOwnerAppointmentId();
        EnumSet<MeetingTypeFlag> meetingTypeFlags = schedView.getMeetingTypeFlags();
        BusyStatus busyStatus = schedView.getBusyStatus();
        BusyStatus intendedBusyStatus = schedView.getIntendedBusyStatus();
        // For some ICAL properties like TRANSP and STATUS, intendedBusyStatus
        // seems closer to what is intended than straight busyStatus
        BusyStatus bestBusyStatus = intendedBusyStatus;
        if (bestBusyStatus == null) {
            bestBusyStatus = busyStatus;
        }
        // An algorithm is used to choose the values for these
        // TimeZoneDefinitions  - they don't necessarily map to single
        // MAPI properties
        TimeZoneDefinition startTimeTZinfo = schedView.getStartDateTimezoneInfo();
        TimeZoneDefinition endTimeTZinfo = schedView.getEndDateTimezoneInfo();
        TimeZoneDefinition recurrenceTZinfo = schedView.getRecurrenceTimezoneInfo();
        recurDef = schedView.getRecurrenceDefinition(recurrenceTZinfo);
        DateTime icalStartDate = schedView.getStartTime();
        DateTime icalEndDate = schedView.getEndTime();
        DateTime icalDueDate = schedView.getDueDate();
        DateTime icalDateTaskCompleted = schedView.getDateTaskCompleted();
        DateTime icalCreateDate = MapiPropertyId.PidTagCreationTime.getDateTimeAsUTC(schedView);
        DateTime icalLastModDate = MapiPropertyId.PidTagLastModificationTime.getDateTimeAsUTC(schedView);
        DateTime recurrenceIdDateTime = schedView.getRecurrenceIdTime();
        DateTime attendeeCriticalChange = schedView.getAttendeeCriticalChange();
        DateTime ownerCriticalChange = schedView.getOwnerCriticalChange();
        int percentComplete = schedView.getPercentComplete();
        TaskStatus taskStatus = schedView.getTaskStatus();
        TaskMode taskMode = schedView.getTaskMode();
        String mileage = schedView.getMileage();
        String billingInfo = schedView.getBillingInfo();
        String companies = schedView.getCompanies();
        Integer actualEffort = schedView.getActualEffort();
        Integer estimatedEffort = schedView.getEstimatedEffort();
        List<String> categories = schedView.getCategories();
        String descriptionText = null;
        String summary = null;
        if (mimeMsg != null) {
            summary = mimeMsg.getSubject();
            PlainTextFinder finder = new PlainTextFinder();
            finder.accept(mimeMsg);
            descriptionText = finder.getPlainText();
        }
        // RTF might be useful as a basis for X-ALT-DESC if we can find a reliable
        // conversion to HTML
        // String rtfText = schedView.getRTF();
        icalOutput.startCalendar();
        // Results in a 2nd PRODID in iCalendar
        // IcalUtil.addProperty(icalOutput, Property.PRODID,
        // "Zimbra-TNEF-iCalendar-Converter");
        IcalUtil.addProperty(icalOutput, method);
        if (recurDef != null) {
            String MsCalScale = recurDef.xMicrosoftCalscale();
            if ((MsCalScale == null) || (MsCalScale.equals(""))) {
                IcalUtil.addProperty(icalOutput, CalScale.GREGORIAN);
            } else {
                IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CALSCALE", MsCalScale);
            }
        } else {
            IcalUtil.addProperty(icalOutput, CalScale.GREGORIAN);
        }
        String startTZname = null;
        String endTZname = null;
        String recurTZname = null;
        if (startTimeTZinfo != null) {
            startTZname = startTimeTZinfo.getTimezoneName();
            startTimeTZinfo.addVtimezone(icalOutput);
        }
        if (endTimeTZinfo != null) {
            endTZname = endTimeTZinfo.getTimezoneName();
            if ((startTZname == null) || (!endTZname.equals(startTZname))) {
                endTimeTZinfo.addVtimezone(icalOutput);
            }
        }
        if (recurrenceTZinfo != null) {
            recurTZname = recurrenceTZinfo.getTimezoneName();
            boolean addName = true;
            if ((startTZname != null) && (recurTZname.equals(startTZname))) {
                addName = false;
            }
            if ((endTZname != null) && (recurTZname.equals(endTZname))) {
                addName = false;
            }
            if (addName) {
                recurrenceTZinfo.addVtimezone(icalOutput);
            }
        }
        if (uid == null) {
            sLog.debug("Unable to map class %s to ICALENDER - no suitable value found for UID", msgClass);
            return false;
        }
        icalOutput.startComponent(icalType.toString());
        IcalUtil.addProperty(icalOutput, Property.UID, uid);
        if ((attendeeCriticalChange != null) && (method.equals(Method.REPLY) || method.equals(Method.COUNTER))) {
            dtstamp = new DtStamp(attendeeCriticalChange);
        } else if (ownerCriticalChange != null) {
            dtstamp = new DtStamp(ownerCriticalChange);
        } else {
            DateTime stampTime = new DateTime("20000101T000000Z");
            dtstamp = new DtStamp(stampTime);
        }
        IcalUtil.addProperty(icalOutput, dtstamp);
        IcalUtil.addProperty(icalOutput, Property.CREATED, icalCreateDate, false);
        IcalUtil.addProperty(icalOutput, Property.LAST_MODIFIED, icalLastModDate, false);
        IcalUtil.addProperty(icalOutput, Property.SEQUENCE, sequenceNum, false);
        if ((summary == null) || (summary.length() == 0)) {
            // TNEF_to_iCalendar.pdf Spec requires SUMMARY for certain method types
            if (this.icalType == ICALENDAR_TYPE.VTODO) {
                if (method.equals(Method.REQUEST)) {
                    summary = new String("Task Request");
                } else if (method.equals(Method.REPLY)) {
                    summary = new String("Task Response");
                } else {
                    summary = new String("Task");
                }
            } else {
                if (method.equals(Method.REPLY)) {
                    summary = new String("Response");
                } else if (method.equals(Method.CANCEL)) {
                    summary = new String("Canceled");
                } else if (method.equals(Method.COUNTER)) {
                    summary = new String("Counter Proposal");
                }
            }
        }
        IcalUtil.addProperty(icalOutput, Property.SUMMARY, summary, false);
        IcalUtil.addProperty(icalOutput, Property.LOCATION, location, false);
        IcalUtil.addProperty(icalOutput, Property.DESCRIPTION, descriptionText, false);
        if (method.equals(Method.COUNTER)) {
            IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, Property.DTSTART, schedView.getProposedStartTime(), startTimeTZinfo, isAllDayEvent);
            IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, Property.DTEND, schedView.getProposedEndTime(), endTimeTZinfo, isAllDayEvent);
            IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "X-MS-OLK-ORIGINALSTART", icalStartDate, startTimeTZinfo, isAllDayEvent);
            IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "X-MS-OLK-ORIGINALEND", icalEndDate, endTimeTZinfo, isAllDayEvent);
        } else {
            if (this.icalType == ICALENDAR_TYPE.VTODO) {
                IcalUtil.addFloatingDateProperty(icalOutput, Property.DTSTART, icalStartDate);
                IcalUtil.addFloatingDateProperty(icalOutput, Property.DUE, icalDueDate);
                Status icalStatus = null;
                if (method.equals(Method.CANCEL)) {
                    icalStatus = Status.VTODO_CANCELLED;
                } else if (taskStatus != null) {
                    if (taskStatus.equals(TaskStatus.COMPLETE)) {
                        icalStatus = Status.VTODO_COMPLETED;
                    } else if (taskStatus.equals(TaskStatus.IN_PROGRESS)) {
                        icalStatus = Status.VTODO_IN_PROCESS;
                    }
                }
                IcalUtil.addProperty(icalOutput, icalStatus);
                if (percentComplete != 0) {
                    IcalUtil.addProperty(icalOutput, Property.PERCENT_COMPLETE, percentComplete, false);
                }
                // COMPLETED must be a UTC DATE-TIME according to rfc5545
                IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, Property.COMPLETED, icalDateTaskCompleted, null, false);
            } else {
                IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, Property.DTSTART, icalStartDate, startTimeTZinfo, isAllDayEvent);
                IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, Property.DTEND, icalEndDate, endTimeTZinfo, isAllDayEvent);
            }
        }
        // just the original start date.
        if (recurrenceIdDateTime != null) {
            IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, Property.RECURRENCE_ID, recurrenceIdDateTime, startTimeTZinfo, isAllDayEvent);
        } else {
            // Outlook messages related to a specific instance still include info on
            // the full recurrence but we don't want to include that.
            addRecurrenceRelatedProps(icalOutput, recurDef, startTimeTZinfo, isAllDayEvent);
        }
        // VTODO REQUEST must have priority according to http://tools.ietf.org/html/rfc5546
        // No harm in always setting it
        Priority priority = Priority.MEDIUM;
        if (importance != null) {
            if (importance == 2) {
                priority = Priority.HIGH;
            } else if (importance == 1) {
                priority = Priority.MEDIUM;
            } else if (importance == 0) {
                priority = Priority.LOW;
            }
        }
        IcalUtil.addProperty(icalOutput, priority);
        IcalUtil.addProperty(icalOutput, icalClass);
        addStatusProperty(icalOutput, bestBusyStatus, meetingTypeFlags);
        addTranspProperty(icalOutput, bestBusyStatus);
        addAttendees(icalOutput, mimeMsg, partstat, replyWanted);
        // Not done as Zimbra doesn't currently support "RESOURCES".
        if (categories != null) {
            CategoryList cl = new CategoryList();
            for (String category : categories) {
                cl.add(category);
            }
            if (cl.size() > 0) {
                Categories myCategories = new Categories(cl);
                IcalUtil.addProperty(icalOutput, myCategories);
            }
        }
        if (taskStatus != null) {
            if (taskStatus.equals(TaskStatus.DEFERRED) || taskStatus.equals(TaskStatus.WAITING_ON_OTHER)) {
                IcalUtil.addProperty(icalOutput, "X-ZIMBRA-TASK-STATUS", taskStatus, false);
            }
        }
        if ((taskMode != null) && (!taskMode.equals(TaskMode.TASK_REQUEST))) {
            IcalUtil.addProperty(icalOutput, "X-ZIMBRA-TASK-MODE", taskMode, false);
        }
        IcalUtil.addProperty(icalOutput, "X-ZIMBRA-MILEAGE", mileage, false);
        IcalUtil.addProperty(icalOutput, "X-ZIMBRA-BILLING-INFO", billingInfo, false);
        IcalUtil.addProperty(icalOutput, "X-ZIMBRA-COMPANIES", companies, false);
        IcalUtil.addProperty(icalOutput, "X-ZIMBRA-ACTUAL-WORK-MINS", actualEffort, false);
        IcalUtil.addProperty(icalOutput, "X-ZIMBRA-TOTAL-WORK-MINS", estimatedEffort, false);
        if (this.icalType == ICALENDAR_TYPE.VEVENT) {
            IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-ALLDAYEVENT", isAllDayEvent ? "TRUE" : "FALSE");
            IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-BUSYSTATUS", busyStatus, false);
            if (method.equals(Method.REQUEST)) {
                IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-INTENDEDSTATUS", intendedBusyStatus, false);
            }
            IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-OWNERAPPTID", ownerApptId, false);
            IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-REPLYTIME", schedView.getAppointmentReplyTime(), false);
            IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-OWNER-CRITICAL-CHANGE", ownerCriticalChange, false);
            Boolean disallowCounter = schedView.isDisallowCounter();
            if (disallowCounter != null) {
                IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-DISALLOW-COUNTER", disallowCounter ? "TRUE" : "FALSE");
            }
        }
        if (reminderSet) {
            addAlarmComponent(icalOutput, schedView.getReminderDelta());
        }
        icalOutput.endComponent(icalType.toString());
        if (recurrenceIdDateTime == null) {
            // If this message primarily relates to a specific instance,
            // exception information is superfluous
            addExceptions(icalOutput, recurDef, recurrenceTZinfo, sequenceNum, ownerApptId, summary, location, isAllDayEvent);
        }
        icalOutput.endCalendar();
        conversionSuccessful = true;
        sLog.info("Calendaring TNEF message mapped to ICALENDAR with UID=%s", uid);
    } catch (ParserException e) {
        sLog.debug("Unexpected ParserException thrown", e);
    } catch (URISyntaxException e) {
        sLog.debug("Unexpected URISyntaxException thrown", e);
    } catch (ParseException e) {
        sLog.debug("Unexpected ParseException thrown", e);
    } catch (MessagingException e) {
        sLog.debug("Unexpected MessagingException thrown", e);
    } catch (NegativeArraySizeException e) {
        sLog.debug("Problem decoding TNEF for ICALENDAR", e);
    } catch (IOException e) {
        sLog.debug("Unexpected IOException thrown", e);
    } catch (UnsupportedTnefCalendaringMsgException e) {
        sLog.debug("Unable to map this message to ICALENDAR", e);
    } catch (TNEFtoIcalendarServiceException e) {
        sLog.debug("Problem encountered mapping this message to ICALENDAR", e);
    } finally {
        try {
            if (tnefStream != null) {
                tnefStream.close();
            }
        } catch (IOException ioe) {
            sLog.debug("Problem encountered closing TNEF stream", ioe);
        }
    }
    return conversionSuccessful;
}
Also used : MAPIProps(net.freeutils.tnef.MAPIProps) TimeZoneDefinition(com.zimbra.cs.util.tnef.mapi.TimeZoneDefinition) Attachment(net.freeutils.tnef.Attachment) BusyStatus(com.zimbra.cs.util.tnef.mapi.BusyStatus) URISyntaxException(java.net.URISyntaxException) TaskMode(com.zimbra.cs.util.tnef.mapi.TaskMode) MAPIProp(net.freeutils.tnef.MAPIProp) DateTime(net.fortuna.ical4j.model.DateTime) DtStamp(net.fortuna.ical4j.model.property.DtStamp) MeetingTypeFlag(com.zimbra.cs.util.tnef.mapi.MeetingTypeFlag) List(java.util.List) ParameterList(net.fortuna.ical4j.model.ParameterList) CategoryList(net.fortuna.ical4j.model.CategoryList) Clazz(net.fortuna.ical4j.model.property.Clazz) UnsupportedTnefCalendaringMsgException(com.zimbra.cs.util.tnef.TNEFtoIcalendarServiceException.UnsupportedTnefCalendaringMsgException) Status(net.fortuna.ical4j.model.property.Status) TaskStatus(com.zimbra.cs.util.tnef.mapi.TaskStatus) BusyStatus(com.zimbra.cs.util.tnef.mapi.BusyStatus) ParserException(net.fortuna.ical4j.data.ParserException) Categories(net.fortuna.ical4j.model.property.Categories) MessagingException(javax.mail.MessagingException) Priority(net.fortuna.ical4j.model.property.Priority) PartStat(net.fortuna.ical4j.model.parameter.PartStat) IOException(java.io.IOException) TNEFInputStream(net.freeutils.tnef.TNEFInputStream) TaskStatus(com.zimbra.cs.util.tnef.mapi.TaskStatus) CategoryList(net.fortuna.ical4j.model.CategoryList) ParseException(java.text.ParseException)

Example 7 with DtStamp

use of net.fortuna.ical4j.model.property.DtStamp in project ofbiz-framework by apache.

the class ICalConverter method loadWorkEffort.

protected static void loadWorkEffort(PropertyList componentProps, GenericValue workEffort) {
    // iCalendar object created date/time
    replaceProperty(componentProps, new DtStamp());
    replaceProperty(componentProps, toClazz(workEffort.getString("scopeEnumId")));
    replaceProperty(componentProps, toCreated(workEffort.getTimestamp("createdDate")));
    replaceProperty(componentProps, toDescription(workEffort.getString("description")));
    replaceProperty(componentProps, toDtStart(workEffort.getTimestamp("estimatedStartDate")));
    replaceProperty(componentProps, toLastModified(workEffort.getTimestamp("lastModifiedDate")));
    replaceProperty(componentProps, toPriority(workEffort.getLong("priority")));
    replaceProperty(componentProps, toLocation(workEffort.getString("locationDesc")));
    replaceProperty(componentProps, toStatus(workEffort.getString("currentStatusId")));
    replaceProperty(componentProps, toSummary(workEffort.getString("workEffortName")));
    Property uid = componentProps.getProperty(Uid.UID);
    if (uid == null) {
        // Don't overwrite UIDs created by calendar clients
        replaceProperty(componentProps, toUid(workEffort.getString("workEffortId")));
    }
    replaceProperty(componentProps, toXProperty(workEffortIdXPropName, workEffort.getString("workEffortId")));
}
Also used : DtStamp(net.fortuna.ical4j.model.property.DtStamp) XProperty(net.fortuna.ical4j.model.property.XProperty) Property(net.fortuna.ical4j.model.Property)

Example 8 with DtStamp

use of net.fortuna.ical4j.model.property.DtStamp in project bw-calendar-engine by Bedework.

the class VFreeUtil method toVFreeBusy.

/**
 * Make a VFreeBusy object from a BwFreeBusy.
 */
/**
 * @param val
 * @return VFreeBusy
 * @throws CalFacadeException
 */
public static VFreeBusy toVFreeBusy(final BwEvent val) throws CalFacadeException {
    try {
        VFreeBusy vfb = new VFreeBusy(IcalUtil.makeDateTime(val.getDtstart()), IcalUtil.makeDateTime(val.getDtend()));
        PropertyList pl = vfb.getProperties();
        Property prop;
        /* ------------------- Attendees -------------------- */
        if (val.getNumAttendees() > 0) {
            for (BwAttendee att : val.getAttendees()) {
                pl.add(setAttendee(att));
            }
        }
        if (val.getNumComments() > 0) {
            for (BwString str : val.getComments()) {
                // LANG
                pl.add(new Comment(str.getValue()));
            }
        }
        if (val.getDtstamp() != null) {
            DtStamp dts = (DtStamp) pl.getProperty(Property.DTSTAMP);
            if (dts == null) {
                prop = new DtStamp(new DateTime(val.getDtstamp()));
                // if (pars.includeDateTimeProperty) {
                // prop.getParameters().add(Value.DATE_TIME);
                // }
                pl.add(prop);
            } else {
                dts.setDateTime(new DateTime(val.getDtstamp()));
            }
        }
        /* ------------------- freebusy -------------------- */
        Collection<BwFreeBusyComponent> times = val.getFreeBusyPeriods();
        if (times != null) {
            for (BwFreeBusyComponent fbc : times) {
                FreeBusy fb = new FreeBusy();
                int type = fbc.getType();
                if (type == BwFreeBusyComponent.typeBusy) {
                    addParameter(fb, FbType.BUSY);
                } else if (type == BwFreeBusyComponent.typeFree) {
                    addParameter(fb, FbType.FREE);
                } else if (type == BwFreeBusyComponent.typeBusyUnavailable) {
                    addParameter(fb, FbType.BUSY_UNAVAILABLE);
                } else if (type == BwFreeBusyComponent.typeBusyTentative) {
                    addParameter(fb, FbType.BUSY_TENTATIVE);
                } else {
                    throw new CalFacadeException("Bad free-busy type " + type);
                }
                PeriodList pdl = fb.getPeriods();
                for (Period p : fbc.getPeriods()) {
                    // XXX inverse.ca plugin cannot handle durations.
                    Period np = new Period(p.getStart(), p.getEnd());
                    pdl.add(np);
                }
                pl.add(fb);
            }
        }
        /* ------------------- Organizer -------------------- */
        BwOrganizer org = val.getOrganizer();
        if (org != null) {
            pl.add(setOrganizer(org));
        }
        if (val.getUid() != null) {
            pl.add(new Uid(val.getUid()));
        }
        return vfb;
    } catch (CalFacadeException cfe) {
        throw cfe;
    } catch (Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : Comment(net.fortuna.ical4j.model.property.Comment) BwFreeBusyComponent(org.bedework.calfacade.BwFreeBusyComponent) VFreeBusy(net.fortuna.ical4j.model.component.VFreeBusy) FreeBusy(net.fortuna.ical4j.model.property.FreeBusy) VFreeBusy(net.fortuna.ical4j.model.component.VFreeBusy) PeriodList(net.fortuna.ical4j.model.PeriodList) Period(net.fortuna.ical4j.model.Period) BwString(org.bedework.calfacade.BwString) DateTime(net.fortuna.ical4j.model.DateTime) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) Uid(net.fortuna.ical4j.model.property.Uid) DtStamp(net.fortuna.ical4j.model.property.DtStamp) PropertyList(net.fortuna.ical4j.model.PropertyList) Property(net.fortuna.ical4j.model.Property) BwAttendee(org.bedework.calfacade.BwAttendee) BwOrganizer(org.bedework.calfacade.BwOrganizer)

Example 9 with DtStamp

use of net.fortuna.ical4j.model.property.DtStamp in project bw-calendar-engine by Bedework.

the class Sharing method share.

@Override
public ShareResultType share(final BwCalendar col, final ShareType share) throws CalFacadeException {
    if (!col.getCanAlias()) {
        throw new CalFacadeForbidden("Cannot share");
    }
    final ShareResultType sr = new ShareResultType();
    final List<String> removePrincipalHrefs = new ArrayList<>();
    final List<AddPrincipal> addPrincipals = new ArrayList<>();
    final String calAddr = principalToCaladdr(getPrincipal());
    final InviteType invite = getInviteStatus(col);
    if (invite.getOrganizer() == null) {
        final OrganizerType org = new OrganizerType();
        org.setHref(calAddr);
        invite.setOrganizer(org);
    }
    final List<InviteNotificationType> notifications = new ArrayList<>();
    boolean addedSharee = false;
    boolean removedSharee = false;
    /* If there are any removal elements in the invite, remove those
     * sharees. We'll flag hrefs as bad if they are not actually sharees.
     *
     * If we do remove a sharee we'll add notifications to the list
     * to send later.
     */
    for (final RemoveType rem : share.getRemove()) {
        final InviteNotificationType n = doRemove(col, rem, calAddr, invite);
        if (n != null) {
            removedSharee = true;
            if ((n.getPreviousStatus() != null) && !n.getPreviousStatus().equals(declineStatus)) {
                // We don't notify if the user had declined
                notifications.add(n);
            }
            sr.addGood(rem.getHref());
            removePrincipalHrefs.add(rem.getHref());
        } else {
            sr.addBad(rem.getHref());
        }
    }
    /* Now deal with the added sharees if there are any.
     */
    for (final SetType set : share.getSet()) {
        final InviteNotificationType n = doSet(col, set, addPrincipals, calAddr, invite);
        if (n != null) {
            addedSharee = true;
            notifications.add(n);
            sr.addGood(set.getHref());
        } else {
            sr.addBad(set.getHref());
        }
    }
    if (!addedSharee && !removedSharee) {
        // Nothing changed
        return sr;
    }
    /* Send any invitations and update the sharing status.
     * If it's a removal and the current status is not
     * accepted then just delete the current invitation
     */
    final Notifications notify = (Notifications) getSvc().getNotificationsHandler();
    sendNotifications: for (final InviteNotificationType in : notifications) {
        final Sharee sh = getSharee(in.getHref());
        final boolean remove = in.getInviteStatus().equals(removeStatus);
        final List<NotificationType> notes = notify.getMatching(in.getHref(), AppleServerTags.inviteNotification);
        if (!Util.isEmpty(notes)) {
            for (final NotificationType n : notes) {
                final InviteNotificationType nin = (InviteNotificationType) n.getNotification();
                if (!nin.getHostUrl().equals(in.getHostUrl())) {
                    continue;
                }
                if (remove) {
                    if (nin.getInviteStatus().equals(noresponseStatus)) {
                        notify.remove(sh.pr, n);
                        continue sendNotifications;
                    }
                } else {
                    notify.remove(sh.pr, n);
                }
            }
        }
        final NotificationType note = new NotificationType();
        note.setDtstamp(new DtStamp(new DateTime(true)).getValue());
        note.setNotification(in);
        notify.send(sh.pr, note);
        /* Add the invite to the set of properties associated with this collection
       * We give it a name consisting of the inviteNotification tag + uid.
       */
        final QName qn = new QName(AppleServerTags.inviteNotification.getNamespaceURI(), AppleServerTags.inviteNotification.getLocalPart() + in.getUid());
        try {
            col.setProperty(NamespaceAbbrevs.prefixed(qn), in.toXml());
        } catch (final CalFacadeException cfe) {
            throw cfe;
        } catch (final Throwable t) {
            throw new CalFacadeException(t);
        }
    }
    if (addedSharee && !col.getShared()) {
        // Mark the collection as shared
        col.setShared(true);
    }
    try {
        col.setQproperty(AppleServerTags.invite, invite.toXml());
        getCols().update(col);
        for (final String principalHref : removePrincipalHrefs) {
            removeAccess(col, principalHref);
        }
        for (final AddPrincipal ap : addPrincipals) {
            setAccess(col, ap);
        }
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    }
    return sr;
}
Also used : ShareResultType(org.bedework.caldav.util.sharing.ShareResultType) InviteNotificationType(org.bedework.caldav.util.sharing.InviteNotificationType) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) CalFacadeForbidden(org.bedework.calfacade.exc.CalFacadeForbidden) OrganizerType(org.bedework.caldav.util.sharing.OrganizerType) DateTime(net.fortuna.ical4j.model.DateTime) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) RemoveType(org.bedework.caldav.util.sharing.RemoveType) DtStamp(net.fortuna.ical4j.model.property.DtStamp) SetType(org.bedework.caldav.util.sharing.SetType) NotificationType(org.bedework.caldav.util.notifications.NotificationType) InviteNotificationType(org.bedework.caldav.util.sharing.InviteNotificationType) InviteType(org.bedework.caldav.util.sharing.InviteType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with DtStamp

use of net.fortuna.ical4j.model.property.DtStamp in project bw-calendar-engine by Bedework.

the class Sharing method updateSharingStatus.

/* ====================================================================
   *                   Private methods
   * ==================================================================== */
/* This requires updating the shared calendar to reflect the accept/decline
   * status
   */
private boolean updateSharingStatus(final String sharerHref, final String path, final InviteReplyType reply, final Holder<AccessType> access) throws CalFacadeException {
    pushPrincipal(sharerHref);
    try {
        final BwCalendar col = getCols().get(path);
        if (col == null) {
            // Bad hosturl?
            throw new CalFacadeForbidden(CalFacadeException.shareTargetNotFound);
        }
        /* See if we have an outstanding invite for this user */
        final QName qn = new QName(AppleServerTags.inviteNotification.getNamespaceURI(), AppleServerTags.inviteNotification.getLocalPart() + reply.getInReplyTo());
        final String pname = NamespaceAbbrevs.prefixed(qn);
        final String xmlInvite = col.getProperty(pname);
        if (xmlInvite == null) {
            // No invite
            if (debug) {
                trace("No invite notification on collection with name: " + pname);
            }
            throw new CalFacadeForbidden(CalFacadeException.noInvite);
        }
        /* Remove the invite */
        col.setProperty(pname, null);
        /* Get the invite property and locate and update this sharee */
        final InviteType invite = getInviteStatus(col);
        UserType uentry = null;
        final String invitee = getSvc().getDirectories().normalizeCua(reply.getHref());
        if (invite != null) {
            uentry = invite.finduser(invitee);
        }
        if (uentry == null) {
            if (debug) {
                trace("Cannot find invitee: " + invitee);
            }
            throw new CalFacadeForbidden(CalFacadeException.noInviteeInUsers);
        }
        if (reply.testAccepted()) {
            uentry.setInviteStatus(AppleServerTags.inviteAccepted);
        } else {
            uentry.setInviteStatus(AppleServerTags.inviteDeclined);
        }
        access.value = uentry.getAccess();
        col.setProperty(NamespaceAbbrevs.prefixed(AppleServerTags.invite), invite.toXml());
        getCols().update(col);
        /* Now send the sharer the reply as a notification */
        final NotificationType note = new NotificationType();
        note.setDtstamp(new DtStamp(new DateTime(true)).getValue());
        final InviteReplyType irt = (InviteReplyType) reply.clone();
        note.setNotification(irt);
        /* Fill in the summary (the sharer's summary) on the reply. */
        irt.setSummary(reply.getSummary());
        getSvc().getNotificationsHandler().add(note);
        return irt.testAccepted();
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    } finally {
        popPrincipal();
    }
}
Also used : DtStamp(net.fortuna.ical4j.model.property.DtStamp) QName(javax.xml.namespace.QName) NotificationType(org.bedework.caldav.util.notifications.NotificationType) InviteNotificationType(org.bedework.caldav.util.sharing.InviteNotificationType) InviteType(org.bedework.caldav.util.sharing.InviteType) InviteReplyType(org.bedework.caldav.util.sharing.InviteReplyType) CalFacadeForbidden(org.bedework.calfacade.exc.CalFacadeForbidden) BwCalendar(org.bedework.calfacade.BwCalendar) UserType(org.bedework.caldav.util.sharing.UserType) DateTime(net.fortuna.ical4j.model.DateTime) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Aggregations

DtStamp (net.fortuna.ical4j.model.property.DtStamp)11 DateTime (net.fortuna.ical4j.model.DateTime)9 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)5 PropertyList (net.fortuna.ical4j.model.PropertyList)4 VEvent (net.fortuna.ical4j.model.component.VEvent)4 Uid (net.fortuna.ical4j.model.property.Uid)4 Calendar (net.fortuna.ical4j.model.Calendar)3 DtEnd (net.fortuna.ical4j.model.property.DtEnd)3 DtStart (net.fortuna.ical4j.model.property.DtStart)3 Summary (net.fortuna.ical4j.model.property.Summary)3 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 GregorianCalendar (java.util.GregorianCalendar)2 List (java.util.List)2 QName (javax.xml.namespace.QName)2 ParameterList (net.fortuna.ical4j.model.ParameterList)2 Property (net.fortuna.ical4j.model.Property)2 TimeZone (net.fortuna.ical4j.model.TimeZone)2