Example usage for org.joda.time DateTime withDate

List of usage examples for org.joda.time DateTime withDate

Introduction

In this page you can find the example usage for org.joda.time DateTime withDate.

Prototype

public DateTime withDate(LocalDate date) 

Source Link

Document

Returns a copy of this datetime with the specified date, retaining the time fields.

Usage

From source file:com.sonicle.webtop.calendar.CalendarManager.java

License:Open Source License

private <T> List<T> calculateRecurringInstances_OLD(Connection con, RecurringInstanceMapper<T> instanceMapper,
        DateTime fromDate, DateTime toDate, DateTimeZone userTimezone, int limit) throws WTException {
    RecurrenceDAO recDao = RecurrenceDAO.getInstance();
    RecurrenceBrokenDAO recbDao = RecurrenceBrokenDAO.getInstance();
    ArrayList<T> instances = new ArrayList<>();

    int eventId = instanceMapper.getEventId();
    DateTime eventStartDate = instanceMapper.getEventStartDate();
    DateTime eventEndDate = instanceMapper.getEventEndDate();

    // Retrieves reccurence and broken dates (if any)
    ORecurrence orec = recDao.selectByEvent(con, eventId);
    if (orec == null) {
        logger.warn("Unable to retrieve recurrence for event [{}]", eventId);

    } else {/*w  w  w .ja va  2s. co m*/
        if (fromDate == null)
            fromDate = orec.getStartDate();
        if (toDate == null)
            toDate = orec.getStartDate().plusYears(1);

        /*
        List<ORecurrenceBroken> obrecs = recbDao.selectByEventRecurrence(con, eventId, orec.getRecurrenceId());
        //TODO: ritornare direttamente l'hashmap da jooq
        // Builds a hashset of broken dates for increasing performances
        HashMap<String, ORecurrenceBroken> brokenDates = new HashMap<>();
        for (ORecurrenceBroken obrec : obrecs) {
           brokenDates.put(obrec.getEventDate().toString(), obrec);
        }
        */

        Map<LocalDate, ORecurrenceBroken> obrecs = recbDao.selectByEventRecurrence(con, eventId,
                orec.getRecurrenceId());
        HashSet<String> brokenDates = new HashSet<>();
        for (LocalDate ld : obrecs.keySet()) {
            brokenDates.add(ld.toString());
        }

        try {
            // Calculate event length in order to generate events like original one
            int eventDays = CalendarUtils.calculateLengthInDays(eventStartDate, eventEndDate);
            RRule rr = new RRule(orec.getRule());

            // Calcutate recurrence set for required dates range
            PeriodList periods = ICal4jUtils.calculateRecurrenceSet(eventStartDate, eventEndDate,
                    orec.getStartDate(), rr, fromDate, toDate, userTimezone);

            // Recurrence start is useful to skip undesired dates at beginning.
            // If event does not starts at recurrence real beginning (eg. event
            // start on MO but first recurrence begin on WE), ical4j lib includes 
            // those dates in calculated recurrence set, as stated in RFC 
            // (http://tools.ietf.org/search/rfc5545#section-3.8.5.3).
            LocalDate rrStart = ICal4jUtils
                    .calculateRecurrenceStart(orec.getStartDate(), rr.getRecur(), userTimezone).toLocalDate();
            //LocalDate rrStart = ICal4jUtils.calculateRecurrenceStart(eventStartDate, rr.getRecur(), userTimezone).toLocalDate(); //TODO: valutare se salvare la data gi aggiornata
            LocalDate rrEnd = orec.getUntilDate().toLocalDate();

            // Iterates returned recurring periods and builds cloned events...
            int count = -1;
            for (net.fortuna.ical4j.model.Period per : (Iterable<net.fortuna.ical4j.model.Period>) periods) {
                count++;
                if ((limit != -1) && (count > limit))
                    break;
                final LocalDate perStart = ICal4jUtils.toJodaDateTime(per.getStart()).toLocalDate();
                final LocalDate perEnd = ICal4jUtils.toJodaDateTime(per.getEnd()).toLocalDate();

                if (brokenDates.contains(perStart.toString()))
                    continue; // Skip broken dates...
                if ((perStart.compareTo(rrStart) >= 0) && (perEnd.compareTo(rrEnd) <= 0)) { // Skip unwanted dates at beginning
                    final DateTime newStart = eventStartDate.withDate(perStart);
                    final DateTime newEnd = eventEndDate.withDate(newStart.plusDays(eventDays).toLocalDate());
                    final String key = EventKey.buildKey(eventId, eventId, perStart);

                    instances.add(instanceMapper.createInstance(key, newStart, newEnd));
                }
            }

        } catch (DAOException ex) {
            throw wrapException(ex);
        } catch (ParseException ex) {
            throw new WTException(ex, "Unable to parse rrule");
        }
    }
    return instances;
}