Skip to content

Instantly share code, notes, and snippets.

@lchanmann
Created September 9, 2020 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lchanmann/b3837bf7890bd1f4e6cc31ec384928fb to your computer and use it in GitHub Desktop.
Save lchanmann/b3837bf7890bd1f4e6cc31ec384928fb to your computer and use it in GitHub Desktop.
diff --git a/nanopay/src/net/nanopay/bank/BankHolidayService.js b/nanopay/src/net/nanopay/bank/BankHolidayService.js
index c3ee295550..87302738ae 100644
--- a/nanopay/src/net/nanopay/bank/BankHolidayService.js
+++ b/nanopay/src/net/nanopay/bank/BankHolidayService.js
@@ -28,7 +28,8 @@ foam.CLASS({
'java.time.ZoneId',
'java.time.ZoneOffset',
'java.util.Date',
- 'java.util.List',
+ 'java.util.HashSet',
+ 'java.util.Set',
'static foam.mlang.MLang.*'
],
@@ -53,13 +54,14 @@ foam.CLASS({
- x : Context object
- requestedDate : Requested date to check against applicable holidays
- address : For determining applicable bank holidays and weekend
- - offset : The number of business days to skip
+ - offset : The number of iterations to skip
+ - step : The number of business days to skip per iteration/step
Eg.
- skipBankHoliday(x, january1_2020, ontario, 0); // returns 2020-01-02 (Thursday)
- skipBankHoliday(x, january1_2010, ontario, 1); // returns 2020-01-03 (Friday)
- skipBankHoliday(x, january1_2010, ontario, 2); // returns 2020-01-06 (Monday) because weekend 04 and 05 are skipped
+ skipBankHoliday(x, january1_2020, ontario, 0, 1); // returns 2020-01-02 (Thursday)
+ skipBankHoliday(x, january1_2010, ontario, 1, 1); // returns 2020-01-03 (Friday)
+ skipBankHoliday(x, january1_2010, ontario, 2, 1); // returns 2020-01-06 (Monday) because weekend 04 and 05 are skipped
See more cases in BankHolidayServiceTest.
`,
@@ -77,30 +79,23 @@ foam.CLASS({
name: 'address'
},
{
- type: 'Integer',
+ type: 'Int',
name: 'offset'
+ },
+ {
+ type: 'Int',
+ name: 'step'
}
],
javaCode: `
- LocalDate localDate = requestedDate.toInstant().atZone(ZoneOffset.UTC).toLocalDate();
- DAO bankHolidayDAO = (DAO) x.get("bankHolidayDAO");
- List<Date> bankHolidayList = ((ArraySink) ((foam.mlang.sink.Map)
- bankHolidayDAO
- .where(AND(
- OR(
- AND(
- EQ(BankHoliday.COUNTRY_ID, address.getCountryId()),
- EQ(BankHoliday.REGION_ID, address.getRegionId())
- ),
- AND(
- EQ(BankHoliday.COUNTRY_ID, address.getCountryId()),
- EQ(BankHoliday.REGION_ID, "")
- )
- ),
- GTE(BankHoliday.DATE, getDate(localDate, ZoneOffset.UTC))))
- .select(MAP(BankHoliday.DATE, new ArraySink()))).getDelegate()).getArray();
+ if ( step == 0 ) {
+ return requestedDate;
+ }
+ LocalDate localDate = requestedDate.toInstant().atZone(ZoneOffset.UTC).toLocalDate();
+ Set<Date> bankHolidayList = getBankHolidays(x, address, localDate);
BankWeekend bankWeekend = findBankWeekend(address);
+
while ( true ) {
if ( ! (bankWeekend.getSaturday() && localDate.getDayOfWeek() == DayOfWeek.SATURDAY)
&& ! (bankWeekend.getSunday() && localDate.getDayOfWeek() == DayOfWeek.SUNDAY)
@@ -109,11 +104,40 @@ foam.CLASS({
) {
break;
}
- localDate = localDate.plusDays(1);
+ localDate = localDate.plusDays(step);
}
return getDate(localDate, ZoneId.systemDefault());
`
},
+ {
+ name: 'getBankHolidays',
+ type: 'Set<Date>',
+ args: [
+ { name: 'x', type: 'Context' },
+ { name: 'address', type: 'foam.nanos.auth.Address' },
+ { name: 'localDate', type: 'LocalDate' }
+ ],
+ javaCode: `
+ var bankHolidayDAO = (DAO) x.get("bankHolidayDAO");
+ var list = ((ArraySink) ((foam.mlang.sink.Map)
+ bankHolidayDAO
+ .where(AND(
+ OR(
+ AND(
+ EQ(BankHoliday.COUNTRY_ID, address.getCountryId()),
+ EQ(BankHoliday.REGION_ID, address.getRegionId())
+ ),
+ AND(
+ EQ(BankHoliday.COUNTRY_ID, address.getCountryId()),
+ EQ(BankHoliday.REGION_ID, "")
+ )
+ ),
+ GTE(BankHoliday.DATE, getDate(localDate.minusMonths(6), ZoneOffset.UTC)),
+ LTE(BankHoliday.DATE, getDate(localDate.plusMonths(6), ZoneOffset.UTC))))
+ .select(MAP(BankHoliday.DATE, new ArraySink()))).getDelegate()).getArray();
+ return new HashSet<>(list);
+ `
+ },
{
name: 'getDate',
type: 'Date',
@@ -158,5 +182,18 @@ foam.CLASS({
return ret;
`
}
+ ],
+
+ axioms: [
+ {
+ name: 'javaExtras',
+ buildJavaClass: function(cls) {
+ cls.extras.push(`
+ public Date skipBankHolidays(foam.core.X x, Date requestedDate, Address address, int offset) {
+ skipBankHolidays(x, requestedDate, address, offset, 1);
+ }
+ `);
+ }
+ }
]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment