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));
}
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)));
}
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);
}
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;
}
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()));
}
}
Aggregations