Search in sources :

Example 6 with MetacardType

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

the class AttributeInjectorImplTest method testInjectIntoMetacard.

@Test
public void testInjectIntoMetacard() {
    final String title = "title";
    final Date created = new Date();
    final MetacardImpl basicMetacard = new MetacardImpl();
    basicMetacard.setTitle(title);
    basicMetacard.setCreatedDate(created);
    final MetacardType expectedBasicMetacardType = new MetacardTypeImpl(BASIC_METACARD.getName(), BASIC_METACARD, Sets.newHashSet(globalAttribute, basicAttribute, basicAndNitfAttribute));
    final Metacard injectedBasicMetacard = attributeInjector.injectAttributes(basicMetacard);
    assertThat(injectedBasicMetacard.getMetacardType(), is(expectedBasicMetacardType));
    assertThat(injectedBasicMetacard.getTitle(), is(title));
    assertThat(injectedBasicMetacard.getCreatedDate(), is(created));
    final MetacardImpl nitfMetacard = new MetacardImpl(NITF_TYPE);
    nitfMetacard.setTitle(title);
    nitfMetacard.setCreatedDate(created);
    final MetacardType expectedNitfMetacardType = new MetacardTypeImpl(NITF, NITF_TYPE, Sets.newHashSet(globalAttribute, basicAndNitfAttribute));
    final Metacard injectedNitfMetacard = attributeInjector.injectAttributes(nitfMetacard);
    assertThat(injectedNitfMetacard.getMetacardType(), is(expectedNitfMetacardType));
    assertThat(injectedNitfMetacard.getTitle(), is(title));
    assertThat(injectedNitfMetacard.getCreatedDate(), is(created));
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) Date(java.util.Date) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) MetacardType(ddf.catalog.data.MetacardType) Test(org.junit.Test)

Example 7 with MetacardType

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

the class AttributeInjectorImplTest method testInjectNothingIntoMetacardType.

@Test
public void testInjectNothingIntoMetacardType() {
    attributeInjector.setInjectableAttributes(Lists.newArrayList(basicInjection));
    final MetacardType injectedMetacardType = attributeInjector.injectAttributes(NITF_TYPE);
    assertThat(injectedMetacardType, is(sameInstance(NITF_TYPE)));
}
Also used : MetacardType(ddf.catalog.data.MetacardType) Test(org.junit.Test)

Example 8 with MetacardType

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

the class DynamicSchemaResolver method addFields.

/**
     * Adds the fields of the Metacard into the {@link SolrInputDocument}
     */
public void addFields(Metacard metacard, SolrInputDocument solrInputDocument) throws MetacardCreationException {
    MetacardType schema = metacard.getMetacardType();
    for (AttributeDescriptor ad : schema.getAttributeDescriptors()) {
        if (metacard.getAttribute(ad.getName()) != null) {
            List<Serializable> attributeValues = metacard.getAttribute(ad.getName()).getValues();
            if (attributeValues != null && attributeValues.size() > 0 && attributeValues.get(0) != null) {
                AttributeFormat format = ad.getType().getAttributeFormat();
                String formatIndexName = ad.getName() + getFieldSuffix(format);
                if (AttributeFormat.XML.equals(format)) {
                    List<String> parsedTexts = parseTextFrom(attributeValues);
                    // text => metadata_txt_ws
                    String whitespaceTokenizedIndexName = ad.getName() + getFieldSuffix(AttributeFormat.STRING) + SchemaFields.WHITESPACE_TEXT_SUFFIX;
                    solrInputDocument.addField(whitespaceTokenizedIndexName, parsedTexts);
                    // text => metadata_txt_ws_has_case
                    String whiteSpaceTokenizedHasCaseIndexName = ad.getName() + getFieldSuffix(AttributeFormat.STRING) + SchemaFields.WHITESPACE_TEXT_SUFFIX + SchemaFields.HAS_CASE;
                    solrInputDocument.addField(whiteSpaceTokenizedHasCaseIndexName, parsedTexts);
                    // text => metadata_txt_tokenized
                    String specialStringIndexName = ad.getName() + getFieldSuffix(AttributeFormat.STRING) + getSpecialIndexSuffix(AttributeFormat.STRING);
                    solrInputDocument.addField(specialStringIndexName, parsedTexts);
                    // text case sensitive
                    solrInputDocument.addField(specialStringIndexName + SchemaFields.HAS_CASE, parsedTexts);
                } else if (AttributeFormat.OBJECT.equals(format)) {
                    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
                    List<Serializable> byteArrays = new ArrayList<>();
                    try (ObjectOutputStream out = new ObjectOutputStream(byteArrayOS)) {
                        for (Serializable serializable : attributeValues) {
                            out.writeObject(serializable);
                            byteArrays.add(byteArrayOS.toByteArray());
                            out.reset();
                        }
                    } catch (IOException e) {
                        throw new MetacardCreationException(COULD_NOT_SERIALIZE_OBJECT_MESSAGE, e);
                    }
                    attributeValues = byteArrays;
                }
                // Prevent adding a field already on document
                if (solrInputDocument.getFieldValue(formatIndexName) == null) {
                    solrInputDocument.addField(formatIndexName, attributeValues);
                    if (AttributeFormat.GEOMETRY.equals(format)) {
                        solrInputDocument.addField(formatIndexName + SchemaFields.SORT_KEY_SUFFIX, createCenterPoint(attributeValues));
                    } else if (!(AttributeFormat.BINARY.equals(format) || AttributeFormat.OBJECT.equals(format))) {
                        solrInputDocument.addField(formatIndexName + SchemaFields.SORT_KEY_SUFFIX, attributeValues.get(0));
                    }
                } else {
                    LOGGER.trace("Skipping adding field already found on document ({})", formatIndexName);
                }
            }
        }
    }
    if (!ConfigurationStore.getInstance().isDisableTextPath()) {
        if (StringUtils.isNotBlank(metacard.getMetadata())) {
            try {
                byte[] luxXml = createTinyBinary(metacard.getMetadata());
                solrInputDocument.addField(LUX_XML_FIELD_NAME, luxXml);
            } catch (XMLStreamException | SaxonApiException e) {
                LOGGER.debug("Unable to parse metadata field.  XPath support unavailable for metacard {}", metacard.getId());
            }
        }
    }
    /*
         * Lastly the metacardType must be added to the solr document. These are internal fields
         */
    String schemaName = String.format("%s#%s", schema.getName(), schema.hashCode());
    solrInputDocument.addField(SchemaFields.METACARD_TYPE_FIELD_NAME, schemaName);
    byte[] metacardTypeBytes = metacardTypeNameToSerialCache.getIfPresent(schemaName);
    if (metacardTypeBytes == null) {
        MetacardType coreMetacardType = new MetacardTypeImpl(schema.getName(), convertAttributeDescriptors(schema.getAttributeDescriptors()));
        metacardTypesCache.put(schemaName, coreMetacardType);
        metacardTypeBytes = serialize(coreMetacardType);
        metacardTypeNameToSerialCache.put(schemaName, metacardTypeBytes);
        addToFieldsCache(coreMetacardType.getAttributeDescriptors());
    }
    solrInputDocument.addField(SchemaFields.METACARD_TYPE_OBJECT_FIELD_NAME, metacardTypeBytes);
}
Also used : Serializable(java.io.Serializable) MetacardCreationException(ddf.catalog.data.MetacardCreationException) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) MetacardType(ddf.catalog.data.MetacardType) AttributeFormat(ddf.catalog.data.AttributeType.AttributeFormat) XMLStreamException(javax.xml.stream.XMLStreamException) List(java.util.List) ArrayList(java.util.ArrayList)

Example 9 with MetacardType

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

the class DynamicSchemaResolverTest method deserializeMetacardType.

private MetacardType deserializeMetacardType(byte[] serializedMetacardType) throws ClassNotFoundException, IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream((byte[]) serializedMetacardType);
    ObjectInputStream in = new ObjectInputStream(bais);
    MetacardType metacardType = (MetacardType) in.readObject();
    IOUtils.closeQuietly(bais);
    IOUtils.closeQuietly(in);
    return metacardType;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MetacardType(ddf.catalog.data.MetacardType) ObjectInputStream(java.io.ObjectInputStream)

Example 10 with MetacardType

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

the class DynamicSchemaResolverTest method testAddFields.

/**
     * Verify that when a metacard type has attribute descriptors that inherit from AttributeDescriptorImpl, the attribute
     * descriptors are recreated as AttributeDescriptorsImpls before serialization into the solr cache.
     */
@Test
public void testAddFields() throws Exception {
    // Setup
    String metacardTypeName = "states";
    Set<AttributeDescriptor> addtributeDescriptors = new HashSet<AttributeDescriptor>(1);
    String propertyName = "title";
    String name = metacardTypeName + "." + propertyName;
    boolean indexed = true;
    boolean stored = true;
    boolean tokenized = false;
    boolean multiValued = false;
    addtributeDescriptors.add(new AttributeDescriptorImplTest(name, propertyName, indexed, stored, tokenized, multiValued, BasicTypes.OBJECT_TYPE));
    Serializable mockValue = mock(Serializable.class);
    Attribute mockAttribute = mock(Attribute.class);
    when(mockAttribute.getValue()).thenReturn(mockValue);
    Metacard mockMetacard = mock(Metacard.class, RETURNS_DEEP_STUBS);
    when(mockMetacard.getMetacardType().getName()).thenReturn(metacardTypeName);
    when(mockMetacard.getMetacardType().getAttributeDescriptors()).thenReturn(addtributeDescriptors);
    when(mockMetacard.getAttribute(name)).thenReturn(mockAttribute);
    ArgumentCaptor<byte[]> metacardTypeBytes = ArgumentCaptor.forClass(byte[].class);
    SolrInputDocument mockSolrInputDocument = mock(SolrInputDocument.class);
    DynamicSchemaResolver resolver = new DynamicSchemaResolver();
    // Perform Test
    resolver.addFields(mockMetacard, mockSolrInputDocument);
    // Verify: Verify that TestAttributeDescritorImpl has been recreated as a AttributeDescriptorImpl.
    verify(mockSolrInputDocument).addField(eq(SchemaFields.METACARD_TYPE_OBJECT_FIELD_NAME), metacardTypeBytes.capture());
    byte[] serializedMetacardType = metacardTypeBytes.getValue();
    MetacardType metacardType = deserializeMetacardType(serializedMetacardType);
    for (AttributeDescriptor attributeDescriptor : metacardType.getAttributeDescriptors()) {
        assertThat(attributeDescriptor.getClass().getName(), is(AttributeDescriptorImpl.class.getName()));
    }
}
Also used : Serializable(java.io.Serializable) Attribute(ddf.catalog.data.Attribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardType(ddf.catalog.data.MetacardType) Metacard(ddf.catalog.data.Metacard) SolrInputDocument(org.apache.solr.common.SolrInputDocument) HashSet(java.util.HashSet) Test(org.junit.Test)

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