Search in sources :

Example 1 with UpdateContentTranslatorParams

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

the class ContentDataSerializerTest method update_propertyTree_populated_with_new_attachment_properties.

@Test
public void update_propertyTree_populated_with_new_attachment_properties() {
    final ContentDataSerializer contentDataSerializer = createContentDataSerializer();
    final String binaryName = "myName";
    final String binaryLabel = "myLabel";
    final String binaryMimeType = "myMimeType";
    final byte[] binaryData = "my binary".getBytes();
    final UpdateContentTranslatorParams params = UpdateContentTranslatorParams.create().editedContent(Content.create().name("myContent").parentPath(ContentPath.ROOT).creator(PrincipalKey.ofAnonymous()).validationErrors(ValidationErrors.create().add(ValidationError.attachmentError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "SOME_CODE"), BinaryReference.from("prevFile")).message("someError").build()).build()).build()).modifier(PrincipalKey.ofAnonymous()).createAttachments(CreateAttachments.from(CreateAttachment.create().byteSource(ByteSource.wrap(binaryData)).label(binaryLabel).mimeType(binaryMimeType).name(binaryName).build())).build();
    final PropertyTree data = contentDataSerializer.toUpdateNodeData(params);
    final PropertySet attachmentData = data.getSet(ContentPropertyNames.ATTACHMENT);
    assertNotNull(attachmentData);
    assertEquals(binaryName, attachmentData.getString(ContentPropertyNames.ATTACHMENT_NAME));
    assertEquals(binaryLabel, attachmentData.getString(ContentPropertyNames.ATTACHMENT_LABEL));
    assertEquals(binaryMimeType, attachmentData.getString(ContentPropertyNames.ATTACHMENT_MIMETYPE));
    assertEquals(binaryData.length + "", attachmentData.getString(ContentPropertyNames.ATTACHMENT_SIZE));
    assertEquals(binaryName, attachmentData.getString(ContentPropertyNames.ATTACHMENT_BINARY_REF));
}
Also used : UpdateContentTranslatorParams(com.enonic.xp.content.UpdateContentTranslatorParams) PropertyTree(com.enonic.xp.data.PropertyTree) PropertySet(com.enonic.xp.data.PropertySet) Test(org.junit.jupiter.api.Test)

Example 2 with UpdateContentTranslatorParams

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

the class ContentDataSerializerTest method update_validationErrors.

@Test
public void update_validationErrors() {
    final ContentDataSerializer contentDataSerializer = createContentDataSerializer();
    final String binaryName = "myName";
    final String binaryLabel = "myLabel";
    final String binaryMimeType = "myMimeType";
    final byte[] binaryData = "my binary".getBytes();
    final ValidationErrors validationErrors = ValidationErrors.create().add(ValidationError.attachmentError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "SOME_CODE"), BinaryReference.from("myName")).message("someError").build()).add(ValidationError.dataError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "SOME_OTHER_CODE"), PropertyPath.from("")).message("someDataError").build()).add(ValidationError.generalError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "SERIOUS_ERROR")).message("someError").build()).build();
    final UpdateContentTranslatorParams params = UpdateContentTranslatorParams.create().editedContent(Content.create().name("myContent").parentPath(ContentPath.ROOT).creator(PrincipalKey.ofAnonymous()).validationErrors(validationErrors).build()).modifier(PrincipalKey.ofAnonymous()).createAttachments(CreateAttachments.from(CreateAttachment.create().byteSource(ByteSource.wrap(binaryData)).label(binaryLabel).mimeType(binaryMimeType).name(binaryName).build())).build();
    final PropertyTree data = contentDataSerializer.toUpdateNodeData(params);
    final Iterable<PropertySet> validationErrorsData = data.getSets("validationErrors");
    assertThat(validationErrorsData).hasSize(3).extracting(propertySet -> propertySet.getString("errorCode")).containsExactly("system:SOME_CODE", "system:SOME_OTHER_CODE", "system:SERIOUS_ERROR");
    assertThat(validationErrorsData).extracting(propertySet -> propertySet.getString("attachment")).containsExactly("myName", null, null);
}
Also used : ChildOrder(com.enonic.xp.index.ChildOrder) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) WorkflowInfo(com.enonic.xp.content.WorkflowInfo) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) WorkflowState(com.enonic.xp.content.WorkflowState) ValidationErrors(com.enonic.xp.content.ValidationErrors) PageDescriptorService(com.enonic.xp.page.PageDescriptorService) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) ExtraData(com.enonic.xp.content.ExtraData) WorkflowCheckState(com.enonic.xp.content.WorkflowCheckState) Map(java.util.Map) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) PropertyPath(com.enonic.xp.data.PropertyPath) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteSource(com.google.common.io.ByteSource) ValidationError(com.enonic.xp.content.ValidationError) XDataName(com.enonic.xp.schema.xdata.XDataName) PropertyTree(com.enonic.xp.data.PropertyTree) PartDescriptorService(com.enonic.xp.region.PartDescriptorService) BinaryReference(com.enonic.xp.util.BinaryReference) ContentPropertyNames(com.enonic.xp.content.ContentPropertyNames) ValidationErrorCode(com.enonic.xp.content.ValidationErrorCode) ContentPath(com.enonic.xp.content.ContentPath) PropertySet(com.enonic.xp.data.PropertySet) Content(com.enonic.xp.content.Content) LayoutDescriptorService(com.enonic.xp.region.LayoutDescriptorService) ExtraDatas(com.enonic.xp.content.ExtraDatas) ApplicationKey(com.enonic.xp.app.ApplicationKey) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) PrincipalKey(com.enonic.xp.security.PrincipalKey) CreateContentTranslatorParams(com.enonic.xp.content.CreateContentTranslatorParams) UpdateContentTranslatorParams(com.enonic.xp.content.UpdateContentTranslatorParams) UpdateContentTranslatorParams(com.enonic.xp.content.UpdateContentTranslatorParams) ValidationErrors(com.enonic.xp.content.ValidationErrors) PropertyTree(com.enonic.xp.data.PropertyTree) PropertySet(com.enonic.xp.data.PropertySet) Test(org.junit.jupiter.api.Test)

Example 3 with UpdateContentTranslatorParams

use of com.enonic.xp.content.UpdateContentTranslatorParams 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 4 with UpdateContentTranslatorParams

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

the class UpdateNodeParamsFactory method toNodeEditor.

private NodeEditor toNodeEditor(final UpdateContentTranslatorParams params) {
    final Content content = params.getEditedContent();
    final PropertyTree nodeData = contentDataSerializer.toUpdateNodeData(params);
    final ContentIndexConfigFactory indexConfigFactory = ContentIndexConfigFactory.create().contentTypeService(contentTypeService).pageDescriptorService(pageDescriptorService).partDescriptorService(partDescriptorService).layoutDescriptorService(layoutDescriptorService).siteService(this.siteService).xDataService(this.xDataService).contentTypeName(content.getType()).page(content.getPage()).siteConfigs(content.isSite() ? ((Site) content).getSiteConfigs() : null).extraDatas(content.getAllExtraData()).language(content.getLanguage() != null ? content.getLanguage().getLanguage() : null).build();
    return editableNode -> {
        editableNode.indexConfigDocument = indexConfigFactory.produce();
        editableNode.data = nodeData;
        editableNode.manualOrderValue = content.getManualOrderValue();
        editableNode.permissions = content.getPermissions();
        editableNode.inheritPermissions = content.inheritsPermissions();
    };
}
Also used : PartDescriptorService(com.enonic.xp.region.PartDescriptorService) SiteService(com.enonic.xp.site.SiteService) Content(com.enonic.xp.content.Content) LayoutDescriptorService(com.enonic.xp.region.LayoutDescriptorService) ContentTypeService(com.enonic.xp.schema.content.ContentTypeService) NodeId(com.enonic.xp.node.NodeId) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams) PageDescriptorService(com.enonic.xp.page.PageDescriptorService) Site(com.enonic.xp.site.Site) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) ContentDataSerializer(com.enonic.xp.core.impl.content.serializer.ContentDataSerializer) Preconditions(com.google.common.base.Preconditions) ContentIndexConfigFactory(com.enonic.xp.core.impl.content.index.ContentIndexConfigFactory) UpdateContentTranslatorParams(com.enonic.xp.content.UpdateContentTranslatorParams) NodeEditor(com.enonic.xp.node.NodeEditor) XDataService(com.enonic.xp.schema.xdata.XDataService) PropertyTree(com.enonic.xp.data.PropertyTree) Content(com.enonic.xp.content.Content) PropertyTree(com.enonic.xp.data.PropertyTree) ContentIndexConfigFactory(com.enonic.xp.core.impl.content.index.ContentIndexConfigFactory)

Aggregations

UpdateContentTranslatorParams (com.enonic.xp.content.UpdateContentTranslatorParams)4 Content (com.enonic.xp.content.Content)3 PropertyTree (com.enonic.xp.data.PropertyTree)3 PageDescriptorService (com.enonic.xp.page.PageDescriptorService)3 LayoutDescriptorService (com.enonic.xp.region.LayoutDescriptorService)3 PartDescriptorService (com.enonic.xp.region.PartDescriptorService)3 CreateAttachment (com.enonic.xp.attachment.CreateAttachment)2 CreateAttachments (com.enonic.xp.attachment.CreateAttachments)2 ValidationErrors (com.enonic.xp.content.ValidationErrors)2 ContentDataSerializer (com.enonic.xp.core.impl.content.serializer.ContentDataSerializer)2 UpdateNodeParams (com.enonic.xp.node.UpdateNodeParams)2 ContentTypeName (com.enonic.xp.schema.content.ContentTypeName)2 Site (com.enonic.xp.site.Site)2 Preconditions (com.google.common.base.Preconditions)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)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