Example usage for com.liferay.portal.kernel.util StringPool UTC

List of usage examples for com.liferay.portal.kernel.util StringPool UTC

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util StringPool UTC.

Prototype

String UTC

To view the source code for com.liferay.portal.kernel.util StringPool UTC.

Click Source Link

Usage

From source file:com.liferay.calendar.util.CalendarBookingIterator.java

License:Open Source License

private TimeZone _getTimeZone(CalendarBooking calendarBooking) {
    try {/*from w  ww  .  j  av  a2 s . c o m*/
        if (calendarBooking.isAllDay()) {
            return TimeZone.getTimeZone(StringPool.UTC);
        }

        return calendarBooking.getTimeZone();
    } catch (Exception e) {
        if (_log.isWarnEnabled()) {
            _log.warn(e, e);
        }
    }

    return TimeZoneUtil.getDefault();
}

From source file:com.liferay.calendar.util.CalendarUtil.java

License:Open Source License

public static JSONObject toCalendarBookingJSONObject(ThemeDisplay themeDisplay, CalendarBooking calendarBooking,
        TimeZone timeZone) {/*  w w  w  .  ja va 2  s .  c o m*/

    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

    jsonObject.put("allDay", calendarBooking.isAllDay());
    jsonObject.put("calendarBookingId", calendarBooking.getCalendarBookingId());
    jsonObject.put("calendarId", calendarBooking.getCalendarId());
    jsonObject.put("description", calendarBooking.getDescription(themeDisplay.getLocale()));

    if (calendarBooking.isAllDay()) {
        timeZone = TimeZone.getTimeZone(StringPool.UTC);
    }

    java.util.Calendar endTimeJCalendar = JCalendarUtil.getJCalendar(calendarBooking.getEndTime(), timeZone);

    _addTimeProperties(jsonObject, "endTime", endTimeJCalendar);

    jsonObject.put("firstReminder", calendarBooking.getFirstReminder());
    jsonObject.put("firstReminderType", calendarBooking.getFirstReminderType());

    List<CalendarBooking> childCalendarBookings = calendarBooking.getChildCalendarBookings();

    jsonObject.put("hasChildCalendarBookings", childCalendarBookings.size() > 1);

    jsonObject.put("instanceIndex", calendarBooking.getInstanceIndex());
    jsonObject.put("location", calendarBooking.getLocation());
    jsonObject.put("parentCalendarBookingId", calendarBooking.getParentCalendarBookingId());
    jsonObject.put("recurrence", calendarBooking.getRecurrence());
    jsonObject.put("secondReminder", calendarBooking.getSecondReminder());
    jsonObject.put("secondReminderType", calendarBooking.getSecondReminder());

    java.util.Calendar startTimeJCalendar = JCalendarUtil.getJCalendar(calendarBooking.getStartTime(),
            timeZone);

    _addTimeProperties(jsonObject, "startTime", startTimeJCalendar);

    jsonObject.put("status", calendarBooking.getStatus());
    jsonObject.put("title", calendarBooking.getTitle(themeDisplay.getLocale()));

    return jsonObject;
}

From source file:com.liferay.calendar.web.internal.portlet.CalendarPortlet.java

License:Open Source License

protected CalendarBooking updateCalendarBooking(long calendarBookingId, Calendar calendar,
        long[] childCalendarIds, Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
        String location, long startTime, long endTime, boolean allDay, Recurrence recurrence, long[] reminders,
        String[] remindersType, int instanceIndex, boolean updateInstance, boolean allFollowing,
        ServiceContext serviceContext) throws PortalException {

    CalendarBooking calendarBooking = _calendarBookingService.fetchCalendarBooking(calendarBookingId);

    TimeZone timeZone = TimeZoneUtil.getTimeZone(StringPool.UTC);

    if (!allDay) {
        timeZone = calendar.getTimeZone();
    }/*from  w  w w.j  a  v a 2 s  . c o m*/

    if (recurrence != null) {
        java.util.Calendar startTimeJCalendar = JCalendarUtil.getJCalendar(startTime, timeZone);

        recurrence = RecurrenceUtil.inTimeZone(recurrence, startTimeJCalendar, timeZone);
    }

    if (calendarBookingId <= 0) {
        calendarBooking = _calendarBookingService.addCalendarBooking(calendar.getCalendarId(), childCalendarIds,
                CalendarBookingConstants.RECURRING_CALENDAR_BOOKING_ID_DEFAULT,
                CalendarBookingConstants.PARENT_CALENDAR_BOOKING_ID_DEFAULT, titleMap, descriptionMap, location,
                startTime, endTime, allDay, RecurrenceSerializer.serialize(recurrence), reminders[0],
                remindersType[0], reminders[1], remindersType[1], serviceContext);
    } else {
        if (calendarBooking.isRecurring()) {
            if (updateInstance) {
                calendarBooking = _calendarBookingService.updateCalendarBookingInstance(calendarBookingId,
                        instanceIndex, calendar.getCalendarId(), childCalendarIds, titleMap, descriptionMap,
                        location, startTime, endTime, allDay, RecurrenceSerializer.serialize(recurrence),
                        allFollowing, reminders[0], remindersType[0], reminders[1], remindersType[1],
                        serviceContext);
            } else {
                calendarBooking = _calendarBookingService.updateRecurringCalendarBooking(calendarBookingId,
                        calendar.getCalendarId(), childCalendarIds, titleMap, descriptionMap, location,
                        startTime, endTime, allDay, RecurrenceSerializer.serialize(recurrence), reminders[0],
                        remindersType[0], reminders[1], remindersType[1], serviceContext);
            }
        } else {
            calendarBooking = _calendarBookingService.getCalendarBookingInstance(calendarBookingId,
                    instanceIndex);

            long duration = endTime - startTime;
            long offset = getOffset(calendarBooking, startTime, recurrence);

            calendarBooking = _calendarBookingService
                    .getNewStartTimeAndDurationCalendarBooking(calendarBookingId, offset, duration);

            calendarBooking = getFirstCalendarBookingInstance(calendarBooking, recurrence, timeZone);

            calendarBooking = _calendarBookingService.updateCalendarBooking(calendarBookingId,
                    calendar.getCalendarId(), childCalendarIds, titleMap, descriptionMap, location,
                    calendarBooking.getStartTime(), calendarBooking.getEndTime(), allDay,
                    RecurrenceSerializer.serialize(recurrence), reminders[0], remindersType[0], reminders[1],
                    remindersType[1], serviceContext);
        }
    }

    return calendarBooking;
}

From source file:com.liferay.portlet.blogs.lar.WordPressImporter.java

License:Open Source License

public static void importData(PortletDataContext context) throws PortalException, SystemException {

    Map<String, Long> userMap = getWordPressUserMap(context);

    String path = getWordPressPath(context, _EXPORT_FILE);

    String fileData = context.getZipEntryAsString(path);

    if (Validator.isNull(fileData)) {
        return;//  w w  w .j  av  a  2 s  . c o  m
    }

    Document wordPressDoc = null;

    try {
        wordPressDoc = SAXReaderUtil.read(fileData);
    } catch (DocumentException de) {
        _log.error("Reading " + path, de);

        return;
    }

    User defaultUser = UserLocalServiceUtil.getDefaultUser(context.getCompanyId());

    Element root = wordPressDoc.getRootElement();

    List<Element> entryEls = root.element("channel").elements("item");

    DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(_DATE_FORMAT);

    dateFormat.setTimeZone(TimeZoneUtil.getTimeZone(StringPool.UTC));

    for (Element entryEl : entryEls) {
        importEntry(context, defaultUser, userMap, dateFormat, entryEl);
    }
}

From source file:com.liferay.portlet.calendar.action.EditEventAction.java

License:Open Source License

protected void updateEvent(ActionRequest actionRequest) throws Exception {
    long eventId = ParamUtil.getLong(actionRequest, "eventId");

    String title = ParamUtil.getString(actionRequest, "title");
    String description = ParamUtil.getString(actionRequest, "description");
    String location = ParamUtil.getString(actionRequest, "location");

    int startDateMonth = ParamUtil.getInteger(actionRequest, "startDateMonth");
    int startDateDay = ParamUtil.getInteger(actionRequest, "startDateDay");
    int startDateYear = ParamUtil.getInteger(actionRequest, "startDateYear");
    int startDateHour = ParamUtil.getInteger(actionRequest, "startDateHour");
    int startDateMinute = ParamUtil.getInteger(actionRequest, "startDateMinute");
    int startDateAmPm = ParamUtil.getInteger(actionRequest, "startDateAmPm");

    if (startDateAmPm == Calendar.PM) {
        startDateHour += 12;//from  w ww  . j a v a 2  s. co  m
    }

    int durationHour = ParamUtil.getInteger(actionRequest, "durationHour");
    int durationMinute = ParamUtil.getInteger(actionRequest, "durationMinute");
    boolean allDay = ParamUtil.getBoolean(actionRequest, "allDay");
    boolean timeZoneSensitive = ParamUtil.getBoolean(actionRequest, "timeZoneSensitive");
    String type = ParamUtil.getString(actionRequest, "type");

    int endDateMonth = ParamUtil.getInteger(actionRequest, "endDateMonth");
    int endDateDay = ParamUtil.getInteger(actionRequest, "endDateDay");
    int endDateYear = ParamUtil.getInteger(actionRequest, "endDateYear");

    boolean repeating = false;

    int recurrenceType = ParamUtil.getInteger(actionRequest, "recurrenceType");

    if (recurrenceType != Recurrence.NO_RECURRENCE) {
        repeating = true;
    }

    Locale locale = null;
    TimeZone timeZone = null;

    if (timeZoneSensitive) {
        User user = PortalUtil.getUser(actionRequest);

        locale = user.getLocale();
        timeZone = user.getTimeZone();
    } else {
        locale = LocaleUtil.getDefault();
        timeZone = TimeZoneUtil.getDefault();
    }

    Calendar startDate = CalendarFactoryUtil.getCalendar(timeZone, locale);

    startDate.set(Calendar.MONTH, startDateMonth);
    startDate.set(Calendar.DATE, startDateDay);
    startDate.set(Calendar.YEAR, startDateYear);
    startDate.set(Calendar.HOUR_OF_DAY, startDateHour);
    startDate.set(Calendar.MINUTE, startDateMinute);
    startDate.set(Calendar.SECOND, 0);
    startDate.set(Calendar.MILLISECOND, 0);

    if (allDay) {
        startDate.set(Calendar.HOUR_OF_DAY, 0);
        startDate.set(Calendar.MINUTE, 0);
        startDate.set(Calendar.SECOND, 0);
        startDate.set(Calendar.MILLISECOND, 0);

        durationHour = 24;
        durationMinute = 0;
    }

    TZSRecurrence recurrence = null;

    if (repeating) {
        Calendar recStartCal = null;

        if (timeZoneSensitive) {
            recStartCal = CalendarFactoryUtil.getCalendar(TimeZoneUtil.getTimeZone(StringPool.UTC));

            recStartCal.setTime(startDate.getTime());
        } else {
            recStartCal = (Calendar) startDate.clone();
        }

        recurrence = new TZSRecurrence(recStartCal, new Duration(1, 0, 0, 0), recurrenceType);

        recurrence.setTimeZone(timeZone);

        recurrence.setWeekStart(Calendar.SUNDAY);

        if (recurrenceType == Recurrence.DAILY) {
            int dailyType = ParamUtil.getInteger(actionRequest, "dailyType");

            if (dailyType == 0) {
                int dailyInterval = ParamUtil.getInteger(actionRequest, "dailyInterval", 1);

                recurrence.setInterval(dailyInterval);
            } else {
                DayAndPosition[] dayPos = { new DayAndPosition(Calendar.MONDAY, 0),
                        new DayAndPosition(Calendar.TUESDAY, 0), new DayAndPosition(Calendar.WEDNESDAY, 0),
                        new DayAndPosition(Calendar.THURSDAY, 0), new DayAndPosition(Calendar.FRIDAY, 0) };

                recurrence.setByDay(dayPos);
            }
        } else if (recurrenceType == Recurrence.WEEKLY) {
            int weeklyInterval = ParamUtil.getInteger(actionRequest, "weeklyInterval", 1);

            recurrence.setInterval(weeklyInterval);

            List<DayAndPosition> dayPos = new ArrayList<DayAndPosition>();

            addWeeklyDayPos(actionRequest, dayPos, Calendar.SUNDAY);
            addWeeklyDayPos(actionRequest, dayPos, Calendar.MONDAY);
            addWeeklyDayPos(actionRequest, dayPos, Calendar.TUESDAY);
            addWeeklyDayPos(actionRequest, dayPos, Calendar.WEDNESDAY);
            addWeeklyDayPos(actionRequest, dayPos, Calendar.THURSDAY);
            addWeeklyDayPos(actionRequest, dayPos, Calendar.FRIDAY);
            addWeeklyDayPos(actionRequest, dayPos, Calendar.SATURDAY);

            if (dayPos.size() == 0) {
                dayPos.add(new DayAndPosition(Calendar.MONDAY, 0));
            }

            recurrence.setByDay(dayPos.toArray(new DayAndPosition[0]));
        } else if (recurrenceType == Recurrence.MONTHLY) {
            int monthlyType = ParamUtil.getInteger(actionRequest, "monthlyType");

            if (monthlyType == 0) {
                int monthlyDay = ParamUtil.getInteger(actionRequest, "monthlyDay0");

                recurrence.setByMonthDay(new int[] { monthlyDay });

                int monthlyInterval = ParamUtil.getInteger(actionRequest, "monthlyInterval0", 1);

                recurrence.setInterval(monthlyInterval);
            } else {
                int monthlyPos = ParamUtil.getInteger(actionRequest, "monthlyPos");
                int monthlyDay = ParamUtil.getInteger(actionRequest, "monthlyDay1");

                DayAndPosition[] dayPos = { new DayAndPosition(monthlyDay, monthlyPos) };

                recurrence.setByDay(dayPos);

                int monthlyInterval = ParamUtil.getInteger(actionRequest, "monthlyInterval1", 1);

                recurrence.setInterval(monthlyInterval);
            }
        } else if (recurrenceType == Recurrence.YEARLY) {
            int yearlyType = ParamUtil.getInteger(actionRequest, "yearlyType");

            if (yearlyType == 0) {
                int yearlyMonth = ParamUtil.getInteger(actionRequest, "yearlyMonth0");
                int yearlyDay = ParamUtil.getInteger(actionRequest, "yearlyDay0");

                recurrence.setByMonth(new int[] { yearlyMonth });
                recurrence.setByMonthDay(new int[] { yearlyDay });

                int yearlyInterval = ParamUtil.getInteger(actionRequest, "yearlyInterval0", 1);

                recurrence.setInterval(yearlyInterval);
            } else {
                int yearlyPos = ParamUtil.getInteger(actionRequest, "yearlyPos");
                int yearlyDay = ParamUtil.getInteger(actionRequest, "yearlyDay1");
                int yearlyMonth = ParamUtil.getInteger(actionRequest, "yearlyMonth1");

                DayAndPosition[] dayPos = { new DayAndPosition(yearlyDay, yearlyPos) };

                recurrence.setByDay(dayPos);

                recurrence.setByMonth(new int[] { yearlyMonth });

                int yearlyInterval = ParamUtil.getInteger(actionRequest, "yearlyInterval1", 1);

                recurrence.setInterval(yearlyInterval);
            }
        }

        int endDateType = ParamUtil.getInteger(actionRequest, "endDateType");

        if (endDateType == 1) {
            int endDateOccurrence = ParamUtil.getInteger(actionRequest, "endDateOccurrence");

            recurrence.setOccurrence(endDateOccurrence);
        } else if (endDateType == 2) {
            Calendar endDate = CalendarFactoryUtil.getCalendar(timeZone);

            endDate.set(Calendar.MONTH, endDateMonth);
            endDate.set(Calendar.DATE, endDateDay);
            endDate.set(Calendar.YEAR, endDateYear);
            endDate.set(Calendar.HOUR_OF_DAY, startDateHour);
            endDate.set(Calendar.MINUTE, startDateMinute);
            endDate.set(Calendar.SECOND, 0);
            endDate.set(Calendar.MILLISECOND, 0);

            Calendar recEndCal = null;

            if (timeZoneSensitive) {
                recEndCal = CalendarFactoryUtil.getCalendar(TimeZoneUtil.getTimeZone(StringPool.UTC));

                recEndCal.setTime(endDate.getTime());
            } else {
                recEndCal = (Calendar) endDate.clone();
            }

            recurrence.setUntil(recEndCal);
        }
    }

    int remindBy = ParamUtil.getInteger(actionRequest, "remindBy");
    int firstReminder = ParamUtil.getInteger(actionRequest, "firstReminder");
    int secondReminder = ParamUtil.getInteger(actionRequest, "secondReminder");

    ServiceContext serviceContext = ServiceContextFactory.getInstance(CalEvent.class.getName(), actionRequest);

    if (eventId <= 0) {

        // Add event

        CalEvent event = CalEventServiceUtil.addEvent(title, description, location, startDateMonth,
                startDateDay, startDateYear, startDateHour, startDateMinute, endDateMonth, endDateDay,
                endDateYear, durationHour, durationMinute, allDay, timeZoneSensitive, type, repeating,
                recurrence, remindBy, firstReminder, secondReminder, serviceContext);

        AssetPublisherUtil.addAndStoreSelection(actionRequest, CalEvent.class.getName(), event.getEventId(),
                -1);
    } else {

        // Update event

        CalEventServiceUtil.updateEvent(eventId, title, description, location, startDateMonth, startDateDay,
                startDateYear, startDateHour, startDateMinute, endDateMonth, endDateDay, endDateYear,
                durationHour, durationMinute, allDay, timeZoneSensitive, type, repeating, recurrence, remindBy,
                firstReminder, secondReminder, serviceContext);
    }
}

From source file:com.liferay.portlet.calendar.service.impl.CalEventLocalServiceImpl.java

License:Open Source License

public void checkEvents() throws PortalException, SystemException {
    List<CalEvent> events = calEventFinder.findByFutureReminders();

    for (CalEvent event : events) {
        User user = userPersistence.fetchByPrimaryKey(event.getUserId());

        if (user == null) {
            deleteEvent(event);//from   ww w . j  a  va 2s .  c o m

            continue;
        }

        Calendar now = CalendarFactoryUtil.getCalendar(user.getTimeZone(), user.getLocale());

        if (!event.isTimeZoneSensitive()) {
            Calendar temp = CalendarFactoryUtil.getCalendar();

            temp.setTime(Time.getDate(now));

            now = temp;
        }

        Calendar startDate = null;

        if (event.isTimeZoneSensitive()) {
            startDate = CalendarFactoryUtil.getCalendar(user.getTimeZone(), user.getLocale());
        } else {
            startDate = CalendarFactoryUtil.getCalendar();
        }

        if (event.isRepeating()) {
            double daysToCheck = Math
                    .ceil(CalEventConstants.REMINDERS[CalEventConstants.REMINDERS.length - 1] / Time.DAY);

            Calendar cal = (Calendar) now.clone();

            for (int i = 0; i <= daysToCheck; i++) {
                Recurrence recurrence = event.getRecurrenceObj();

                Calendar tzICal = CalendarFactoryUtil.getCalendar(TimeZoneUtil.getTimeZone(StringPool.UTC));

                tzICal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));

                Calendar recurrenceCal = getRecurrenceCal(cal, tzICal, event);

                if (recurrence.isInRecurrence(recurrenceCal)) {
                    remindUser(event, user, recurrenceCal, now);
                }

                cal.add(Calendar.DAY_OF_YEAR, 1);
            }
        } else {
            startDate.setTime(event.getStartDate());

            remindUser(event, user, startDate, now);
        }
    }
}

From source file:com.liferay.portlet.calendar.service.impl.CalEventLocalServiceImpl.java

License:Open Source License

public List<CalEvent> getEvents(long groupId, Calendar cal, String[] types) throws SystemException {

    if (types != null) {
        types = ArrayUtil.distinct(types);

        Arrays.sort(types);//from  w  w  w  .j av a  2s.  c o  m
    }

    Map<String, List<CalEvent>> eventsPool = CalEventLocalUtil.getEventsPool(groupId);

    String key = CalUtil.toString(cal, types);

    List<CalEvent> events = eventsPool.get(key);

    if (events == null) {

        // Time zone sensitive

        List<CalEvent> events1 = calEventFinder.findByG_SD_T(groupId, CalendarUtil.getGTDate(cal),
                CalendarUtil.getLTDate(cal), true, types);

        // Time zone insensitive

        Calendar tzICal = CalendarFactoryUtil.getCalendar(TimeZoneUtil.getTimeZone(StringPool.UTC));

        tzICal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));

        List<CalEvent> events2 = calEventFinder.findByG_SD_T(groupId, CalendarUtil.getGTDate(tzICal),
                CalendarUtil.getLTDate(tzICal), false, types);

        // Create new list

        events = new ArrayList<CalEvent>();

        events.addAll(events1);
        events.addAll(events2);

        // Add repeating events

        events.addAll(getRepeatingEvents(groupId, cal, types));

        events = new UnmodifiableList<CalEvent>(events);

        eventsPool.put(key, events);
    }

    return events;
}

From source file:com.liferay.portlet.calendar.service.impl.CalEventLocalServiceImpl.java

License:Open Source License

public List<CalEvent> getRepeatingEvents(long groupId, Calendar cal, String[] types) throws SystemException {

    Map<String, List<CalEvent>> eventsPool = CalEventLocalUtil.getEventsPool(groupId);

    String key = "recurrence".concat(CalUtil.toString(null, types));

    List<CalEvent> events = eventsPool.get(key);

    if (events == null) {
        if ((types != null) && (types.length > 0) && ((types.length > 1) || Validator.isNotNull(types[0]))) {

            events = calEventPersistence.findByG_T_R(groupId, types, true);
        } else {/*from  ww w  .j a  v  a  2  s  .  com*/
            events = calEventPersistence.findByG_R(groupId, true);
        }

        events = new UnmodifiableList<CalEvent>(events);

        eventsPool.put(key, events);
    }

    if (cal != null) {

        // Time zone insensitive

        Calendar tzICal = CalendarFactoryUtil.getCalendar(TimeZoneUtil.getTimeZone(StringPool.UTC));

        tzICal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));

        Iterator<CalEvent> itr = events.iterator();

        List<CalEvent> repeatingEvents = new ArrayList<CalEvent>();

        while (itr.hasNext()) {
            CalEvent event = itr.next();

            TZSRecurrence recurrence = event.getRecurrenceObj();

            try {

                // LEP-3468

                if ((recurrence.getFrequency() != Recurrence.NO_RECURRENCE)
                        && (recurrence.getInterval() <= 0)) {

                    recurrence.setInterval(1);

                    event.setRecurrenceObj(recurrence);

                    event = calEventPersistence.update(event, false);

                    recurrence = event.getRecurrenceObj();
                }

                if (recurrence.isInRecurrence(getRecurrenceCal(cal, tzICal, event))) {

                    repeatingEvents.add(event);
                }
            } catch (Exception e) {
                _log.error(e, e);
            }
        }

        events = new UnmodifiableList<CalEvent>(repeatingEvents);
    }

    return events;
}

From source file:com.liferay.portlet.calendar.service.impl.CalEventLocalServiceImpl.java

License:Open Source License

protected Calendar getRecurrenceCal(Calendar cal, Calendar tzICal, CalEvent event) {

    Calendar eventCal = CalendarFactoryUtil.getCalendar(TimeZoneUtil.getTimeZone(StringPool.UTC));

    eventCal.setTime(event.getStartDate());

    Calendar recurrenceCal = (Calendar) tzICal.clone();
    recurrenceCal.set(Calendar.HOUR_OF_DAY, eventCal.get(Calendar.HOUR_OF_DAY));
    recurrenceCal.set(Calendar.MINUTE, eventCal.get(Calendar.MINUTE));
    recurrenceCal.set(Calendar.SECOND, 0);
    recurrenceCal.set(Calendar.MILLISECOND, 0);

    if (event.isTimeZoneSensitive()) {
        int gmtDate = eventCal.get(Calendar.DATE);
        long gmtMills = eventCal.getTimeInMillis();

        eventCal.setTimeZone(cal.getTimeZone());

        int tziDate = eventCal.get(Calendar.DATE);
        long tziMills = Time.getDate(eventCal).getTime();

        if (gmtDate != tziDate) {
            int diffDate = 0;

            if (gmtMills > tziMills) {
                diffDate = (int) Math.ceil((double) (gmtMills - tziMills) / Time.DAY);
            } else {
                diffDate = (int) Math.floor((double) (gmtMills - tziMills) / Time.DAY);
            }/*from ww  w . j av  a 2  s  .c o m*/

            recurrenceCal.add(Calendar.DATE, diffDate);
        }
    }

    return recurrenceCal;
}

From source file:com.liferay.portlet.calendar.service.impl.CalEventLocalServiceImpl.java

License:Open Source License

protected void importICal4j(long userId, long groupId, VEvent event) throws PortalException, SystemException {

    User user = userPersistence.findByPrimaryKey(userId);

    TimeZone timeZone = user.getTimeZone();

    // X iCal property

    Property timeZoneXProperty = event.getProperty(TimeZoneSensitive.PROPERTY_NAME);

    boolean timeZoneXPropertyValue = true;

    if ((timeZoneXProperty != null) && timeZoneXProperty.getValue().equals("FALSE")) {

        timeZoneXPropertyValue = false;/*w w  w .  j av a2  s.c  om*/
    }

    // Title

    String title = StringPool.BLANK;

    if (event.getSummary() != null) {
        title = ModelHintsUtil.trimString(CalEvent.class.getName(), "title", event.getSummary().getValue());
    }

    // Description

    String description = StringPool.BLANK;

    if (event.getDescription() != null) {
        description = event.getDescription().getValue();
    }

    // Location

    String location = StringPool.BLANK;

    if (event.getLocation() != null) {
        location = event.getLocation().getValue();
    }

    // Start date

    DtStart dtStart = event.getStartDate();

    Calendar startDate = toCalendar(dtStart, timeZone, timeZoneXPropertyValue);

    startDate.setTime(dtStart.getDate());

    // End date

    Calendar endDate = null;

    DtEnd dtEnd = event.getEndDate(true);

    RRule rrule = (RRule) event.getProperty(Property.RRULE);

    if (dtEnd != null) {
        endDate = toCalendar(dtEnd, timeZone, timeZoneXPropertyValue);

        endDate.setTime(dtEnd.getDate());
    } else {
        endDate = (Calendar) startDate.clone();
        endDate.add(Calendar.DATE, 1);
    }

    // Duration

    long diffMillis = 0;
    long durationHours = 24;
    long durationMins = 0;
    boolean multiDayEvent = false;

    if (dtEnd != null) {
        diffMillis = dtEnd.getDate().getTime() - startDate.getTimeInMillis();
        durationHours = diffMillis / Time.HOUR;
        durationMins = (diffMillis / Time.MINUTE) - (durationHours * 60);

        if ((durationHours > 24) || ((durationHours == 24) && (durationMins > 0))) {

            durationHours = 24;
            durationMins = 0;
            multiDayEvent = true;
        }
    }

    // All day

    boolean allDay = false;

    if (isICal4jDateOnly(event.getStartDate()) || multiDayEvent) {
        allDay = true;
    }

    // Time zone sensitive

    boolean timeZoneSensitive = true;

    if (allDay || !timeZoneXPropertyValue) {
        timeZoneSensitive = false;
    }

    // Type

    String type = StringPool.BLANK;

    Property comment = event.getProperty(Property.COMMENT);

    if ((comment != null) && ArrayUtil.contains(CalEventConstants.TYPES, comment.getValue())) {

        type = comment.getValue();
    }

    // Recurrence

    boolean repeating = false;
    TZSRecurrence recurrence = null;

    if (multiDayEvent) {
        repeating = true;

        Calendar recStartCal = CalendarFactoryUtil.getCalendar(TimeZoneUtil.getTimeZone(StringPool.UTC));

        recStartCal.setTime(startDate.getTime());

        com.liferay.portal.kernel.cal.Duration duration = new com.liferay.portal.kernel.cal.Duration(1, 0, 0,
                0);

        recurrence = new TZSRecurrence(recStartCal, duration, Recurrence.DAILY);

        Calendar until = CalendarFactoryUtil.getCalendar(TimeZoneUtil.getTimeZone(StringPool.UTC));

        until.setTimeInMillis(until.getTimeInMillis() + diffMillis);

        recurrence.setUntil(until);

        endDate.setTime(recurrence.getUntil().getTime());
    } else if (rrule != null) {
        repeating = true;
        recurrence = toRecurrence(rrule, startDate);

        if (recurrence.getUntil() != null) {
            endDate.setTime(recurrence.getUntil().getTime());
        }
    }

    // Reminder

    int remindBy = CalEventConstants.REMIND_BY_NONE;
    int firstReminder = 300000;
    int secondReminder = 300000;

    // Permissions

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);
    serviceContext.setScopeGroupId(groupId);

    // Merge event

    String uuid = null;

    CalEvent existingEvent = null;

    if (event.getUid() != null) {
        Uid uid = event.getUid();

        if (existingEvent == null) {

            // VEvent exported by Liferay portal

            uuid = uid.getValue();

            existingEvent = calEventPersistence.fetchByUUID_G(uuid, groupId);
        }

        if (existingEvent == null) {

            // VEvent exported by external application

            uuid = PortalUUIDUtil.generate(uid.getValue().getBytes());

            existingEvent = calEventPersistence.fetchByUUID_G(uuid, groupId);
        }
    }

    int startDateMonth = startDate.get(Calendar.MONTH);
    int startDateDay = startDate.get(Calendar.DAY_OF_MONTH);
    int startDateYear = startDate.get(Calendar.YEAR);
    int startDateHour = startDate.get(Calendar.HOUR_OF_DAY);
    int startDateMinute = startDate.get(Calendar.MINUTE);
    int endDateMonth = endDate.get(Calendar.MONTH);
    int endDateDay = endDate.get(Calendar.DAY_OF_MONTH);
    int endDateYear = endDate.get(Calendar.YEAR);
    int durationHour = (int) durationHours;
    int durationMinute = (int) durationMins;

    if (existingEvent == null) {
        serviceContext.setUuid(uuid);

        addEvent(userId, title, description, location, startDateMonth, startDateDay, startDateYear,
                startDateHour, startDateMinute, endDateMonth, endDateDay, endDateYear, durationHour,
                durationMinute, allDay, timeZoneSensitive, type, repeating, recurrence, remindBy, firstReminder,
                secondReminder, serviceContext);
    } else {
        updateEvent(userId, existingEvent.getEventId(), title, description, location, startDateMonth,
                startDateDay, startDateYear, startDateHour, startDateMinute, endDateMonth, endDateDay,
                endDateYear, durationHour, durationMinute, allDay, timeZoneSensitive, type, repeating,
                recurrence, remindBy, firstReminder, secondReminder, serviceContext);
    }
}