com.liferay.calendar.util.RecurrenceUtil Maven / Gradle / Ivy
The newest version!
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.calendar.util;
import com.google.ical.iter.RecurrenceIterator;
import com.google.ical.iter.RecurrenceIteratorFactory;
import com.google.ical.util.TimeUtils;
import com.google.ical.values.DateValue;
import com.google.ical.values.DateValueImpl;
import com.liferay.calendar.model.CalendarBooking;
import com.liferay.calendar.recurrence.Frequency;
import com.liferay.calendar.recurrence.PositionalWeekday;
import com.liferay.calendar.recurrence.Recurrence;
import com.liferay.calendar.recurrence.Weekday;
import com.liferay.calendar.util.comparator.CalendarBookingStartTimeComparator;
import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.HashMapBuilder;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.StringUtil;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
/**
* @author Marcellus Tavares
*/
public class RecurrenceUtil {
public static List expandCalendarBooking(
CalendarBooking calendarBooking, long startTime, long endTime,
int maxSize) {
return expandCalendarBooking(
calendarBooking, startTime, endTime, calendarBooking.getTimeZone(),
maxSize);
}
public static List expandCalendarBooking(
CalendarBooking calendarBooking, long startTime, long endTime,
TimeZone displayTimeZone, int maxSize) {
List expandedCalendarBookings = new ArrayList<>();
try {
CalendarBookingIterator calendarBookingIterator =
new CalendarBookingIterator(calendarBooking, displayTimeZone);
while (calendarBookingIterator.hasNext()) {
CalendarBooking newCalendarBooking =
calendarBookingIterator.next();
if (newCalendarBooking.getEndTime() < startTime) {
continue;
}
if (newCalendarBooking.getStartTime() > endTime) {
break;
}
expandedCalendarBookings.add(newCalendarBooking);
if ((maxSize > 0) &&
(expandedCalendarBookings.size() >= maxSize)) {
break;
}
}
}
catch (ParseException parseException) {
_log.error("Unable to parse data ", parseException);
}
return expandedCalendarBookings;
}
public static List expandCalendarBookings(
List calendarBookings, long startTime, long endTime) {
return expandCalendarBookings(calendarBookings, startTime, endTime, 0);
}
public static List expandCalendarBookings(
List calendarBookings, long startTime, long endTime,
int maxSize) {
List expandedCalendarBookings = new ArrayList<>();
for (CalendarBooking calendarBooking : calendarBookings) {
List expandedCalendarBooking =
expandCalendarBooking(
calendarBooking, startTime, endTime, maxSize);
expandedCalendarBookings.addAll(expandedCalendarBooking);
}
return expandedCalendarBookings;
}
public static List expandCalendarBookings(
List calendarBookings, long startTime, long endTime,
TimeZone displayTimeZone) {
return expandCalendarBookings(
calendarBookings, startTime, endTime, displayTimeZone, 0);
}
public static List expandCalendarBookings(
List calendarBookings, long startTime, long endTime,
TimeZone displayTimeZone, int maxSize) {
List expandedCalendarBookings = new ArrayList<>();
for (CalendarBooking calendarBooking : calendarBookings) {
List expandedCalendarBooking =
expandCalendarBooking(
calendarBooking, startTime, endTime, displayTimeZone,
maxSize);
expandedCalendarBookings.addAll(expandedCalendarBooking);
}
return expandedCalendarBookings;
}
public static CalendarBooking getCalendarBookingInstance(
CalendarBooking calendarBooking, int instanceIndex) {
try {
CalendarBookingIterator calendarBookingIterator =
new CalendarBookingIterator(calendarBooking);
while (calendarBookingIterator.hasNext()) {
CalendarBooking calendarBookingInstance =
calendarBookingIterator.next();
if (calendarBookingInstance.getInstanceIndex() ==
instanceIndex) {
return calendarBookingInstance;
}
}
}
catch (ParseException parseException) {
_log.error("Unable to parse data ", parseException);
}
return null;
}
public static int getIndexOfInstance(
String recurrence, long recurrenceStartTime, long instanceStartTime) {
int count = 0;
DateValue instanceDateValue = _toDateValue(instanceStartTime);
try {
RecurrenceIterator recurrenceIterator =
RecurrenceIteratorFactory.createRecurrenceIterator(
recurrence, _toDateValue(recurrenceStartTime),
TimeUtils.utcTimezone());
while (recurrenceIterator.hasNext()) {
DateValue dateValue = recurrenceIterator.next();
if (dateValue.compareTo(instanceDateValue) >= 0) {
break;
}
count++;
}
}
catch (ParseException parseException) {
_log.error("Unable to parse data ", parseException);
}
return count;
}
public static CalendarBooking getLastInstanceCalendarBooking(
List calendarBookings) {
calendarBookings = ListUtil.sort(
calendarBookings,
CalendarBookingStartTimeComparator.getInstance(false));
CalendarBooking lastCalendarBooking = calendarBookings.get(0);
long lastStartTime = 0;
for (CalendarBooking calendarBooking : calendarBookings) {
Recurrence recurrence = calendarBooking.getRecurrenceObj();
if (recurrence == null) {
continue;
}
if (!hasLimit(recurrence)) {
lastCalendarBooking = calendarBooking;
break;
}
CalendarBooking lastCalendarBookingInstance =
getLastCalendarBookingInstance(calendarBooking);
if (lastCalendarBookingInstance.getStartTime() > lastStartTime) {
lastStartTime = lastCalendarBookingInstance.getStartTime();
lastCalendarBooking = calendarBooking;
}
}
return lastCalendarBooking;
}
public static String getSummary(
CalendarBooking calendarBooking, Recurrence recurrence) {
if (recurrence == null) {
return LanguageUtil.get(
LocaleUtil.getMostRelevantLocale(), "false");
}
List