I have a shift class, that returns the information for the shift.
public class Shift {
@PlanningId
private String id;
private LocalDateTime start;
private LocalDateTime end;
public Long getShiftDuration() {
long minutes = ChronoUnit.MINUTES.between(start, end);
return minutes;
}
public String getBusinessDate() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateStr = getStart().toLocalDate().format(formatter);
return dateStr;
}
}
Then I have a class DaySettingFlat
, that contains the list of dates that we need to calculate sum of shift durations. The date mentioned in DaySettingFlat
contains a variable businessDate
which will contain a date. With this date value we have to sum the durations of the Shift collection.
public class DaySettingFlat {
String businessDate;
Integer sumOfShiftDuration;
}
The both class is registered as @ProblemFactCollectionProperty
. Now for each DaySettingFlat entry, we have to sum the duration of the shifts for a date stored in businessDate
. Both the Shift
and DaySettingFlat
are used as a collection.
I tried like follows but it’s not working:
Constraint daySettingStandardNumberOfHours(ConstraintFactory constraintFactory) {
return constraintFactory.forEach(DaySettingFlat.class)
.forEach(Shift.class)
.groupBy((daySetting, shift)->
Shift::getStartDate,
ConstraintCollectors.toList(),
ConstraintCollectors.sumLong(Shift::getShiftDuration))
.penalize((daySetting, shift)HardMediumSoftScore.ONE_HARD, (shift, date, list, totalDuration){
});
return totalDuration;
}