Search in sources :

Example 6 with XData

use of com.enonic.xp.schema.xdata.XData in project xp by enonic.

the class ImageContentProcessor method extractMetadata.

private ExtraDatas extractMetadata(final MediaInfo mediaInfo, final XDatas xDatas, final CreateAttachment sourceAttachment) {
    final ExtraDatas.Builder extradatasBuilder = ExtraDatas.create();
    final Map<XDataName, ExtraData> metadataMap = new HashMap<>();
    final ExtraData geoData = extractGeoLocation(mediaInfo, xDatas);
    if (geoData != null) {
        metadataMap.put(MediaInfo.GPS_INFO_METADATA_NAME, geoData);
        extradatasBuilder.add(geoData);
    }
    for (Map.Entry<String, Collection<String>> entry : mediaInfo.getMetadata().asMap().entrySet()) {
        for (XData xData : xDatas) {
            final String formItemName = getConformityName(entry.getKey());
            final FormItem formItem = xData.getForm().getFormItems().getItemByName(formItemName);
            if (formItem == null) {
                continue;
            }
            ExtraData extraData = metadataMap.get(xData.getName());
            if (extraData == null) {
                extraData = new ExtraData(xData.getName(), new PropertyTree());
                metadataMap.put(xData.getName(), extraData);
                extradatasBuilder.add(extraData);
            }
            if (FormItemType.INPUT.equals(formItem.getType())) {
                Input input = (Input) formItem;
                if (InputTypeName.DATE_TIME.equals(input.getInputType())) {
                    extraData.getData().addLocalDateTime(formItemName, ValueTypes.LOCAL_DATE_TIME.convert(entry.getValue().toArray()[0]));
                } else if (InputTypeName.LONG.equals(input.getInputType())) {
                    final Long[] longValues = entry.getValue().stream().map(Long::parseLong).toArray(Long[]::new);
                    extraData.getData().addLongs(formItemName, longValues);
                } else {
                    extraData.getData().addStrings(formItemName, entry.getValue());
                }
            }
        }
    }
    fillComputedFormItems(metadataMap.values(), mediaInfo, sourceAttachment);
    return extradatasBuilder.build();
}
Also used : HashMap(java.util.HashMap) FormItem(com.enonic.xp.form.FormItem) ExtraData(com.enonic.xp.content.ExtraData) XDataName(com.enonic.xp.schema.xdata.XDataName) Input(com.enonic.xp.form.Input) PropertyTree(com.enonic.xp.data.PropertyTree) ExtraDatas(com.enonic.xp.content.ExtraDatas) Collection(java.util.Collection) XData(com.enonic.xp.schema.xdata.XData) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 7 with XData

use of com.enonic.xp.schema.xdata.XData in project xp by enonic.

the class ImageContentProcessor method extractGeoLocation.

private ExtraData extractGeoLocation(final MediaInfo mediaInfo, final XDatas xDatas) {
    final ImmutableMultimap<String, String> mediaItems = mediaInfo.getMetadata();
    final Double geoLat = parseDouble(mediaItems.get(GEO_LATITUDE).stream().findFirst().orElse(null));
    final Double geoLong = parseDouble(mediaItems.get(GEO_LONGITUDE).stream().findFirst().orElse(null));
    if (geoLat == null || geoLong == null) {
        return null;
    }
    final XData geoMixin = xDatas.getXData(MediaInfo.GPS_INFO_METADATA_NAME);
    if (geoMixin == null) {
        return null;
    }
    final ExtraData extraData = new ExtraData(geoMixin.getName(), new PropertyTree());
    final FormItem formItem = geoMixin.getForm().getFormItems().getItemByName(MediaInfo.GPS_INFO_GEO_POINT);
    if (FormItemType.INPUT.equals(formItem.getType())) {
        final Input input = (Input) formItem;
        if (InputTypeName.GEO_POINT.equals(input.getInputType())) {
            final GeoPoint geoPoint = new GeoPoint(geoLat, geoLong);
            extraData.getData().addGeoPoint(formItem.getName(), ValueTypes.GEO_POINT.convert(geoPoint));
        }
    }
    return extraData;
}
Also used : GeoPoint(com.enonic.xp.util.GeoPoint) Input(com.enonic.xp.form.Input) FormItem(com.enonic.xp.form.FormItem) PropertyTree(com.enonic.xp.data.PropertyTree) XData(com.enonic.xp.schema.xdata.XData) ExtraData(com.enonic.xp.content.ExtraData)

Example 8 with XData

use of com.enonic.xp.schema.xdata.XData in project xp by enonic.

the class ImageContentProcessorTest method testProcessCreateWithExtraData.

@Test
public void testProcessCreateWithExtraData() 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 CreateContentParams params = createContentParams(createAttachments());
    final ProcessCreateParams processCreateParams = new ProcessCreateParams(params, MediaInfo.create().addMetadata("exposure time", "1").addMetadata("gps altitude ", "2").build());
    final ProcessCreateResult result = this.imageContentProcessor.processCreate(processCreateParams);
    final ExtraData extraData = result.getCreateContentParams().getExtraDatas().first();
    assertEquals(xDataInfo.getName(), extraData.getName());
    assertEquals(extraData.getData().getString("shutterTime", 0), "1");
    assertEquals(extraData.getData().getString("altitude", 0), "2");
    assertEquals(13, extraData.getData().getLong(MediaInfo.MEDIA_INFO_BYTE_SIZE, 0));
}
Also used : Form(com.enonic.xp.form.Form) CreateContentParams(com.enonic.xp.content.CreateContentParams) ProcessCreateResult(com.enonic.xp.content.processor.ProcessCreateResult) XData(com.enonic.xp.schema.xdata.XData) ExtraData(com.enonic.xp.content.ExtraData) ProcessCreateParams(com.enonic.xp.content.processor.ProcessCreateParams) Test(org.junit.jupiter.api.Test)

Example 9 with XData

use of com.enonic.xp.schema.xdata.XData 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 10 with XData

use of com.enonic.xp.schema.xdata.XData in project xp by enonic.

the class XDataServiceImplTest method testApplications.

@Test
public void testApplications() {
    initializeApps();
    final XDatas types1 = this.service.getAll();
    assertNotNull(types1);
    assertEquals(9, types1.getSize());
    final XDatas types2 = this.service.getByApplication(ApplicationKey.from("myapp1"));
    assertNotNull(types2);
    assertEquals(2, types2.getSize());
    final XDatas types3 = this.service.getByApplication(ApplicationKey.from("myapp2"));
    assertNotNull(types3);
    assertEquals(4, types3.getSize());
    final XData xData = service.getByName(XDataName.from("myapp2:xdata1"));
    assertNotNull(xData);
}
Also used : XDatas(com.enonic.xp.schema.xdata.XDatas) XData(com.enonic.xp.schema.xdata.XData) Test(org.junit.jupiter.api.Test) AbstractSchemaTest(com.enonic.xp.core.impl.schema.AbstractSchemaTest)

Aggregations

XData (com.enonic.xp.schema.xdata.XData)17 Test (org.junit.jupiter.api.Test)8 ExtraData (com.enonic.xp.content.ExtraData)7 PropertyTree (com.enonic.xp.data.PropertyTree)6 Form (com.enonic.xp.form.Form)5 CreateContentParams (com.enonic.xp.content.CreateContentParams)4 XDataName (com.enonic.xp.schema.xdata.XDataName)4 XDatas (com.enonic.xp.schema.xdata.XDatas)4 ProcessCreateParams (com.enonic.xp.content.processor.ProcessCreateParams)3 ProcessCreateResult (com.enonic.xp.content.processor.ProcessCreateResult)3 AbstractSchemaTest (com.enonic.xp.core.impl.schema.AbstractSchemaTest)3 ContentType (com.enonic.xp.schema.content.ContentType)3 FormItem (com.enonic.xp.form.FormItem)2 FormItemSet (com.enonic.xp.form.FormItemSet)2 Input (com.enonic.xp.form.Input)2 GetContentTypeParams (com.enonic.xp.schema.content.GetContentTypeParams)2 GeoPoint (com.enonic.xp.util.GeoPoint)2 Application (com.enonic.xp.app.Application)1 CreateAttachments (com.enonic.xp.attachment.CreateAttachments)1 EditableContent (com.enonic.xp.content.EditableContent)1