Search in sources :

Example 11 with ActionServiceException

use of org.alfresco.service.cmr.action.ActionServiceException 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)

Example 12 with ActionServiceException

use of org.alfresco.service.cmr.action.ActionServiceException in project alfresco-repository by Alfresco.

the class NumericPropertyValueComparator method compare.

/**
 * @see org.alfresco.repo.action.evaluator.compare.PropertyValueComparator#compare(java.io.Serializable, java.io.Serializable, org.alfresco.repo.action.evaluator.compare.ComparePropertyValueOperation)
 */
public boolean compare(Serializable propertyValue, Serializable compareValue, ComparePropertyValueOperation operation) {
    boolean result = false;
    if (operation == null) {
        operation = ComparePropertyValueOperation.EQUALS;
    }
    // TODO need to check that doing this potential conversion does not cause a problem
    double property = ((Number) propertyValue).doubleValue();
    double compare = ((Number) compareValue).doubleValue();
    switch(operation) {
        case EQUALS:
            {
                result = (property == compare);
                break;
            }
        case GREATER_THAN:
            {
                result = (property > compare);
                break;
            }
        case GREATER_THAN_EQUAL:
            {
                result = (property >= compare);
                break;
            }
        case LESS_THAN:
            {
                result = (property < compare);
                break;
            }
        case LESS_THAN_EQUAL:
            {
                result = (property <= compare);
                break;
            }
        default:
            {
                // Raise an invalid operation exception
                throw new ActionServiceException(MSGID_INVALID_OPERATION, new Object[] { operation.toString() });
            }
    }
    return result;
}
Also used : ActionServiceException(org.alfresco.service.cmr.action.ActionServiceException)

Example 13 with ActionServiceException

use of org.alfresco.service.cmr.action.ActionServiceException in project alfresco-repository by Alfresco.

the class ComparePropertyValueEvaluatorTest method testNumericComparison.

/**
 * Test numeric comparisions
 */
@Test
public void testNumericComparison() {
    ActionConditionImpl condition = new ActionConditionImpl(GUID.generate(), ComparePropertyValueEvaluator.NAME);
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_PROPERTY, PROP_INT);
    // Test the default operation
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, INT_VALUE);
    assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 101);
    assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
    // Test equals operation
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.EQUALS.toString());
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, INT_VALUE);
    assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 101);
    assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
    // Test equals greater than operation
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.GREATER_THAN.toString());
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 99);
    assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 101);
    assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
    // Test equals greater than operation
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.GREATER_THAN_EQUAL.toString());
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 99);
    assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 100);
    assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 101);
    assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
    // Test equals less than operation
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.LESS_THAN.toString());
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 101);
    assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 99);
    assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
    // Test equals less than equals operation
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.LESS_THAN_EQUAL.toString());
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 101);
    assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 100);
    assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 99);
    assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
    // Ensure other operators are invalid
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.BEGINS.toString());
    try {
        this.evaluator.evaluate(condition, this.nodeRef);
        fail("An exception should have been raised here.");
    } catch (ActionServiceException exception) {
        exception.printStackTrace();
    }
    ;
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.ENDS.toString());
    try {
        this.evaluator.evaluate(condition, this.nodeRef);
        fail("An exception should have been raised here.");
    } catch (ActionServiceException exception) {
    }
    ;
    condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.CONTAINS.toString());
    try {
        this.evaluator.evaluate(condition, this.nodeRef);
        fail("An exception should have been raised here.");
    } catch (ActionServiceException exception) {
    }
    ;
}
Also used : ActionConditionImpl(org.alfresco.repo.action.ActionConditionImpl) ActionServiceException(org.alfresco.service.cmr.action.ActionServiceException) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Aggregations

ActionServiceException (org.alfresco.service.cmr.action.ActionServiceException)13 ActionConditionImpl (org.alfresco.repo.action.ActionConditionImpl)4 BaseSpringTest (org.alfresco.util.BaseSpringTest)4 Test (org.junit.Test)4 Date (java.util.Date)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)2 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)2 QName (org.alfresco.service.namespace.QName)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 Serializable (java.io.Serializable)1 ExecutionException (java.util.concurrent.ExecutionException)1 FutureTask (java.util.concurrent.FutureTask)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 ActionExecuter (org.alfresco.repo.action.executer.ActionExecuter)1 ACPExportPackageHandler (org.alfresco.repo.exporter.ACPExportPackageHandler)1