@Override
  public CalendarResource updateCalendarResource(
      long calendarResourceId,
      Map<Locale, String> nameMap,
      Map<Locale, String> descriptionMap,
      boolean active,
      ServiceContext serviceContext)
      throws PortalException {

    // Calendar resource

    validate(nameMap);

    CalendarResource calendarResource =
        calendarResourcePersistence.findByPrimaryKey(calendarResourceId);

    calendarResource.setModifiedDate(serviceContext.getModifiedDate(null));
    calendarResource.setNameMap(nameMap);
    calendarResource.setDescriptionMap(descriptionMap);
    calendarResource.setActive(active);

    calendarResourcePersistence.update(calendarResource);

    // Asset

    updateAsset(
        calendarResource.getUserId(),
        calendarResource,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames());

    return calendarResource;
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    CalendarResource newCalendarResource = _persistence.create(pk);

    newCalendarResource.setUuid(RandomTestUtil.randomString());

    newCalendarResource.setGroupId(RandomTestUtil.nextLong());

    newCalendarResource.setCompanyId(RandomTestUtil.nextLong());

    newCalendarResource.setUserId(RandomTestUtil.nextLong());

    newCalendarResource.setUserName(RandomTestUtil.randomString());

    newCalendarResource.setCreateDate(RandomTestUtil.nextDate());

    newCalendarResource.setModifiedDate(RandomTestUtil.nextDate());

    newCalendarResource.setResourceBlockId(RandomTestUtil.nextLong());

    newCalendarResource.setClassNameId(RandomTestUtil.nextLong());

    newCalendarResource.setClassPK(RandomTestUtil.nextLong());

    newCalendarResource.setClassUuid(RandomTestUtil.randomString());

    newCalendarResource.setCode(RandomTestUtil.randomString());

    newCalendarResource.setName(RandomTestUtil.randomString());

    newCalendarResource.setDescription(RandomTestUtil.randomString());

    newCalendarResource.setActive(RandomTestUtil.randomBoolean());

    newCalendarResource.setLastPublishDate(RandomTestUtil.nextDate());

    _calendarResources.add(_persistence.update(newCalendarResource));

    CalendarResource existingCalendarResource =
        _persistence.findByPrimaryKey(newCalendarResource.getPrimaryKey());

    Assert.assertEquals(existingCalendarResource.getUuid(), newCalendarResource.getUuid());
    Assert.assertEquals(
        existingCalendarResource.getCalendarResourceId(),
        newCalendarResource.getCalendarResourceId());
    Assert.assertEquals(existingCalendarResource.getGroupId(), newCalendarResource.getGroupId());
    Assert.assertEquals(
        existingCalendarResource.getCompanyId(), newCalendarResource.getCompanyId());
    Assert.assertEquals(existingCalendarResource.getUserId(), newCalendarResource.getUserId());
    Assert.assertEquals(existingCalendarResource.getUserName(), newCalendarResource.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingCalendarResource.getCreateDate()),
        Time.getShortTimestamp(newCalendarResource.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingCalendarResource.getModifiedDate()),
        Time.getShortTimestamp(newCalendarResource.getModifiedDate()));
    Assert.assertEquals(
        existingCalendarResource.getResourceBlockId(), newCalendarResource.getResourceBlockId());
    Assert.assertEquals(
        existingCalendarResource.getClassNameId(), newCalendarResource.getClassNameId());
    Assert.assertEquals(existingCalendarResource.getClassPK(), newCalendarResource.getClassPK());
    Assert.assertEquals(
        existingCalendarResource.getClassUuid(), newCalendarResource.getClassUuid());
    Assert.assertEquals(existingCalendarResource.getCode(), newCalendarResource.getCode());
    Assert.assertEquals(existingCalendarResource.getName(), newCalendarResource.getName());
    Assert.assertEquals(
        existingCalendarResource.getDescription(), newCalendarResource.getDescription());
    Assert.assertEquals(existingCalendarResource.getActive(), newCalendarResource.getActive());
    Assert.assertEquals(
        Time.getShortTimestamp(existingCalendarResource.getLastPublishDate()),
        Time.getShortTimestamp(newCalendarResource.getLastPublishDate()));
  }
  @Override
  public CalendarResource addCalendarResource(
      long userId,
      long groupId,
      long classNameId,
      long classPK,
      String classUuid,
      String code,
      Map<Locale, String> nameMap,
      Map<Locale, String> descriptionMap,
      boolean active,
      ServiceContext serviceContext)
      throws PortalException {

    // Calendar resource

    User user = userPersistence.findByPrimaryKey(userId);

    long calendarResourceId = counterLocalService.increment();

    if (classNameId == classNameLocalService.getClassNameId(CalendarResource.class)) {

      classPK = calendarResourceId;
    }

    if (PortletPropsValues.CALENDAR_RESOURCE_FORCE_AUTOGENERATE_CODE || Validator.isNull(code)) {

      code = String.valueOf(calendarResourceId);
    } else {
      code = code.trim();
      code = StringUtil.toUpperCase(code);
    }

    Date now = new Date();

    validate(groupId, classNameId, classPK, code, nameMap);

    CalendarResource calendarResource = calendarResourcePersistence.create(calendarResourceId);

    calendarResource.setUuid(serviceContext.getUuid());
    calendarResource.setGroupId(groupId);
    calendarResource.setCompanyId(user.getCompanyId());
    calendarResource.setUserId(user.getUserId());
    calendarResource.setUserName(user.getFullName());
    calendarResource.setCreateDate(serviceContext.getCreateDate(now));
    calendarResource.setModifiedDate(serviceContext.getModifiedDate(now));
    calendarResource.setClassNameId(classNameId);
    calendarResource.setClassPK(classPK);
    calendarResource.setClassUuid(classUuid);
    calendarResource.setCode(code);
    calendarResource.setNameMap(nameMap);
    calendarResource.setDescriptionMap(descriptionMap);
    calendarResource.setActive(active);

    calendarResourcePersistence.update(calendarResource);

    // Resources

    resourceLocalService.addModelResources(calendarResource, serviceContext);

    // Calendar

    if (!ExportImportThreadLocal.isImportInProcess()) {
      serviceContext.setAddGroupPermissions(true);
      serviceContext.setAddGuestPermissions(true);

      calendarLocalService.addCalendar(
          userId,
          calendarResource.getGroupId(),
          calendarResourceId,
          nameMap,
          descriptionMap,
          calendarResource.getTimeZoneId(),
          PortletPropsValues.CALENDAR_COLOR_DEFAULT,
          true,
          false,
          false,
          serviceContext);
    }

    // Asset

    updateAsset(
        calendarResource.getUserId(),
        calendarResource,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames());

    return calendarResource;
  }