use of com.enonic.xp.attachment.CreateAttachments in project xp by enonic.
the class ImageContentProcessorTest method testProcessUpdateWithMediaInfo.
@Test
public void testProcessUpdateWithMediaInfo() throws IOException {
final Form.Builder form = Form.create();
form.addFormItem(createTextLine("shutterTime", "Exposure Time").occurrences(0, 1).build());
form.addFormItem(createTextLine("altitude", "Gps Altitude").occurrences(0, 1).build());
final XData xDataInfo = createXData(MediaInfo.IMAGE_INFO_METADATA_NAME, "Extra Info", form.build());
Mockito.when(this.xDataService.getFromContentType(Mockito.any())).thenReturn(XDatas.from(xDataInfo));
final CreateAttachments createAttachments = createAttachments();
final ProcessUpdateParams processUpdateParams = ProcessUpdateParams.create().contentType(ContentType.create().superType(ContentTypeName.imageMedia()).name("myContent").build()).mediaInfo(MediaInfo.create().addMetadata("exposure time", "1").addMetadata("gps altitude ", "2").build()).createAttachments(createAttachments).build();
final ProcessUpdateResult result = this.imageContentProcessor.processUpdate(processUpdateParams);
final PropertyTree data = new PropertyTree();
final EditableContent editableContent = new EditableContent(Content.create().name("myContentName").parentPath(ContentPath.ROOT).data(data).build());
result.getEditor().edit(editableContent);
assertEquals(editableContent.extraDatas.first().getData().getString("shutterTime", 0), "1");
assertEquals(editableContent.extraDatas.first().getData().getString("altitude", 0), "2");
assertEquals(13, editableContent.extraDatas.first().getData().getLong(MediaInfo.MEDIA_INFO_BYTE_SIZE, 0));
}
use of com.enonic.xp.attachment.CreateAttachments in project xp by enonic.
the class ContentServiceImplTest_update method update_content_image.
@Test
public void update_content_image() throws Exception {
final ByteSource image = loadImage("cat-small.jpg");
final CreateContentParams createContentParams = CreateContentParams.create().contentData(new PropertyTree()).displayName("This is my content").parent(ContentPath.ROOT).type(ContentTypeName.imageMedia()).createAttachments(createAttachment("cat", "image/jpg", image)).build();
final Content content = this.contentService.create(createContentParams);
final UpdateContentParams updateContentParams = new UpdateContentParams();
updateContentParams.contentId(content.getId()).editor(edit -> {
edit.displayName = "new display name";
}).clearAttachments(true).createAttachments(createAttachment("darth", "image/jpg", loadImage("darth-small.jpg")));
this.contentService.update(updateContentParams);
final Content storedContent = this.contentService.getById(content.getId());
final Attachments attachments = storedContent.getAttachments();
assertEquals(1, attachments.getSize());
}
use of com.enonic.xp.attachment.CreateAttachments 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.attachment.CreateAttachments in project xp by enonic.
the class ImageContentProcessor method processUpdate.
@Override
public ProcessUpdateResult processUpdate(final ProcessUpdateParams params) {
final CreateAttachments createAttachments = params.getCreateAttachments();
final MediaInfo mediaInfo = params.getMediaInfo();
final CreateAttachment sourceAttachment = createAttachments == null ? null : createAttachments.first();
final ContentEditor editor;
if (mediaInfo != null) {
editor = editable -> {
final Map<XDataName, ExtraData> extraDatas = editable.extraDatas.stream().collect(Collectors.toMap(ExtraData::getName, o -> o));
final XDatas contentXDatas = getXDatas(params.getContentType().getName());
extractMetadata(mediaInfo, contentXDatas, sourceAttachment).forEach(extraData -> extraDatas.put(extraData.getName(), extraData));
editable.extraDatas = ExtraDatas.create().addAll(extraDatas.values()).build();
};
} else {
editor = editable -> {
if (!params.getContentType().getName().isDescendantOfMedia()) {
return;
}
editable.extraDatas = updateImageMetadata(editable);
};
}
return new ProcessUpdateResult(createAttachments, editor);
}
use of com.enonic.xp.attachment.CreateAttachments 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);
}
Aggregations