Search in sources :

Example 66 with MetacardType

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

the class AttributeRegistryImplTest method generateMetacardTypeWithNulls.

private static MetacardType generateMetacardTypeWithNulls() {
    Set<AttributeDescriptor> attributeDescriptorSet = new HashSet<>();
    attributeDescriptorSet.add(new AttributeDescriptorImpl(null, true, true, true, true, BasicTypes.STRING_TYPE));
    attributeDescriptorSet.add(new AttributeDescriptorImpl("test", true, true, true, true, BasicTypes.STRING_TYPE));
    attributeDescriptorSet.add(null);
    MetacardType metacardType = new MetacardTypeImpl("nullAttributeMetacardType", attributeDescriptorSet);
    return metacardType;
}
Also used : AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardType(ddf.catalog.data.MetacardType) HashSet(java.util.HashSet)

Example 67 with MetacardType

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

the class DynamicSchemaResolver method getMetacardType.

public MetacardType getMetacardType(SolrDocument doc) throws MetacardCreationException {
    String mTypeFieldName = doc.getFirstValue(SchemaFields.METACARD_TYPE_FIELD_NAME).toString();
    MetacardType cachedMetacardType = metacardTypesCache.getIfPresent(mTypeFieldName);
    if (cachedMetacardType != null) {
        return cachedMetacardType;
    }
    byte[] bytes = (byte[]) doc.getFirstValue(SchemaFields.METACARD_TYPE_OBJECT_FIELD_NAME);
    ByteArrayInputStream bais = null;
    ObjectInputStream in = null;
    try {
        bais = new ByteArrayInputStream(bytes);
        in = new ObjectInputStream(bais);
        cachedMetacardType = (MetacardType) in.readObject();
    } catch (IOException e) {
        LOGGER.info("IO exception loading cached metacard type", e);
        throw new MetacardCreationException(COULD_NOT_READ_METACARD_TYPE_MESSAGE);
    } catch (ClassNotFoundException e) {
        LOGGER.info("Class exception loading cached metacard type", e);
        throw new MetacardCreationException(COULD_NOT_READ_METACARD_TYPE_MESSAGE);
    } finally {
        IOUtils.closeQuietly(bais);
        IOUtils.closeQuietly(in);
    }
    metacardTypeNameToSerialCache.put(mTypeFieldName, bytes);
    metacardTypesCache.put(mTypeFieldName, cachedMetacardType);
    addToFieldsCache(cachedMetacardType.getAttributeDescriptors());
    return cachedMetacardType;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) MetacardType(ddf.catalog.data.MetacardType) ObjectInputStream(java.io.ObjectInputStream)

Example 68 with MetacardType

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

the class SolrMetacardClientImpl method createMetacard.

public MetacardImpl createMetacard(SolrDocument doc) throws MetacardCreationException {
    MetacardType metacardType = resolver.getMetacardType(doc);
    MetacardImpl metacard = new MetacardImpl(metacardType);
    for (String solrFieldName : doc.getFieldNames()) {
        if (!resolver.isPrivateField(solrFieldName)) {
            Collection<Object> fieldValues = doc.getFieldValues(solrFieldName);
            Attribute attr = new AttributeImpl(resolver.resolveFieldName(solrFieldName), resolver.getDocValues(solrFieldName, fieldValues));
            metacard.setAttribute(attr);
        }
    }
    return metacard;
}
Also used : Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) MetacardType(ddf.catalog.data.MetacardType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 69 with MetacardType

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

the class WfsSource method buildFeatureFilters.

private void buildFeatureFilters(List<FeatureTypeType> featureTypes, List<String> supportedGeo) throws SecurityServiceException {
    // Use local Map for metacardtype registrations and once they are populated with latest
    // MetacardTypes, then do actual registration
    Map<String, MetacardTypeRegistration> mcTypeRegs = new HashMap<String, MetacardTypeRegistration>();
    Wfs wfs = factory.getClient();
    for (FeatureTypeType featureTypeType : featureTypes) {
        String ftName = featureTypeType.getName().getLocalPart();
        if (StringUtils.isNotBlank(forcedFeatureType) && !StringUtils.equals(forcedFeatureType, ftName)) {
            continue;
        }
        if (mcTypeRegs.containsKey(ftName)) {
            LOGGER.debug("WfsSource {}: MetacardType {} is already registered - skipping to next metacard type", getId(), ftName);
            continue;
        }
        LOGGER.debug("ftName: {}", ftName);
        try {
            XmlSchema schema = wfs.describeFeatureType(new DescribeFeatureTypeRequest(featureTypeType.getName()));
            if ((schema != null)) {
                FeatureMetacardType ftMetacard = new FeatureMetacardType(schema, featureTypeType.getName(), nonQueryableProperties != null ? Arrays.asList(nonQueryableProperties) : new ArrayList<String>(), Wfs10Constants.GML_NAMESPACE);
                Dictionary<String, Object> props = new Hashtable<String, Object>();
                props.put(Metacard.CONTENT_TYPE, new String[] { ftName });
                LOGGER.debug("WfsSource {}: Registering MetacardType: {}", getId(), ftName);
                // Update local map with enough info to create actual MetacardType registrations
                // later
                mcTypeRegs.put(ftName, new MetacardTypeRegistration(ftMetacard, props, featureTypeType.getSRS()));
                FeatureConverter featureConverter = null;
                if (!CollectionUtils.isEmpty(featureConverterFactories)) {
                    for (FeatureConverterFactory factory : featureConverterFactories) {
                        if (ftName.equalsIgnoreCase(factory.getFeatureType())) {
                            featureConverter = factory.createConverter();
                            LOGGER.debug("WFS Source {}: Features of type: {} will be converted using {}", getId(), ftName, featureConverter.getClass().getSimpleName());
                            break;
                        }
                    }
                    if (featureConverter == null) {
                        LOGGER.debug("WfsSource {}: Unable to find a feature specific converter; {} will be converted using the GenericFeatureConverter", getId(), ftName);
                        featureConverter = new GenericFeatureConverter(featureTypeType.getSRS());
                    }
                } else {
                    LOGGER.debug("WfsSource {}: Unable to find a feature specific converter; {} will be converted using the GenericFeatureConverter", getId(), ftName);
                    featureConverter = new GenericFeatureConverter(featureTypeType.getSRS());
                }
                featureConverter.setSourceId(getId());
                featureConverter.setMetacardType(ftMetacard);
                featureConverter.setWfsUrl(wfsUrl);
                // Add the Feature Type name as an alias for xstream
                featureCollectionReader.registerConverter(featureConverter);
            }
        } catch (WfsException | IllegalArgumentException wfse) {
            LOGGER.debug(WFS_ERROR_MESSAGE, wfse);
        } catch (WebApplicationException wae) {
            LOGGER.debug(handleWebApplicationException(wae), wae);
        }
    }
    // Unregister all MetacardType services - the DescribeFeatureTypeRequest should
    // have returned all of the most current metacard types that will now be registered.
    // As Source(s) are added/removed from this instance or to other Source(s)
    // that this instance is federated to, the list of metacard types will change.
    // This is done here vs. inside the above loop so that minimal time is spent clearing and
    // registering the MetacardTypes - the concern is that if this registration is too lengthy
    // a query could come in that is handled while the MetacardType registrations are
    // in a state of flux.
    unregisterAllMetacardTypes();
    this.featureTypeFilters.clear();
    if (!mcTypeRegs.isEmpty()) {
        Set<Entry<String, MetacardTypeRegistration>> entries = mcTypeRegs.entrySet();
        for (Map.Entry<String, MetacardTypeRegistration> entry : mcTypeRegs.entrySet()) {
            MetacardTypeRegistration mcTypeReg = entry.getValue();
            FeatureMetacardType ftMetacard = mcTypeReg.getFtMetacard();
            ServiceRegistration serviceRegistration = context.registerService(MetacardType.class.getName(), ftMetacard, mcTypeReg.getProps());
            this.metacardTypeServiceRegistrations.put(entry.getKey(), serviceRegistration);
            this.featureTypeFilters.put(ftMetacard.getFeatureType(), new WfsFilterDelegate(ftMetacard, supportedGeo, mcTypeReg.getSrs()));
        }
    }
    if (featureTypeFilters.isEmpty()) {
        LOGGER.info("Wfs Source {}: No Feature Type schemas validated. Marking source as unavailable", getId());
    }
    LOGGER.debug("Wfs Source {}: Number of validated Features = {}", getId(), featureTypeFilters.size());
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Entry(java.util.Map.Entry) GenericFeatureConverter(org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GenericFeatureConverter) FeatureConverterFactory(org.codice.ddf.spatial.ogc.wfs.v1_0_0.catalog.converter.FeatureConverterFactory) FeatureMetacardType(org.codice.ddf.spatial.ogc.wfs.catalog.common.FeatureMetacardType) FeatureConverter(org.codice.ddf.spatial.ogc.wfs.catalog.converter.FeatureConverter) GenericFeatureConverter(org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GenericFeatureConverter) ServiceRegistration(org.osgi.framework.ServiceRegistration) FeatureTypeType(ogc.schema.opengis.wfs_capabilities.v_1_0_0.FeatureTypeType) Wfs(org.codice.ddf.spatial.ogc.wfs.v1_0_0.catalog.common.Wfs) Hashtable(java.util.Hashtable) MetacardType(ddf.catalog.data.MetacardType) FeatureMetacardType(org.codice.ddf.spatial.ogc.wfs.catalog.common.FeatureMetacardType) XmlSchema(org.apache.ws.commons.schema.XmlSchema) WfsException(org.codice.ddf.spatial.ogc.wfs.catalog.common.WfsException) DescribeFeatureTypeRequest(org.codice.ddf.spatial.ogc.wfs.v1_0_0.catalog.common.DescribeFeatureTypeRequest) Map(java.util.Map) HashMap(java.util.HashMap)

Example 70 with MetacardType

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

the class InputTransformerErrorHandler method getMetacard.

public Metacard getMetacard(String id) {
    MetacardType metacardType = getMetacardType(id);
    if (metacardType == null) {
        metacardType = BasicTypes.BASIC_METACARD;
        LOGGER.debug("No metacard type found. Defaulting to Basic Metacard.");
    }
    /*
         * Create a new MetacardImpl with the proper MetacardType
         */
    Metacard metacard = new MetacardImpl(metacardType);
    /*
             * If any major errors occur during parsing, print them out and add them to the metacard as validation errors
             */
    InputTransformerErrorHandler inputTransformerErrorHandler = (InputTransformerErrorHandler) parser.getErrorHandler();
    if (inputTransformerErrorHandler != null) {
        String parseWarningsErrors = inputTransformerErrorHandler.getParseWarningsErrors();
        if (StringUtils.isNotBlank(parseWarningsErrors)) {
            LOGGER.debug(parseWarningsErrors);
            List<Serializable> warningsAndErrors = new ArrayList<>();
            warningsAndErrors.add(parseWarningsErrors);
            if (metacard.getAttribute(Validation.VALIDATION_ERRORS) != null) {
                List<Serializable> values = metacard.getAttribute(Validation.VALIDATION_ERRORS).getValues();
                if (values != null) {
                    warningsAndErrors.addAll(values);
                }
            }
            metacard.setAttribute(new AttributeImpl(Validation.VALIDATION_ERRORS, Collections.unmodifiableList(warningsAndErrors)));
        }
    }
    /*
         * Populate metacard with all attributes constructed in SaxEventHandlers during parsing
         */
    Map<String, Boolean> multiValuedMap = saxEventHandlerUtils.getMultiValuedNameMap(metacardType.getAttributeDescriptors());
    for (SaxEventHandler eventHandler : eventHandlers) {
        List<Attribute> attributes = eventHandler.getAttributes();
        for (Attribute attribute : attributes) {
            /*
                 * If metacard already has values in the attribute, skip the attribute,
                 * instead of simply overwriting the existing values.
                 */
            if (metacard.getAttribute(attribute.getName()) == null) {
                metacard.setAttribute(attribute);
            } else if (MapUtils.getBoolean(multiValuedMap, attribute.getName(), false)) {
                metacard.getAttribute(attribute.getName()).getValues().addAll(attribute.getValues());
            }
        }
    }
    return metacard;
}
Also used : Serializable(java.io.Serializable) Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) SaxEventHandler(org.codice.ddf.transformer.xml.streaming.SaxEventHandler) MetacardType(ddf.catalog.data.MetacardType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Metacard(ddf.catalog.data.Metacard)

Aggregations

MetacardType (ddf.catalog.data.MetacardType)88 Test (org.junit.Test)57 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)41 Metacard (ddf.catalog.data.Metacard)35 ArrayList (java.util.ArrayList)29 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)20 MetacardTypeImpl (ddf.catalog.data.impl.MetacardTypeImpl)17 HashSet (java.util.HashSet)15 Attribute (ddf.catalog.data.Attribute)12 Serializable (java.io.Serializable)10 IOException (java.io.IOException)8 Date (java.util.Date)8 HashMap (java.util.HashMap)8 List (java.util.List)8 Map (java.util.Map)8 AttributeDescriptorImpl (ddf.catalog.data.impl.AttributeDescriptorImpl)7 InputStream (java.io.InputStream)7 Set (java.util.Set)7 ContentItem (ddf.catalog.content.data.ContentItem)6 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)6