mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Moving to root below branch label
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2005 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import org.alfresco.service.cmr.action.ActionCondition;
|
||||
import org.alfresco.service.cmr.action.ActionConditionDefinition;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
|
||||
/**
|
||||
* Action Condition Evaluator
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public interface ActionConditionEvaluator
|
||||
{
|
||||
/**
|
||||
* Get the action condition deinfinition
|
||||
*
|
||||
* @return the action condition definition
|
||||
*/
|
||||
public ActionConditionDefinition getActionConditionDefintion();
|
||||
|
||||
/**
|
||||
* Evaluate the action condition
|
||||
*
|
||||
* @param actionCondition the action condition
|
||||
* @param actionedUponNodeRef the actioned upon node
|
||||
* @return true if the condition passes, false otherwise
|
||||
*/
|
||||
public boolean evaluate(
|
||||
ActionCondition actionCondition,
|
||||
NodeRef actionedUponNodeRef);
|
||||
}
|
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import org.alfresco.repo.action.ActionConditionDefinitionImpl;
|
||||
import org.alfresco.repo.action.ParameterizedItemAbstractBase;
|
||||
import org.alfresco.service.cmr.action.ActionCondition;
|
||||
import org.alfresco.service.cmr.action.ActionConditionDefinition;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
|
||||
/**
|
||||
* Rule condition evaluator abstract base implementation.
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public abstract class ActionConditionEvaluatorAbstractBase extends ParameterizedItemAbstractBase implements ActionConditionEvaluator
|
||||
{
|
||||
/**
|
||||
* Indicates whether the condition is public or not
|
||||
*/
|
||||
private boolean publicCondition = true;
|
||||
|
||||
/**
|
||||
* The action condition definition
|
||||
*/
|
||||
protected ActionConditionDefinition actionConditionDefinition;
|
||||
|
||||
/**
|
||||
* Initialise method
|
||||
*/
|
||||
public void init()
|
||||
{
|
||||
if (this.publicCondition == true)
|
||||
{
|
||||
// Call back to the action service to register the condition
|
||||
this.runtimeActionService.registerActionConditionEvaluator(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value that indicates whether a condition is public or not
|
||||
*
|
||||
* @param publicCondition true if the condition is public, false otherwise
|
||||
*/
|
||||
public void setPublicCondition(boolean publicCondition)
|
||||
{
|
||||
this.publicCondition = publicCondition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the action condition definition.
|
||||
*
|
||||
* @return the action condition definition
|
||||
*/
|
||||
public ActionConditionDefinition getActionConditionDefintion()
|
||||
{
|
||||
if (this.actionConditionDefinition == null)
|
||||
{
|
||||
this.actionConditionDefinition = new ActionConditionDefinitionImpl(this.name);
|
||||
((ActionConditionDefinitionImpl)this.actionConditionDefinition).setTitleKey(getTitleKey());
|
||||
((ActionConditionDefinitionImpl)this.actionConditionDefinition).setDescriptionKey(getDescriptionKey());
|
||||
((ActionConditionDefinitionImpl)this.actionConditionDefinition).setAdhocPropertiesAllowed(getAdhocPropertiesAllowed());
|
||||
((ActionConditionDefinitionImpl)this.actionConditionDefinition).setConditionEvaluator(this.name);
|
||||
((ActionConditionDefinitionImpl)this.actionConditionDefinition).setParameterDefinitions(getParameterDefintions());
|
||||
}
|
||||
return this.actionConditionDefinition;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.evaluator.ActionConditionEvaluator#evaluate(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef)
|
||||
*/
|
||||
public boolean evaluate(ActionCondition actionCondition, NodeRef actionedUponNodeRef)
|
||||
{
|
||||
checkMandatoryProperties(actionCondition, getActionConditionDefintion());
|
||||
boolean result = evaluateImpl(actionCondition, actionedUponNodeRef);
|
||||
if (actionCondition.getInvertCondition() == true)
|
||||
{
|
||||
result = !result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluation implementation
|
||||
*
|
||||
* @param actionCondition the action condition
|
||||
* @param actionedUponNodeRef the actioned upon node reference
|
||||
* @return the result of the condition evaluation
|
||||
*/
|
||||
protected abstract boolean evaluateImpl(ActionCondition actionCondition, NodeRef actionedUponNodeRef);
|
||||
}
|
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.action.evaluator.compare.ContentPropertyName;
|
||||
import org.alfresco.service.cmr.action.ActionCondition;
|
||||
import org.alfresco.service.cmr.action.ActionServiceException;
|
||||
import org.alfresco.service.cmr.action.ParameterDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* Compare mime type evaluator
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class CompareMimeTypeEvaluator extends ComparePropertyValueEvaluator
|
||||
{
|
||||
/**
|
||||
* Evaluator constants
|
||||
*/
|
||||
public static final String NAME = "compare-mime-type";
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final String ERRID_NOT_A_CONTENT_TYPE = "compare_mime_type_evaluator.not_a_content_type";
|
||||
private static final String ERRID_NO_PROPERTY_DEFINTION_FOUND = "compare_mime_type_evaluator.no_property_definition_found";
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.evaluator.ActionConditionEvaluatorAbstractBase#evaluateImpl(org.alfresco.service.cmr.action.ActionCondition, org.alfresco.service.cmr.repository.NodeRef)
|
||||
*/
|
||||
public boolean evaluateImpl(ActionCondition actionCondition, NodeRef actionedUponNodeRef)
|
||||
{
|
||||
QName propertyQName = (QName)actionCondition.getParameterValue(ComparePropertyValueEvaluator.PARAM_PROPERTY);
|
||||
if (propertyQName == null)
|
||||
{
|
||||
// Default to the standard content property
|
||||
actionCondition.setParameterValue(ComparePropertyValueEvaluator.PARAM_PROPERTY, ContentModel.PROP_CONTENT);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Ensure that we are dealing with a content property
|
||||
QName propertyTypeQName = null;
|
||||
PropertyDefinition propertyDefintion = this.dictionaryService.getProperty(propertyQName);
|
||||
if (propertyDefintion != null)
|
||||
{
|
||||
propertyTypeQName = propertyDefintion.getDataType().getName();
|
||||
if (DataTypeDefinition.CONTENT.equals(propertyTypeQName) == false)
|
||||
{
|
||||
throw new ActionServiceException(ERRID_NOT_A_CONTENT_TYPE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ActionServiceException(ERRID_NO_PROPERTY_DEFINTION_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
// Set the content property to be MIMETYPE
|
||||
actionCondition.setParameterValue(ComparePropertyValueEvaluator.PARAM_CONTENT_PROPERTY, ContentPropertyName.MIME_TYPE.toString());
|
||||
|
||||
return super.evaluateImpl(actionCondition, actionedUponNodeRef);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.ParameterizedItemAbstractBase#addParameterDefintions(java.util.List)
|
||||
*/
|
||||
@Override
|
||||
protected void addParameterDefintions(List<ParameterDefinition> paramList)
|
||||
{
|
||||
super.addParameterDefintions(paramList);
|
||||
}
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.action.ActionConditionImpl;
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.ContentWriter;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.util.BaseSpringTest;
|
||||
import org.alfresco.util.GUID;
|
||||
|
||||
/**
|
||||
* Compare property value evaluator test
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class CompareMimeTypeEvaluatorTest extends BaseSpringTest
|
||||
{
|
||||
private NodeService nodeService;
|
||||
private ContentService contentService;
|
||||
private StoreRef testStoreRef;
|
||||
private NodeRef rootNodeRef;
|
||||
private NodeRef nodeRef;
|
||||
private CompareMimeTypeEvaluator evaluator;
|
||||
|
||||
/**
|
||||
* @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpInTransaction()
|
||||
*/
|
||||
@Override
|
||||
protected void onSetUpInTransaction() throws Exception
|
||||
{
|
||||
|
||||
this.nodeService = (NodeService)this.applicationContext.getBean("nodeService");
|
||||
this.contentService = (ContentService)this.applicationContext.getBean("contentService");
|
||||
|
||||
// Create the store and get the root node
|
||||
this.testStoreRef = this.nodeService.createStore(
|
||||
StoreRef.PROTOCOL_WORKSPACE, "Test_"
|
||||
+ System.currentTimeMillis());
|
||||
this.rootNodeRef = this.nodeService.getRootNode(this.testStoreRef);
|
||||
|
||||
// Create the node used for tests
|
||||
this.nodeRef = this.nodeService.createNode(
|
||||
this.rootNodeRef,
|
||||
ContentModel.ASSOC_CHILDREN,
|
||||
QName.createQName("{test}testnode"),
|
||||
ContentModel.TYPE_CONTENT).getChildRef();
|
||||
|
||||
this.evaluator = (CompareMimeTypeEvaluator)this.applicationContext.getBean(CompareMimeTypeEvaluator.NAME);
|
||||
}
|
||||
|
||||
public void testContentPropertyComparisons()
|
||||
{
|
||||
ActionConditionImpl condition = new ActionConditionImpl(GUID.generate(), ComparePropertyValueEvaluator.NAME);
|
||||
|
||||
// What happens if you do this and the node has no content set yet !!
|
||||
|
||||
// Add some content to the node reference
|
||||
ContentWriter contentWriter = this.contentService.getWriter(this.nodeRef, ContentModel.PROP_CONTENT, true);
|
||||
contentWriter.setEncoding("UTF-8");
|
||||
contentWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
|
||||
contentWriter.putContent("This is some test content.");
|
||||
|
||||
// Test matching the mimetype
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, MimetypeMap.MIMETYPE_TEXT_PLAIN);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, MimetypeMap.MIMETYPE_HTML);
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
}
|
||||
}
|
@@ -0,0 +1,266 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.action.ParameterDefinitionImpl;
|
||||
import org.alfresco.repo.action.evaluator.compare.ComparePropertyValueOperation;
|
||||
import org.alfresco.repo.action.evaluator.compare.ContentPropertyName;
|
||||
import org.alfresco.repo.action.evaluator.compare.PropertyValueComparator;
|
||||
import org.alfresco.service.cmr.action.ActionCondition;
|
||||
import org.alfresco.service.cmr.action.ActionServiceException;
|
||||
import org.alfresco.service.cmr.action.ParameterDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
|
||||
import org.alfresco.service.cmr.repository.ContentData;
|
||||
import org.alfresco.service.cmr.repository.ContentReader;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* Compare property value evaluator
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class ComparePropertyValueEvaluator extends ActionConditionEvaluatorAbstractBase
|
||||
{
|
||||
/**
|
||||
* Evaluator constants
|
||||
*/
|
||||
public final static String NAME = "compare-property-value";
|
||||
public final static String PARAM_PROPERTY = "property";
|
||||
public final static String PARAM_CONTENT_PROPERTY = "content-property";
|
||||
public final static String PARAM_VALUE = "value";
|
||||
public final static String PARAM_OPERATION = "operation";
|
||||
|
||||
/**
|
||||
* The default property to check if none is specified in the properties
|
||||
*/
|
||||
private final static QName DEFAULT_PROPERTY = ContentModel.PROP_NAME;
|
||||
|
||||
/**
|
||||
* I18N message ID's
|
||||
*/
|
||||
private static final String MSGID_INVALID_OPERATION = "compare_property_value_evaluator.invalid_operation";
|
||||
private static final String MSGID_NO_CONTENT_PROPERTY = "compare_property_value_evaluator.no_content_property";
|
||||
|
||||
/**
|
||||
* Map of comparators used by different property types
|
||||
*/
|
||||
private Map<QName, PropertyValueComparator> comparators = new HashMap<QName, PropertyValueComparator>();
|
||||
|
||||
/**
|
||||
* The node service
|
||||
*/
|
||||
protected NodeService nodeService;
|
||||
|
||||
/**
|
||||
* The content service
|
||||
*/
|
||||
protected ContentService contentService;
|
||||
|
||||
/**
|
||||
* The dictionary service
|
||||
*/
|
||||
protected DictionaryService dictionaryService;
|
||||
|
||||
/**
|
||||
* Set node service
|
||||
*
|
||||
* @param nodeService the node service
|
||||
*/
|
||||
public void setNodeService(NodeService nodeService)
|
||||
{
|
||||
this.nodeService = nodeService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the content service
|
||||
*
|
||||
* @param contentService the content service
|
||||
*/
|
||||
public void setContentService(ContentService contentService)
|
||||
{
|
||||
this.contentService = contentService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the dictionary service
|
||||
*
|
||||
* @param dictionaryService the dictionary service
|
||||
*/
|
||||
public void setDictionaryService(DictionaryService dictionaryService)
|
||||
{
|
||||
this.dictionaryService = dictionaryService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the list of property value comparators
|
||||
*
|
||||
* @param comparators the list of property value comparators
|
||||
*/
|
||||
public void setPropertyValueComparators(List<PropertyValueComparator> comparators)
|
||||
{
|
||||
for (PropertyValueComparator comparator : comparators)
|
||||
{
|
||||
comparator.registerComparator(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a comparator for a given property data type.
|
||||
*
|
||||
* @param dataType property data type
|
||||
* @param comparator property value comparator
|
||||
*/
|
||||
public void registerComparator(QName dataType, PropertyValueComparator comparator)
|
||||
{
|
||||
this.comparators.put(dataType, comparator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add paremeter defintions
|
||||
*/
|
||||
@Override
|
||||
protected void addParameterDefintions(List<ParameterDefinition> paramList)
|
||||
{
|
||||
paramList.add(new ParameterDefinitionImpl(PARAM_PROPERTY, DataTypeDefinition.QNAME, false, getParamDisplayLabel(PARAM_PROPERTY)));
|
||||
paramList.add(new ParameterDefinitionImpl(PARAM_CONTENT_PROPERTY, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_CONTENT_PROPERTY)));
|
||||
paramList.add(new ParameterDefinitionImpl(PARAM_VALUE, DataTypeDefinition.ANY, true, getParamDisplayLabel(PARAM_VALUE)));
|
||||
paramList.add(new ParameterDefinitionImpl(PARAM_OPERATION, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_OPERATION)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ActionConditionEvaluatorAbstractBase#evaluateImpl(ActionCondition, NodeRef)
|
||||
*/
|
||||
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)
|
||||
{
|
||||
propertyQName = DEFAULT_PROPERTY;
|
||||
}
|
||||
|
||||
// Get the origional 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 dicitionary)
|
||||
QName propertyTypeQName = DataTypeDefinition.ANY;
|
||||
PropertyDefinition propertyDefintion = this.dictionaryService.getProperty(propertyQName);
|
||||
if (propertyDefintion != null)
|
||||
{
|
||||
propertyTypeQName = propertyDefintion.getDataType().getName();
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
// Call the comparator for the property type
|
||||
result = comparator.compare(propertyValue, compareValue, operation);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,507 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.action.ActionConditionImpl;
|
||||
import org.alfresco.repo.action.evaluator.compare.ComparePropertyValueOperation;
|
||||
import org.alfresco.repo.action.evaluator.compare.ContentPropertyName;
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.repo.dictionary.DictionaryDAO;
|
||||
import org.alfresco.repo.dictionary.M2Model;
|
||||
import org.alfresco.repo.dictionary.M2Property;
|
||||
import org.alfresco.repo.dictionary.M2Type;
|
||||
import org.alfresco.service.cmr.action.ActionServiceException;
|
||||
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.ContentWriter;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.util.BaseSpringTest;
|
||||
import org.alfresco.util.GUID;
|
||||
|
||||
/**
|
||||
* Compare property value evaluator test
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class ComparePropertyValueEvaluatorTest extends BaseSpringTest
|
||||
{
|
||||
private static final String TEST_TYPE_NAMESPACE = "testNamespace";
|
||||
private static final QName TEST_TYPE_QNAME = QName.createQName(TEST_TYPE_NAMESPACE, "testType");
|
||||
private static final QName PROP_TEXT = QName.createQName(TEST_TYPE_NAMESPACE, "propText");
|
||||
private static final QName PROP_INT = QName.createQName(TEST_TYPE_NAMESPACE, "propInt");
|
||||
private static final QName PROP_DATETIME = QName.createQName(TEST_TYPE_NAMESPACE, "propDatetime");
|
||||
private static final QName PROP_NODEREF = QName.createQName(TEST_TYPE_NAMESPACE, "propNodeRef");
|
||||
|
||||
private static final String TEXT_VALUE = "myDocument.doc";
|
||||
private static final int INT_VALUE = 100;
|
||||
|
||||
private Date beforeDateValue;
|
||||
private Date dateValue;
|
||||
private Date afterDateValue;
|
||||
private NodeRef nodeValue;
|
||||
|
||||
private DictionaryDAO dictionaryDAO;
|
||||
private NodeService nodeService;
|
||||
private ContentService contentService;
|
||||
private StoreRef testStoreRef;
|
||||
private NodeRef rootNodeRef;
|
||||
private NodeRef nodeRef;
|
||||
private ComparePropertyValueEvaluator evaluator;
|
||||
|
||||
/**
|
||||
* Sets the meta model DAO
|
||||
*
|
||||
* @param dictionaryDAO the meta model DAO
|
||||
*/
|
||||
public void setDictionaryDAO(DictionaryDAO dictionaryDAO)
|
||||
{
|
||||
this.dictionaryDAO = dictionaryDAO;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpInTransaction()
|
||||
*/
|
||||
@Override
|
||||
protected void onSetUpInTransaction() throws Exception
|
||||
{
|
||||
// Need to create model to contain our custom type
|
||||
createTestModel();
|
||||
|
||||
this.nodeService = (NodeService)this.applicationContext.getBean("nodeService");
|
||||
this.contentService = (ContentService)this.applicationContext.getBean("contentService");
|
||||
|
||||
// Create the store and get the root node
|
||||
this.testStoreRef = this.nodeService.createStore(
|
||||
StoreRef.PROTOCOL_WORKSPACE, "Test_"
|
||||
+ System.currentTimeMillis());
|
||||
this.rootNodeRef = this.nodeService.getRootNode(this.testStoreRef);
|
||||
|
||||
this.nodeValue = new NodeRef(this.testStoreRef, "1234");
|
||||
|
||||
this.beforeDateValue = new Date();
|
||||
Thread.sleep(2000);
|
||||
this.dateValue = new Date();
|
||||
Thread.sleep(2000);
|
||||
this.afterDateValue = new Date();
|
||||
|
||||
Map<QName, Serializable> props = new HashMap<QName, Serializable>();
|
||||
props.put(PROP_TEXT, TEXT_VALUE);
|
||||
props.put(PROP_INT, INT_VALUE);
|
||||
props.put(PROP_DATETIME, this.dateValue);
|
||||
props.put(PROP_NODEREF, this.nodeValue);
|
||||
|
||||
// Create the node used for tests
|
||||
this.nodeRef = this.nodeService.createNode(
|
||||
this.rootNodeRef,
|
||||
ContentModel.ASSOC_CHILDREN,
|
||||
QName.createQName("{test}testnode"),
|
||||
TEST_TYPE_QNAME,
|
||||
props).getChildRef();
|
||||
|
||||
this.evaluator = (ComparePropertyValueEvaluator)this.applicationContext.getBean(ComparePropertyValueEvaluator.NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test numeric comparisions
|
||||
*/
|
||||
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) {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Test date comparison
|
||||
*/
|
||||
public void testDateComparison()
|
||||
{
|
||||
ActionConditionImpl condition = new ActionConditionImpl(GUID.generate(), ComparePropertyValueEvaluator.NAME);
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_PROPERTY, PROP_DATETIME);
|
||||
|
||||
// Test the default operation
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, this.dateValue);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, new Date());
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
// Test the equals operation
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.EQUALS.toString());
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, this.dateValue);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, new Date());
|
||||
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, this.beforeDateValue);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, this.afterDateValue);
|
||||
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, this.beforeDateValue);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, this.dateValue);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, this.afterDateValue);
|
||||
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, this.afterDateValue);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, this.beforeDateValue);
|
||||
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, this.afterDateValue);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, this.dateValue);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, this.beforeDateValue);
|
||||
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) {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Test text comparison
|
||||
*/
|
||||
public void testTextComparison()
|
||||
{
|
||||
ActionConditionImpl condition = new ActionConditionImpl(GUID.generate(), ComparePropertyValueEvaluator.NAME);
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_PROPERTY, PROP_TEXT);
|
||||
|
||||
// Test default operations implied by presence and position of *
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "*.doc");
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "*.xls");
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "my*");
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "bad*");
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "Document");
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "bobbins");
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
// Test equals operator
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.EQUALS.toString());
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, TEXT_VALUE);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "bobbins");
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
// Test contains operator
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.CONTAINS.toString());
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "Document");
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "bobbins");
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
// Test begins operator
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.BEGINS.toString());
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "my");
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "bobbins");
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
// Test ends operator
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.ENDS.toString());
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "doc");
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "bobbins");
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
// Ensure other operators are invalid
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.GREATER_THAN.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.GREATER_THAN_EQUAL.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.LESS_THAN.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.LESS_THAN_EQUAL.toString());
|
||||
try { this.evaluator.evaluate(condition, this.nodeRef); fail("An exception should have been raised here."); } catch (ActionServiceException exception) {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Test some combinations of test file names that had been failing
|
||||
*/
|
||||
public void testTempFileNames()
|
||||
{
|
||||
ActionConditionImpl condition = new ActionConditionImpl(GUID.generate(), ComparePropertyValueEvaluator.NAME);
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_PROPERTY, PROP_TEXT);
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "~*.doc");
|
||||
this.nodeService.setProperty(this.nodeRef, PROP_TEXT, "~1234.doc");
|
||||
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test comparison of properties that do not have a registered comparitor
|
||||
*/
|
||||
public void testOtherComparison()
|
||||
{
|
||||
NodeRef badNodeRef = new NodeRef(this.testStoreRef, "badId");
|
||||
|
||||
ActionConditionImpl condition = new ActionConditionImpl(GUID.generate(), ComparePropertyValueEvaluator.NAME);
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_PROPERTY, PROP_NODEREF);
|
||||
|
||||
// Test default operation
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, this.nodeValue);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, badNodeRef);
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "this isn't even the correct type!");
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
// Test equals operation
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.EQUALS.toString());
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, this.nodeValue);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, badNodeRef);
|
||||
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) {};
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.GREATER_THAN.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.GREATER_THAN_EQUAL.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.LESS_THAN.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.LESS_THAN_EQUAL.toString());
|
||||
try { this.evaluator.evaluate(condition, this.nodeRef); fail("An exception should have been raised here."); } catch (ActionServiceException exception) {};
|
||||
|
||||
}
|
||||
|
||||
public void testContentPropertyComparisons()
|
||||
{
|
||||
ActionConditionImpl condition = new ActionConditionImpl(GUID.generate(), ComparePropertyValueEvaluator.NAME);
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_PROPERTY, ContentModel.PROP_CONTENT);
|
||||
|
||||
// What happens if you do this and the node has no content set yet !!
|
||||
|
||||
// Add some content to the node reference
|
||||
ContentWriter contentWriter = this.contentService.getWriter(this.nodeRef, ContentModel.PROP_CONTENT, true);
|
||||
contentWriter.setEncoding("UTF-8");
|
||||
contentWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
|
||||
contentWriter.putContent("This is some test content.");
|
||||
|
||||
// Test matching the mimetype
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_CONTENT_PROPERTY, ContentPropertyName.MIME_TYPE.toString());
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, MimetypeMap.MIMETYPE_TEXT_PLAIN);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, MimetypeMap.MIMETYPE_HTML);
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
// Test matching the encoding
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_CONTENT_PROPERTY, ContentPropertyName.ENCODING.toString());
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "UTF-8");
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, "UTF-16");
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
// Test comparision to the size of the content
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_CONTENT_PROPERTY, ContentPropertyName.SIZE.toString());
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_OPERATION, ComparePropertyValueOperation.LESS_THAN.toString());
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 50);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
condition.setParameterValue(ComparePropertyValueEvaluator.PARAM_VALUE, 2);
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void createTestModel()
|
||||
{
|
||||
M2Model model = M2Model.createModel("test:comparepropertyvalueevaluatortest");
|
||||
model.createNamespace(TEST_TYPE_NAMESPACE, "test");
|
||||
model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, NamespaceService.DICTIONARY_MODEL_PREFIX);
|
||||
model.createImport(NamespaceService.SYSTEM_MODEL_1_0_URI, NamespaceService.SYSTEM_MODEL_PREFIX);
|
||||
model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);
|
||||
|
||||
M2Type testType = model.createType("test:" + TEST_TYPE_QNAME.getLocalName());
|
||||
testType.setParentName("cm:" + ContentModel.TYPE_CONTENT.getLocalName());
|
||||
|
||||
M2Property prop1 = testType.createProperty("test:" + PROP_TEXT.getLocalName());
|
||||
prop1.setMandatory(false);
|
||||
prop1.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
|
||||
prop1.setMultiValued(false);
|
||||
|
||||
M2Property prop2 = testType.createProperty("test:" + PROP_INT.getLocalName());
|
||||
prop2.setMandatory(false);
|
||||
prop2.setType("d:" + DataTypeDefinition.INT.getLocalName());
|
||||
prop2.setMultiValued(false);
|
||||
|
||||
M2Property prop3 = testType.createProperty("test:" + PROP_DATETIME.getLocalName());
|
||||
prop3.setMandatory(false);
|
||||
prop3.setType("d:" + DataTypeDefinition.DATETIME.getLocalName());
|
||||
prop3.setMultiValued(false);
|
||||
|
||||
M2Property prop4 = testType.createProperty("test:" + PROP_NODEREF.getLocalName());
|
||||
prop4.setMandatory(false);
|
||||
prop4.setType("d:" + DataTypeDefinition.NODE_REF.getLocalName());
|
||||
prop4.setMultiValued(false);
|
||||
|
||||
dictionaryDAO.putModel(model);
|
||||
}
|
||||
}
|
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.repo.action.ParameterDefinitionImpl;
|
||||
import org.alfresco.service.cmr.action.ActionCondition;
|
||||
import org.alfresco.service.cmr.action.ParameterDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* Has aspect evaluator
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class HasAspectEvaluator extends ActionConditionEvaluatorAbstractBase
|
||||
{
|
||||
/**
|
||||
* Evaluator constants
|
||||
*/
|
||||
public static final String NAME = "has-aspect";
|
||||
public static final String PARAM_ASPECT = "aspect";
|
||||
|
||||
/**
|
||||
* The node service
|
||||
*/
|
||||
private NodeService nodeService;
|
||||
|
||||
/**
|
||||
* Set node service
|
||||
*
|
||||
* @param nodeService the node service
|
||||
*/
|
||||
public void setNodeService(NodeService nodeService)
|
||||
{
|
||||
this.nodeService = nodeService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.evaluator.ActionConditionEvaluatorAbstractBase#evaluateImpl(org.alfresco.service.cmr.action.ActionCondition, org.alfresco.service.cmr.repository.NodeRef)
|
||||
*/
|
||||
public boolean evaluateImpl(ActionCondition ruleCondition, NodeRef actionedUponNodeRef)
|
||||
{
|
||||
boolean result = false;
|
||||
|
||||
if (this.nodeService.exists(actionedUponNodeRef) == true)
|
||||
{
|
||||
if (this.nodeService.hasAspect(actionedUponNodeRef, (QName)ruleCondition.getParameterValue(PARAM_ASPECT)) == true)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.ParameterizedItemAbstractBase#addParameterDefintions(java.util.List)
|
||||
*/
|
||||
@Override
|
||||
protected void addParameterDefintions(List<ParameterDefinition> paramList)
|
||||
{
|
||||
paramList.add(new ParameterDefinitionImpl(PARAM_ASPECT, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_ASPECT)));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.action.ActionConditionImpl;
|
||||
import org.alfresco.service.cmr.action.ActionCondition;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.util.BaseSpringTest;
|
||||
import org.alfresco.util.GUID;
|
||||
|
||||
/**
|
||||
* Is sub class evaluator test
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class HasAspectEvaluatorTest extends BaseSpringTest
|
||||
{
|
||||
private NodeService nodeService;
|
||||
private StoreRef testStoreRef;
|
||||
private NodeRef rootNodeRef;
|
||||
private NodeRef nodeRef;
|
||||
private HasAspectEvaluator evaluator;
|
||||
|
||||
private final static String ID = GUID.generate();
|
||||
|
||||
@Override
|
||||
protected void onSetUpInTransaction() throws Exception
|
||||
{
|
||||
this.nodeService = (NodeService)this.applicationContext.getBean("nodeService");
|
||||
|
||||
// Create the store and get the root node
|
||||
this.testStoreRef = this.nodeService.createStore(
|
||||
StoreRef.PROTOCOL_WORKSPACE, "Test_"
|
||||
+ System.currentTimeMillis());
|
||||
this.rootNodeRef = this.nodeService.getRootNode(this.testStoreRef);
|
||||
|
||||
// Create the node used for tests
|
||||
this.nodeRef = this.nodeService.createNode(
|
||||
this.rootNodeRef,
|
||||
ContentModel.ASSOC_CHILDREN,
|
||||
QName.createQName("{test}testnode"),
|
||||
ContentModel.TYPE_CONTENT).getChildRef();
|
||||
|
||||
this.evaluator = (HasAspectEvaluator)this.applicationContext.getBean(HasAspectEvaluator.NAME);
|
||||
}
|
||||
|
||||
public void testMandatoryParamsMissing()
|
||||
{
|
||||
ActionCondition condition = new ActionConditionImpl(ID, HasAspectEvaluator.NAME, null);
|
||||
|
||||
try
|
||||
{
|
||||
this.evaluator.evaluate(condition, this.nodeRef);
|
||||
fail("The fact that a mandatory parameter has not been set should have been detected.");
|
||||
}
|
||||
catch (Throwable exception)
|
||||
{
|
||||
// Do nothing since this is correct
|
||||
}
|
||||
}
|
||||
|
||||
public void testPass()
|
||||
{
|
||||
this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE, null);
|
||||
ActionCondition condition = new ActionConditionImpl(ID, HasAspectEvaluator.NAME, null);
|
||||
condition.setParameterValue(HasAspectEvaluator.PARAM_ASPECT, ContentModel.ASPECT_VERSIONABLE);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
}
|
||||
|
||||
public void testFail()
|
||||
{
|
||||
ActionCondition condition = new ActionConditionImpl(ID, HasAspectEvaluator.NAME, null);
|
||||
condition.setParameterValue(HasAspectEvaluator.PARAM_ASPECT, ContentModel.ASPECT_VERSIONABLE);
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
}
|
||||
}
|
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.service.cmr.action.ActionCondition;
|
||||
import org.alfresco.service.cmr.action.ParameterDefinition;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.version.VersionHistory;
|
||||
import org.alfresco.service.cmr.version.VersionService;
|
||||
|
||||
/**
|
||||
* Has version history evaluator
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class HasVersionHistoryEvaluator extends ActionConditionEvaluatorAbstractBase
|
||||
{
|
||||
/**
|
||||
* Evaluator constants
|
||||
*/
|
||||
public static final String NAME = "has-version-history";
|
||||
|
||||
/**
|
||||
* The node service
|
||||
*/
|
||||
private NodeService nodeService;
|
||||
|
||||
private VersionService versionService;
|
||||
|
||||
/**
|
||||
* Set node service
|
||||
*
|
||||
* @param nodeService the node service
|
||||
*/
|
||||
public void setNodeService(NodeService nodeService)
|
||||
{
|
||||
this.nodeService = nodeService;
|
||||
}
|
||||
|
||||
public void setVersionService(VersionService versionService)
|
||||
{
|
||||
this.versionService = versionService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.evaluator.ActionConditionEvaluatorAbstractBase#evaluateImpl(org.alfresco.service.cmr.action.ActionCondition, org.alfresco.service.cmr.repository.NodeRef)
|
||||
*/
|
||||
public boolean evaluateImpl(ActionCondition ruleCondition, NodeRef actionedUponNodeRef)
|
||||
{
|
||||
boolean result = false;
|
||||
|
||||
if (this.nodeService.exists(actionedUponNodeRef) == true &&
|
||||
this.nodeService.hasAspect(actionedUponNodeRef, ContentModel.ASPECT_VERSIONABLE) == true)
|
||||
{
|
||||
VersionHistory versionHistory = this.versionService.getVersionHistory(actionedUponNodeRef);
|
||||
if (versionHistory != null && versionHistory.getAllVersions().size() != 0)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.ParameterizedItemAbstractBase#addParameterDefintions(java.util.List)
|
||||
*/
|
||||
@Override
|
||||
protected void addParameterDefintions(List<ParameterDefinition> paramList)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.action.ParameterDefinitionImpl;
|
||||
import org.alfresco.service.cmr.action.ActionCondition;
|
||||
import org.alfresco.service.cmr.action.ParameterDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* In category evaluator implementation.
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class InCategoryEvaluator extends ActionConditionEvaluatorAbstractBase
|
||||
{
|
||||
/**
|
||||
* Rule constants
|
||||
*/
|
||||
public static final String NAME = "in-category";
|
||||
public static final String PARAM_CATEGORY_ASPECT = "category-aspect";
|
||||
public static final String PARAM_CATEGORY_VALUE = "category-value";
|
||||
|
||||
/**
|
||||
* The node service
|
||||
*/
|
||||
private NodeService nodeService;
|
||||
|
||||
/**
|
||||
* The dictionary service
|
||||
*/
|
||||
private DictionaryService dictionaryService;
|
||||
|
||||
/**
|
||||
* Sets the node service
|
||||
*
|
||||
* @param nodeService the node service
|
||||
*/
|
||||
public void setNodeService(NodeService nodeService)
|
||||
{
|
||||
this.nodeService = nodeService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the dictionary service
|
||||
*
|
||||
* @param dictionaryService the dictionary service
|
||||
*/
|
||||
public void setDictionaryService(DictionaryService dictionaryService)
|
||||
{
|
||||
this.dictionaryService = dictionaryService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the parameter definitions
|
||||
*/
|
||||
@Override
|
||||
protected void addParameterDefintions(List<ParameterDefinition> paramList)
|
||||
{
|
||||
paramList.add(new ParameterDefinitionImpl(PARAM_CATEGORY_ASPECT, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_CATEGORY_ASPECT)));
|
||||
paramList.add(new ParameterDefinitionImpl(PARAM_CATEGORY_VALUE, DataTypeDefinition.NODE_REF, true, getParamDisplayLabel(PARAM_CATEGORY_VALUE)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.evaluator.ActionConditionEvaluatorAbstractBase#evaluateImpl(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef)
|
||||
*/
|
||||
@Override
|
||||
protected boolean evaluateImpl(
|
||||
ActionCondition ruleCondition,
|
||||
NodeRef actionedUponNodeRef)
|
||||
{
|
||||
boolean result = false;
|
||||
|
||||
// Double check that the node still exists
|
||||
if (this.nodeService.exists(actionedUponNodeRef) == true)
|
||||
{
|
||||
// Get the rule parameter values
|
||||
QName categoryAspect = (QName)ruleCondition.getParameterValue(PARAM_CATEGORY_ASPECT);
|
||||
NodeRef categoryValue = (NodeRef)ruleCondition.getParameterValue(PARAM_CATEGORY_VALUE);
|
||||
|
||||
// Check that the apect is classifiable and is currently applied to the node
|
||||
if (this.dictionaryService.isSubClass(categoryAspect, ContentModel.ASPECT_CLASSIFIABLE) == true &&
|
||||
this.nodeService.hasAspect(actionedUponNodeRef, categoryAspect) == true)
|
||||
{
|
||||
// Get the category property qname
|
||||
QName categoryProperty = null;
|
||||
Map<QName, PropertyDefinition> propertyDefs = this.dictionaryService.getAspect(categoryAspect).getProperties();
|
||||
for (Map.Entry<QName, PropertyDefinition> entry : propertyDefs.entrySet())
|
||||
{
|
||||
if (DataTypeDefinition.CATEGORY.equals(entry.getValue().getDataType().getName()) == true)
|
||||
{
|
||||
// Found the category property
|
||||
categoryProperty = entry.getKey();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (categoryProperty != null)
|
||||
{
|
||||
// Check to see if the category value is in the list of currently set category values
|
||||
Serializable value = this.nodeService.getProperty(actionedUponNodeRef, categoryProperty);
|
||||
Collection<NodeRef> actualCategories = DefaultTypeConverter.INSTANCE.getCollection(NodeRef.class, value);
|
||||
for (NodeRef nodeRef : actualCategories)
|
||||
{
|
||||
if (nodeRef.equals(categoryValue) == true)
|
||||
{
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.repo.action.ParameterDefinitionImpl;
|
||||
import org.alfresco.service.cmr.action.ActionCondition;
|
||||
import org.alfresco.service.cmr.action.ParameterDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* No condition evaluator implmentation.
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class IsSubTypeEvaluator extends ActionConditionEvaluatorAbstractBase
|
||||
{
|
||||
/**
|
||||
* Evaluator constants
|
||||
*/
|
||||
public static final String NAME = "is-subtype";
|
||||
public static final String PARAM_TYPE = "type";
|
||||
|
||||
/**
|
||||
* The node service
|
||||
*/
|
||||
private NodeService nodeService;
|
||||
|
||||
/**
|
||||
* The dictionary service
|
||||
*/
|
||||
private DictionaryService dictionaryService;
|
||||
|
||||
/**
|
||||
* Set node service
|
||||
*
|
||||
* @param nodeService the node service
|
||||
*/
|
||||
public void setNodeService(NodeService nodeService)
|
||||
{
|
||||
this.nodeService = nodeService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set dictionary service
|
||||
*
|
||||
* @param dictionaryService the dictionary service
|
||||
*/
|
||||
public void setDictionaryService(DictionaryService dictionaryService)
|
||||
{
|
||||
this.dictionaryService = dictionaryService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.evaluator.ActionConditionEvaluatorAbstractBase#evaluateImpl(org.alfresco.service.cmr.action.ActionCondition, org.alfresco.service.cmr.repository.NodeRef)
|
||||
*/
|
||||
public boolean evaluateImpl(ActionCondition ruleCondition, NodeRef actionedUponNodeRef)
|
||||
{
|
||||
boolean result = false;
|
||||
|
||||
if (this.nodeService.exists(actionedUponNodeRef) == true)
|
||||
{
|
||||
// TODO: Move this type check into its own Class Evaluator
|
||||
QName nodeType = nodeService.getType(actionedUponNodeRef);
|
||||
if (dictionaryService.isSubClass(nodeType, (QName)ruleCondition.getParameterValue(PARAM_TYPE)))
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.ParameterizedItemAbstractBase#addParameterDefintions(java.util.List)
|
||||
*/
|
||||
@Override
|
||||
protected void addParameterDefintions(List<ParameterDefinition> paramList)
|
||||
{
|
||||
paramList.add(new ParameterDefinitionImpl(PARAM_TYPE, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_TYPE)));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.action.ActionConditionImpl;
|
||||
import org.alfresco.service.cmr.action.ActionCondition;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.util.BaseSpringTest;
|
||||
import org.alfresco.util.GUID;
|
||||
|
||||
/**
|
||||
* Is sub class evaluator test
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class IsSubTypeEvaluatorTest extends BaseSpringTest
|
||||
{
|
||||
private NodeService nodeService;
|
||||
private StoreRef testStoreRef;
|
||||
private NodeRef rootNodeRef;
|
||||
private NodeRef nodeRef;
|
||||
private IsSubTypeEvaluator evaluator;
|
||||
|
||||
private final static String ID = GUID.generate();
|
||||
|
||||
@Override
|
||||
protected void onSetUpInTransaction() throws Exception
|
||||
{
|
||||
this.nodeService = (NodeService)this.applicationContext.getBean("nodeService");
|
||||
|
||||
// Create the store and get the root node
|
||||
this.testStoreRef = this.nodeService.createStore(
|
||||
StoreRef.PROTOCOL_WORKSPACE, "Test_"
|
||||
+ System.currentTimeMillis());
|
||||
this.rootNodeRef = this.nodeService.getRootNode(this.testStoreRef);
|
||||
|
||||
// Create the node used for tests
|
||||
this.nodeRef = this.nodeService.createNode(
|
||||
this.rootNodeRef,
|
||||
ContentModel.ASSOC_CHILDREN,
|
||||
QName.createQName("{test}testnode"),
|
||||
ContentModel.TYPE_CONTENT).getChildRef();
|
||||
|
||||
this.evaluator = (IsSubTypeEvaluator)this.applicationContext.getBean(IsSubTypeEvaluator.NAME);
|
||||
}
|
||||
|
||||
public void testMandatoryParamsMissing()
|
||||
{
|
||||
ActionCondition condition = new ActionConditionImpl(ID, IsSubTypeEvaluator.NAME, null);
|
||||
|
||||
try
|
||||
{
|
||||
this.evaluator.evaluate(condition, this.nodeRef);
|
||||
fail("The fact that a mandatory parameter has not been set should have been detected.");
|
||||
}
|
||||
catch (Throwable exception)
|
||||
{
|
||||
// Do nothing since this is correct
|
||||
}
|
||||
}
|
||||
|
||||
public void testPass()
|
||||
{
|
||||
ActionCondition condition = new ActionConditionImpl(ID, IsSubTypeEvaluator.NAME, null);
|
||||
condition.setParameterValue(IsSubTypeEvaluator.PARAM_TYPE, ContentModel.TYPE_CONTENT);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
condition.setParameterValue(IsSubTypeEvaluator.PARAM_TYPE, ContentModel.TYPE_CMOBJECT);
|
||||
assertTrue(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
}
|
||||
|
||||
public void testFail()
|
||||
{
|
||||
ActionCondition condition = new ActionConditionImpl(ID, IsSubTypeEvaluator.NAME, null);
|
||||
condition.setParameterValue(IsSubTypeEvaluator.PARAM_TYPE, ContentModel.TYPE_FOLDER);
|
||||
assertFalse(this.evaluator.evaluate(condition, this.nodeRef));
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.service.cmr.action.ActionCondition;
|
||||
import org.alfresco.service.cmr.action.ParameterDefinition;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
|
||||
/**
|
||||
* No condition evaluator implmentation.
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class NoConditionEvaluator extends ActionConditionEvaluatorAbstractBase
|
||||
{
|
||||
/**
|
||||
* Evaluator constants
|
||||
*/
|
||||
public static final String NAME = "no-condition";
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.evaluator.ActionConditionEvaluatorAbstractBase#evaluateImpl(org.alfresco.service.cmr.action.ActionCondition, org.alfresco.service.cmr.repository.NodeRef)
|
||||
*/
|
||||
public boolean evaluateImpl(ActionCondition ruleCondition, NodeRef actionedUponNodeRef)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.ParameterizedItemAbstractBase#addParameterDefintions(java.util.List)
|
||||
*/
|
||||
@Override
|
||||
protected void addParameterDefintions(List<ParameterDefinition> paramList)
|
||||
{
|
||||
// No parameters to add
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
package org.alfresco.repo.action.evaluator.compare;
|
||||
|
||||
/**
|
||||
* ComparePropertyValueOperation enum.
|
||||
* <p>
|
||||
* Contains the operations that can be used when evaluating whether the value of a property
|
||||
* matches the value set.
|
||||
* <p>
|
||||
* Some operations can only be used with specific types. If a mismatch is encountered an error will
|
||||
* be raised.
|
||||
*/
|
||||
public enum ComparePropertyValueOperation
|
||||
{
|
||||
EQUALS, // All property types
|
||||
CONTAINS, // String properties only
|
||||
BEGINS, // String properties only
|
||||
ENDS, // String properties only
|
||||
GREATER_THAN, // Numeric and date properties only
|
||||
GREATER_THAN_EQUAL, // Numeric and date properties only
|
||||
LESS_THAN, // Numeric and date properties only
|
||||
LESS_THAN_EQUAL // Numeric and date properties only
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator.compare;
|
||||
|
||||
/**
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public enum ContentPropertyName
|
||||
{
|
||||
MIME_TYPE,
|
||||
ENCODING,
|
||||
SIZE
|
||||
}
|
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator.compare;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
import org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator;
|
||||
import org.alfresco.service.cmr.action.ActionServiceException;
|
||||
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
|
||||
|
||||
/**
|
||||
* Date property value comparator
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class DatePropertyValueComparator implements PropertyValueComparator
|
||||
{
|
||||
/**
|
||||
* I18N message ids
|
||||
*/
|
||||
private static final String MSGID_INVALID_OPERATION = "date_property_value_comparator.invalid_operation";
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
Date propertyDate = (Date)propertyValue;
|
||||
Date compareDate = (Date)compareValue;
|
||||
|
||||
switch (operation)
|
||||
{
|
||||
case EQUALS:
|
||||
{
|
||||
result = propertyDate.equals(compareDate);
|
||||
break;
|
||||
}
|
||||
case LESS_THAN:
|
||||
{
|
||||
result = propertyDate.before(compareDate);
|
||||
break;
|
||||
}
|
||||
case LESS_THAN_EQUAL:
|
||||
{
|
||||
result = (propertyDate.equals(compareDate) || propertyDate.before(compareDate));
|
||||
break;
|
||||
}
|
||||
case GREATER_THAN:
|
||||
{
|
||||
result = propertyDate.after(compareDate);
|
||||
break;
|
||||
}
|
||||
case GREATER_THAN_EQUAL:
|
||||
{
|
||||
result = (propertyDate.equals(compareDate) || propertyDate.after(compareDate));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
// Raise an invalid operation exception
|
||||
throw new ActionServiceException(
|
||||
MSGID_INVALID_OPERATION,
|
||||
new Object[]{operation.toString()});
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.evaluator.compare.PropertyValueComparator#registerComparator(org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator)
|
||||
*/
|
||||
public void registerComparator(ComparePropertyValueEvaluator evaluator)
|
||||
{
|
||||
evaluator.registerComparator(DataTypeDefinition.DATE, this);
|
||||
evaluator.registerComparator(DataTypeDefinition.DATETIME, this);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator.compare;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator;
|
||||
import org.alfresco.service.cmr.action.ActionServiceException;
|
||||
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
|
||||
|
||||
/**
|
||||
* Numeric property value comparator.
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class NumericPropertyValueComparator implements PropertyValueComparator
|
||||
{
|
||||
/**
|
||||
* I18N message ids
|
||||
*/
|
||||
private static final String MSGID_INVALID_OPERATION = "numeric_property_value_comparator.invalid_operation";
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.evaluator.compare.PropertyValueComparator#registerComparator(org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator)
|
||||
*/
|
||||
public void registerComparator(ComparePropertyValueEvaluator evaluator)
|
||||
{
|
||||
evaluator.registerComparator(DataTypeDefinition.DOUBLE, this);
|
||||
evaluator.registerComparator(DataTypeDefinition.FLOAT, this);
|
||||
evaluator.registerComparator(DataTypeDefinition.INT, this);
|
||||
evaluator.registerComparator(DataTypeDefinition.LONG, this);
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator.compare;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator;
|
||||
|
||||
/**
|
||||
* Property value comparator interface
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public interface PropertyValueComparator
|
||||
{
|
||||
/**
|
||||
* Callback method to register this comparator with the evaluator.
|
||||
*
|
||||
* @param evaluator the compare property value evaluator
|
||||
*/
|
||||
void registerComparator(ComparePropertyValueEvaluator evaluator);
|
||||
|
||||
/**
|
||||
* Compares the value of a property with the compare value, using the operator passed.
|
||||
*
|
||||
* @param propertyValue the property value
|
||||
* @param compareValue the compare value
|
||||
* @param operation the operation used to compare the two values
|
||||
* @return the result of the comparision, true if successful false otherwise
|
||||
*/
|
||||
boolean compare(
|
||||
Serializable propertyValue,
|
||||
Serializable compareValue,
|
||||
ComparePropertyValueOperation operation);
|
||||
}
|
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.action.evaluator.compare;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator;
|
||||
import org.alfresco.service.cmr.action.ActionServiceException;
|
||||
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
|
||||
|
||||
/**
|
||||
* Test property value comparator
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class TextPropertyValueComparator implements PropertyValueComparator
|
||||
{
|
||||
/**
|
||||
* I18N message ids
|
||||
*/
|
||||
private static final String MSGID_INVALID_OPERATION = "text_property_value_comparator.invalid_operation";
|
||||
|
||||
/**
|
||||
* Special star string
|
||||
*/
|
||||
private static final String STAR = "*";
|
||||
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
String compareText = (String)compareValue;
|
||||
|
||||
boolean result = false;
|
||||
if (operation == null)
|
||||
{
|
||||
// Check for a trailing or leading star since it implies special behaviour when no default operation is specified
|
||||
if (compareText.startsWith(STAR) == true)
|
||||
{
|
||||
// Remove the star and set the operation to endsWith
|
||||
operation = ComparePropertyValueOperation.ENDS;
|
||||
compareText = compareText.substring(1);
|
||||
}
|
||||
else if (compareText.endsWith(STAR) == true)
|
||||
{
|
||||
// Remove the star and set the operation to startsWith
|
||||
operation = ComparePropertyValueOperation.BEGINS;
|
||||
compareText = compareText.substring(0, (compareText.length()-2));
|
||||
}
|
||||
else
|
||||
{
|
||||
operation = ComparePropertyValueOperation.CONTAINS;
|
||||
}
|
||||
}
|
||||
|
||||
// Build the reg ex
|
||||
String regEx = buildRegEx(compareText, operation);
|
||||
|
||||
// Do the match
|
||||
if (propertyValue != null)
|
||||
{
|
||||
result = ((String)propertyValue).toLowerCase().matches(regEx);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the regular expressin that it used to make the match
|
||||
*
|
||||
* @param matchText the raw text to be matched
|
||||
* @param operation the operation
|
||||
* @return the regular expression string
|
||||
*/
|
||||
private String buildRegEx(String matchText, ComparePropertyValueOperation operation)
|
||||
{
|
||||
String result = escapeText(matchText.toLowerCase());
|
||||
switch (operation)
|
||||
{
|
||||
case CONTAINS:
|
||||
result = "^.*" + result + ".*$";
|
||||
break;
|
||||
case BEGINS:
|
||||
result = "^" + result + ".*$";
|
||||
break;
|
||||
case ENDS:
|
||||
result = "^.*" + result + "$";
|
||||
break;
|
||||
case EQUALS:
|
||||
break;
|
||||
default:
|
||||
// Raise an invalid operation exception
|
||||
throw new ActionServiceException(
|
||||
MSGID_INVALID_OPERATION,
|
||||
new Object[]{operation.toString()});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes the text before it is turned into a regualr expression
|
||||
*
|
||||
* @param matchText the raw text
|
||||
* @return the escaped text
|
||||
*/
|
||||
private String escapeText(String matchText)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder(matchText.length());
|
||||
for (char charValue : matchText.toCharArray())
|
||||
{
|
||||
if (charValue == '*')
|
||||
{
|
||||
builder.append(".");
|
||||
}
|
||||
else if (getEscapeCharList().contains(charValue) == true)
|
||||
{
|
||||
builder.append("\\");
|
||||
}
|
||||
builder.append(charValue);
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* List of escape characters
|
||||
*/
|
||||
private static List<Character> ESCAPE_CHAR_LIST = null;
|
||||
|
||||
/**
|
||||
* Get the list of escape chars
|
||||
*
|
||||
* @return list of excape chars
|
||||
*/
|
||||
private List<Character> getEscapeCharList()
|
||||
{
|
||||
if (ESCAPE_CHAR_LIST == null)
|
||||
{
|
||||
//([{\^$|)?*+.
|
||||
ESCAPE_CHAR_LIST = new ArrayList<Character>(4);
|
||||
ESCAPE_CHAR_LIST.add('.');
|
||||
ESCAPE_CHAR_LIST.add('^');
|
||||
ESCAPE_CHAR_LIST.add('$');
|
||||
ESCAPE_CHAR_LIST.add('(');
|
||||
ESCAPE_CHAR_LIST.add('[');
|
||||
ESCAPE_CHAR_LIST.add('{');
|
||||
ESCAPE_CHAR_LIST.add('\\');
|
||||
ESCAPE_CHAR_LIST.add('|');
|
||||
ESCAPE_CHAR_LIST.add(')');
|
||||
ESCAPE_CHAR_LIST.add('?');
|
||||
ESCAPE_CHAR_LIST.add('+');
|
||||
}
|
||||
return ESCAPE_CHAR_LIST;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.action.evaluator.compare.PropertyValueComparator#registerComparator(org.alfresco.repo.action.evaluator.ComparePropertyValueEvaluator)
|
||||
*/
|
||||
public void registerComparator(ComparePropertyValueEvaluator evaluator)
|
||||
{
|
||||
evaluator.registerComparator(DataTypeDefinition.TEXT, this);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user