use of com.enonic.xp.util.BinaryReferences in project xp by enonic.
the class ContentDataSerializer method mergeExistingAndUpdatedAttachments.
private Attachments mergeExistingAndUpdatedAttachments(final Attachments existingAttachments, final UpdateContentTranslatorParams params) {
CreateAttachments createAttachments = params.getCreateAttachments();
BinaryReferences removeAttachments = params.getRemoveAttachments();
if (createAttachments == null && removeAttachments == null && !params.isClearAttachments()) {
return existingAttachments;
}
createAttachments = createAttachments == null ? CreateAttachments.empty() : createAttachments;
removeAttachments = removeAttachments == null ? BinaryReferences.empty() : removeAttachments;
final Map<BinaryReference, Attachment> attachments = new LinkedHashMap<>();
if (!params.isClearAttachments()) {
existingAttachments.stream().forEach((a) -> attachments.put(a.getBinaryReference(), a));
}
removeAttachments.stream().forEach(attachments::remove);
// added attachments with same BinaryReference will replace existing ones
for (final CreateAttachment createAttachment : createAttachments) {
final Attachment attachment = Attachment.create().name(createAttachment.getName()).label(createAttachment.getLabel()).mimeType(createAttachment.getMimeType()).size(attachmentSize(createAttachment)).textContent(createAttachment.getTextContent()).build();
attachments.put(attachment.getBinaryReference(), attachment);
}
return Attachments.from(attachments.values());
}
use of com.enonic.xp.util.BinaryReferences in project xp by enonic.
the class UpdateContentCommand method doExecute.
private Content doExecute() {
final Content contentBeforeChange = getContent(params.getContentId());
Content editedContent = editContent(params.getEditor(), contentBeforeChange);
if (params.stopInherit()) {
if (editedContent.getInherit().contains(ContentInheritType.CONTENT)) {
nodeService.commit(NodeCommitEntry.create().message("Base inherited version").build(), NodeIds.from(params.getContentId().toString()));
editedContent.getInherit().remove(ContentInheritType.CONTENT);
}
editedContent.getInherit().remove(ContentInheritType.NAME);
}
final BinaryReferences removeAttachments = Objects.requireNonNullElseGet(params.getRemoveAttachments(), BinaryReferences::empty);
if (contentBeforeChange.equals(editedContent) && params.getCreateAttachments() == null && removeAttachments.isEmpty() && !this.params.isClearAttachments()) {
return contentBeforeChange;
}
editedContent = processContent(contentBeforeChange, editedContent);
validateBlockingChecks(editedContent);
final ValidationErrors.Builder validationErrorsBuilder = ValidationErrors.create();
if (!params.isClearAttachments() && contentBeforeChange.getValidationErrors() != null) {
contentBeforeChange.getValidationErrors().stream().filter(validationError -> validationError instanceof AttachmentValidationError).map(validationError -> (AttachmentValidationError) validationError).filter(validationError -> !removeAttachments.contains(validationError.getAttachment())).forEach(validationErrorsBuilder::add);
}
final ValidationErrors validationErrors = ValidateContentDataCommand.create().contentId(editedContent.getId()).data(editedContent.getData()).extraDatas(editedContent.getAllExtraData()).contentTypeName(editedContent.getType()).contentName(editedContent.getName()).displayName(editedContent.getDisplayName()).createAttachments(params.getCreateAttachments()).contentValidators(this.contentValidators).contentTypeService(this.contentTypeService).validationErrorsBuilder(validationErrorsBuilder).build().execute();
if (params.isRequireValid()) {
validationErrors.stream().findFirst().ifPresent(validationError -> {
throw new ContentDataValidationException(validationError.getMessage());
});
}
editedContent = Content.create(editedContent).valid(!validationErrors.hasErrors()).validationErrors(validationErrors).build();
editedContent = attachThumbnail(editedContent);
editedContent = setModifiedTime(editedContent);
final UpdateContentTranslatorParams updateContentTranslatorParams = UpdateContentTranslatorParams.create().editedContent(editedContent).createAttachments(this.params.getCreateAttachments()).removeAttachments(this.params.getRemoveAttachments()).clearAttachments(this.params.isClearAttachments()).modifier(getCurrentUser().getKey()).build();
final UpdateNodeParams updateNodeParams = UpdateNodeParamsFactory.create(updateContentTranslatorParams).contentTypeService(this.contentTypeService).xDataService(this.xDataService).pageDescriptorService(this.pageDescriptorService).partDescriptorService(this.partDescriptorService).layoutDescriptorService(this.layoutDescriptorService).contentDataSerializer(this.contentDataSerializer).siteService(this.siteService).build().produce();
final Node editedNode = this.nodeService.update(updateNodeParams);
return translator.fromNode(editedNode, true);
}
use of com.enonic.xp.util.BinaryReferences in project xp by enonic.
the class RemoveAttachmentHandler method execute.
public void execute() {
UpdateContentParams updateContent = new UpdateContentParams();
if (!this.key.startsWith("/")) {
updateContent.contentId(ContentId.from(this.key));
} else {
final Content contentByPath = this.contentService.getByPath(ContentPath.from(key));
updateContent.contentId(contentByPath.getId());
}
BinaryReferences binaryRefs = BinaryReferences.from(Arrays.stream(this.names).map(BinaryReference::from).collect(toList()));
updateContent.removeAttachments(binaryRefs);
contentService.update(updateContent);
}
Aggregations