Search in sources :

Example 31 with AttributeDescriptor

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

the class TestMetacardTypeAdapter method testUnmarshalWithUnknownTypeName.

@Test
public void testUnmarshalWithUnknownTypeName() throws CatalogTransformerException {
    MetacardType unknownMetacardType = new MetacardTypeImpl(KNOWN_TYPE_NAME, (Set<AttributeDescriptor>) null);
    List<MetacardType> metacardTypes = new ArrayList<MetacardType>(1);
    metacardTypes.add(unknownMetacardType);
    MetacardTypeAdapter metacardTypeAdpater = new MetacardTypeAdapter(metacardTypes);
    MetacardType metacardType = metacardTypeAdpater.unmarshal(UNKNOWN_TYPE_NAME);
    assertThat(metacardType.getName(), is(BasicTypes.BASIC_METACARD.getName()));
}
Also used : ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeAdapter(ddf.catalog.transformer.xml.adapter.MetacardTypeAdapter) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) MetacardType(ddf.catalog.data.MetacardType) Test(org.junit.Test)

Example 32 with AttributeDescriptor

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

the class TestXmlInputTransformer method testTransformWithExtensibleMetacardType.

@Test
public void testTransformWithExtensibleMetacardType() throws IOException, CatalogTransformerException {
    List<MetacardType> metacardTypes = new ArrayList<MetacardType>(1);
    MetacardType extensibleType = new MetacardTypeImpl("extensible.metacard", BasicTypes.BASIC_METACARD.getAttributeDescriptors());
    metacardTypes.add(extensibleType);
    xit.setMetacardTypes(metacardTypes);
    Metacard metacard = xit.transform(new FileInputStream("src/test/resources/extensibleMetacard.xml"));
    LOGGER.info("ID: {}", metacard.getId());
    LOGGER.info("Type: {}", metacard.getMetacardType().getName());
    LOGGER.info("Source: {}", metacard.getSourceId());
    LOGGER.info("Attributes: ");
    for (AttributeDescriptor descriptor : metacard.getMetacardType().getAttributeDescriptors()) {
        Attribute attribute = metacard.getAttribute(descriptor.getName());
        LOGGER.info("\t" + descriptor.getName() + ": " + ((attribute == null) ? attribute : attribute.getValue()));
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) Attribute(ddf.catalog.data.Attribute) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) MetacardType(ddf.catalog.data.MetacardType) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 33 with AttributeDescriptor

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

the class AbstractFeatureConverter method createMetacardFromFeature.

protected Metacard createMetacardFromFeature(HierarchicalStreamReader hreader, MetacardType metacardType) {
    StringWriter metadataWriter = new StringWriter();
    HierarchicalStreamReader reader = copyXml(hreader, metadataWriter);
    MetacardImpl mc = new MetacardImpl(metacardType);
    mc.setContentTypeName(metacardType.getName());
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        String featureProperty = prefix + reader.getNodeName();
        AttributeDescriptor attributeDescriptor = metacardType.getAttributeDescriptor(featureProperty);
        //Check MetacardMapper for mappings of incoming values
        String mappedMetacardAttribute = null;
        if (metacardMapper != null) {
            LOGGER.debug("Looking up metacard attribute for feature property {} using metacard mapper", featureProperty);
            mappedMetacardAttribute = metacardMapper.getMetacardAttribute(featureProperty);
            LOGGER.debug("Found metacard attribute {} for feature property {}", mappedMetacardAttribute, featureProperty);
        }
        Serializable value = null;
        if (attributeDescriptor != null && (StringUtils.isNotBlank(reader.getValue()) || BasicTypes.GEO_TYPE.getAttributeFormat().equals(attributeDescriptor.getType().getAttributeFormat()) || BasicTypes.DATE_TYPE.getAttributeFormat().equals(attributeDescriptor.getType().getAttributeFormat()))) {
            if (StringUtils.isNotBlank(mappedMetacardAttribute)) {
                if (StringUtils.equals(mappedMetacardAttribute, Core.RESOURCE_SIZE)) {
                    String sizeBeforeConversion = reader.getValue();
                    String bytes = convertToBytes(reader, metacardMapper.getDataUnit());
                    if (StringUtils.isNotBlank(bytes)) {
                        LOGGER.debug("Setting mapped metacard attribute {} with value {}", mappedMetacardAttribute, bytes);
                        mc.setAttribute(mappedMetacardAttribute, bytes);
                    }
                    if (StringUtils.isNotBlank(sizeBeforeConversion)) {
                        LOGGER.debug("Setting metacard attribute {} with value {}", featureProperty, sizeBeforeConversion);
                        mc.setAttribute(featureProperty, sizeBeforeConversion);
                    }
                } else {
                    value = getValueForMetacardAttribute(attributeDescriptor.getType().getAttributeFormat(), reader);
                    if (value != null) {
                        LOGGER.debug("Setting mapped metacard attribute {} with value {}", mappedMetacardAttribute, value);
                        mc.setAttribute(mappedMetacardAttribute, value);
                        mc.setAttribute(featureProperty, value);
                    }
                }
            } else {
                value = getValueForMetacardAttribute(attributeDescriptor.getType().getAttributeFormat(), reader);
                if (value != null) {
                    LOGGER.debug("Setting metacard attribute {} with value {}", featureProperty, value);
                    mc.setAttribute(featureProperty, value);
                }
            }
            if (BasicTypes.GEO_TYPE.getAttributeFormat().equals(attributeDescriptor.getType().getAttributeFormat())) {
                mc.setLocation((String) value);
            }
            // populate that field as well
            if (isBasicMetacardAttribute(reader.getNodeName())) {
                LOGGER.debug("Setting metacard basic attribute: {} = {}", reader.getNodeName(), value);
                mc.setAttribute(reader.getNodeName(), value);
            }
        }
        reader.moveUp();
    }
    mc.setMetadata(metadataWriter.toString());
    try {
        if (metacardType instanceof FeatureMetacardType) {
            URI namespaceUri = new URI(((FeatureMetacardType) metacardType).getNamespaceURI());
            mc.setTargetNamespace(namespaceUri);
        }
    } catch (URISyntaxException e) {
        LOGGER.debug("Error setting target namespace uri on metacard.", e);
    }
    return mc;
}
Also used : Serializable(java.io.Serializable) StringWriter(java.io.StringWriter) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) URISyntaxException(java.net.URISyntaxException) FeatureMetacardType(org.codice.ddf.spatial.ogc.wfs.catalog.common.FeatureMetacardType) URI(java.net.URI) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 34 with AttributeDescriptor

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

the class MetacardCreatorTest method testNullIntAttribute.

@Test
public void testNullIntAttribute() {
    Set<AttributeDescriptor> extraAttributes = new HashSet<>();
    extraAttributes.add(new AttributeDescriptorImpl(Media.DURATION, false, false, false, false, BasicTypes.INTEGER_TYPE));
    final Metadata metadata = new Metadata();
    metadata.add(MetacardCreator.COMPRESSION_TYPE_METADATA_KEY, null);
    MetacardTypeImpl extendedMetacardType = new MetacardTypeImpl(BasicTypes.BASIC_METACARD.getName(), BasicTypes.BASIC_METACARD, extraAttributes);
    final String id = "id";
    final String metadataXml = "<xml>test</xml>";
    Metacard metacard = MetacardCreator.createMetacard(metadata, id, metadataXml, extendedMetacardType);
    assertThat(metacard.getMetadata(), is(metadataXml));
    assertThat(metacard.getAttribute(Media.COMPRESSION), is(nullValue()));
}
Also used : Metacard(ddf.catalog.data.Metacard) Metadata(org.apache.tika.metadata.Metadata) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 35 with AttributeDescriptor

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

the class MetacardCreatorTest method testDoubleAttributeWithNumberFormatException.

@Test
public void testDoubleAttributeWithNumberFormatException() {
    Set<AttributeDescriptor> extraAttributes = new HashSet<>();
    extraAttributes.add(new AttributeDescriptorImpl(Media.DURATION, false, false, false, false, BasicTypes.DOUBLE_TYPE));
    final Metadata metadata = new Metadata();
    String durationValue = "Not actually a double";
    metadata.add(MetacardCreator.DURATION_METDATA_KEY, durationValue);
    MetacardTypeImpl extendedMetacardType = new MetacardTypeImpl(BasicTypes.BASIC_METACARD.getName(), BasicTypes.BASIC_METACARD, extraAttributes);
    final String id = "id";
    final String metadataXml = "<xml>test</xml>";
    Metacard metacard = MetacardCreator.createMetacard(metadata, id, metadataXml, extendedMetacardType);
    assertThat(metacard.getMetadata(), is(metadataXml));
    assertThat(metacard.getAttribute(Media.DURATION), is(nullValue()));
}
Also used : Metacard(ddf.catalog.data.Metacard) Metadata(org.apache.tika.metadata.Metadata) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) HashSet(java.util.HashSet) Test(org.junit.Test)

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