Search in sources :

Example 6 with MetacardTypeImpl

use of ddf.catalog.data.impl.MetacardTypeImpl in project ddf by codice.

the class SolrProviderTest method testNumericalOperations.

@Test()
public void testNumericalOperations() throws Exception {
    deleteAllIn(provider);
    /* SETUP */
    String doubleField = "hertz";
    double doubleFieldValue = 16065.435;
    String floatField = "inches";
    float floatFieldValue = 4.435f;
    String intField = "count";
    int intFieldValue = 4;
    String longField = "milliseconds";
    long longFieldValue = 9876543293L;
    String shortField = "daysOfTheWeek";
    short shortFieldValue = 1;
    Set<AttributeDescriptor> descriptors = numericalDescriptors(doubleField, floatField, intField, longField, shortField);
    MetacardTypeImpl mType = new MetacardTypeImpl("anotherCustom", descriptors);
    MetacardImpl customMetacard1 = new MetacardImpl(mType);
    customMetacard1.setAttribute(Metacard.ID, "");
    customMetacard1.setAttribute(doubleField, doubleFieldValue);
    customMetacard1.setAttribute(floatField, floatFieldValue);
    customMetacard1.setAttribute(intField, intFieldValue);
    customMetacard1.setAttribute(longField, longFieldValue);
    customMetacard1.setAttribute(shortField, shortFieldValue);
    MetacardImpl customMetacard2 = new MetacardImpl(mType);
    customMetacard2.setAttribute(Metacard.ID, "");
    customMetacard2.setAttribute(doubleField, doubleFieldValue + 10.0);
    customMetacard2.setAttribute(floatField, (floatFieldValue + 10.0f));
    customMetacard2.setAttribute(intField, intFieldValue + 1);
    customMetacard2.setAttribute(longField, longFieldValue + 10L);
    customMetacard2.setAttribute(shortField, (shortFieldValue + 1));
    create(Arrays.asList((Metacard) customMetacard1, customMetacard2));
    // on exact DOUBLE
    greaterThanQueryAssertion(doubleField, doubleFieldValue, 1);
    greaterThanOrEqualToQueryAssertion(doubleField, doubleFieldValue, 2);
    // beyond the DOUBLE
    greaterThanQueryAssertion(doubleField, doubleFieldValue - 0.00000001, 2);
    greaterThanOrEqualToQueryAssertion(doubleField, doubleFieldValue - 0.00000001, 2);
    greaterThanQueryAssertion(doubleField, doubleFieldValue + 12.0, 0);
    greaterThanOrEqualToQueryAssertion(doubleField, doubleFieldValue + 12.0, 0);
    // on exact FLOAT
    greaterThanQueryAssertion(floatField, floatFieldValue, 1);
    greaterThanOrEqualToQueryAssertion(floatField, floatFieldValue, 2);
    // beyond the FLOAT
    greaterThanQueryAssertion(floatField, floatFieldValue - 0.00001f, 2);
    greaterThanOrEqualToQueryAssertion(floatField, floatFieldValue - 0.00001f, 2);
    greaterThanQueryAssertion(floatField, floatFieldValue + 12.0f, 0);
    greaterThanOrEqualToQueryAssertion(floatField, floatFieldValue + 12.0f, 0);
    // on exact LONG
    greaterThanQueryAssertion(longField, longFieldValue, 1);
    greaterThanOrEqualToQueryAssertion(longField, longFieldValue, 2);
    // beyond the LONG
    greaterThanQueryAssertion(longField, longFieldValue - 1L, 2);
    greaterThanOrEqualToQueryAssertion(longField, longFieldValue - 1L, 2);
    greaterThanQueryAssertion(longField, longFieldValue + 12L, 0);
    greaterThanOrEqualToQueryAssertion(longField, longFieldValue + 12L, 0);
    // on exact INT
    greaterThanQueryAssertion(intField, intFieldValue, 1);
    greaterThanOrEqualToQueryAssertion(intField, intFieldValue, 2);
    // beyond the INT
    greaterThanQueryAssertion(intField, intFieldValue - 1, 2);
    greaterThanOrEqualToQueryAssertion(intField, intFieldValue - 1, 2);
    greaterThanQueryAssertion(intField, intFieldValue + 2, 0);
    greaterThanOrEqualToQueryAssertion(intField, intFieldValue + 2, 0);
    // on exact SHORT
    greaterThanQueryAssertion(shortField, shortFieldValue, 1);
    greaterThanOrEqualToQueryAssertion(shortField, shortFieldValue, 2);
    // beyond the SHORT
    greaterThanQueryAssertion(shortField, (short) (shortFieldValue - 1), 2);
    greaterThanOrEqualToQueryAssertion(shortField, (short) (shortFieldValue - 1), 2);
    greaterThanQueryAssertion(shortField, (short) (shortFieldValue + 2), 0);
    greaterThanOrEqualToQueryAssertion(shortField, (short) (shortFieldValue + 2), 0);
}
Also used : Metacard(ddf.catalog.data.Metacard) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) Matchers.containsString(org.hamcrest.Matchers.containsString) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Example 7 with MetacardTypeImpl

use of ddf.catalog.data.impl.MetacardTypeImpl in project ddf by codice.

the class TestGetRecordsResponseConverter method createMetacardList.

private List<Metacard> createMetacardList(int start, int finish) {
    List<Metacard> list = new LinkedList<>();
    for (int i = start; i <= finish; i++) {
        MetacardImpl metacard = new MetacardImpl();
        metacard.setId(ID_PREFIX + i);
        metacard.setSourceId(SOURCE_PREFIX + i);
        metacard.setTitle(TITLE_PREFIX + i);
        // for testing an attribute with multiple values
        AttributeDescriptor ad = new AttributeDescriptorImpl(FORMAT, true, true, true, true, BasicTypes.STRING_TYPE);
        Set<AttributeDescriptor> ads = new HashSet<>(metacard.getMetacardType().getAttributeDescriptors());
        ads.add(ad);
        metacard.setType(new MetacardTypeImpl("test", ads));
        metacard.setLocation(WKT);
        AttributeImpl attr = new AttributeImpl(FORMAT, FORMAT);
        attr.addValue(FORMAT);
        metacard.setAttribute(attr);
        // for testing an attribute with no attribute descriptor
        metacard.setAttribute(RELATION, RELATION);
        list.add(metacard);
    }
    return list;
}
Also used : Metacard(ddf.catalog.data.Metacard) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) AttributeDescriptorImpl(ddf.catalog.data.impl.AttributeDescriptorImpl) LinkedList(java.util.LinkedList) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) HashSet(java.util.HashSet)

Example 8 with MetacardTypeImpl

use of ddf.catalog.data.impl.MetacardTypeImpl in project ddf by codice.

the class ValidationParser method parseMetacardTypes.

private List<Callable<Boolean>> parseMetacardTypes(Changeset changeset, List<Outer.MetacardType> metacardTypes) {
    List<Callable<Boolean>> staged = new ArrayList<>();
    BundleContext context = getBundleContext();
    for (Outer.MetacardType metacardType : metacardTypes) {
        Set<AttributeDescriptor> attributeDescriptors = new HashSet<>(BasicTypes.BASIC_METACARD.getAttributeDescriptors());
        Set<String> requiredAttributes = new HashSet<>();
        metacardType.attributes.forEach((attributeName, attribute) -> {
            AttributeDescriptor descriptor = attributeRegistry.lookup(attributeName).orElseThrow(() -> new IllegalStateException(String.format("Metacard type [%s] includes the attribute [%s], but that attribute is not in the attribute registry.", metacardType.type, attributeName)));
            attributeDescriptors.add(descriptor);
            if (attribute.required) {
                requiredAttributes.add(attributeName);
            }
        });
        if (!requiredAttributes.isEmpty()) {
            final MetacardValidator validator = new RequiredAttributesMetacardValidator(metacardType.type, requiredAttributes);
            staged.add(() -> {
                ServiceRegistration<MetacardValidator> registration = context.registerService(MetacardValidator.class, validator, null);
                changeset.metacardValidatorServices.add(registration);
                return registration != null;
            });
        }
        Dictionary<String, Object> properties = new Hashtable<>();
        properties.put(NAME_PROPERTY, metacardType.type);
        MetacardType type = new MetacardTypeImpl(metacardType.type, attributeDescriptors);
        staged.add(() -> {
            ServiceRegistration<MetacardType> registration = context.registerService(MetacardType.class, type, properties);
            changeset.metacardTypeServices.add(registration);
            return registration != null;
        });
    }
    return staged;
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) Callable(java.util.concurrent.Callable) MetacardType(ddf.catalog.data.MetacardType) MetacardValidator(ddf.catalog.validation.MetacardValidator) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator) RequiredAttributesMetacardValidator(ddf.catalog.validation.impl.validator.RequiredAttributesMetacardValidator) BundleContext(org.osgi.framework.BundleContext) HashSet(java.util.HashSet)

Example 9 with MetacardTypeImpl

use of ddf.catalog.data.impl.MetacardTypeImpl in project ddf by codice.

the class TikaInputTransformer method transform.

@Override
public Metacard transform(InputStream input, String id) throws IOException, CatalogTransformerException {
    LOGGER.debug("Transforming input stream using Tika.");
    if (input == null) {
        throw new CatalogTransformerException("Cannot transform null input.");
    }
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            IOUtils.copy(input, fileBackedOutputStream);
        } catch (IOException e) {
            throw new CatalogTransformerException("Could not copy bytes of content message.", e);
        }
        Parser parser = new AutoDetectParser();
        ToXMLContentHandler xmlContentHandler = new ToXMLContentHandler();
        ToTextContentHandler textContentHandler = null;
        ContentHandler contentHandler;
        if (!contentMetadataExtractors.isEmpty()) {
            textContentHandler = new ToTextContentHandler();
            contentHandler = new TeeContentHandler(xmlContentHandler, textContentHandler);
        } else {
            contentHandler = xmlContentHandler;
        }
        TikaMetadataExtractor tikaMetadataExtractor = new TikaMetadataExtractor(parser, contentHandler);
        Metadata metadata;
        try (InputStream inputStreamCopy = fileBackedOutputStream.asByteSource().openStream()) {
            metadata = tikaMetadataExtractor.parseMetadata(inputStreamCopy, new ParseContext());
        }
        String metadataText = xmlContentHandler.toString();
        if (templates != null) {
            metadataText = transformToXml(metadataText);
        }
        String metacardContentType = metadata.get(Metadata.CONTENT_TYPE);
        MetacardType metacardType = getMetacardTypeFromMimeType(metacardContentType);
        if (metacardType == null) {
            metacardType = commonTikaMetacardType;
        }
        Metacard metacard;
        if (textContentHandler != null) {
            String plainText = textContentHandler.toString();
            Set<AttributeDescriptor> attributes = contentMetadataExtractors.values().stream().map(ContentMetadataExtractor::getMetacardAttributes).flatMap(Collection::stream).collect(Collectors.toSet());
            MetacardTypeImpl extendedMetacardType = new MetacardTypeImpl(metacardType.getName(), metacardType, attributes);
            metacard = MetacardCreator.createMetacard(metadata, id, metadataText, extendedMetacardType);
            for (ContentMetadataExtractor contentMetadataExtractor : contentMetadataExtractors.values()) {
                contentMetadataExtractor.process(plainText, metacard);
            }
        } else {
            metacard = MetacardCreator.createMetacard(metadata, id, metadataText, metacardType);
        }
        if (StringUtils.isNotBlank(metacardContentType)) {
            metacard.setAttribute(new AttributeImpl(Core.DATATYPE, getDatatype(metacardContentType)));
        }
        if (StringUtils.startsWith(metacardContentType, "image")) {
            try (InputStream inputStreamCopy = fileBackedOutputStream.asByteSource().openStream()) {
                createThumbnail(inputStreamCopy, metacard);
            }
        }
        LOGGER.debug("Finished transforming input stream using Tika.");
        return metacard;
    }
}
Also used : ToXMLContentHandler(org.apache.tika.sax.ToXMLContentHandler) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) CloseShieldInputStream(org.apache.tika.io.CloseShieldInputStream) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) Metadata(org.apache.tika.metadata.Metadata) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) IOException(java.io.IOException) ToXMLContentHandler(org.apache.tika.sax.ToXMLContentHandler) TeeContentHandler(org.apache.tika.sax.TeeContentHandler) ToTextContentHandler(org.apache.tika.sax.ToTextContentHandler) ContentHandler(org.xml.sax.ContentHandler) MetacardType(ddf.catalog.data.MetacardType) Parser(org.apache.tika.parser.Parser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) ToTextContentHandler(org.apache.tika.sax.ToTextContentHandler) TikaMetadataExtractor(ddf.catalog.transformer.common.tika.TikaMetadataExtractor) Metacard(ddf.catalog.data.Metacard) ParseContext(org.apache.tika.parser.ParseContext) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) ContentMetadataExtractor(ddf.catalog.content.operation.ContentMetadataExtractor) TeeContentHandler(org.apache.tika.sax.TeeContentHandler)

Example 10 with MetacardTypeImpl

use of ddf.catalog.data.impl.MetacardTypeImpl in project ddf by codice.

the class FilterPluginTest method getMoreRolesMetacard.

private Metacard getMoreRolesMetacard() {
    MetacardImpl metacard = new MetacardImpl();
    metacard.setResourceSize("100");
    try {
        metacard.setResourceURI(new URI("http://some.fancy.uri/goes/here"));
    } catch (URISyntaxException e) {
        LOGGER.error("", e);
    }
    metacard.setContentTypeName("Resource");
    metacard.setTitle("Metacard 1");
    metacard.setContentTypeVersion("1");
    metacard.setType(new MetacardTypeImpl(MetacardType.DEFAULT_METACARD_TYPE_NAME, new HashSet<>()));
    HashMap<String, List<String>> security = new HashMap<>();
    security.put("Roles", Arrays.asList("A", "B", "CR", "WS"));
    metacard.setSecurity(security);
    return metacard;
}
Also used : HashMap(java.util.HashMap) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) List(java.util.List) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) HashSet(java.util.HashSet)

Aggregations

MetacardTypeImpl (ddf.catalog.data.impl.MetacardTypeImpl)40 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)22 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)21 Test (org.junit.Test)21 Metacard (ddf.catalog.data.Metacard)20 HashSet (java.util.HashSet)16 ArrayList (java.util.ArrayList)15 MetacardType (ddf.catalog.data.MetacardType)14 AttributeDescriptorImpl (ddf.catalog.data.impl.AttributeDescriptorImpl)10 URI (java.net.URI)9 HashMap (java.util.HashMap)6 List (java.util.List)6 Metadata (org.apache.tika.metadata.Metadata)6 URISyntaxException (java.net.URISyntaxException)5 SourceResponse (ddf.catalog.operation.SourceResponse)4 QueryImpl (ddf.catalog.operation.impl.QueryImpl)3 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)3 MetacardTypeAdapter (ddf.catalog.transformer.xml.adapter.MetacardTypeAdapter)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ContentItem (ddf.catalog.content.data.ContentItem)2