Search in sources :

Example 26 with ExtraData

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

the class ExtraDataSerializer method toData.

@Override
public void toData(final ExtraDatas extraDatas, final PropertySet parent) {
    final PropertySet metaSet = parent.addSet(EXTRA_DATA);
    for (final ExtraData extraData : extraDatas) {
        final String xDataApplicationPrefix = extraData.getApplicationPrefix();
        PropertySet xDataApplication = metaSet.getSet(xDataApplicationPrefix);
        if (xDataApplication == null) {
            xDataApplication = metaSet.addSet(xDataApplicationPrefix);
        }
        xDataApplication.addSet(extraData.getName().getLocalName(), extraData.getData().getRoot().copy(metaSet.getTree()));
    }
}
Also used : PropertySet(com.enonic.xp.data.PropertySet) ExtraData(com.enonic.xp.content.ExtraData)

Example 27 with ExtraData

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

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

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

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

the class ExtraDataValidator method validate.

@Override
public void validate(final ContentValidatorParams params, final ValidationErrors.Builder validationErrorsBuilder) {
    for (final ExtraData extraData : params.getExtraDatas()) {
        final XDataName name = extraData.getName();
        final XData xData = this.xDataService.getByName(name);
        if (xData == null) {
            LOG.warn("Mixin not found: '" + name);
            continue;
        }
        final Form mixinForm = xData.getForm();
        if (extraData.getData().getRoot().getPropertySize() > 0) {
            OccurrenceValidator.validate(mixinForm, extraData.getData().getRoot(), validationErrorsBuilder);
        }
    }
}
Also used : Form(com.enonic.xp.form.Form) XData(com.enonic.xp.schema.xdata.XData) ExtraData(com.enonic.xp.content.ExtraData) XDataName(com.enonic.xp.schema.xdata.XDataName)

Aggregations

ExtraData (com.enonic.xp.content.ExtraData)31 PropertyTree (com.enonic.xp.data.PropertyTree)18 Test (org.junit.jupiter.api.Test)10 XData (com.enonic.xp.schema.xdata.XData)8 XDataName (com.enonic.xp.schema.xdata.XDataName)8 Content (com.enonic.xp.content.Content)7 CreateContentParams (com.enonic.xp.content.CreateContentParams)5 ExtraDatas (com.enonic.xp.content.ExtraDatas)5 ApplicationKey (com.enonic.xp.app.ApplicationKey)4 Media (com.enonic.xp.content.Media)4 ProcessCreateParams (com.enonic.xp.content.processor.ProcessCreateParams)4 ProcessCreateResult (com.enonic.xp.content.processor.ProcessCreateResult)4 PropertySet (com.enonic.xp.data.PropertySet)4 Attachment (com.enonic.xp.attachment.Attachment)3 FormItem (com.enonic.xp.form.FormItem)3 Input (com.enonic.xp.form.Input)3 GeoPoint (com.enonic.xp.util.GeoPoint)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3 CreateAttachment (com.enonic.xp.attachment.CreateAttachment)2