public int updateFileContent(UpdateFileContentParams params) {
    validateUpdateFileContentParams(params);

    ContentVersionKey contentVersionKey =
        resolveContentVersionKey(
            params.createNewVersion, params.contentKey, params.contentVersionKey);

    UpdateContentCommand command;
    if (params.createNewVersion) {
      command = UpdateContentCommand.storeNewVersionEvenIfUnchanged(contentVersionKey);
    } else {
      command = UpdateContentCommand.updateExistingVersion2(contentVersionKey);
    }

    command.setContentKey(new ContentKey(params.contentKey));
    command.setSyncRelatedContent(false);
    command.setSyncAccessRights(false);
    command.setModifier(securityService.getImpersonatedPortalUser());
    command.setAvailableFrom(params.publishFrom);
    command.setAvailableTo(params.publishTo);
    command.setStatus(ContentStatus.get(params.status));
    if (params.siteKey != null) {
      command.setSiteKey(new SiteKey(params.siteKey));
    }

    LegacyFileContentData newContentData;
    List<BinaryDataAndBinary> binariesToAdd = null;
    List<BinaryDataKey> binariesToRemove = null;
    if (params.fileContentData != null) {
      newContentData =
          (LegacyFileContentData) fileContentResolver.resolveContentdata(params.fileContentData);
      command.setContentData(newContentData);
      if (!params.createNewVersion) {
        // only delete previous binaries if we are overwriting current version
        ContentVersionEntity persistedVersion = contentVersionDao.findByKey(contentVersionKey);
        LegacyFileContentData previousContentData =
            (LegacyFileContentData) persistedVersion.getContentData();
        binariesToRemove = previousContentData.getRemovedBinaries(newContentData);
      }
      // Find new binaries
      binariesToAdd = newContentData.getBinaryDataAndBinaryList();
    } else {
      // only update the meta data in this case..
    }

    command.setUpdateAsMainVersion(params.setAsCurrentVersion);
    command.setUseCommandsBinaryDataToAdd(true);
    command.setBinaryDataToAdd(binariesToAdd);

    command.setUseCommandsBinaryDataToRemove(true);
    command.setBinaryDataToRemove(binariesToRemove);
    if (params.siteKey != null) {
      command.setSiteKey(new SiteKey(params.siteKey));
    }

    UpdateContentResult updateContentResult = contentService.updateContent(command);

    if (updateContentResult.isAnyChangesMade()) {
      new PageCacheInvalidatorForContent(pageCacheService)
          .invalidateForContent(updateContentResult.getTargetedVersion());
    }

    return updateContentResult.getTargetedVersionKey().toInt();
  }
  private UpdateContentCommand updateContentCommand(
      ContentKey contentKeyToUpdate, ContentData contentData, String updaterUid) {
    ContentEntity contentToUpdate = fixture.findContentByKey(contentKeyToUpdate);

    UpdateContentCommand command =
        UpdateContentCommand.storeNewVersionEvenIfUnchanged(
            contentToUpdate.getMainVersion().getKey());
    command.setUpdateAsMainVersion(true);
    command.setSyncAccessRights(false);
    command.setSyncRelatedContent(true);
    command.setContentKey(contentToUpdate.getKey());
    command.setUpdateStrategy(UpdateContentCommand.UpdateStrategy.MODIFY);
    command.setModifier(fixture.findUserByName(updaterUid).getKey());
    command.setPriority(0);
    command.setLanguage(fixture.findLanguageByCode("en"));
    command.setStatus(ContentStatus.APPROVED);
    command.setContentData(contentData);
    command.setAvailableFrom(DATE_TIME_2010_01_01.toDate());
    return command;
  }
  public int updateContent(UpdateContentParams params) {
    validateUpdateContentParams(params);

    final ContentVersionKey contentVersionKey =
        resolveContentVersionKey(
            params.createNewVersion, params.contentKey, params.contentVersionKey);

    UpdateContentCommand command;
    if (params.createNewVersion) {
      command = UpdateContentCommand.storeNewVersionEvenIfUnchanged(contentVersionKey);
    } else {
      command = UpdateContentCommand.updateExistingVersion2(contentVersionKey);
    }

    command.setContentName(params.name);
    command.setSyncRelatedContent(true);
    command.setSyncAccessRights(false);
    command.setModifier(securityService.getImpersonatedPortalUser());
    command.setUpdateAsMainVersion(params.setAsCurrentVersion);
    command.setContentKey(new ContentKey(params.contentKey));
    command.setAvailableFrom(params.publishFrom);
    command.setAvailableTo(params.publishTo);
    command.setStatus(ContentStatus.get(params.status));
    command.setUseCommandsBinaryDataToRemove(true);
    command.setUseCommandsBinaryDataToAdd(true);
    command.setChangeComment(params.changeComment);
    if (params.siteKey != null) {
      command.setSiteKey(new SiteKey(params.siteKey));
    }

    if (params.contentData != null) {
      final ContentTypeEntity contentType = resolveContentType(params.contentKey);
      final ContentDataResolver customContentResolver = new ContentDataResolver();
      final CustomContentData newContentData =
          customContentResolver.resolveContentdata(params.contentData, contentType);
      command.setContentData(newContentData);
      if (!params.createNewVersion) {
        // only delete previous binaries if we are overwriting current version
        final ContentVersionEntity persistedVersion =
            contentVersionDao.findByKey(contentVersionKey);
        final CustomContentData persistedContentData =
            (CustomContentData) persistedVersion.getContentData();
        final List<BinaryDataEntry> deletedBinaries =
            persistedContentData.getRemovedBinaryDataEntries(newContentData);
        command.setBinaryDataToRemove(BinaryDataEntry.createBinaryDataKeyList(deletedBinaries));
        command.setUseCommandsBinaryDataToRemove(true);
      }

      // Find new binaries
      final List<BinaryDataEntry> binaryEntries = newContentData.getBinaryDataEntryList();
      command.setBinaryDataToAdd(BinaryDataAndBinary.convert(binaryEntries));
      command.setUseCommandsBinaryDataToAdd(true);
    } else {
      // only update the meta data in this case..
    }

    if (params.updateStrategy == ContentDataInputUpdateStrategy.REPLACE_NEW) {
      command.setUpdateStrategy(UpdateStrategy.MODIFY);
    }

    final UpdateContentResult updateContentResult = contentService.updateContent(command);

    if (updateContentResult.isAnyChangesMade()) {
      new PageCacheInvalidatorForContent(pageCacheService)
          .invalidateForContent(updateContentResult.getTargetedVersion());
    }

    return updateContentResult.getTargetedVersionKey().toInt();
  }
  @Test
  public void testUpdateCurrentVersion() {
    ContentKey relatedContentKey1 = storeSimpleContent("rel1");
    ContentKey relatedContentKey2 = storeSimpleContent("rel2");
    ContentKey relatedContentKey3 = storeSimpleContent("rel3");
    ContentKey relatedContentKey4 = storeSimpleContent("rel4");
    ContentKey relatedContentKey5 = storeSimpleContent("rel5");

    ContentEntity content = factory.createContent("MyCategory", "en", "testuser", "0", new Date());
    ContentVersionEntity version = factory.createContentVersion("0", "testuser");

    ContentTypeConfig contentTypeConfig =
        ContentTypeConfigParser.parse(ContentHandlerName.CUSTOM, configEl);
    CustomContentData contentData = new CustomContentData(contentTypeConfig);
    TextDataEntryConfig titleConfig =
        new TextDataEntryConfig("myTitle", true, "Tittel", "contentdata/mytitle");
    contentData.add(new TextDataEntry(titleConfig, "test title"));

    RelatedContentDataEntryConfig multipleRelatedContentsConfig =
        (RelatedContentDataEntryConfig)
            contentTypeConfig.getInputConfig("myMultipleRelatedContent");

    contentData.add(
        new RelatedContentsDataEntry(multipleRelatedContentsConfig)
            .add(new RelatedContentDataEntry(multipleRelatedContentsConfig, relatedContentKey1))
            .add(new RelatedContentDataEntry(multipleRelatedContentsConfig, relatedContentKey2)));

    RelatedContentDataEntryConfig soleRelatedConfig =
        (RelatedContentDataEntryConfig) contentTypeConfig.getInputConfig("mySoleRelatedContent");

    contentData.add(new RelatedContentDataEntry(soleRelatedConfig, relatedContentKey3));

    version.setContentData(contentData);

    UserEntity runningUser = fixture.findUserByName("testuser");

    CreateContentCommand createContentCommand = new CreateContentCommand();
    createContentCommand.setCreator(runningUser);

    createContentCommand.populateCommandWithContentValues(content);
    createContentCommand.populateCommandWithContentVersionValues(version);

    createContentCommand.setBinaryDatas(new ArrayList<BinaryDataAndBinary>());
    createContentCommand.setUseCommandsBinaryDataToAdd(true);

    ContentKey contentKey = contentService.createContent(createContentCommand);

    hibernateTemplate.flush();
    hibernateTemplate.clear();

    ContentEntity persistedContent = contentDao.findByKey(contentKey);
    assertNotNull(persistedContent);

    ContentVersionEntity persistedVersion = persistedContent.getMainVersion();
    assertNotNull(persistedVersion);

    assertEquals(3, persistedVersion.getRelatedChildren(true).size());

    ContentEntity changedContent =
        factory.createContent("MyCategory", "en", "testuser", "0", new Date());
    changedContent.setKey(contentKey);
    ContentVersionEntity changedVersion = factory.createContentVersion("0", "testuser");
    changedVersion.setKey(persistedVersion.getKey());

    CustomContentData changedCD = new CustomContentData(contentTypeConfig);

    TextDataEntryConfig changedTitleConfig =
        new TextDataEntryConfig("myTitle", true, "Tittel", "contentdata/mytitle");
    changedCD.add(new TextDataEntry(changedTitleConfig, "changed title"));

    changedCD.add(
        new RelatedContentsDataEntry(multipleRelatedContentsConfig)
            .add(new RelatedContentDataEntry(multipleRelatedContentsConfig, relatedContentKey3))
            .add(new RelatedContentDataEntry(multipleRelatedContentsConfig, relatedContentKey5)));

    changedCD.add(new RelatedContentDataEntry(soleRelatedConfig, relatedContentKey4));

    changedVersion.setContentData(changedCD);

    UpdateContentCommand updateContentCommand =
        UpdateContentCommand.updateExistingVersion2(persistedVersion.getKey());
    updateContentCommand.setModifier(runningUser);
    updateContentCommand.setUpdateAsMainVersion(false);

    updateContentCommand.populateContentValuesFromContent(persistedContent);
    updateContentCommand.populateContentVersionValuesFromContentVersion(changedVersion);

    contentService.updateContent(updateContentCommand);

    hibernateTemplate.flush();
    hibernateTemplate.clear();

    ContentEntity contentAfterUpdate = contentDao.findByKey(contentKey);
    ContentVersionEntity versionAfterUpdate =
        contentVersionDao.findByKey(persistedVersion.getKey());

    Document contentDataXmlAfterUpdate = versionAfterUpdate.getContentDataAsJDomDocument();

    AssertTool.assertXPathEquals(
        "/contentdata/mysolerelatedcontent/@key",
        contentDataXmlAfterUpdate,
        relatedContentKey4.toString());
    AssertTool.assertXPathEquals(
        "/contentdata/myrelatedcontents/content[1]/@key",
        contentDataXmlAfterUpdate,
        relatedContentKey3.toString());
    AssertTool.assertXPathEquals(
        "/contentdata/myrelatedcontents/content[2]/@key",
        contentDataXmlAfterUpdate,
        relatedContentKey5.toString());

    assertEquals(3, versionAfterUpdate.getRelatedChildren(true).size());
  }