[jbpm-commits] JBoss JBPM SVN: r6593 - in jbpm4/trunk/modules/pvm/src: test/java/org/jbpm/pvm/internal/cal and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Sun Aug 15 00:31:29 EDT 2010


Author: rebody
Date: 2010-08-15 00:31:28 -0400 (Sun, 15 Aug 2010)
New Revision: 6593

Modified:
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cal/BusinessCalendarImpl.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cal/Day.java
   jbpm4/trunk/modules/pvm/src/test/java/org/jbpm/pvm/internal/cal/BusinessCalendarTest.java
Log:
JBPM-2814 working on check unconfigured days

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cal/BusinessCalendarImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cal/BusinessCalendarImpl.java	2010-08-15 03:39:21 UTC (rev 6592)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cal/BusinessCalendarImpl.java	2010-08-15 04:31:28 UTC (rev 6593)
@@ -52,16 +52,16 @@
   protected long secondInMillis = 1000;
   protected long minuteInMillis = 60000;
   protected long hourInMillis = 3600000;
-  protected long dayInMillis = 24*hourInMillis;
-  protected long weekInMillis = 7*dayInMillis;
-  protected long monthInMillis = 30*dayInMillis;
-  protected long yearInMillis = 365*dayInMillis;
+  protected long dayInMillis = 24 * hourInMillis;
+  protected long weekInMillis = 7 * dayInMillis;
+  protected long monthInMillis = 30 * dayInMillis;
+  protected long yearInMillis = 365 * dayInMillis;
 
   //TODO: calculate these numbers based on business calendar config
-  protected long businessDayInMillis = 8*hourInMillis;
-  protected long businessWeekInMillis = 40*hourInMillis;
-  protected long businessMonthInMillis = 21*dayInMillis;
-  protected long businessYearInMillis = 220*dayInMillis;
+  protected long businessDayInMillis = 8 * hourInMillis;
+  protected long businessWeekInMillis = 40 * hourInMillis;
+  protected long businessMonthInMillis = 21 * dayInMillis;
+  protected long businessYearInMillis = 220 * dayInMillis;
 
   /** constructor for persistence and creating an empty business calendar */
   public BusinessCalendarImpl() {
@@ -84,22 +84,31 @@
 
   public Date subtract(Date date, Duration duration) {
     Date end = null;
+    DayPart dayPart = null;
     if (duration.isBusinessTime()) {
-        DayPart dayPart = findDayPart(date);
-        boolean isInbusinessHours = (dayPart!=null);
-        if (! isInbusinessHours) {
-          Object[] result = new Object[2];
-          Day day = findDay(date);
-          day.findPreviousDayPartStart((day.getDayParts()!=null ? day.getDayParts().length-1 : 0), date, result);
+      dayPart = findDayPart(date);
+      boolean isInbusinessHours = (dayPart != null);
+      if (! isInbusinessHours) {
+        Object[] result = new Object[2];
+        Day day = findDay(date);
+        if (day != null) {
+          day.findPreviousDayPartStart(
+            (day.getDayParts() != null ? day.getDayParts().length - 1 : 0),
+            date,
+            result);
           date = (Date) result[0];
           dayPart = (DayPart) result[1];
         }
-        long millis = convertToMillis(duration);
-        end = dayPart.subtract(date, millis, duration.isBusinessTime());
+      }
+    }
+
+    long millis = convertToMillis(duration);
+    if (dayPart == null) {
+      end = new Date(date.getTime() - millis);
     } else {
-        long millis = convertToMillis(duration);
-        end = new Date(date.getTime()-millis);
+      end = dayPart.subtract(date, millis, duration.isBusinessTime());
     }
+
     if (end.before(Clock.getTime())) {
         if (log.isWarnEnabled()) {
           log.warn("Duedate : [" + end + "] in the past of start : [" + date + "]");
@@ -112,21 +121,28 @@
 
   public Date add(Date date, Duration duration) {
     Date end = null;
+    DayPart dayPart = null;
     if (duration.isBusinessTime()) {
-      DayPart dayPart = findDayPart(date);
+      dayPart = findDayPart(date);
       boolean isInbusinessHours = (dayPart!=null);
       if (! isInbusinessHours) {
         Object[] result = new Object[2];
-        findDay(date).findNextDayPartStart(0, date, result);
-        date = (Date) result[0];
-        dayPart = (DayPart) result[1];
+        Day day = findDay(date);
+        if (day != null) {
+          day.findNextDayPartStart(0, date, result);
+          date = (Date) result[0];
+          dayPart = (DayPart) result[1];
+        }
       }
-      long millis = convertToMillis(duration);
+    }
+
+    long millis = convertToMillis(duration);
+    if (dayPart == null) {
+      end = new Date(date.getTime() + millis);
+    } else {
       end = dayPart.add(date, millis, duration.isBusinessTime());
-    } else {
-      long millis = convertToMillis(duration);
-      end = new Date(date.getTime()+millis);
     }
+
     return end;
   }
 
@@ -150,11 +166,11 @@
   }
 
   public boolean isInBusinessHours(Date date) {
-    return (findDayPart(date)!=null);
+    return (findDayPart(date) != null);
   }
 
   public boolean isHoliday(Date date) {
-    if (holidays!=null) {
+    if (holidays != null) {
       for(Holiday holiday: holidays) {
         if (holiday.includes(date)) {
           return true;
@@ -173,7 +189,7 @@
     calendar.set(Calendar.SECOND, 0);
     calendar.set(Calendar.MILLISECOND, 0);
     date = calendar.getTime();
-    while(isHoliday(date)) {
+    while (isHoliday(date)) {
       calendar.setTime(date);
       calendar.add(Calendar.DATE, 1);
       date = calendar.getTime();
@@ -203,6 +219,10 @@
   }
 
   protected Day findDay(Date date) {
+    if (days == null) {
+      log.trace("business days is unconfigured.");
+      return null;
+    }
     Calendar calendar = createCalendar();
     calendar.setTime(date);
     int weekDayIndex = calendar.get(Calendar.DAY_OF_WEEK);
@@ -213,9 +233,12 @@
     DayPart dayPart = null;
     if (! isHoliday(date)) {
       Day day = findDay(date);
+      if (day == null) {
+        return null;
+      }
       DayPart[] dayParts = day.getDayParts();
-      if (dayParts!=null) {
-        for (int i=0; ((i < dayParts.length) && (dayPart==null)); i++) {
+      if (dayParts != null) {
+        for (int i = 0; ((i < dayParts.length) && (dayPart == null)); i++) {
           DayPart candidate = dayParts[i];
           if (candidate.includes(date)) {
             dayPart = candidate;
@@ -234,6 +257,9 @@
         date = findStartOfNextDay(date);
         Object result[] = new Object[2];
         Day day = findDay(date);
+        if (day == null) {
+          return null;
+        }
         day.findNextDayPartStart(0, date, result);
         nextDayPart = (DayPart) result[1];
       }

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cal/Day.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cal/Day.java	2010-08-15 03:39:21 UTC (rev 6592)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cal/Day.java	2010-08-15 04:31:28 UTC (rev 6593)
@@ -56,23 +56,26 @@
   }
 
   public void findPreviousDayPartStart(int dayPartIndex, Date date, Object[] result) {
-        // if there is a day part in this day that starts before the given date
-        if ( (dayParts!=null)
-             && (dayPartIndex >= 0)
-           ) {
-          if (dayParts[dayPartIndex].isEndBefore(date)) {
-            result[0] = dayParts[dayPartIndex].getEndTime(date);
-            result[1] = dayParts[dayPartIndex];
-          } else {
-              findPreviousDayPartStart(dayPartIndex-1, date, result);
-          }
-        } else {
-          // descend recursively
-          date = businessCalendarImpl.findStartOfPreviousDay(date);
-          Day previousDay = businessCalendarImpl.findDay(date);
-          previousDay.findPreviousDayPartStart((previousDay.getDayParts()!=null ? previousDay.getDayParts().length-1 : 0), date, result);
-        }
+    // if there is a day part in this day that starts before the given date
+    if ( (dayParts != null)
+         && (dayPartIndex >= 0)
+       ) {
+      if (dayParts[dayPartIndex].isEndBefore(date)) {
+        result[0] = dayParts[dayPartIndex].getEndTime(date);
+        result[1] = dayParts[dayPartIndex];
+      } else {
+        findPreviousDayPartStart(dayPartIndex - 1, date, result);
       }
+    } else {
+      // descend recursively
+      date = businessCalendarImpl.findStartOfPreviousDay(date);
+      Day previousDay = businessCalendarImpl.findDay(date);
+      previousDay.findPreviousDayPartStart(
+        (previousDay.getDayParts() != null ? previousDay.getDayParts().length - 1 : 0),
+        date,
+        result);
+    }
+  }
 
   public BusinessCalendarImpl getBusinessCalendar() {
     return businessCalendarImpl;

Modified: jbpm4/trunk/modules/pvm/src/test/java/org/jbpm/pvm/internal/cal/BusinessCalendarTest.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/test/java/org/jbpm/pvm/internal/cal/BusinessCalendarTest.java	2010-08-15 03:39:21 UTC (rev 6592)
+++ jbpm4/trunk/modules/pvm/src/test/java/org/jbpm/pvm/internal/cal/BusinessCalendarTest.java	2010-08-15 04:31:28 UTC (rev 6593)
@@ -14,4 +14,13 @@
 
     assertEquals(end.getYear(), start.getYear());
   }
+
+  public void testWithoutSettingDays() {
+    BusinessCalendarImpl businessCalendar = new BusinessCalendarImpl();
+
+    Date start = new Date();
+    Date end = businessCalendar.subtract(start, new Duration("1 business day"));
+
+    assertEquals(end.getDay(), start.getDay());
+  }
 }



More information about the jbpm-commits mailing list