Search in sources :

Example 36 with AttributeDescriptor

use of ddf.catalog.data.AttributeDescriptor in project ddf by codice.

the class GeoJsonInputTransformer method transform.

@Override
public Metacard transform(InputStream input, String id) throws IOException, CatalogTransformerException {
    if (input == null) {
        throw new CatalogTransformerException("Cannot transform null input.");
    }
    Map<String, Object> rootObject = MAPPER.parser().parseMap(input);
    if (rootObject == null) {
        throw new CatalogTransformerException("Unable to parse JSON for metacard.");
    }
    Object typeValue = rootObject.get(CompositeGeometry.TYPE_KEY);
    if (typeValue == null || !typeValue.equals("Feature")) {
        throw new CatalogTransformerException(new UnsupportedOperationException("Only supported type is Feature, not [" + typeValue + "]"));
    }
    Map<String, Object> properties = (Map<String, Object>) rootObject.get(CompositeGeometry.PROPERTIES_KEY);
    if (properties == null) {
        throw new CatalogTransformerException("Properties are required to create a Metacard.");
    }
    final String propertyTypeName = (String) properties.get(METACARD_TYPE_PROPERTY_KEY);
    MetacardImpl metacard;
    if (isEmpty(propertyTypeName) || metacardTypes == null) {
        LOGGER.debug("MetacardType specified in input is null or empty.  Assuming default MetacardType");
        metacard = new MetacardImpl();
    } else {
        MetacardType metacardType = metacardTypes.stream().filter(type -> type.getName().equals(propertyTypeName)).findFirst().orElseThrow(() -> new CatalogTransformerException("MetacardType specified in input has not been registered with the system.  Cannot parse input.  MetacardType name: " + propertyTypeName));
        LOGGER.debug("Found registered MetacardType: {}", propertyTypeName);
        metacard = new MetacardImpl(metacardType);
    }
    MetacardType metacardType = metacard.getMetacardType();
    LOGGER.debug("Metacard type name: {}", metacardType.getName());
    // retrieve geometry
    Map<String, Object> geometryJson = (Map<String, Object>) rootObject.get(CompositeGeometry.GEOMETRY_KEY);
    CompositeGeometry geoJsonGeometry = null;
    if (geometryJson != null) {
        if (geometryJson.get(CompositeGeometry.TYPE_KEY) != null && (geometryJson.get(CompositeGeometry.COORDINATES_KEY) != null || geometryJson.get(CompositeGeometry.GEOMETRIES_KEY) != null)) {
            String geometryTypeJson = geometryJson.get(CompositeGeometry.TYPE_KEY).toString();
            geoJsonGeometry = CompositeGeometry.getCompositeGeometry(geometryTypeJson, geometryJson);
        } else {
            LOGGER.debug("Could not find geometry type.");
        }
    }
    if (geoJsonGeometry != null && StringUtils.isNotEmpty(geoJsonGeometry.toWkt())) {
        metacard.setLocation(geoJsonGeometry.toWkt());
    }
    Map<String, AttributeDescriptor> attributeDescriptorMap = metacardType.getAttributeDescriptors().stream().collect(toMap(AttributeDescriptor::getName, Function.identity()));
    for (Map.Entry<String, Object> entry : properties.entrySet()) {
        final String key = entry.getKey();
        final Object value = entry.getValue();
        try {
            if (attributeDescriptorMap.containsKey(key)) {
                AttributeDescriptor ad = attributeDescriptorMap.get(key);
                metacard.setAttribute(key, convertProperty(value, ad));
            } else {
                Optional<AttributeDescriptor> optional = attributeRegistry.lookup(key);
                if (optional.isPresent()) {
                    metacard.setAttribute(key, convertProperty(value, optional.get()));
                }
            }
        } catch (NumberFormatException | ParseException e) {
            LOGGER.info("GeoJSON input for attribute name '{}' does not match the expected AttributeType. This attribute will not be added to the metacard.", key, e);
        }
    }
    if (isNotEmpty(metacard.getSourceId())) {
        properties.put(SOURCE_ID_PROPERTY, metacard.getSourceId());
    }
    if (id != null) {
        metacard.setId(id);
    }
    return metacard;
}
Also used : CompositeGeometry(ddf.geo.formatter.CompositeGeometry) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) MetacardType(ddf.catalog.data.MetacardType) ParseException(java.text.ParseException) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map)

Example 37 with AttributeDescriptor

use of ddf.catalog.data.AttributeDescriptor in project ddf by codice.

the class TestGeoJsonExtensible method sampleMetacardTypeA.

private MetacardType sampleMetacardTypeA() {
    Set<AttributeDescriptor> descriptors = new HashSet<AttributeDescriptor>();
    descriptors.add(new AttributeDescriptorImpl("frequency", true, /* indexed */
    true, /* stored */
    false, /* tokenized */
    false, /* multivalued */
    BasicTypes.LONG_TYPE));
    descriptors.add(new AttributeDescriptorImpl("min-frequency", true, /* indexed */
    true, /* stored */
    false, /* tokenized */
    false, /* multivalued */
    BasicTypes.LONG_TYPE));
    descriptors.add(new AttributeDescriptorImpl("max-frequency", true, /* indexed */
    true, /* stored */
    false, /* tokenized */
    false, /* multivalued */
    BasicTypes.LONG_TYPE));
    descriptors.add(new AttributeDescriptorImpl("angle", true, /* indexed */
    true, /* stored */
    false, /* tokenized */
    false, /* multivalued */
    BasicTypes.INTEGER_TYPE));
    descriptors.add(new AttributeDescriptorImpl("multi-string", true, /* indexed */
    true, /* stored */
    false, /* tokenized */
    true, /* multivalued */
    BasicTypes.STRING_TYPE));
    descriptors.add(new AttributeDescriptorImpl(Metacard.ID, true, /* indexed */
    true, /* stored */
    false, /* tokenized */
    false, /* multivalued */
    BasicTypes.STRING_TYPE));
    descriptors.add(new AttributeDescriptorImpl(Metacard.TITLE, true, /* indexed */
    true, /* stored */
    true, /* tokenized */
    false, /* multivalued */
    BasicTypes.STRING_TYPE));
    return new MetacardTypeImpl(SAMPLE_A_METACARD_TYPE_NAME, descriptors);
}
Also used : AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) HashSet(java.util.HashSet)

Example 38 with AttributeDescriptor

use of ddf.catalog.data.AttributeDescriptor in project ddf by codice.

the class TestGeoJsonExtensible method sampleMetacardTypeB.

private MetacardType sampleMetacardTypeB() {
    Set<AttributeDescriptor> descriptors = new HashSet<AttributeDescriptor>();
    descriptors.add(new AttributeDescriptorImpl(COLUMNS_ATTRIBUTE_KEY, true, /* indexed */
    true, /* stored */
    false, /* tokenized */
    false, /* multivalued */
    BasicTypes.INTEGER_TYPE));
    descriptors.add(new AttributeDescriptorImpl(ROWS_ATTRIBUTE_KEY, true, /* indexed */
    true, /* stored */
    false, /* tokenized */
    false, /* multivalued */
    BasicTypes.INTEGER_TYPE));
    descriptors.add(new AttributeDescriptorImpl(DESCRIPTION_ATTRIBUTE_KEY, true, /* indexed */
    true, /* stored */
    false, /* tokenized */
    false, /* multivalued */
    BasicTypes.STRING_TYPE));
    descriptors.add(new AttributeDescriptorImpl(Metacard.ID, true, /* indexed */
    true, /* stored */
    false, /* tokenized */
    false, /* multivalued */
    BasicTypes.STRING_TYPE));
    descriptors.add(new AttributeDescriptorImpl(Metacard.TITLE, true, /* indexed */
    true, /* stored */
    true, /* tokenized */
    false, /* multivalued */
    BasicTypes.STRING_TYPE));
    descriptors.add(new AttributeDescriptorImpl(REVIEWED_ATTRIBUTE_KEY, true, /* indexed */
    true, /* stored */
    true, /* tokenized */
    false, /* multivalued */
    BasicTypes.BOOLEAN_TYPE));
    descriptors.add(new AttributeDescriptorImpl(PRECISE_LENGTH_METERS_ATTRIBUTE_KEY, true, /* indexed */
    true, /* stored */
    true, /* tokenized */
    false, /* multivalued */
    BasicTypes.DOUBLE_TYPE));
    descriptors.add(new AttributeDescriptorImpl(PRECISE_HEIGHT_METERS_ATTRIBUTE_KEY, true, /* indexed */
    true, /* stored */
    true, /* tokenized */
    false, /* multivalued */
    BasicTypes.FLOAT_TYPE));
    descriptors.add(new AttributeDescriptorImpl(NUMBER_REVIEWERS_ATTRIBUTE_KEY, true, /* indexed */
    true, /* stored */
    true, /* tokenized */
    false, /* multivalued */
    BasicTypes.SHORT_TYPE));
    return new MetacardTypeImpl("MetacardTypeB", descriptors);
}
Also used : AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) HashSet(java.util.HashSet)

Example 39 with AttributeDescriptor

use of ddf.catalog.data.AttributeDescriptor in project ddf by codice.

the class MetacardImpl method writeObject.

/**
     * Serializes this {@link MetacardImpl} instance.
     *
     * @param stream the {@link ObjectOutputStream} that contains the object to be serialized
     * @throws IOException
     * @serialData First, all non-transient fields are written out by the default Java serialization
     * implementation ( {@link ObjectOutputStream#defaultWriteObject()}) . Next, the
     * {@link MetacardType} is written out as a {@link MetacardTypeImpl}. Then the
     * <i>number</i> of {@code Attribute} objects is written as an {@code int}. After
     * the number of objects, each {@code Attribute} object is written out.
     * <p>
     * <p>
     * The MetacardType object is written out as a {@link MetacardTypeImpl} object
     * because {@link MetacardTypeImpl} is a class that is part of the DDF API and is
     * guaranteed to be on the classpath when this object is deserialized. Secondly, the
     * {@link MetacardTypeImpl} has a trusted serialization implementation where the
     * object's logical representation is serialized.
     * </p>
     */
private void writeObject(ObjectOutputStream stream) throws IOException {
    /*
         * defaultWriteObject() is invoked for greater flexibility and compatibility. See the
         * *Serialization Note* in MetacardImpl's class Javadoc.
         */
    stream.defaultWriteObject();
    /*
         * Cannot allow unknown implementations of MetacardType to be serialized. Must convert them
         * to our implementation to guarantee it is serializing the logical representation and not
         * the physical representation.
         */
    if (type instanceof MetacardTypeImpl) {
        stream.writeObject(type);
    } else {
        MetacardTypeImpl mt = new MetacardTypeImpl(type.getName(), type.getAttributeDescriptors());
        stream.writeObject(mt);
    }
    if (map != null) {
        stream.writeInt(map.size());
        for (Attribute attribute : this.map.values()) {
            stream.writeObject(attribute);
        }
    } else {
        if (wrappedMetacard != null && wrappedMetacard.getMetacardType() != null) {
            MetacardType metacardType = wrappedMetacard.getMetacardType();
            List<Attribute> attributes = new ArrayList<Attribute>();
            if (metacardType.getAttributeDescriptors() == null) {
                // no descriptors, means no attributes can be defined.
                // no attributes defined, means no attributes written to
                // disk
                stream.writeInt(0);
            } else {
                for (AttributeDescriptor ad : metacardType.getAttributeDescriptors()) {
                    Attribute attribute = wrappedMetacard.getAttribute(ad.getName());
                    if (attribute != null) {
                        attributes.add(attribute);
                    }
                }
                // Must loop again because the size of the attributes list
                // is not known until list has been fully populated.
                stream.writeInt(attributes.size());
                for (Attribute attribute : attributes) {
                    stream.writeObject(attribute);
                }
            }
        }
    }
}
Also used : Attribute(ddf.catalog.data.Attribute) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardType(ddf.catalog.data.MetacardType)

Example 40 with AttributeDescriptor

use of ddf.catalog.data.AttributeDescriptor in project ddf by codice.

the class MetacardImpl method readObject.

/**
     * Deserializes this {@link MetacardImpl}'s instance.
     *
     * @param stream the {@link ObjectInputStream} that contains the bytes of the object
     * @throws IOException
     * @throws ClassNotFoundException
     */
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
    /*
         * defaultReadObject() is invoked for greater flexibility and compatibility. See the
         * *Serialization Note* in MetacardImpl's class Javadoc.
         */
    stream.defaultReadObject();
    map = new HashMap<String, Attribute>();
    wrappedMetacard = null;
    type = (MetacardType) stream.readObject();
    if (type == null) {
        throw new InvalidObjectException(MetacardType.class.getName() + " instance cannot be null.");
    }
    int numElements = stream.readInt();
    for (int i = 0; i < numElements; i++) {
        Attribute attribute = (Attribute) stream.readObject();
        if (attribute != null) {
            AttributeDescriptor attributeDescriptor = getMetacardType().getAttributeDescriptor(attribute.getName());
            if (attributeDescriptor != null && attribute.getValue() != null) {
                attributeDescriptor.getType().getAttributeFormat();
                attributeDescriptor.getType().getClass();
            }
        }
        setAttribute(attribute);
    }
}
Also used : Attribute(ddf.catalog.data.Attribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) InvalidObjectException(java.io.InvalidObjectException)

Aggregations

AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)111 Test (org.junit.Test)51 MetacardType (ddf.catalog.data.MetacardType)40 Metacard (ddf.catalog.data.Metacard)38 HashSet (java.util.HashSet)29 Attribute (ddf.catalog.data.Attribute)26 AttributeDescriptorImpl (ddf.catalog.data.impl.AttributeDescriptorImpl)24 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)24 MetacardTypeImpl (ddf.catalog.data.impl.MetacardTypeImpl)24 ArrayList (java.util.ArrayList)23 Serializable (java.io.Serializable)16 FeatureMetacardType (org.codice.ddf.spatial.ogc.wfs.catalog.common.FeatureMetacardType)9 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)7 IOException (java.io.IOException)7 Date (java.util.Date)7 List (java.util.List)7 QName (javax.xml.namespace.QName)7