mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Added LIST constraint
- An optionally case-insensitive list of supported values - Supports any types that can be converted to String by our type-converter. Simple parameter values for constraints must not be in a <value> element, just like Spring properties (sorry) Constraint parameters of type java.util.List are now supported: <list> <value> <value> </list> git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2660 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -126,7 +126,7 @@ public class DictionaryDAOTest extends TestCase
|
||||
// get the constraints for a property without constraints
|
||||
QName propNoConstraintsQName = QName.createQName(TEST_URL, "fileprop");
|
||||
PropertyDefinition propNoConstraintsDef = service.getProperty(propNoConstraintsQName);
|
||||
assertNotNull("Property without constraints returned empty list", propNoConstraintsDef.getConstraints());
|
||||
assertNotNull("Property without constraints returned null list", propNoConstraintsDef.getConstraints());
|
||||
|
||||
// get the constraints defined for the property
|
||||
QName prop1QName = QName.createQName(TEST_URL, "prop1");
|
||||
|
@@ -18,6 +18,7 @@ package org.alfresco.repo.dictionary;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
|
||||
import org.alfresco.repo.dictionary.constraint.NumericRangeConstraint;
|
||||
import org.alfresco.repo.dictionary.constraint.RegexConstraint;
|
||||
import org.alfresco.repo.dictionary.constraint.StringLengthConstraint;
|
||||
@@ -29,7 +30,8 @@ import org.alfresco.service.namespace.NamespacePrefixResolver;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.beans.TypeMismatchException;
|
||||
import org.springframework.beans.InvalidPropertyException;
|
||||
import org.springframework.beans.PropertyAccessException;
|
||||
|
||||
/**
|
||||
* Compiled Property Constraint
|
||||
@@ -44,6 +46,7 @@ import org.springframework.beans.TypeMismatchException;
|
||||
public static final String ERR_REF_NOT_FOUND = "d_dictionary.constraint.err.ref_not_found";
|
||||
public static final String ERR_ANON_NEEDS_PROPERTY = "d_dictionary.constraint.err.anon_needs_property";
|
||||
public static final String ERR_INVALID_TYPE = "d_dictionary.constraint.err.invalid_type";
|
||||
public static final String ERR_SIMPLE_AND_LIST = "d_dictionary.constraint.err.property_simple_and_list";
|
||||
public static final String ERR_CONSTRUCT_FAILURE = "d_dictionary.constraint.err.construct_failure";
|
||||
public static final String ERR_PROPERTY_MISMATCH = "d_dictionary.constraint.err.property_mismatch";
|
||||
|
||||
@@ -178,11 +181,28 @@ import org.springframework.beans.TypeMismatchException;
|
||||
List<M2NamedValue> constraintNamedValues = m2Constraint.getParameters();
|
||||
for (M2NamedValue namedValue : constraintNamedValues)
|
||||
{
|
||||
Object value = null;
|
||||
if (namedValue.getSimpleValue() != null && namedValue.getListValue() != null)
|
||||
{
|
||||
throw new DictionaryException(ERR_SIMPLE_AND_LIST, shortName, namedValue.getName());
|
||||
}
|
||||
else if (namedValue.getSimpleValue() != null)
|
||||
{
|
||||
value = namedValue.getSimpleValue();
|
||||
}
|
||||
else if (namedValue.getListValue() != null)
|
||||
{
|
||||
value = namedValue.getListValue();
|
||||
}
|
||||
try
|
||||
{
|
||||
beanWrapper.setPropertyValue(namedValue.getName(), namedValue.getValue());
|
||||
beanWrapper.setPropertyValue(namedValue.getName(), value);
|
||||
}
|
||||
catch (TypeMismatchException e)
|
||||
catch (PropertyAccessException e)
|
||||
{
|
||||
throw new DictionaryException(ERR_PROPERTY_MISMATCH, e, namedValue.getName(), shortName);
|
||||
}
|
||||
catch (InvalidPropertyException e)
|
||||
{
|
||||
throw new DictionaryException(ERR_PROPERTY_MISMATCH, e, namedValue.getName(), shortName);
|
||||
}
|
||||
@@ -244,6 +264,14 @@ import org.springframework.beans.TypeMismatchException;
|
||||
{
|
||||
return new StringLengthConstraint();
|
||||
}
|
||||
},
|
||||
LIST
|
||||
{
|
||||
@Override
|
||||
protected Constraint newInstance()
|
||||
{
|
||||
return new ListOfValuesConstraint();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@@ -16,6 +16,8 @@
|
||||
*/
|
||||
package org.alfresco.repo.dictionary;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Definition of a named value that can be used for property injection.
|
||||
*
|
||||
@@ -24,8 +26,8 @@ package org.alfresco.repo.dictionary;
|
||||
public class M2NamedValue
|
||||
{
|
||||
private String name;
|
||||
private String value;
|
||||
|
||||
private String simpleValue;
|
||||
private List<String> listValue;
|
||||
|
||||
/*package*/ M2NamedValue()
|
||||
{
|
||||
@@ -35,7 +37,7 @@ public class M2NamedValue
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return (name + "=" + value);
|
||||
return (name + "=" + (simpleValue == null ? listValue : simpleValue));
|
||||
}
|
||||
|
||||
public String getName()
|
||||
@@ -46,8 +48,16 @@ public class M2NamedValue
|
||||
/**
|
||||
* @return Returns the raw, unconverted value
|
||||
*/
|
||||
public String getValue()
|
||||
public String getSimpleValue()
|
||||
{
|
||||
return value;
|
||||
return simpleValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the list of raw, unconverted values
|
||||
*/
|
||||
public List<String> getListValue()
|
||||
{
|
||||
return listValue;
|
||||
}
|
||||
}
|
||||
|
@@ -33,6 +33,22 @@ public abstract class AbstractConstraint implements Constraint
|
||||
public static final String ERR_PROP_NOT_SET = "d_dictionary.constraint.err.property_not_set";
|
||||
public static final String ERR_EVALUATE_EXCEPTION = "d_dictionary.constraint.err.evaluate_exception";
|
||||
|
||||
/**
|
||||
* Check that the given value is not <tt>null</tt>.
|
||||
*
|
||||
* @param name the name of the property
|
||||
* @param value the value to check for <tt>null</tt>
|
||||
*
|
||||
* @throws DictionaryException if the the property is null
|
||||
*/
|
||||
protected void checkPropertyNotNull(String name, Object value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new DictionaryException(AbstractConstraint.ERR_PROP_NOT_SET, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #evaluateSingleValue(Object)
|
||||
* @see #evaluateCollection(Collection)
|
||||
|
@@ -18,6 +18,7 @@ package org.alfresco.repo.dictionary.constraint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
@@ -148,6 +149,32 @@ public class ConstraintsTest extends TestCase
|
||||
evaluate(constraint, Arrays.asList("abc", "abcdefg"), true);
|
||||
}
|
||||
|
||||
public void testListOfValuesConstraint() throws Exception
|
||||
{
|
||||
ListOfValuesConstraint constraint = new ListOfValuesConstraint();
|
||||
try
|
||||
{
|
||||
constraint.setAllowedValues(Collections.<String>emptyList());
|
||||
}
|
||||
catch (DictionaryException e)
|
||||
{
|
||||
// expected
|
||||
checkI18NofExceptionMessage(e);
|
||||
}
|
||||
List<String> allowedValues = Arrays.asList(new String[] {"abc", "def", "ghi"});
|
||||
constraint.setAllowedValues(allowedValues);
|
||||
|
||||
evaluate(constraint, "def", false);
|
||||
evaluate(constraint, "DEF", true);
|
||||
evaluate(constraint, Arrays.asList("abc", "def"), false);
|
||||
evaluate(constraint, Arrays.asList("abc", "DEF"), true);
|
||||
|
||||
// now make it case-insensitive
|
||||
constraint.setCaseSensitive(false);
|
||||
evaluate(constraint, "DEF", false);
|
||||
evaluate(constraint, Arrays.asList("abc", "DEF"), false);
|
||||
}
|
||||
|
||||
public void testNumericRangeConstraint() throws Exception
|
||||
{
|
||||
NumericRangeConstraint constraint = new NumericRangeConstraint();
|
||||
|
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* 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.dictionary.constraint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.service.cmr.dictionary.ConstraintException;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryException;
|
||||
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
|
||||
import org.alfresco.service.cmr.repository.datatype.TypeConversionException;
|
||||
|
||||
/**
|
||||
* Constraint implementation that ensures the value is one of a constrained
|
||||
* <i>list of values</i>. By default, this constraint is case-sensitive.
|
||||
*
|
||||
* @see #setAllowedValues(List)
|
||||
* @see #setCaseSensitive(boolean)
|
||||
*
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
public class ListOfValuesConstraint extends AbstractConstraint
|
||||
{
|
||||
private static final String ERR_NO_VALUES = "d_dictionary.constraint.list_of_values.no_values";
|
||||
private static final String ERR_NON_STRING = "d_dictionary.constraint.string_length.non_string";
|
||||
private static final String ERR_INVALID_VALUE = "d_dictionary.constraint.list_of_values.invalid_value";
|
||||
|
||||
private List<String> allowedValues;
|
||||
private List<String> allowedValuesUpper;
|
||||
private boolean caseSensitive;
|
||||
|
||||
public ListOfValuesConstraint()
|
||||
{
|
||||
caseSensitive = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder(80);
|
||||
sb.append("ListOfValuesConstraint")
|
||||
.append("[ allowedValues=").append(allowedValues)
|
||||
.append(", caseSensitive=").append(caseSensitive)
|
||||
.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the allowed values. Note that these are <tt>String</tt> instances, but may
|
||||
* represent non-<tt>String</tt> values. It is up to the caller to distinguish.
|
||||
*
|
||||
* @return Returns the values allowed
|
||||
*/
|
||||
public List<String> getAllowedValues()
|
||||
{
|
||||
return allowedValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the values that are allowed by the constraint.
|
||||
*
|
||||
* @param values a list of allowed values
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setAllowedValues(List allowedValues)
|
||||
{
|
||||
if (allowedValues == null)
|
||||
{
|
||||
throw new DictionaryException(ERR_NO_VALUES);
|
||||
}
|
||||
int valueCount = allowedValues.size();
|
||||
if (valueCount == 0)
|
||||
{
|
||||
throw new DictionaryException(ERR_NO_VALUES);
|
||||
}
|
||||
this.allowedValues = Collections.unmodifiableList(allowedValues);
|
||||
// make the upper case versions
|
||||
this.allowedValuesUpper = new ArrayList<String>(valueCount);
|
||||
for (String allowedValue : this.allowedValues)
|
||||
{
|
||||
allowedValuesUpper.add(allowedValue.toUpperCase());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns <tt>true</tt> if this constraint is case-sensitive (default)
|
||||
*/
|
||||
public boolean isCaseSensitive()
|
||||
{
|
||||
return caseSensitive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the handling of case checking.
|
||||
*
|
||||
* @param caseSensitive <tt>true</tt> if the constraint is case-sensitive (default),
|
||||
* or <tt>false</tt> for case-insensitive.
|
||||
*/
|
||||
public void setCaseSensitive(boolean caseSensitive)
|
||||
{
|
||||
this.caseSensitive = caseSensitive;
|
||||
}
|
||||
|
||||
public void initialize()
|
||||
{
|
||||
checkPropertyNotNull("allowedValues", allowedValues);
|
||||
}
|
||||
|
||||
protected void evaluateSingleValue(Object value)
|
||||
{
|
||||
// convert the value to a String
|
||||
String valueStr = null;
|
||||
try
|
||||
{
|
||||
valueStr = DefaultTypeConverter.INSTANCE.convert(String.class, value);
|
||||
}
|
||||
catch (TypeConversionException e)
|
||||
{
|
||||
throw new ConstraintException(ERR_NON_STRING, value);
|
||||
}
|
||||
// check that the value is in the set of allowed values
|
||||
if (caseSensitive)
|
||||
{
|
||||
if (!allowedValues.contains(valueStr))
|
||||
{
|
||||
throw new ConstraintException(ERR_INVALID_VALUE, value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!allowedValuesUpper.contains(valueStr.toUpperCase()))
|
||||
{
|
||||
throw new ConstraintException(ERR_INVALID_VALUE, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -20,7 +20,6 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.alfresco.service.cmr.dictionary.ConstraintException;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryException;
|
||||
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
|
||||
|
||||
/**
|
||||
@@ -94,13 +93,11 @@ public class RegexConstraint extends AbstractConstraint
|
||||
{
|
||||
this.requiresMatch = requiresMatch;
|
||||
}
|
||||
|
||||
|
||||
public void initialize()
|
||||
{
|
||||
if (expression == null)
|
||||
{
|
||||
throw new DictionaryException(AbstractConstraint.ERR_PROP_NOT_SET, "expression");
|
||||
}
|
||||
checkPropertyNotNull("expression", expression);
|
||||
|
||||
this.patternMatcher = Pattern.compile(expression);
|
||||
}
|
||||
|
||||
|
@@ -24,16 +24,25 @@
|
||||
|
||||
<constraints>
|
||||
<constraint name="test:regex1" type="REGEX">
|
||||
<parameter name="expression">[A-Z]*</parameter>
|
||||
<parameter name="requiresMatch">false</parameter>
|
||||
<parameter name="expression"><value>[A-Z]*</value></parameter>
|
||||
<parameter name="requiresMatch"><value>false</value></parameter>
|
||||
</constraint>
|
||||
<constraint name="test:stringLength1" type="LENGTH">
|
||||
<parameter name="minLength">0</parameter>
|
||||
<parameter name="maxLength">256</parameter>
|
||||
<parameter name="minLength"><value>0</value></parameter>
|
||||
<parameter name="maxLength"><value>256</value></parameter>
|
||||
</constraint>
|
||||
<constraint name="test:minMax1" type="MINMAX">
|
||||
<parameter name="minValue">0</parameter>
|
||||
<parameter name="maxValue">256</parameter>
|
||||
<parameter name="minValue"><value>0</value></parameter>
|
||||
<parameter name="maxValue"><value>256</value></parameter>
|
||||
</constraint>
|
||||
<constraint name="test:list1" type="LIST">
|
||||
<parameter name="allowedValues">
|
||||
<list>
|
||||
<value>ABC</value>
|
||||
<value>DEF</value>
|
||||
</list>
|
||||
</parameter>
|
||||
<parameter name="caseSensitive"><value>true</value></parameter>
|
||||
</constraint>
|
||||
</constraints>
|
||||
|
||||
|
@@ -119,7 +119,16 @@
|
||||
|
||||
<mapping abstract="true" class="org.alfresco.repo.dictionary.M2NamedValue">
|
||||
<value style="attribute" name="name" field="name" />
|
||||
<value style="text" field="value"/>
|
||||
<structure name="value" usage="optional">
|
||||
<value style="text" field="simpleValue" />
|
||||
</structure>
|
||||
<structure name="list" usage="optional">
|
||||
<collection field="listValue" factory="org.alfresco.repo.dictionary.M2Model.createList" usage="optional" >
|
||||
<structure name="value" usage="optional" >
|
||||
<value style="text" />
|
||||
</structure>
|
||||
</collection>
|
||||
</structure>
|
||||
</mapping>
|
||||
|
||||
<mapping name="constraint" class="org.alfresco.repo.dictionary.M2Constraint">
|
||||
|
Reference in New Issue
Block a user