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