private List<ShiftHour> mergeOverlappedHours(final List<ShiftHour> hours) { if (hours.size() < 2) { return hours; } List<ShiftHour> mergedHours = Lists.newArrayList(); ShiftHour currentHour = hours.get(0); for (int i = 1; i < hours.size(); i++) { if (currentHour.getDateTo().before(hours.get(i).getDateFrom())) { mergedHours.add(currentHour); currentHour = hours.get(i); } else if (currentHour.getDateTo().before(hours.get(i).getDateTo())) { currentHour = new ShiftHour(currentHour.getDateFrom(), hours.get(i).getDateTo()); } } mergedHours.add(currentHour); return mergedHours; }
if (hour.getDateFrom().compareTo(to) >= 0) { continue; if (hour.getDateTo().compareTo(from) <= 0) { continue; if (hour.getDateTo().compareTo(to) <= 0 && hour.getDateFrom().compareTo(from) >= 0) { hoursToRemove.add(hour); if (hour.getDateTo().compareTo(to) >= 0 && hour.getDateFrom().compareTo(from) >= 0) { hoursToRemove.add(hour); hoursToAdd.add(new ShiftHour(to, hour.getDateTo())); if (hour.getDateTo().compareTo(to) <= 0 && hour.getDateFrom().compareTo(from) <= 0) { hoursToRemove.add(hour); hoursToAdd.add(new ShiftHour(hour.getDateFrom(), from)); if (hour.getDateTo().compareTo(to) >= 0 && hour.getDateFrom().compareTo(from) <= 0) { hoursToRemove.add(hour); hoursToAdd.add(new ShiftHour(hour.getDateFrom(), from)); hoursToAdd.add(new ShiftHour(to, hour.getDateTo()));
@Override public Date findDateFromForOrder(final Date dateTo, final long seconds) { if (dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_SHIFT).find().list() .getTotalNumberOfEntities() == 0) { return null; } long stop = dateTo.getTime(); long remaining = seconds; while (remaining >= 0) { List<ShiftHour> hours = getHoursForAllShifts(new Date(stop - STEP), new Date(stop)); for (int i = hours.size() - 1; i >= 0; i--) { ShiftHour hour = hours.get(i); long diff = (hour.getDateTo().getTime() - hour.getDateFrom().getTime()) / 1000; if (diff >= remaining) { return new Date(hour.getDateTo().getTime() - (remaining * 1000)); } else { remaining -= diff; } } stop -= STEP; if (stop < MIN_TIMESTAMP) { return null; } } return null; }
@Override public Date findDateToForOrder(final Date dateFrom, final long seconds) { if (dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_SHIFT).find().list() .getTotalNumberOfEntities() == 0) { return null; } long start = dateFrom.getTime(); long remaining = seconds; while (remaining >= 0) { List<ShiftHour> hours = getHoursForAllShifts(new Date(start), new Date(start + STEP)); for (ShiftHour hour : hours) { long diff = (hour.getDateTo().getTime() - hour.getDateFrom().getTime()) / 1000; if (diff >= remaining) { return new Date(hour.getDateFrom().getTime() + (remaining * 1000)); } else { remaining -= diff; } } start += STEP; if (start > MAX_TIMESTAMP) { return null; } } return null; }
private Collection<ShiftHour> getHourForDay(final Entity shift, final Date dateFrom, final Date dateTo, final String day, final int offset) { if ((Boolean) shift.getField(day + WORKING_LITERAL) && StringUtils.hasText(shift.getStringField(day + HOURS_LITERAL))) { List<ShiftHour> hours = Lists.newArrayList(); LocalTime[][] dayHours = convertDayHoursToInt(shift.getStringField(day + HOURS_LITERAL)); DateTime from = new DateTime(dateFrom).withSecondOfMinute(0); DateTime to = new DateTime(dateTo); DateTime current = from.plusDays(offset - from.getDayOfWeek()); if (current.compareTo(from) < 0) { current = current.plusDays(7); } while (current.compareTo(to) <= 0) { for (LocalTime[] dayHour : dayHours) { hours.add(new ShiftHour(current.withHourOfDay(dayHour[0].getHourOfDay()) .withMinuteOfHour(dayHour[0].getMinuteOfHour()).toDate(), current .withHourOfDay(dayHour[1].getHourOfDay()).withMinuteOfHour(dayHour[1].getMinuteOfHour()).toDate())); } current = current.plusDays(7); } return hours; } else { return Collections.emptyList(); } }
private List<ShiftHour> removeHoursOutOfRange(final List<ShiftHour> hours, final Date dateFrom, final Date dateTo) { List<ShiftHour> list = Lists.newArrayList(); for (ShiftHour hour : hours) { if (hour.getDateTo().compareTo(dateFrom) <= 0 || hour.getDateFrom().compareTo(dateTo) >= 0) { continue; } if (hour.getDateFrom().compareTo(dateFrom) >= 0 && hour.getDateTo().compareTo(dateTo) <= 0) { list.add(hour); } else if (hour.getDateFrom().compareTo(dateFrom) < 0 && hour.getDateTo().compareTo(dateTo) > 0) { list.add(new ShiftHour(dateFrom, dateTo)); } else if (hour.getDateFrom().compareTo(dateFrom) < 0 && hour.getDateTo().compareTo(dateTo) <= 0) { list.add(new ShiftHour(dateFrom, hour.getDateTo())); } else if (hour.getDateFrom().compareTo(dateFrom) >= 0 && hour.getDateTo().compareTo(dateTo) > 0) { list.add(new ShiftHour(hour.getDateFrom(), dateTo)); } } return list; }
private void addWorkTimeExceptions(final List<ShiftHour> hours, final List<Entity> exceptions) { for (Entity exception : exceptions) { if (!"02workTime".equals(exception.getStringField(TYPE_FIELD))) { continue; } Date from = (Date) exception.getField(FROM_DATE_FIELD); Date to = (Date) exception.getField(TO_DATE_FIELD); hours.add(new ShiftHour(from, to)); } }
private Set<Interval> getWorkIntervals(final TimeGapsContext context) { Interval searchInterval = context.getInterval(); Date fromDate = searchInterval.getStart().toDate(); Date toDate = searchInterval.getEnd().toDate(); List<ShiftHour> shiftHours = shiftsService.getHoursForAllShifts(fromDate, toDate); Set<Interval> shiftWorkTimeIntervals = Sets.newHashSet(); for (ShiftHour shiftHour : shiftHours) { Interval shiftWorkTimeInterval = new Interval(shiftHour.getDateFrom().getTime(), shiftHour.getDateTo().getTime()); shiftWorkTimeIntervals.add(shiftWorkTimeInterval); } return shiftWorkTimeIntervals; }
@Override public int compare(final ShiftHour o1, final ShiftHour o2) { int i = o1.getDateFrom().compareTo(o2.getDateFrom()); if (i == 0) { return o1.getDateTo().compareTo(o2.getDateTo()); } else { return i; } }
private BigDecimal getWorkedHoursOfWorker(final Entity shift, final DateTime dateOfDay) { BigDecimal hours = BigDecimal.ZERO; List<ShiftHour> workedHours = shiftsService.getHoursForShift(shift, dateOfDay.toDate(), dateOfDay.plusDays(1).toDate()); for (ShiftHour shiftHour : workedHours) { DateTime dateFrom = new DateTime(shiftHour.getDateFrom()); DateTime dateTo = new DateTime(shiftHour.getDateTo()); Period p = new Period(dateFrom, dateTo); hours = hours.add(new BigDecimal(p.getHours())); } return hours; }
@Override public List<GanttChartItem> getItemsForShift(final Entity shift, final GanttChartScale scale) { String shiftName = shift.getStringField("name"); List<ShiftHour> hours = shiftsService.getHoursForShift(shift, scale.getDateFrom(), scale.getDateTo()); List<GanttChartItem> items = new ArrayList<GanttChartItem>(); for (ShiftHour hour : hours) { items.add(scale.createGanttChartItem(shiftName, shiftName, null, hour.getDateFrom(), hour.getDateTo())); } return items; }