Search in sources :

Example 81 with Attribute

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;
}
Also used : Metacard(ddf.catalog.data.Metacard) Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 82 with Attribute

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);
}
Also used : Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) Date(java.util.Date)

Example 83 with Attribute

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;
}
Also used : Attribute(ddf.catalog.data.Attribute) Date(java.util.Date)

Example 84 with Attribute

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;
}
Also used : Serializable(java.io.Serializable) Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) SaxEventHandler(org.codice.ddf.transformer.xml.streaming.SaxEventHandler) MetacardType(ddf.catalog.data.MetacardType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Metacard(ddf.catalog.data.Metacard)

Example 85 with Attribute

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());
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList)

Aggregations

Attribute (ddf.catalog.data.Attribute)103 Metacard (ddf.catalog.data.Metacard)39 Test (org.junit.Test)37 ArrayList (java.util.ArrayList)30 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)29 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)26 Serializable (java.io.Serializable)23 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)15 HashMap (java.util.HashMap)15 Result (ddf.catalog.data.Result)14 List (java.util.List)14 MetacardType (ddf.catalog.data.MetacardType)11 QueryResponse (ddf.catalog.operation.QueryResponse)11 Date (java.util.Date)11 Map (java.util.Map)11 HashSet (java.util.HashSet)10 Optional (java.util.Optional)8 Set (java.util.Set)8 Filter (org.opengis.filter.Filter)8 UpdateRequestImpl (ddf.catalog.operation.impl.UpdateRequestImpl)7