public CalendarBooking addCalendarBooking(
      long userId,
      long calendarId,
      long[] childCalendarIds,
      long parentCalendarBookingId,
      Map<Locale, String> titleMap,
      Map<Locale, String> descriptionMap,
      String location,
      long startDate,
      long endDate,
      boolean allDay,
      String recurrence,
      long firstReminder,
      String firstReminderType,
      long secondReminder,
      String secondReminderType,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Calendar booking

    User user = userPersistence.findByPrimaryKey(userId);
    Calendar calendar = calendarPersistence.findByPrimaryKey(calendarId);

    java.util.Calendar startDateJCalendar = JCalendarUtil.getJCalendar(startDate);
    java.util.Calendar endDateJCalendar = JCalendarUtil.getJCalendar(endDate);

    if (allDay) {
      startDateJCalendar = JCalendarUtil.toMidnightJCalendar(startDateJCalendar);
      endDateJCalendar = JCalendarUtil.toLastHourJCalendar(endDateJCalendar);
    }

    if (firstReminder < secondReminder) {
      long originalSecondReminder = secondReminder;

      secondReminder = firstReminder;
      firstReminder = originalSecondReminder;
    }

    Date now = new Date();

    validate(titleMap, startDateJCalendar, endDateJCalendar);

    long calendarBookingId = counterLocalService.increment();

    CalendarBooking calendarBooking = calendarBookingPersistence.create(calendarBookingId);

    calendarBooking.setUuid(serviceContext.getUuid());
    calendarBooking.setGroupId(calendar.getGroupId());
    calendarBooking.setCompanyId(user.getCompanyId());
    calendarBooking.setUserId(user.getUserId());
    calendarBooking.setUserName(user.getFullName());
    calendarBooking.setCreateDate(serviceContext.getCreateDate(now));
    calendarBooking.setModifiedDate(serviceContext.getModifiedDate(now));
    calendarBooking.setCalendarId(calendarId);
    calendarBooking.setCalendarResourceId(calendar.getCalendarResourceId());

    if (parentCalendarBookingId > 0) {
      calendarBooking.setParentCalendarBookingId(parentCalendarBookingId);
    } else {
      calendarBooking.setParentCalendarBookingId(calendarBookingId);
    }

    calendarBooking.setTitleMap(titleMap);
    calendarBooking.setDescriptionMap(descriptionMap);
    calendarBooking.setLocation(location);
    calendarBooking.setStartDate(startDateJCalendar.getTimeInMillis());
    calendarBooking.setEndDate(endDateJCalendar.getTimeInMillis());
    calendarBooking.setAllDay(allDay);
    calendarBooking.setRecurrence(recurrence);
    calendarBooking.setFirstReminder(firstReminder);
    calendarBooking.setFirstReminderType(firstReminderType);
    calendarBooking.setSecondReminder(secondReminder);
    calendarBooking.setSecondReminderType(secondReminderType);

    int status = CalendarBookingWorkflowConstants.STATUS_PENDING;

    if (parentCalendarBookingId == 0) {
      status = CalendarBookingWorkflowConstants.STATUS_APPROVED;
    }

    calendarBooking.setStatus(status);

    calendarBooking.setStatusDate(serviceContext.getModifiedDate(now));

    calendarBookingPersistence.update(calendarBooking, false);

    addChildCalendarBookings(calendarBooking, childCalendarIds, serviceContext);

    // Workflow

    calendarBookingApprovalWorkflow.startWorkflow(userId, calendarBookingId, serviceContext);

    return calendarBooking;
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, Calendar calendar)
      throws Exception {

    long userId = portletDataContext.getUserId(calendar.getUserUuid());

    Map<Long, Long> calendarResourceIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(CalendarResource.class);

    long calendarResourceId =
        MapUtil.getLong(
            calendarResourceIds,
            calendar.getCalendarResourceId(),
            calendar.getCalendarResourceId());

    Map<Locale, String> calendarNameMap = getCalendarNameMap(portletDataContext, calendar);

    ServiceContext serviceContext = portletDataContext.createServiceContext(calendar);

    Calendar importedCalendar = null;

    if (portletDataContext.isDataStrategyMirror()) {
      Calendar existingCalendar =
          fetchStagedModelByUuidAndGroupId(
              calendar.getUuid(), portletDataContext.getScopeGroupId());

      if (existingCalendar == null) {
        serviceContext.setUuid(calendar.getUuid());

        importedCalendar =
            CalendarLocalServiceUtil.addCalendar(
                userId,
                portletDataContext.getScopeGroupId(),
                calendarResourceId,
                calendarNameMap,
                calendar.getDescriptionMap(),
                calendar.getTimeZoneId(),
                calendar.getColor(),
                calendar.isDefaultCalendar(),
                calendar.isEnableComments(),
                calendar.isEnableRatings(),
                serviceContext);
      } else {
        importedCalendar =
            CalendarLocalServiceUtil.updateCalendar(
                existingCalendar.getCalendarId(),
                calendar.getNameMap(),
                calendar.getDescriptionMap(),
                calendar.getTimeZoneId(),
                calendar.getColor(),
                calendar.isDefaultCalendar(),
                calendar.isEnableComments(),
                calendar.isEnableRatings(),
                serviceContext);
      }
    } else {
      importedCalendar =
          CalendarLocalServiceUtil.addCalendar(
              userId,
              portletDataContext.getScopeGroupId(),
              calendarResourceId,
              calendarNameMap,
              calendar.getDescriptionMap(),
              calendar.getTimeZoneId(),
              calendar.getColor(),
              calendar.isDefaultCalendar(),
              calendar.isEnableComments(),
              calendar.isEnableRatings(),
              serviceContext);
    }

    portletDataContext.importClassedModel(calendar, importedCalendar);
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    Calendar newCalendar = _persistence.create(pk);

    newCalendar.setUuid(RandomTestUtil.randomString());

    newCalendar.setGroupId(RandomTestUtil.nextLong());

    newCalendar.setCompanyId(RandomTestUtil.nextLong());

    newCalendar.setUserId(RandomTestUtil.nextLong());

    newCalendar.setUserName(RandomTestUtil.randomString());

    newCalendar.setCreateDate(RandomTestUtil.nextDate());

    newCalendar.setModifiedDate(RandomTestUtil.nextDate());

    newCalendar.setResourceBlockId(RandomTestUtil.nextLong());

    newCalendar.setCalendarResourceId(RandomTestUtil.nextLong());

    newCalendar.setName(RandomTestUtil.randomString());

    newCalendar.setDescription(RandomTestUtil.randomString());

    newCalendar.setTimeZoneId(RandomTestUtil.randomString());

    newCalendar.setColor(RandomTestUtil.nextInt());

    newCalendar.setDefaultCalendar(RandomTestUtil.randomBoolean());

    newCalendar.setEnableComments(RandomTestUtil.randomBoolean());

    newCalendar.setEnableRatings(RandomTestUtil.randomBoolean());

    newCalendar.setLastPublishDate(RandomTestUtil.nextDate());

    _calendars.add(_persistence.update(newCalendar));

    Calendar existingCalendar = _persistence.findByPrimaryKey(newCalendar.getPrimaryKey());

    Assert.assertEquals(existingCalendar.getUuid(), newCalendar.getUuid());
    Assert.assertEquals(existingCalendar.getCalendarId(), newCalendar.getCalendarId());
    Assert.assertEquals(existingCalendar.getGroupId(), newCalendar.getGroupId());
    Assert.assertEquals(existingCalendar.getCompanyId(), newCalendar.getCompanyId());
    Assert.assertEquals(existingCalendar.getUserId(), newCalendar.getUserId());
    Assert.assertEquals(existingCalendar.getUserName(), newCalendar.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingCalendar.getCreateDate()),
        Time.getShortTimestamp(newCalendar.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingCalendar.getModifiedDate()),
        Time.getShortTimestamp(newCalendar.getModifiedDate()));
    Assert.assertEquals(existingCalendar.getResourceBlockId(), newCalendar.getResourceBlockId());
    Assert.assertEquals(
        existingCalendar.getCalendarResourceId(), newCalendar.getCalendarResourceId());
    Assert.assertEquals(existingCalendar.getName(), newCalendar.getName());
    Assert.assertEquals(existingCalendar.getDescription(), newCalendar.getDescription());
    Assert.assertEquals(existingCalendar.getTimeZoneId(), newCalendar.getTimeZoneId());
    Assert.assertEquals(existingCalendar.getColor(), newCalendar.getColor());
    Assert.assertEquals(existingCalendar.getDefaultCalendar(), newCalendar.getDefaultCalendar());
    Assert.assertEquals(existingCalendar.getEnableComments(), newCalendar.getEnableComments());
    Assert.assertEquals(existingCalendar.getEnableRatings(), newCalendar.getEnableRatings());
    Assert.assertEquals(
        Time.getShortTimestamp(existingCalendar.getLastPublishDate()),
        Time.getShortTimestamp(newCalendar.getLastPublishDate()));
  }