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()));
}
}
use of ddf.catalog.data.MetacardType in project ddf by codice.
the class CachedResourceMetacardComparator method allMetacardAttributesEqual.
private static boolean allMetacardAttributesEqual(Metacard cachedMetacard, Metacard updatedMetacard) {
MetacardType cachedMetacardType = cachedMetacard.getMetacardType();
MetacardType updatedMetacardType = updatedMetacard.getMetacardType();
if (!Objects.equals(cachedMetacardType, updatedMetacardType)) {
return false;
}
// attributes need to be compared so we can return true.
if (cachedMetacardType == null) {
return true;
}
Set<AttributeDescriptor> cachedDescriptors = cachedMetacardType.getAttributeDescriptors();
// as well and no attributes need to be compared so we can return true.
if (cachedDescriptors == null) {
return true;
}
Optional<String> difference = cachedDescriptors.stream().map(AttributeDescriptor::getName).filter(attributeName -> !ATTRIBUTES_TO_IGNORE.contains(attributeName)).filter(attributeName -> !Objects.equals(cachedMetacard.getAttribute(attributeName), updatedMetacard.getAttribute(attributeName))).findFirst();
if (LOGGER.isDebugEnabled() && difference.isPresent()) {
String attributeName = difference.get();
LOGGER.debug("Metacard updated. Attribute changed: {}, cached value: {}, updated value: {}", attributeName, cachedMetacard.getAttribute(attributeName), updatedMetacard.getAttribute(attributeName));
}
return !difference.isPresent();
}
use of ddf.catalog.data.MetacardType in project ddf by codice.
the class CatalogFrameworkImplTest method testInjectsAttributesOnDelete.
@Test
public void testInjectsAttributesOnDelete() throws Exception {
final String title = "Delete this";
final String injectAttributeName = "new attribute";
final double injectAttributeValue = 11.1;
final MetacardImpl metacard = new MetacardImpl();
metacard.setTitle(title);
metacard.setAttribute(injectAttributeName, injectAttributeValue);
final String id = framework.create(new CreateRequestImpl(Collections.singletonList(metacard), null)).getCreatedMetacards().get(0).getId();
final DeleteRequest request = new DeleteRequestImpl(id);
final AttributeDescriptor injectAttribute = new AttributeDescriptorImpl(injectAttributeName, true, true, false, false, BasicTypes.DOUBLE_TYPE);
stubMetacardInjection(injectAttribute);
List<Result> mockFederationResults = Stream.of(metacard).map(m -> {
Result mockResult = mock(Result.class);
when(mockResult.getMetacard()).thenReturn(m);
return mockResult;
}).collect(Collectors.toList());
QueryResponseImpl queryResponse = new QueryResponseImpl(mock(QueryRequest.class), mockFederationResults, 1);
when(mockFederationStrategy.federate(anyList(), anyObject())).thenReturn(queryResponse);
when(mockRemoteDeleteOperations.performRemoteDelete(any(), any())).then(returnsSecondArg());
deleteOperations.setRemoteDeleteOperations(mockRemoteDeleteOperations);
final DeleteResponse response = framework.delete(request);
final Metacard deletedMetacard = response.getDeletedMetacards().get(0);
final MetacardType originalMetacardType = metacard.getMetacardType();
final MetacardType deletedMetacardType = deletedMetacard.getMetacardType();
assertThat(deletedMetacardType.getName(), is(originalMetacardType.getName()));
final Set<AttributeDescriptor> expectedAttributeDescriptors = new HashSet<>(originalMetacardType.getAttributeDescriptors());
expectedAttributeDescriptors.add(injectAttribute);
assertThat(deletedMetacardType.getAttributeDescriptors(), is(expectedAttributeDescriptors));
assertThat(deletedMetacard.getTitle(), is(title));
assertThat(deletedMetacard.getAttribute(injectAttributeName).getValue(), is(injectAttributeValue));
}
Aggregations