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:
@@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user