use of ddf.catalog.data.Attribute in project ddf by codice.
the class TestExpirationDatePlugin method createMockMetacardsWithNoExpirationDate.
private List<Metacard> createMockMetacardsWithNoExpirationDate(int number) {
List<Metacard> mockMetacards = new ArrayList(number);
for (int i = 0; i < number; i++) {
Metacard mockMetacard = new MetacardImpl();
Attribute id = new AttributeImpl(Metacard.ID, Integer.toString(i));
mockMetacard.setAttribute(id);
Attribute title = new AttributeImpl(Metacard.TITLE, Integer.toString(i));
mockMetacard.setAttribute(title);
Attribute createdDate = new AttributeImpl(Core.METACARD_CREATED, CREATED_DATE.toDate());
mockMetacard.setAttribute(createdDate);
mockMetacards.add(mockMetacard);
}
return mockMetacards;
}
use of ddf.catalog.data.Attribute in project ddf by codice.
the class ExpirationDatePlugin method updateExpirationDate.
/**
* Updates a metacard's expiration date.
*
* @param metacard
* the metacard to update.
*/
private void updateExpirationDate(Metacard metacard) {
Date currentExpirationDate = metacard.getExpirationDate();
if (currentExpirationDate == null && !overwriteIfBlank) {
// Don't overwrite empty expiration date if configuration disallows
LOGGER.debug("The Expiration Date Pre-Ingest Plugin is not configured to overwrite 'empty' expiration dates. Not overwriting null expiration date for metacard ID [{}]. ", metacard.getId());
return;
} else if (currentExpirationDate != null && !overwriteIfExists) {
// Don't overwrite existing expiration date if configuration disallows
LOGGER.debug("The Expiration Date Pre-Ingest Plugin is not configured to overwrite 'existing' expiration dates. Not overwriting the existing expiration date of {} for metacard ID [{}]. ", currentExpirationDate, metacard.getId());
return;
}
Date metacardCreatedDate = getMetacardCreatedDate(metacard);
Date newExpirationDate = calculateNewExpirationDate(metacardCreatedDate);
LOGGER.debug("Metacard ID [{}] has an expiration date of {}. Calculating new expiration date by adding {} day(s) to the created date of {}. The new expiration date is {}.", metacard.getId(), currentExpirationDate, this.offsetFromCreatedDate, metacardCreatedDate, newExpirationDate);
Attribute expirationDate = new AttributeImpl(Metacard.EXPIRATION, newExpirationDate);
metacard.setAttribute(expirationDate);
}
use of ddf.catalog.data.Attribute in project ddf by codice.
the class ExpirationDatePlugin method getMetacardCreatedDate.
private Date getMetacardCreatedDate(Metacard metacard) {
// initialize creation to now in case metacard groomer plugin failed to set creation date
// or parsing fails
Date createdDate = new Date();
Attribute metacardCreatedAttribute = metacard.getAttribute(Core.METACARD_CREATED);
if (metacardCreatedAttribute != null && (metacardCreatedAttribute.getValue() instanceof Date)) {
createdDate = (Date) metacardCreatedAttribute.getValue();
}
return createdDate;
}
use of ddf.catalog.data.Attribute 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;
}
use of ddf.catalog.data.Attribute in project ddf by codice.
the class SaxEventHandlerUtils method getCombinedMultiValuedAttributes.
/**
* This method iterates through the attribute list provided and combines the values of common attributes. The Attribute Descriptors are used to determine if the Attribute is permitted to have multiple values.
*
* @param descriptors A set of attribute descriptors. Used to determine if the attribute can have multiple values. If empty or null, a validation warning will be added to the attributes and the attribute will default to not allow multiple values.
* @param attributes The list of attributes to combine.
* @return The list of attributes with multiple attribute values combined to a list on a single attribute. Returns null or an empty list if the attribute list provided was null or empty.
*/
public List<Attribute> getCombinedMultiValuedAttributes(Set<AttributeDescriptor> descriptors, List<Attribute> attributes) {
if (CollectionUtils.isEmpty(attributes)) {
return attributes;
}
Map<String, Boolean> multiValuedMap = getMultiValuedNameMap(descriptors);
List<String> validationWarnings = new ArrayList<>();
Map<String, Attribute> attributeMap = new HashMap<>();
for (Attribute attribute : attributes) {
String attributeName = attribute.getName();
Attribute addedAttribute = attributeMap.putIfAbsent(attributeName, attribute);
if (addedAttribute != null) {
Boolean addValue = multiValuedMap.get(attributeName);
if (addValue == null) {
validationWarnings.add(String.format("No attribute descriptor was found for attribute: '%s'. Handling the attribute as non-multi-valued.", attributeName));
addValue = false;
}
if (addValue) {
attributeMap.get(attributeName).getValues().addAll(attribute.getValues());
} else {
validationWarnings.add(String.format("Multiple values found for a non-multi-valued attribute. Attribute: '%s', Existing value: '%s', New value: '%s'. Existing value will be kept, new value will be ignored.", attributeName, attributeMap.get(attributeName).getValue(), attribute.getValue()));
}
}
}
if (!validationWarnings.isEmpty()) {
Attribute validationAttribute = attributeMap.get(ValidationAttributes.VALIDATION_WARNINGS);
if (validationAttribute != null) {
attributeMap.get(ValidationAttributes.VALIDATION_WARNINGS).getValues().addAll(validationWarnings);
} else {
attributeMap.put(ValidationAttributes.VALIDATION_WARNINGS, new AttributeImpl(ValidationAttributes.VALIDATION_WARNINGS, (Serializable) validationWarnings));
}
}
return attributeMap.values().stream().collect(Collectors.toList());
}
Aggregations