Search in sources :

Example 1 with BinaryReferences

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());
}
Also used : CreateAttachments(com.enonic.xp.attachment.CreateAttachments) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) BinaryReferences(com.enonic.xp.util.BinaryReferences) BinaryReference(com.enonic.xp.util.BinaryReference) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) Attachment(com.enonic.xp.attachment.Attachment) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with BinaryReferences

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);
}
Also used : ContentDataValidationException(com.enonic.xp.content.ContentDataValidationException) BinaryReferences(com.enonic.xp.util.BinaryReferences) MediaInfo(com.enonic.xp.media.MediaInfo) Node(com.enonic.xp.node.Node) LoggerFactory(org.slf4j.LoggerFactory) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) ValidationErrors(com.enonic.xp.content.ValidationErrors) PageDescriptorService(com.enonic.xp.page.PageDescriptorService) ContentEditor(com.enonic.xp.content.ContentEditor) GetContentTypeParams(com.enonic.xp.schema.content.GetContentTypeParams) Thumbnail(com.enonic.xp.icon.Thumbnail) PartDescriptorService(com.enonic.xp.region.PartDescriptorService) Media(com.enonic.xp.content.Media) Logger(org.slf4j.Logger) ContentAccessException(com.enonic.xp.content.ContentAccessException) ProcessUpdateParams(com.enonic.xp.content.processor.ProcessUpdateParams) ContentInheritType(com.enonic.xp.content.ContentInheritType) Content(com.enonic.xp.content.Content) ContentProcessor(com.enonic.xp.content.processor.ContentProcessor) InputTypes(com.enonic.xp.inputtype.InputTypes) ContentPublishInfo(com.enonic.xp.content.ContentPublishInfo) LayoutDescriptorService(com.enonic.xp.region.LayoutDescriptorService) Instant(java.time.Instant) ContentType(com.enonic.xp.schema.content.ContentType) AttachmentValidationError(com.enonic.xp.content.AttachmentValidationError) UpdateContentParams(com.enonic.xp.content.UpdateContentParams) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams) Objects(java.util.Objects) EditableContent(com.enonic.xp.content.EditableContent) NodeCommitEntry(com.enonic.xp.node.NodeCommitEntry) Site(com.enonic.xp.site.Site) InputValidator(com.enonic.xp.core.impl.content.validate.InputValidator) EditableSite(com.enonic.xp.content.EditableSite) NodeAccessException(com.enonic.xp.node.NodeAccessException) ContentDataSerializer(com.enonic.xp.core.impl.content.serializer.ContentDataSerializer) Preconditions(com.google.common.base.Preconditions) UpdateContentTranslatorParams(com.enonic.xp.content.UpdateContentTranslatorParams) NodeIds(com.enonic.xp.node.NodeIds) ProcessUpdateResult(com.enonic.xp.content.processor.ProcessUpdateResult) UpdateContentTranslatorParams(com.enonic.xp.content.UpdateContentTranslatorParams) ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) EditableContent(com.enonic.xp.content.EditableContent) ContentDataValidationException(com.enonic.xp.content.ContentDataValidationException) BinaryReferences(com.enonic.xp.util.BinaryReferences) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams) Node(com.enonic.xp.node.Node) AttachmentValidationError(com.enonic.xp.content.AttachmentValidationError)

Example 3 with BinaryReferences

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);
}
Also used : UpdateContentParams(com.enonic.xp.content.UpdateContentParams) Content(com.enonic.xp.content.Content) BinaryReferences(com.enonic.xp.util.BinaryReferences) BinaryReference(com.enonic.xp.util.BinaryReference)

Aggregations

BinaryReferences (com.enonic.xp.util.BinaryReferences)3 Content (com.enonic.xp.content.Content)2 UpdateContentParams (com.enonic.xp.content.UpdateContentParams)2 BinaryReference (com.enonic.xp.util.BinaryReference)2 Attachment (com.enonic.xp.attachment.Attachment)1 CreateAttachment (com.enonic.xp.attachment.CreateAttachment)1 CreateAttachments (com.enonic.xp.attachment.CreateAttachments)1 AttachmentValidationError (com.enonic.xp.content.AttachmentValidationError)1 ContentAccessException (com.enonic.xp.content.ContentAccessException)1 ContentDataValidationException (com.enonic.xp.content.ContentDataValidationException)1 ContentEditor (com.enonic.xp.content.ContentEditor)1 ContentInheritType (com.enonic.xp.content.ContentInheritType)1 ContentPublishInfo (com.enonic.xp.content.ContentPublishInfo)1 EditableContent (com.enonic.xp.content.EditableContent)1 EditableSite (com.enonic.xp.content.EditableSite)1 Media (com.enonic.xp.content.Media)1 UpdateContentTranslatorParams (com.enonic.xp.content.UpdateContentTranslatorParams)1 ValidationErrors (com.enonic.xp.content.ValidationErrors)1 ContentProcessor (com.enonic.xp.content.processor.ContentProcessor)1 ProcessUpdateParams (com.enonic.xp.content.processor.ProcessUpdateParams)1