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;
  }
  public CalendarBooking updateCalendarBooking(
      long userId,
      long calendarBookingId,
      long calendarId,
      long[] childCalendarIds,
      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,
      int status,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Calendar booking

    User user = userPersistence.findByPrimaryKey(userId);
    CalendarBooking calendarBooking =
        calendarBookingPersistence.findByPrimaryKey(calendarBookingId);

    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;
    }

    validate(titleMap, startDateJCalendar, endDateJCalendar);

    calendarBooking.setCompanyId(user.getCompanyId());
    calendarBooking.setUserId(user.getUserId());
    calendarBooking.setUserName(user.getFullName());
    calendarBooking.setModifiedDate(serviceContext.getModifiedDate(null));
    calendarBooking.setCalendarId(calendarId);
    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);

    calendarBookingPersistence.update(calendarBooking, false);

    addChildCalendarBookings(calendarBooking, childCalendarIds, serviceContext);

    // Workflow

    calendarBookingApprovalWorkflow.invokeTransition(
        userId,
        calendarBookingId,
        CalendarBookingWorkflowConstants.toLabel(status),
        serviceContext);

    return calendarBooking;
  }