@Test
  public void testResetOriginalValues() throws Exception {
    CalendarResource newCalendarResource = addCalendarResource();

    _persistence.clearCache();

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

    Assert.assertTrue(
        Validator.equals(
            existingCalendarResource.getUuid(),
            ReflectionTestUtil.invoke(
                existingCalendarResource, "getOriginalUuid", new Class<?>[0])));
    Assert.assertEquals(
        Long.valueOf(existingCalendarResource.getGroupId()),
        ReflectionTestUtil.<Long>invoke(
            existingCalendarResource, "getOriginalGroupId", new Class<?>[0]));

    Assert.assertEquals(
        Long.valueOf(existingCalendarResource.getClassNameId()),
        ReflectionTestUtil.<Long>invoke(
            existingCalendarResource, "getOriginalClassNameId", new Class<?>[0]));
    Assert.assertEquals(
        Long.valueOf(existingCalendarResource.getClassPK()),
        ReflectionTestUtil.<Long>invoke(
            existingCalendarResource, "getOriginalClassPK", new Class<?>[0]));
  }
  protected void doExportStagedModel(
      PortletDataContext portletDataContext, CalendarResource calendarResource) throws Exception {

    Element calendarResourceElement = portletDataContext.getExportDataElement(calendarResource);

    for (Calendar calendar : calendarResource.getCalendars()) {
      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, calendarResource, calendar, PortletDataContext.REFERENCE_TYPE_STRONG);
    }

    if (calendarResource.getClassNameId() == PortalUtil.getClassNameId(User.class)) {

      User user = UserLocalServiceUtil.getUser(calendarResource.getClassPK());

      portletDataContext.addReferenceElement(
          calendarResource,
          calendarResourceElement,
          user,
          User.class,
          PortletDataContext.REFERENCE_TYPE_DEPENDENCY_DISPOSABLE,
          true);
    }

    portletDataContext.addClassedModel(
        calendarResourceElement,
        ExportImportPathUtil.getModelPath(calendarResource),
        calendarResource);
  }
  protected long getClassPK(
      PortletDataContext portletDataContext, CalendarResource calendarResource, long userId) {

    long classPK = 0;

    if (calendarResource.getClassNameId() == PortalUtil.getClassNameId(Group.class)) {

      classPK = portletDataContext.getScopeGroupId();
    } else if (calendarResource.getClassNameId() == PortalUtil.getClassNameId(User.class)) {

      classPK = userId;
    }

    return classPK;
  }
  protected boolean countStagedModel(
      PortletDataContext portletDataContext, CalendarResource calendarResource) {

    if (calendarResource.getClassNameId() == PortalUtil.getClassNameId(CalendarResource.class)) {

      return true;
    }

    return false;
  }
  @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()));
  }
  protected void doImportStagedModel(
      PortletDataContext portletDataContext, CalendarResource calendarResource) throws Exception {

    prepareLanguagesForImport(calendarResource);

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

    StagedModelDataHandlerUtil.importReferenceStagedModels(
        portletDataContext, calendarResource, Calendar.class);

    long classPK = getClassPK(portletDataContext, calendarResource, userId);
    Map<Locale, String> calendarResourceNameMap =
        getCalendarResourceNameMap(portletDataContext, calendarResource);

    ServiceContext serviceContext = portletDataContext.createServiceContext(calendarResource);

    CalendarResource importedCalendarResource = null;

    if (portletDataContext.isDataStrategyMirror()) {
      CalendarResource existingCalendarResource =
          CalendarResourceLocalServiceUtil.fetchCalendarResourceByUuidAndGroupId(
              calendarResource.getUuid(), portletDataContext.getScopeGroupId());

      if (existingCalendarResource == null) {
        existingCalendarResource =
            CalendarResourceLocalServiceUtil.fetchCalendarResource(
                calendarResource.getClassNameId(), classPK);
      }

      if (existingCalendarResource == null) {
        serviceContext.setUuid(calendarResource.getUuid());

        importedCalendarResource =
            CalendarResourceLocalServiceUtil.addCalendarResource(
                userId,
                portletDataContext.getScopeGroupId(),
                calendarResource.getClassNameId(),
                classPK,
                calendarResource.getClassUuid(),
                calendarResource.getCode(),
                calendarResourceNameMap,
                calendarResource.getDescriptionMap(),
                calendarResource.isActive(),
                serviceContext);
      } else {
        importedCalendarResource =
            CalendarResourceLocalServiceUtil.updateCalendarResource(
                existingCalendarResource.getCalendarResourceId(),
                calendarResource.getNameMap(),
                calendarResource.getDescriptionMap(),
                calendarResource.isActive(),
                serviceContext);
      }
    } else {
      importedCalendarResource =
          CalendarResourceLocalServiceUtil.addCalendarResource(
              userId,
              portletDataContext.getScopeGroupId(),
              calendarResource.getClassNameId(),
              classPK,
              calendarResource.getClassUuid(),
              calendarResource.getCode(),
              calendarResourceNameMap,
              calendarResource.getDescriptionMap(),
              calendarResource.isActive(),
              serviceContext);
    }

    updateCalendars(portletDataContext, calendarResource, importedCalendarResource);

    portletDataContext.importClassedModel(calendarResource, importedCalendarResource);
  }