Another scripting example to share:
A few forum members have asked about how to calculate dates based on Business Calendars / Business Hours/Days:
Example: M-F 8am-5pm are working hours. If something is due “Next Day”, and it’s currently a Friday, it would be due by Monday at 5pm. BUT if Monday is a holiday, then it would be due on Tuesday by 5pm. So how do we calculate this, and how do we do it without having to write a lot of code.
How do we determine if the current DateTime is within Business Hours, where Business Hours are based on a complex schedule + Holidays.
How do we add and subtract business days. etc, etc.
There are some Business Calendar libraries out there, but many of them are very poorly documented (I am talking to you jBPM!!!).
There are two that I found that looked promising:
- GitHub - dhatim/business-hours-java: Java 8 implementation of Business Hours
- GitHub - olejnikk/bizdays: Java date and time calculator for operations on business days and working time
GitHub - dhatim/business-hours-java: Java 8 implementation of Business Hours looks especially interesting because it will generate Cron Expressions, and so this can be really interesting usage for BPMN Timer events for Cycles.
For these libraries to work, you need to use Java 8. So we deploy Camunda 7.8
We have a dockerfile for this:
FROM camunda/camunda-bpm-platform:tomcat-7.8.0
# Copy third-party Java libraries
COPY docker/camunda/lib/* /camunda/lib/
Where we are coping in the following .jar
file: camunda-variations/business-hours.jar at master · DigitalState/camunda-variations · GitHub
This is the GitHub - dhatim/business-hours-java: Java 8 implementation of Business Hours library.
Then it is as simple as running a Javascript function:
with (new JavaImporter(org.dhatim.businesshours, java.time))
{
var businessHours = new BusinessHours("wday{Mon-Fri} hour{9am-6pm}, wday{Sat} hour{9am-12pm}");
var open = businessHours.isOpen(LocalDateTime.now());
}
execution.setVariable('isOpen', open)
This example is a modification from the examples in: GitHub - dhatim/business-hours-java: Java 8 implementation of Business Hours readme.
Check out the Github Repos for all of the different usage examples.
You can full source code examples in the Business-Calendar folder:
Further Use Cases
- Loading Business Hours/Days schedule from a file deployed along with the BPMN
- Using DMN to provide business calendar schedules
- Conditional Business Schedules based on DMN
- Gateway Logic scripts that change flow based on if its currently working hours or if its a holiday
- Loading Business Hours/Days schedules from a third-party URL
- Calculating future dates based on the planned during of a task (if something takes 6 business days (8 hours per day)(48 hours) to complete, and it is currently Wed, then the due date is the following Wed (even though its 8 days away).
- Calculation of number of business days between two dates.
- Working Schedules based on User’s personal working schedule
- Tie in with Work Assignment logic to choose the current working Staffer.
- Run some process(es) at the start / opening of each business day and end of each business day. and then runs some different processes on holidays