From d1776fcbc4eeff76c0ef97c0707bd8950e3ee986 Mon Sep 17 00:00:00 2001 From: Adrian Crum Date: Fri, 12 Jun 2009 17:03:42 +0000 Subject: [PATCH] Improved iCalendar integration: 1. Work effort tasks are now converted to VTODO. 2. Event reminders can be a popup alarm 3. Fixed invalid iCalendar creation Tested with Mozilla Sunbird. Sunbird has some annoying bugs when using read-only calendars - which they are aware of but haven't fixed. Someday I'll make the iCalendar read/write. This commit includes a migration service for anyone who was using this feature previously. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@784195 13f79535-47bb-0310-9956-ffa450edef68 --- .../workeffort/config/WorkEffortUiLabels.xml | 10 +- .../workeffort/entitydef/entitymodel.xml | 7 +- .../workeffort/servicedef/services.xml | 4 + .../workeffort/ICalendarWorker.java | 171 +++++++++++++++--- .../workeffort/WorkEffortServices.java | 43 ++++- .../workeffort/widget/WorkEffortForms.xml | 6 +- .../webcommon/includes/timeDuration.ftl | 4 +- 7 files changed, 204 insertions(+), 41 deletions(-) diff --git a/applications/workeffort/config/WorkEffortUiLabels.xml b/applications/workeffort/config/WorkEffortUiLabels.xml index 9b12148c72..99d243c441 100644 --- a/applications/workeffort/config/WorkEffortUiLabels.xml +++ b/applications/workeffort/config/WorkEffortUiLabels.xml @@ -288,16 +288,16 @@ Classifica การประเมิณ - - Recurrence Offset - Récurrence de décalage - Intervallo ricorrenza - Reminder Date Time Date heure de rappel Data ora reminder + + Reminder Offset + Récurrence de décalage + Intervallo ricorrenza + Repeat Interval Répéter intervalle diff --git a/applications/workeffort/entitydef/entitymodel.xml b/applications/workeffort/entitydef/entitymodel.xml index a53eebe2aa..2c39351af2 100644 --- a/applications/workeffort/entitydef/entitymodel.xml +++ b/applications/workeffort/entitydef/entitymodel.xml @@ -468,17 +468,16 @@ under the License. package-name="org.ofbiz.workeffort.workeffort" title="Work Effort Event Reminder Entity"> - + The millisecond interval between reminder repeats - - If the work effort is recurring, the millisecond offset from - the recurring event that will be used to calculate the reminder date/time + + The millisecond offset from the event to activate a reminder diff --git a/applications/workeffort/servicedef/services.xml b/applications/workeffort/servicedef/services.xml index 5cc0730f33..75ebee2f48 100644 --- a/applications/workeffort/servicedef/services.xml +++ b/applications/workeffort/servicedef/services.xml @@ -685,6 +685,10 @@ under the License. location="org.ofbiz.workeffort.workeffort.WorkEffortServices" invoke="processWorkEffortEventReminders" auth="true"> Process work effort event reminders. This service is run by the job scheduler. + + Migrate work effort event reminders. Run this service to update work effort reminders. + + * iCal4J library. */ public class ICalendarWorker { public static final String module = ICalendarWorker.class.getName(); - protected static ProdId prodId = new ProdId("-//Apache Open For Business//Work Effort Calendar//EN"); - protected static Map statusMap = UtilMisc.toMap("CAL_TENTATIVE", Status.VEVENT_TENTATIVE, + + protected static final ProdId prodId = new ProdId("-//Apache Open For Business//Work Effort Calendar//EN"); + protected static final Map statusMap = UtilMisc.toMap("CAL_TENTATIVE", Status.VEVENT_TENTATIVE, "CAL_CONFIRMED", Status.VEVENT_CONFIRMED, "CAL_CANCELLED", Status.VEVENT_CANCELLED); - protected static String workEffortIdPropName = "X-ORG-OFBIZ-WORKEFFORT-ID"; + protected static final String uidPrefix = "org-apache-ofbiz-we-"; + /** Returns a calendar derived from a Work Effort calendar publish point. + * + * @param delegator + * @param workEffortId ID of a work effort with workEffortTypeId equal to + * PUBLISH_PROPS. + * @return A net.fortuna.ical4j.model.Calendar instance, or null + * if workEffortId is invalid. + * @throws GenericEntityException + */ public static net.fortuna.ical4j.model.Calendar getICalendar(GenericDelegator delegator, String workEffortId) throws GenericEntityException { - GenericValue calendarProperties = delegator.findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", workEffortId)); - if (calendarProperties == null || !"PUBLISH_PROPS".equals(calendarProperties.get("workEffortTypeId"))) { + GenericValue publishProperties = delegator.findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", workEffortId)); + if (publishProperties == null || !"PUBLISH_PROPS".equals(publishProperties.get("workEffortTypeId"))) { return null; } - net.fortuna.ical4j.model.Calendar calendar = makeCalendar(calendarProperties); + net.fortuna.ical4j.model.Calendar calendar = makeCalendar(publishProperties); ComponentList components = calendar.getComponents(); - List workEfforts = getRelatedWorkEfforts(calendarProperties); + List workEfforts = getRelatedWorkEfforts(publishProperties); for (GenericValue workEffort : workEfforts) { - components.add(makeEvent(workEffort)); + components.add(makeCalendarComponent(workEffort)); + } + if (Debug.verboseOn()) { + try { + calendar.validate(true); + Debug.logVerbose("iCalendar passes validation", module); + } catch (ValidationException e) { + Debug.logVerbose("iCalendar fails validation: " + e, module); + } } return calendar; } + /** Returns a List of work efforts related to a work effort calendar + * publish point.

The List includes:

  • All public work efforts of all + * parties related to the publish point work effort
  • All public work efforts + * of all fixed assets related to the publish point work effort
  • All + * child work efforts of the publish point work effort

+ * + * @param workEffort + * @return A List of related work efforts + * @throws GenericEntityException + */ public static List getRelatedWorkEfforts(GenericValue workEffort) throws GenericEntityException { GenericDelegator delegator = workEffort.getDelegator(); String workEffortId = workEffort.getString("workEffortId"); @@ -93,7 +124,15 @@ public class ICalendarWorker { return WorkEffortWorker.removeDuplicateWorkEfforts(workEfforts); } - public static VEvent makeEvent(GenericValue workEffort) throws GenericEntityException { + /** Returns a Component instance based on a work effort. + * If the work effort is a task, then a VToDo is returned, + * otherwise a VEvent is returned. + * + * @param workEffort + * @return A VToDo or VEvent instance + * @throws GenericEntityException + */ + public static Component makeCalendarComponent(GenericValue workEffort) throws GenericEntityException { GenericDelegator delegator = workEffort.getDelegator(); String workEffortId = workEffort.getString("workEffortId"); PropertyList eventProps = new PropertyList(); @@ -104,7 +143,7 @@ public class ICalendarWorker { if (workEffort.getTimestamp("lastModifiedDate") != null) { eventProps.add(new LastModified(new DateTime(workEffort.getTimestamp("lastModifiedDate")))); } - eventProps.add(new XProperty(workEffortIdPropName, workEffort.getString("workEffortId"))); + eventProps.add(new Uid(uidPrefix.concat(workEffortId))); eventProps.add(new Summary(workEffort.getString("workEffortName"))); Status eventStatus = statusMap.get(workEffort.getString("currentStatusId")); if (eventStatus != null) { @@ -126,9 +165,9 @@ public class ICalendarWorker { // paramList.add(new XParameter(partyIdPropName, partyValue.getString("partyId"))); try { if ("CAL_ORGANIZER~CAL_OWNER".contains(partyValue.getString("roleTypeId"))) { - eventProps.add(new Organizer(paramList, "")); + eventProps.add(new Organizer("CN:".concat(partyName))); } else { - eventProps.add(new Attendee(paramList, "")); + eventProps.add(new Attendee("CN:".concat(partyName))); } } catch (Exception e) {} } @@ -152,20 +191,42 @@ public class ICalendarWorker { if (workEffort.getString("description") != null) { eventProps.add(new Description(workEffort.getString("description"))); } - return new VEvent(eventProps); + ComponentList alarms = null; + Component result = null; + if ("TASK".equals(workEffort.get("workEffortTypeId"))) { + VToDo toDo = new VToDo(eventProps); + alarms = toDo.getAlarms(); + result = toDo; + } else { + VEvent event = new VEvent(eventProps); + alarms = event.getAlarms(); + result = event; + } + getAlarms(workEffort, alarms); + if (Debug.verboseOn()) { + try { + result.validate(true); + Debug.logVerbose("iCalendar component passes validation", module); + } catch (ValidationException e) { + Debug.logVerbose("iCalendar component fails validation: " + e, module); + } + } + return result; } + /** Returns a new net.fortuna.ical4j.model.Calendar instance, + * based on a work effort calendar publish point. + * + * @param workEffort + * @return + * @throws GenericEntityException + */ public static net.fortuna.ical4j.model.Calendar makeCalendar(GenericValue workEffort) throws GenericEntityException { net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar(); PropertyList propList = calendar.getProperties(); propList.add(prodId); propList.add(Version.VERSION_2_0); propList.add(CalScale.GREGORIAN); - if (workEffort.get("description") != null) { - propList.add(new Description(workEffort.getString("description"))); - } else { - propList.add(new Description(workEffort.getString("workEffortName"))); - } // TODO: Get time zone from publish properties value java.util.TimeZone tz = java.util.TimeZone.getDefault(); TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry(); @@ -173,4 +234,72 @@ public class ICalendarWorker { calendar.getComponents().add(timezone.getVTimeZone()); return calendar; } + + /** Converts WorkEffortEventReminder entities to VAlarm + * instances, and adds them to a ComponentList. + * + * @param workEffort The work effort to get the event reminders for + * @param alarms The ComponentList that will contain the + * VAlarm instances + * @throws GenericEntityException + */ + public static void getAlarms(GenericValue workEffort, ComponentList alarms) throws GenericEntityException { + Description description = null; + if (workEffort.get("description") != null) { + description = new Description(workEffort.getString("description")); + } else { + description = new Description(workEffort.getString("workEffortName")); + } + Summary summary = new Summary(UtilProperties.getMessage("WorkEffortUiLabels", "WorkEffortEventReminder", Locale.getDefault())); + GenericDelegator delegator = workEffort.getDelegator(); + List reminderList = delegator.findList("WorkEffortEventReminder", EntityCondition.makeCondition("workEffortId", EntityOperator.EQUALS, workEffort.get("workEffortId")), null, null, null, false); + for (GenericValue reminder : reminderList) { + VAlarm alarm = createAlarm(reminder); + PropertyList alarmProps = alarm.getProperties(); + GenericValue contactMech = reminder.getRelatedOne("ContactMech"); + if (contactMech != null && "EMAIL_ADDRESS".equals(contactMech.get("contactMechTypeId"))) { + try { + alarmProps.add(new Attendee(contactMech.getString("infoString"))); + alarmProps.add(Action.EMAIL); + alarmProps.add(summary); + alarmProps.add(description); + } catch (URISyntaxException e) { + alarmProps.add(Action.DISPLAY); + alarmProps.add(new Description("Error encountered while creating iCalendar: " + e)); + } + } else { + alarmProps.add(Action.DISPLAY); + alarmProps.add(description); + } + if (Debug.verboseOn()) { + try { + alarm.validate(true); + Debug.logVerbose("iCalendar alarm passes validation", module); + } catch (ValidationException e) { + Debug.logVerbose("iCalendar alarm fails validation: " + e, module); + } + } + alarms.add(alarm); + } + } + + /** Converts a WorkEffortEventReminder entity to a + * VAlarm instance. + * + * @param workEffortEventReminder + * @return A VAlarm instance + * @throws GenericEntityException + */ + public static VAlarm createAlarm(GenericValue workEffortEventReminder) { + VAlarm alarm = null; + Timestamp reminderStamp = workEffortEventReminder.getTimestamp("reminderDateTime"); + if (reminderStamp != null) { + alarm = new VAlarm(new DateTime(reminderStamp)); + } else { + long reminderOffset = workEffortEventReminder.get("reminderOffset") == null ? 0 : workEffortEventReminder.getLong("reminderOffset").longValue(); + TimeDuration duration = TimeDuration.fromLong(reminderOffset); + alarm = new VAlarm(new Dur(duration.days(), duration.hours(), duration.minutes(), duration.seconds())); + } + return alarm; + } } diff --git a/applications/workeffort/src/org/ofbiz/workeffort/workeffort/WorkEffortServices.java b/applications/workeffort/src/org/ofbiz/workeffort/workeffort/WorkEffortServices.java index 35538a5121..a090d502ae 100644 --- a/applications/workeffort/src/org/ofbiz/workeffort/workeffort/WorkEffortServices.java +++ b/applications/workeffort/src/org/ofbiz/workeffort/workeffort/WorkEffortServices.java @@ -49,6 +49,8 @@ import org.ofbiz.entity.condition.EntityConditionList; import org.ofbiz.entity.condition.EntityExpr; import org.ofbiz.entity.condition.EntityJoinOperator; import org.ofbiz.entity.condition.EntityOperator; +import org.ofbiz.entity.model.ModelEntity; +import org.ofbiz.entity.model.ModelField; import org.ofbiz.entity.util.EntityUtil; import org.ofbiz.security.Security; import org.ofbiz.service.DispatchContext; @@ -747,6 +749,9 @@ public class WorkEffortServices { return ServiceUtil.returnError("Error while retrieving work effort event reminders: " + e); } for (GenericValue reminder : eventReminders) { + if (UtilValidate.isEmpty(reminder.get("contactMechId"))) { + continue; + } int repeatCount = reminder.get("repeatCount") == null ? 0 : reminder.getLong("repeatCount").intValue(); int currentCount = reminder.get("currentCount") == null ? 0 : reminder.getLong("currentCount").intValue(); GenericValue workEffort = null; @@ -780,11 +785,11 @@ public class WorkEffortServices { if (temporalExpression != null) { eventDateTime = temporalExpression.first(cal).getTime(); Date reminderDateTime = null; - long recurrenceOffset = reminder.get("recurrenceOffset") == null ? 0 : reminder.getLong("recurrenceOffset").longValue(); + long reminderOffset = reminder.get("reminderOffset") == null ? 0 : reminder.getLong("reminderOffset").longValue(); if (reminderStamp == null) { - if (recurrenceOffset != 0) { + if (reminderOffset != 0) { cal.setTime(eventDateTime); - TimeDuration duration = TimeDuration.fromLong(recurrenceOffset); + TimeDuration duration = TimeDuration.fromLong(reminderOffset); duration.addToCalendar(cal); reminderDateTime = cal.getTime(); } else { @@ -802,11 +807,11 @@ public class WorkEffortServices { } else { cal.setTime(reminderDateTime); Date newReminderDateTime = null; - if (recurrenceOffset != 0) { - TimeDuration duration = TimeDuration.fromLong(-recurrenceOffset); + if (reminderOffset != 0) { + TimeDuration duration = TimeDuration.fromLong(-reminderOffset); duration.addToCalendar(cal); cal.setTime(temporalExpression.next(cal).getTime()); - duration = TimeDuration.fromLong(recurrenceOffset); + duration = TimeDuration.fromLong(reminderOffset); duration.addToCalendar(cal); newReminderDateTime = cal.getTime(); } else { @@ -876,4 +881,30 @@ public class WorkEffortServices { // TODO: Other contact mechanism types Debug.logWarning("Invalid event reminder contact mech, workEffortId = " + reminder.get("workEffortId") + ", contactMechId = " + reminder.get("contactMechId"), module); } + + /** Migrate work effort event reminders. + * @param ctx + * @param context + * @return + */ + @SuppressWarnings("deprecation") + public static Map migrateWorkEffortEventReminders(DispatchContext ctx, Map context) { + GenericDelegator delegator = ctx.getDelegator(); + ModelEntity modelEntity = delegator.getModelEntity("WorkEffortEventReminder"); + if (modelEntity != null && modelEntity.getField("recurrenceOffset") != null) { + List eventReminders = null; + try { + eventReminders = delegator.findAll("WorkEffortEventReminder"); + for (GenericValue reminder : eventReminders) { + if (UtilValidate.isNotEmpty(reminder.get("recurrenceOffset"))) { + reminder.set("reminderOffset", reminder.get("recurrenceOffset")); + reminder.store(); + } + } + } catch (GenericEntityException e) { + return ServiceUtil.returnError("Error while migrating work effort event reminders: " + e); + } + } + return ServiceUtil.returnSuccess(); + } } diff --git a/applications/workeffort/widget/WorkEffortForms.xml b/applications/workeffort/widget/WorkEffortForms.xml index 944ef1d64f..ebe0ce8dd2 100644 --- a/applications/workeffort/widget/WorkEffortForms.xml +++ b/applications/workeffort/widget/WorkEffortForms.xml @@ -1309,7 +1309,7 @@ under the License. - + @@ -1325,11 +1325,11 @@ under the License. - + - + diff --git a/framework/common/webcommon/includes/timeDuration.ftl b/framework/common/webcommon/includes/timeDuration.ftl index fd1f80daf1..c84d2ec0fd 100644 --- a/framework/common/webcommon/includes/timeDuration.ftl +++ b/framework/common/webcommon/includes/timeDuration.ftl @@ -44,7 +44,7 @@ under the License. ${uiLabelMap.CommonWeek} - + ${uiLabelMap.CommonDay} @@ -88,7 +88,7 @@ under the License. ${uiLabelMap.CommonMilliSecond} - +