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