Search in sources :

Example 1 with PropertyValueComparator

use of org.alfresco.repo.action.evaluator.compare.PropertyValueComparator in project alfresco-repository by Alfresco.

the class ComparePropertyValueEvaluator method evaluateImpl.

/**
 * @see ActionConditionEvaluatorAbstractBase#evaluateImpl(ActionCondition, NodeRef)
 */
@SuppressWarnings("unchecked")
public boolean evaluateImpl(ActionCondition ruleCondition, NodeRef actionedUponNodeRef) {
    boolean result = false;
    if (this.nodeService.exists(actionedUponNodeRef) == true) {
        // Get the name value of the node
        QName propertyQName = (QName) ruleCondition.getParameterValue(PARAM_PROPERTY);
        if (propertyQName == null) {
            if (logger.isWarnEnabled())
                logger.warn("ComparePropertyValue - Property is NULL.  Setting to " + DEFAULT_PROPERTY);
            propertyQName = DEFAULT_PROPERTY;
        }
        // Get the original value and the value to match
        Serializable propertyValue = this.nodeService.getProperty(actionedUponNodeRef, propertyQName);
        Serializable compareValue = ruleCondition.getParameterValue(PARAM_VALUE);
        // Get the operation
        ComparePropertyValueOperation operation = null;
        String operationString = (String) ruleCondition.getParameterValue(PARAM_OPERATION);
        if (operationString != null) {
            operation = ComparePropertyValueOperation.valueOf(operationString);
        }
        // Look at the type of the property (assume to be ANY if none found in dictionary)
        QName propertyTypeQName = DataTypeDefinition.ANY;
        PropertyDefinition propertyDefintion = this.dictionaryService.getProperty(propertyQName);
        if (propertyDefintion != null) {
            propertyTypeQName = propertyDefintion.getDataType().getName();
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Evaluating Property Parameters, propertyQName - [" + propertyQName + "] getInverted? [" + ruleCondition.getInvertCondition() + "] operation [" + operation + "]");
            logger.debug("Compare Value [" + compareValue + "] Actual Value [" + propertyValue + "]");
        }
        // Sort out what to do if the property is a content property
        if (DataTypeDefinition.CONTENT.equals(propertyTypeQName) == true) {
            // Get the content property name
            ContentPropertyName contentProperty = null;
            String contentPropertyString = (String) ruleCondition.getParameterValue(PARAM_CONTENT_PROPERTY);
            if (contentPropertyString == null) {
                // Error if no content property has been set
                throw new ActionServiceException(MSGID_NO_CONTENT_PROPERTY);
            } else {
                contentProperty = ContentPropertyName.valueOf(contentPropertyString);
            }
            // Get the content data
            if (propertyValue != null) {
                ContentData contentData = DefaultTypeConverter.INSTANCE.convert(ContentData.class, propertyValue);
                switch(contentProperty) {
                    case ENCODING:
                        {
                            propertyTypeQName = DataTypeDefinition.TEXT;
                            propertyValue = contentData.getEncoding();
                            break;
                        }
                    case SIZE:
                        {
                            propertyTypeQName = DataTypeDefinition.LONG;
                            propertyValue = contentData.getSize();
                            break;
                        }
                    case MIME_TYPE:
                        {
                            propertyTypeQName = DataTypeDefinition.TEXT;
                            propertyValue = contentData.getMimetype();
                            break;
                        }
                }
            }
        }
        if (propertyValue != null) {
            // Try and get a matching comparator
            PropertyValueComparator comparator = this.comparators.get(propertyTypeQName);
            if (comparator != null) {
                // Figure out if property is multivalued, compare all of the entries till finding a match
                PropertyDefinition propertyDef = dictionaryService.getProperty(propertyQName);
                if (propertyDef.isMultiValued()) {
                    for (Serializable value : ((ArrayList<Serializable>) propertyValue)) {
                        boolean success = comparator.compare(value, compareValue, operation);
                        if (success) {
                            result = true;
                            break;
                        }
                    }
                } else {
                    // Call the comparator for the property type
                    result = comparator.compare(propertyValue, compareValue, operation);
                }
            } else {
                if (logger.isWarnEnabled()) {
                    logger.warn("Comparator not found for property type " + propertyTypeQName);
                }
                // The default behaviour is to assume the property can only be compared using equals
                if (operation != null && operation != ComparePropertyValueOperation.EQUALS) {
                    // Error since only the equals operation is valid
                    throw new ActionServiceException(MSGID_INVALID_OPERATION, new Object[] { operation.toString(), propertyTypeQName.toString() });
                }
                // Use equals to compare the values
                result = compareValue.equals(propertyValue);
            }
        } else {
            if (logger.isInfoEnabled()) {
                logger.info("Condition Comparator encountered null value for property [" + propertyTypeQName + "]");
            }
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Returning result " + result);
    }
    return result;
}
Also used : Serializable(java.io.Serializable) ContentData(org.alfresco.service.cmr.repository.ContentData) PropertyValueComparator(org.alfresco.repo.action.evaluator.compare.PropertyValueComparator) ContentPropertyName(org.alfresco.repo.action.evaluator.compare.ContentPropertyName) QName(org.alfresco.service.namespace.QName) ComparePropertyValueOperation(org.alfresco.repo.action.evaluator.compare.ComparePropertyValueOperation) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) ActionServiceException(org.alfresco.service.cmr.action.ActionServiceException)

Aggregations

Serializable (java.io.Serializable)1 ComparePropertyValueOperation (org.alfresco.repo.action.evaluator.compare.ComparePropertyValueOperation)1 ContentPropertyName (org.alfresco.repo.action.evaluator.compare.ContentPropertyName)1 PropertyValueComparator (org.alfresco.repo.action.evaluator.compare.PropertyValueComparator)1 ActionServiceException (org.alfresco.service.cmr.action.ActionServiceException)1 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)1 ContentData (org.alfresco.service.cmr.repository.ContentData)1 QName (org.alfresco.service.namespace.QName)1