Example usage for com.liferay.portal.kernel.util Time WEEK

List of usage examples for com.liferay.portal.kernel.util Time WEEK

Introduction

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

Prototype

long WEEK

To view the source code for com.liferay.portal.kernel.util Time WEEK.

Click Source Link

Usage

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

License:Open Source License

protected void importICalEvent(long calendarId, VEvent vEvent) throws Exception {

    Calendar calendar = CalendarLocalServiceUtil.getCalendar(calendarId);

    // Title// ww  w  . j  av a 2 s . c o  m

    User user = UserLocalServiceUtil.getUser(calendar.getUserId());

    Map<Locale, String> titleMap = new HashMap<Locale, String>();

    Summary summary = vEvent.getSummary();

    if (summary != null) {
        String title = ModelHintsUtil.trimString(CalendarBooking.class.getName(), "title", summary.getValue());

        titleMap.put(user.getLocale(), title);
    }

    // Description

    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();

    Description description = vEvent.getDescription();

    if (description != null) {
        descriptionMap.put(user.getLocale(), description.getValue());
    }

    // Location

    String locationString = StringPool.BLANK;

    Location location = vEvent.getLocation();

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

    // Dates

    DtStart dtStart = vEvent.getStartDate();

    Date startDate = dtStart.getDate();

    DtEnd dtEnd = vEvent.getEndDate();

    Date endDate = dtEnd.getDate();

    // All day

    boolean allDay = false;

    if (isICalDateOnly(dtStart)) {
        allDay = true;

        long time = endDate.getTime();

        endDate.setTime(time - 1);
    }

    // Recurrence

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

    String recurrence = StringPool.BLANK;

    if (rrule != null) {
        recurrence = StringUtil.trim(rrule.toString());

        PropertyList propertyList = vEvent.getProperties(Property.EXDATE);

        if (!propertyList.isEmpty()) {
            StringBundler sb = new StringBundler();

            Iterator<ExDate> iterator = propertyList.iterator();

            while (iterator.hasNext()) {
                ExDate exDate = iterator.next();

                DateList dateList = exDate.getDates();

                ListIterator<Date> listIterator = dateList.listIterator();

                while (listIterator.hasNext()) {
                    Date date = listIterator.next();

                    java.util.Calendar jCalendar = JCalendarUtil.getJCalendar(date.getTime());

                    int year = jCalendar.get(java.util.Calendar.YEAR);
                    int month = jCalendar.get(java.util.Calendar.MONTH) + 1;
                    int day = jCalendar.get(java.util.Calendar.DATE);
                    int hour = jCalendar.get(java.util.Calendar.HOUR_OF_DAY);
                    int minute = jCalendar.get(java.util.Calendar.MINUTE);
                    int second = jCalendar.get(java.util.Calendar.SECOND);

                    sb.append(String.format(_EXDATE_FORMAT, year, month, day, hour, minute, second));

                    if (listIterator.hasNext()) {
                        sb.append(StringPool.COMMA);
                    }
                }

                if (iterator.hasNext()) {
                    sb.append(StringPool.COMMA);
                }
            }

            recurrence = recurrence.concat(StringPool.NEW_LINE).concat(_EXDATE).concat(sb.toString());
        }
    }

    // Reminders

    ComponentList componentList = vEvent.getAlarms();

    long[] reminders = new long[componentList.size()];
    String[] reminderTypes = new String[componentList.size()];

    int i = 0;

    for (Iterator<VAlarm> iterator = componentList.iterator(); iterator.hasNext();) {

        VAlarm vAlarm = iterator.next();

        Action action = vAlarm.getAction();

        String value = StringUtil.lowerCase(action.getValue());

        if (!isActionSupported(value)) {
            continue;
        }

        reminderTypes[i] = value;

        Trigger trigger = vAlarm.getTrigger();

        long time = 0;

        DateTime dateTime = trigger.getDateTime();

        Dur dur = trigger.getDuration();

        if ((dateTime == null) && (dur == null)) {
            continue;
        }

        if (dateTime != null) {
            time = startDate.getTime() - dateTime.getTime();

            if (time < 0) {
                continue;
            }
        } else {
            if (!dur.isNegative()) {
                continue;
            }

            time += dur.getWeeks() * Time.WEEK;
            time += dur.getDays() * Time.DAY;
            time += dur.getHours() * Time.HOUR;
            time += dur.getMinutes() * Time.MINUTE;
            time += dur.getSeconds() * Time.SECOND;
        }

        reminders[i] = time;

        i++;
    }

    long firstReminder = 0;
    String firstReminderType = null;
    long secondReminder = 0;
    String secondReminderType = null;

    if (i > 0) {
        firstReminder = reminders[0];
        firstReminderType = reminderTypes[0];
    }

    if (i > 1) {
        secondReminder = reminders[1];
        secondReminderType = reminderTypes[1];
    }

    // Attendees

    PropertyList propertyList = vEvent.getProperties(Property.ATTENDEE);

    List<Long> childCalendarIds = new ArrayList<Long>();

    for (Iterator<Attendee> iterator = propertyList.iterator(); iterator.hasNext();) {

        Attendee attendee = iterator.next();

        URI uri = attendee.getCalAddress();

        if (uri == null) {
            continue;
        }

        User attendeeUser = UserLocalServiceUtil.fetchUserByEmailAddress(calendar.getCompanyId(),
                uri.getSchemeSpecificPart());

        if ((attendeeUser == null) || (calendar.getUserId() == attendeeUser.getUserId())) {

            continue;
        }

        ServiceContext serviceContext = new ServiceContext();

        serviceContext.setCompanyId(calendar.getCompanyId());
        serviceContext.setScopeGroupId(calendar.getGroupId());

        CalendarResource calendarResource = CalendarResourceUtil
                .getUserCalendarResource(attendeeUser.getUserId(), serviceContext);

        if (calendarResource == null) {
            continue;
        }

        childCalendarIds.add(calendarResource.getDefaultCalendarId());
    }

    long[] childCalendarIdsArray = ArrayUtil
            .toArray(childCalendarIds.toArray(new Long[childCalendarIds.size()]));

    // Merge calendar booking

    CalendarBooking calendarBooking = null;

    String uuid = null;

    Uid uid = vEvent.getUid();

    if (uid != null) {
        uuid = uid.getValue();

        calendarBooking = CalendarBookingLocalServiceUtil.fetchCalendarBooking(uuid, calendar.getGroupId());
    }

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);
    serviceContext.setAttribute("sendNotification", Boolean.FALSE);
    serviceContext.setScopeGroupId(calendar.getGroupId());

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

        CalendarBookingServiceUtil.addCalendarBooking(calendarId, childCalendarIdsArray,
                CalendarBookingConstants.PARENT_CALENDAR_BOOKING_ID_DEFAULT, titleMap, descriptionMap,
                locationString, startDate.getTime(), endDate.getTime(), allDay, recurrence, firstReminder,
                firstReminderType, secondReminder, secondReminderType, serviceContext);
    } else {
        CalendarBookingServiceUtil.updateCalendarBooking(calendarBooking.getCalendarBookingId(), calendarId,
                childCalendarIdsArray, titleMap, descriptionMap, locationString, startDate.getTime(),
                endDate.getTime(), allDay, recurrence, firstReminder, firstReminderType, secondReminder,
                secondReminderType, calendarBooking.getStatus(), serviceContext);
    }
}

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

License:Open Source License

protected Dur toICalDur(long reminder) {
    int weeks = (int) (reminder / Time.WEEK);

    if (weeks > 0) {
        return new Dur(weeks);
    }/*from www .j  ava2s . c  o  m*/

    int days = (int) (reminder / Time.DAY);

    if (days > 0) {
        return new Dur(days, 0, 0, 0);
    }

    int hours = (int) (reminder / Time.HOUR);

    if (hours > 0) {
        return new Dur(0, hours, 0, 0);
    }

    int minutes = (int) (reminder / Time.MINUTE);

    if (minutes > 0) {
        return new Dur(0, 0, minutes, 0);
    }

    int seconds = (int) (reminder / Time.SECOND);

    if (seconds > 0) {
        return new Dur(0, 0, 0, seconds);
    }

    return null;
}

From source file:com.liferay.exportimport.test.ExportImportDateUtilTest.java

License:Open Source License

@Test
public void testUpdateLastPublishDateFirstPublishLayoutSet() throws Exception {

    Date now = new Date();

    Date startDate = new Date(now.getTime() + Time.DAY);
    Date endDate = new Date(now.getTime() + Time.WEEK);

    DateRange dateRange = new DateRange(startDate, endDate);

    ExportImportDateUtil.updateLastPublishDate(_layoutSet.getGroupId(), _layoutSet.isPrivateLayout(), dateRange,
            endDate);// w  ww .  j ava 2s .  c om

    _layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(_layoutSet.getLayoutSetId());

    Date lastPublishDate = ExportImportDateUtil.getLastPublishDate(_layoutSet);

    // It should be null, since no update should have happened, because it
    // would result in a gap for contents

    Assert.assertNull(lastPublishDate);
}

From source file:com.liferay.exportimport.test.ExportImportDateUtilTest.java

License:Open Source License

@Test
public void testUpdateLastPublishDateFirstPublishPortlet() throws Exception {

    Date now = new Date();

    Date startDate = new Date(now.getTime() + Time.DAY);
    Date endDate = new Date(now.getTime() + Time.WEEK);

    DateRange dateRange = new DateRange(startDate, endDate);

    ExportImportDateUtil.updateLastPublishDate(PortletKeys.EXPORT_IMPORT, _portletPreferences, dateRange,
            endDate);//from  www.  j  ava2  s .c  om

    Date lastPublishDate = ExportImportDateUtil.getLastPublishDate(_portletPreferences);

    // It should be null, since no update should have happened, because it
    // would result in a gap for contents

    Assert.assertNull(lastPublishDate);
}

From source file:com.liferay.exportimport.test.ExportImportDateUtilTest.java

License:Open Source License

@Test
public void testUpdateLastPublishDateOverlappingRangeLayoutSet() throws Exception {

    Date now = new Date();

    updateLastPublishDate(_layoutSet, now);

    Date startDate = new Date(now.getTime() - Time.DAY);
    Date endDate = new Date(now.getTime() + Time.WEEK);

    DateRange dateRange = new DateRange(startDate, endDate);

    ExportImportDateUtil.updateLastPublishDate(_layoutSet.getGroupId(), _layoutSet.isPrivateLayout(), dateRange,
            endDate);/*from   ww  w.j  a  v a 2  s  .c  o m*/

    _layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(_layoutSet.getLayoutSetId());

    Date lastPublishDate = ExportImportDateUtil.getLastPublishDate(_layoutSet);

    Assert.assertEquals(endDate.getTime(), lastPublishDate.getTime());
}

From source file:com.liferay.exportimport.test.ExportImportDateUtilTest.java

License:Open Source License

@Test
public void testUpdateLastPublishDateOverlappingRangePortlet() throws Exception {

    Date now = new Date();

    updateLastPublishDate(_portletPreferences, now);

    Date startDate = new Date(now.getTime() - Time.DAY);
    Date endDate = new Date(now.getTime() + Time.WEEK);

    DateRange dateRange = new DateRange(startDate, endDate);

    ExportImportDateUtil.updateLastPublishDate(PortletKeys.EXPORT_IMPORT, _portletPreferences, dateRange,
            endDate);//  w ww  .ja  va  2  s . c o  m

    Date lastPublishDate = ExportImportDateUtil.getLastPublishDate(_portletPreferences);

    Assert.assertEquals(endDate.getTime(), lastPublishDate.getTime());
}

From source file:com.liferay.exportimport.test.ExportImportDateUtilTest.java

License:Open Source License

@Test
public void testUpdateLastPublishDateRangeBeforeLastPublishDateLayoutSet() throws Exception {

    Date now = new Date();

    updateLastPublishDate(_layoutSet, now);

    Date startDate = new Date(now.getTime() - Time.WEEK);
    Date endDate = new Date(now.getTime() - Time.DAY);

    DateRange dateRange = new DateRange(startDate, endDate);

    ExportImportDateUtil.updateLastPublishDate(_layoutSet.getGroupId(), _layoutSet.isPrivateLayout(), dateRange,
            endDate);// w ww  . j  a v  a 2  s  . c om

    _layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(_layoutSet.getLayoutSetId());

    Date lastPublishDate = ExportImportDateUtil.getLastPublishDate(_layoutSet);

    Assert.assertEquals(now.getTime(), lastPublishDate.getTime());
}

From source file:com.liferay.exportimport.test.ExportImportDateUtilTest.java

License:Open Source License

@Test
public void testUpdateLastPublishDateRangeBeforeLastPublishDatePortlet() throws Exception {

    Date now = new Date();

    updateLastPublishDate(_portletPreferences, now);

    Date startDate = new Date(now.getTime() - Time.WEEK);
    Date endDate = new Date(now.getTime() - Time.DAY);

    DateRange dateRange = new DateRange(startDate, endDate);

    ExportImportDateUtil.updateLastPublishDate(PortletKeys.EXPORT_IMPORT, _portletPreferences, dateRange,
            endDate);//from   www.j  ava2  s . c o m

    Date lastPublishDate = ExportImportDateUtil.getLastPublishDate(_portletPreferences);

    Assert.assertEquals(now.getTime(), lastPublishDate.getTime());
}

From source file:com.liferay.exportimport.test.ExportImportDateUtilTest.java

License:Open Source License

@Test
public void testUpdateLastPublishDateWithGapLayoutSet() throws Exception {
    Date now = new Date();

    updateLastPublishDate(_layoutSet, now);

    Date startDate = new Date(now.getTime() + Time.DAY);
    Date endDate = new Date(now.getTime() + Time.WEEK);

    DateRange dateRange = new DateRange(startDate, endDate);

    ExportImportDateUtil.updateLastPublishDate(_layoutSet.getGroupId(), _layoutSet.isPrivateLayout(), dateRange,
            endDate);//from   w w  w .  j  a  v  a2s  .c  om

    _layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(_layoutSet.getLayoutSetId());

    Date lastPublishDate = ExportImportDateUtil.getLastPublishDate(_layoutSet);

    Assert.assertEquals(now.getTime(), lastPublishDate.getTime());
}

From source file:com.liferay.exportimport.test.ExportImportDateUtilTest.java

License:Open Source License

@Test
public void testUpdateLastPublishDateWithGapPortlet() throws Exception {
    Date now = new Date();

    updateLastPublishDate(_portletPreferences, now);

    Date startDate = new Date(now.getTime() + Time.DAY);
    Date endDate = new Date(now.getTime() + Time.WEEK);

    DateRange dateRange = new DateRange(startDate, endDate);

    ExportImportDateUtil.updateLastPublishDate(PortletKeys.EXPORT_IMPORT, _portletPreferences, dateRange,
            endDate);//from  w  ww.  j a  v  a 2  s .c  o  m

    Date lastPublishDate = ExportImportDateUtil.getLastPublishDate(_portletPreferences);

    Assert.assertEquals(now.getTime(), lastPublishDate.getTime());
}