I have a reference shift shift1
. From the date and time of shift1
, we have to calculate the sum of all shift durations for the past 7 days. The explanation of the variables are as follows:
RuleSetting -> It is a problem fact used on or off the rule.
shift1 -> It is a problem fact used as a reference point. From this shift other shift’s work duration is summed up. The shift1 also will be included.
shift2 -> It is a problem fact that has been used for the last 7 days’ shifts which will sum up the duration.
foreignerType-> It is a problem fact that is used for settings for foreign employees.
Constraint workingHoursGreaterThanMaximumForForeignerPerWeek(ConstraintFactory constraintFactory) {
return constraintFactory.forEach(RuleSetting.class)
.filter(ruleSetting -> ruleSetting.getRuleName().equals("E016") && ruleSetting.getIsActive() == true)
.join(Shift.class)
.join(Shift.class)
.filter((ruleSetting, shift1, shift2) -> shift1.getEmployee() != null
&& shift2.getEmployee() != null
&& shift1.getEmployee().equals(shift2.getEmployee())
&& (shift1.getStartDateTime() != null && shift2.getStartDateTime() != null)
&& (shift1.getStartDateTime().isAfter(shift2.getStartDateTime()) || shift1.getStartDateTime().isEqual(shift2.getStartDateTime()))
&& (shift1.getEmployee().getForeignerType() != null)
&& Math.abs(ShiftUtil.differenceInDays(shift1, shift2)) < 7)
.join(ForeignerType.class)
.filter((ruleSetting, shift1, shift2, foreignerType) -> foreignerType.getForeignerType().equals(shift1.getEmployee().getForeignerType()))
.groupBy((ruleSetting, shift1, shift2, foreignerType)->ruleSetting,
(ruleSetting, shift1, shift2, foreignerType)->shift2.getEmployee(),
(ruleSetting, shift1, shift2, foreignerType)->shift1,
// My understanding is the above line is wrong. But I need the shift1 in the later part of the code.
ConstraintCollectors.sumLong((ruleSetting, shift1, shift2, foreignerType)->shift2.getWorkMinutes()))
.map((ruleSetting, employee, shift1, totalDuration) -> new QuadTuple<RuleSetting, Employee, Shift, Long>(ruleSetting, employee, shift1, totalDuration))
.join(ForeignerType.class)
.filter((quad, foreignerType) -> quad.getB().getForeignerType() != null
&& foreignerType.getForeignerType().equals(quad.getB().getForeignerType())
&& quad.getD() > foreignerType.getMaxMinutesInWeek())
.penalizeConfigurable((quad, foreignerType)->{
Double result = Math.abs(((double)quad.getD() / (double) foreignerType.getMaxMinutesInWeek()));
result = result * Math.abs(quad.getA().getWeight());
return result.intValue();
})
.indictWith((quad, foreignerType) ->{
EmployeeScoreKeyItems res = new EmployeeScoreKeyItems(EmployeeScoreKeyItems.KeyType.BY_DAY,
quad.getA().getRuleName(),
quad.getB().getEmployeeId(),
null, quad.getC().getBusinessDate(), null, null);
return List.of(res);
})
.asConstraint("E016");
}
So, how can we down stream the shift1 to lower part of the code. Please check the commented line inside the code. And also get the summed up duration for the shift for past 7 days.