Search in sources :

Example 6 with TimeRange

use of com.qcadoo.commons.dateTime.TimeRange in project mes by qcadoo.

the class ShiftsServiceImpl method getNearestWorkingDateForShiftWorkTime.

private void getNearestWorkingDateForShiftWorkTime(final TimeRange shiftWorkTime, final DateTime dateFrom, final DateTime currentDate, final List<DateTimeRange> workTimes) {
    LocalTime currentTime = currentDate.toLocalTime();
    LocalTime timeTo = shiftWorkTime.getTo();
    LocalTime timeFrom = shiftWorkTime.getFrom();
    if (!currentDate.equals(dateFrom.minusDays(1)) || timeFrom.isAfter(timeTo)) {
        if (timeFrom.isAfter(timeTo) && currentDate.equals(dateFrom.minusDays(1))) {
            if (currentTime.compareTo(LocalTime.MIDNIGHT) >= 0 && currentTime.compareTo(timeTo) <= 0) {
                Optional<Interval> interval = createInterval(currentDate.plusDays(1), currentTime, timeTo);
                interval.ifPresent(i -> workTimes.add(new DateTimeRange(i)));
            }
        } else {
            if (currentDate.equals(dateFrom)) {
                if (timeFrom.compareTo(currentTime) < 0 && timeTo.compareTo(currentTime) > 0) {
                    Optional<Interval> interval = createInterval(currentDate, currentTime, timeTo);
                    interval.ifPresent(i -> workTimes.add(new DateTimeRange(i)));
                } else if (timeFrom.isAfter(timeTo)) {
                    if (timeFrom.compareTo(currentTime) < 0) {
                        Optional<Interval> interval = createInterval(currentDate, currentTime, timeTo);
                        interval.ifPresent(i -> workTimes.add(new DateTimeRange(i)));
                    } else {
                        Optional<Interval> interval = createInterval(currentDate, timeFrom, timeTo);
                        interval.ifPresent(i -> workTimes.add(new DateTimeRange(i)));
                    }
                } else if (timeFrom.compareTo(currentTime) >= 0) {
                    Optional<Interval> interval = createInterval(currentDate, timeFrom, timeTo);
                    interval.ifPresent(i -> workTimes.add(new DateTimeRange(i)));
                }
            } else {
                Optional<Interval> interval = createInterval(currentDate, timeFrom, timeTo);
                interval.ifPresent(i -> workTimes.add(new DateTimeRange(i)));
            }
        }
    }
}
Also used : DataDefinitionService(com.qcadoo.model.api.DataDefinitionService) DateTimeZone(org.joda.time.DateTimeZone) ShiftFields(com.qcadoo.mes.basic.constants.ShiftFields) Date(java.util.Date) ComponentState(com.qcadoo.view.api.ComponentState) QcadooViewConstants(com.qcadoo.view.constants.QcadooViewConstants) LocalTime(org.joda.time.LocalTime) Autowired(org.springframework.beans.factory.annotation.Autowired) Shift(com.qcadoo.mes.basic.shift.Shift) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) Interval(org.joda.time.Interval) Lists(com.google.common.collect.Lists) Calendar(java.util.Calendar) BasicConstants(com.qcadoo.mes.basic.constants.BasicConstants) Service(org.springframework.stereotype.Service) FormComponent(com.qcadoo.view.api.components.FormComponent) Map(java.util.Map) Days(org.joda.time.Days) TimetableExceptionType(com.qcadoo.mes.basic.constants.TimetableExceptionType) LinkedList(java.util.LinkedList) Period(org.joda.time.Period) SearchRestrictions(com.qcadoo.model.api.search.SearchRestrictions) DateTime(org.joda.time.DateTime) IllegalFieldValueException(org.joda.time.IllegalFieldValueException) Maps(com.google.common.collect.Maps) DataDefinition(com.qcadoo.model.api.DataDefinition) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Entity(com.qcadoo.model.api.Entity) List(java.util.List) DateTimeRange(com.qcadoo.mes.basic.util.DateTimeRange) FieldComponent(com.qcadoo.view.api.components.FieldComponent) Optional(java.util.Optional) TimeRange(com.qcadoo.commons.dateTime.TimeRange) Comparator(java.util.Comparator) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) LocalTime(org.joda.time.LocalTime) Optional(java.util.Optional) DateTimeRange(com.qcadoo.mes.basic.util.DateTimeRange) Interval(org.joda.time.Interval)

Example 7 with TimeRange

use of com.qcadoo.commons.dateTime.TimeRange in project mes by qcadoo.

the class Shift method findWorkTimeAt.

/**
 * Returns date range containing given date. This method IS AWARE of timetable exceptions.
 *
 * <b>Be aware</b> - this method doesn't compose returned date range with the timetable exclusions/inclusions. This means that
 * if you have a shift which works at Monday from 8:00-16:00 and there is defined work time exclusion from 12:00-20:00 and you
 * ask for 10:00 then you will get date range from 8:00-16:00 (as in plan). But if you ask for 14:00 you will get
 * Optional.absent().
 *
 * @param date
 *            date with time for which work dates range you want to find.
 * @return
 */
public Optional<DateRange> findWorkTimeAt(final Date date) {
    if (timetableExceptions.hasFreeTimeAt(date)) {
        return Optional.absent();
    }
    DateTime dateTime = new DateTime(date);
    Optional<TimeRange> maybeTimeRangeFromPlan = findWorkTimeAt(dateTime.getDayOfWeek(), dateTime.toLocalTime());
    for (TimeRange timeRangeFromPlan : maybeTimeRangeFromPlan.asSet()) {
        return Optional.of(buildDateRangeFrom(timeRangeFromPlan, date));
    }
    return timetableExceptions.findDateRangeFor(TimetableExceptionType.WORK_TIME, date);
}
Also used : TimeRange(com.qcadoo.commons.dateTime.TimeRange) DateTime(org.joda.time.DateTime)

Example 8 with TimeRange

use of com.qcadoo.commons.dateTime.TimeRange in project mes by qcadoo.

the class WorkingHours method stringToInterval.

private TimeRange stringToInterval(final String hoursRange) {
    String[] lowerUpperBound = StringUtils.split(hoursRange, '-');
    LocalTime lower = LocalTime.parse(lowerUpperBound[0], TIME_FORMATTER);
    LocalTime upper = LocalTime.parse(lowerUpperBound[1], TIME_FORMATTER);
    return new TimeRange(lower, upper);
}
Also used : TimeRange(com.qcadoo.commons.dateTime.TimeRange) LocalTime(org.joda.time.LocalTime)

Example 9 with TimeRange

use of com.qcadoo.commons.dateTime.TimeRange in project mes by qcadoo.

the class WorkingHoursTest method shouldBuildWorkingHoursFromSingleTimeRangeWithTwoDigitsHour.

@Test
public final void shouldBuildWorkingHoursFromSingleTimeRangeWithTwoDigitsHour() {
    // given
    String timeRangeString = "09:00-17:00";
    // when
    WorkingHours workingHours = new WorkingHours(timeRangeString);
    // then
    assertEquals(1, workingHours.getTimeRanges().size());
    TimeRange timeRange = workingHours.getTimeRanges().iterator().next();
    assertEquals(new LocalTime(9, 0, 0), timeRange.getFrom());
    assertEquals(new LocalTime(17, 0, 0), timeRange.getTo());
}
Also used : TimeRange(com.qcadoo.commons.dateTime.TimeRange) LocalTime(org.joda.time.LocalTime) Test(org.junit.Test)

Example 10 with TimeRange

use of com.qcadoo.commons.dateTime.TimeRange in project mes by qcadoo.

the class WorkingHoursTest method shouldBuildWorkingHoursFromSingleTimeRangeWithOneDigitsHour.

@Test
public final void shouldBuildWorkingHoursFromSingleTimeRangeWithOneDigitsHour() {
    // given
    String timeRangeString = "9:00-17:00";
    // when
    WorkingHours workingHours = new WorkingHours(timeRangeString);
    // then
    assertEquals(1, workingHours.getTimeRanges().size());
    TimeRange timeRange = workingHours.getTimeRanges().iterator().next();
    assertEquals(new LocalTime(9, 0, 0), timeRange.getFrom());
    assertEquals(new LocalTime(17, 0, 0), timeRange.getTo());
}
Also used : TimeRange(com.qcadoo.commons.dateTime.TimeRange) LocalTime(org.joda.time.LocalTime) Test(org.junit.Test)

Aggregations

TimeRange (com.qcadoo.commons.dateTime.TimeRange)19 DateTime (org.joda.time.DateTime)12 LocalTime (org.joda.time.LocalTime)12 Shift (com.qcadoo.mes.basic.shift.Shift)10 Entity (com.qcadoo.model.api.Entity)6 LocalDate (org.joda.time.LocalDate)5 DateTimeRange (com.qcadoo.mes.basic.util.DateTimeRange)4 Date (java.util.Date)4 Test (org.junit.Test)4 Lists (com.google.common.collect.Lists)2 Calendar (java.util.Calendar)2 Collections (java.util.Collections)2 List (java.util.List)2 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)2 Service (org.springframework.stereotype.Service)2 Function (com.google.common.base.Function)1 Optional (com.google.common.base.Optional)1 FluentIterable (com.google.common.collect.FluentIterable)1 Maps (com.google.common.collect.Maps)1 FluentOptional (com.qcadoo.commons.functional.FluentOptional)1