Search in sources :

Example 1 with AttachmentValidationError

use of com.enonic.xp.content.AttachmentValidationError in project xp by enonic.

the class ContentDataSerializer method addValidationErrors.

private void addValidationErrors(final ValidationErrors validationErrors, final PropertySet contentAsData) {
    if (validationErrors != null && validationErrors.hasErrors()) {
        contentAsData.addSets(VALIDATION_ERRORS, validationErrors.stream().map(validationError -> {
            final PropertySet propertySet = new PropertySet();
            propertySet.addString("errorCode", validationError.getErrorCode().toString());
            propertySet.addString("message", validationError.getMessage());
            propertySet.addString("i18n", validationError.getI18n());
            if (!validationError.getArgs().isEmpty()) {
                try {
                    propertySet.addString("args", OBJECT_MAPPER.writeValueAsString(validationError.getArgs()));
                } catch (JsonProcessingException e) {
                    throw new UncheckedIOException(e);
                }
            }
            if (validationError instanceof DataValidationError) {
                propertySet.addString("propertyPath", ((DataValidationError) validationError).getPropertyPath().toString());
            } else if (validationError instanceof AttachmentValidationError) {
                propertySet.addString("attachment", ((AttachmentValidationError) validationError).getAttachment().toString());
            }
            return propertySet;
        }).toArray(PropertySet[]::new));
    }
}
Also used : PropertySet(com.enonic.xp.data.PropertySet) UncheckedIOException(java.io.UncheckedIOException) AttachmentValidationError(com.enonic.xp.content.AttachmentValidationError) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DataValidationError(com.enonic.xp.content.DataValidationError)

Example 2 with AttachmentValidationError

use of com.enonic.xp.content.AttachmentValidationError 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)

Aggregations

AttachmentValidationError (com.enonic.xp.content.AttachmentValidationError)2 Content (com.enonic.xp.content.Content)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 DataValidationError (com.enonic.xp.content.DataValidationError)1 EditableContent (com.enonic.xp.content.EditableContent)1 EditableSite (com.enonic.xp.content.EditableSite)1 Media (com.enonic.xp.content.Media)1 UpdateContentParams (com.enonic.xp.content.UpdateContentParams)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 ProcessUpdateResult (com.enonic.xp.content.processor.ProcessUpdateResult)1 ContentDataSerializer (com.enonic.xp.core.impl.content.serializer.ContentDataSerializer)1 InputValidator (com.enonic.xp.core.impl.content.validate.InputValidator)1 PropertySet (com.enonic.xp.data.PropertySet)1