Search in sources :

Example 11 with CreateAttachments

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));
}
Also used : CreateAttachments(com.enonic.xp.attachment.CreateAttachments) Form(com.enonic.xp.form.Form) ProcessUpdateParams(com.enonic.xp.content.processor.ProcessUpdateParams) PropertyTree(com.enonic.xp.data.PropertyTree) EditableContent(com.enonic.xp.content.EditableContent) ProcessUpdateResult(com.enonic.xp.content.processor.ProcessUpdateResult) XData(com.enonic.xp.schema.xdata.XData) Test(org.junit.jupiter.api.Test)

Example 12 with CreateAttachments

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());
}
Also used : UpdateContentParams(com.enonic.xp.content.UpdateContentParams) CreateContentParams(com.enonic.xp.content.CreateContentParams) Content(com.enonic.xp.content.Content) PropertyTree(com.enonic.xp.data.PropertyTree) ByteSource(com.google.common.io.ByteSource) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) Attachments(com.enonic.xp.attachment.Attachments) Test(org.junit.jupiter.api.Test)

Example 13 with CreateAttachments

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());
}
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 14 with CreateAttachments

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);
}
Also used : MediaInfo(com.enonic.xp.media.MediaInfo) Cropping(com.enonic.xp.image.Cropping) ContentTypeService(com.enonic.xp.schema.content.ContentTypeService) ContentEditor(com.enonic.xp.content.ContentEditor) MEDIA_INFO_BYTE_SIZE(com.enonic.xp.media.MediaInfo.MEDIA_INFO_BYTE_SIZE) Map(java.util.Map) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) ImageIO(javax.imageio.ImageIO) GetContentTypeParams(com.enonic.xp.schema.content.GetContentTypeParams) XDataName(com.enonic.xp.schema.xdata.XDataName) Media(com.enonic.xp.content.Media) Property(com.enonic.xp.data.Property) IMAGE_INFO_PIXEL_SIZE(com.enonic.xp.media.MediaInfo.IMAGE_INFO_PIXEL_SIZE) BufferedImage(java.awt.image.BufferedImage) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ContentProcessor(com.enonic.xp.content.processor.ContentProcessor) IMAGE_INFO_IMAGE_WIDTH(com.enonic.xp.media.MediaInfo.IMAGE_INFO_IMAGE_WIDTH) ContentType(com.enonic.xp.schema.content.ContentType) Collectors(java.util.stream.Collectors) ExtraDatas(com.enonic.xp.content.ExtraDatas) EditableContent(com.enonic.xp.content.EditableContent) InputTypeName(com.enonic.xp.inputtype.InputTypeName) FormItem(com.enonic.xp.form.FormItem) IMAGE_INFO_IMAGE_HEIGHT(com.enonic.xp.media.MediaInfo.IMAGE_INFO_IMAGE_HEIGHT) ProcessUpdateResult(com.enonic.xp.content.processor.ProcessUpdateResult) CreateContentParams(com.enonic.xp.content.CreateContentParams) ContentService(com.enonic.xp.content.ContentService) IMAGE_INFO(com.enonic.xp.media.MediaInfo.IMAGE_INFO) ProcessCreateResult(com.enonic.xp.content.processor.ProcessCreateResult) HashMap(java.util.HashMap) Attachment(com.enonic.xp.attachment.Attachment) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) ProcessCreateParams(com.enonic.xp.content.processor.ProcessCreateParams) Component(org.osgi.service.component.annotations.Component) FormItemType(com.enonic.xp.form.FormItemType) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) ExtraData(com.enonic.xp.content.ExtraData) XData(com.enonic.xp.schema.xdata.XData) Exceptions(com.enonic.xp.util.Exceptions) GeoPoint(com.enonic.xp.util.GeoPoint) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ByteSource(com.google.common.io.ByteSource) XDataService(com.enonic.xp.schema.xdata.XDataService) PropertyTree(com.enonic.xp.data.PropertyTree) ProcessUpdateParams(com.enonic.xp.content.processor.ProcessUpdateParams) XDatas(com.enonic.xp.schema.xdata.XDatas) IOException(java.io.IOException) ValueTypes(com.enonic.xp.data.ValueTypes) Preconditions(com.google.common.base.Preconditions) Input(com.enonic.xp.form.Input) Reference(org.osgi.service.component.annotations.Reference) InputStream(java.io.InputStream) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) MediaInfo(com.enonic.xp.media.MediaInfo) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) ContentEditor(com.enonic.xp.content.ContentEditor) XDatas(com.enonic.xp.schema.xdata.XDatas) ProcessUpdateResult(com.enonic.xp.content.processor.ProcessUpdateResult) ExtraData(com.enonic.xp.content.ExtraData) XDataName(com.enonic.xp.schema.xdata.XDataName)

Example 15 with CreateAttachments

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

Aggregations

CreateAttachments (com.enonic.xp.attachment.CreateAttachments)16 CreateAttachment (com.enonic.xp.attachment.CreateAttachment)11 CreateContentParams (com.enonic.xp.content.CreateContentParams)10 Test (org.junit.jupiter.api.Test)10 PropertyTree (com.enonic.xp.data.PropertyTree)9 ByteSource (com.google.common.io.ByteSource)9 Content (com.enonic.xp.content.Content)8 ContentTypeName (com.enonic.xp.schema.content.ContentTypeName)7 ContentPath (com.enonic.xp.content.ContentPath)6 UpdateContentParams (com.enonic.xp.content.UpdateContentParams)6 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)6 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)6 AttachmentNames (com.enonic.xp.attachment.AttachmentNames)5 ExtraDatas (com.enonic.xp.content.ExtraDatas)5 ContentPropertyNames (com.enonic.xp.content.ContentPropertyNames)4 ExtraData (com.enonic.xp.content.ExtraData)4 List (java.util.List)4 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)4 Attachments (com.enonic.xp.attachment.Attachments)3 ContentId (com.enonic.xp.content.ContentId)3