org.alfresco.service.cmr.dictionary.ConstraintException Java Examples

The following examples show how to use org.alfresco.service.cmr.dictionary.ConstraintException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AuthorityNameConstraint.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void evaluateSingleValue(Object value)
{
    // ensure that the value can be converted to a String
    String checkValue = null;
    try
    {
        checkValue = DefaultTypeConverter.INSTANCE.convert(String.class, value);
    }
    catch (TypeConversionException e)
    {
        throw new ConstraintException(ERR_NON_STRING, value);
    }
    
    AuthorityType type = AuthorityType.getAuthorityType(checkValue);
    if((type != AuthorityType.GROUP) && (type != AuthorityType.ROLE))
    {
        throw new ConstraintException(ERR_INVALID_AUTHORITY_NAME, value, type);
    }
}
 
Example #2
Source File: UserNameConstraint.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void evaluateSingleValue(Object value)
{
    // ensure that the value can be converted to a String
    String checkValue = null;
    try
    {
        checkValue = DefaultTypeConverter.INSTANCE.convert(String.class, value);
    }
    catch (TypeConversionException e)
    {
        throw new ConstraintException(ERR_NON_STRING, value);
    }
    
    AuthorityType type = AuthorityType.getAuthorityType(checkValue);
    if((type != AuthorityType.USER) && (type != AuthorityType.GUEST))
    {
        throw new ConstraintException(ERR_INVALID_USERNAME, value, type);
    }
}
 
Example #3
Source File: SystemTemplateLocationsConstraint.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public List<String> getRawAllowedValues()
{
    List<String> result = null;
    try
    {
        result = loadClasspathTemplates(templatesParentClasspath,
                                        "json");
    }
    catch (IOException e)
    {
        throw new ConstraintException("ListTemplateTypesConstraints",
                                      e);
    }
    List<String> repositoryTemplates = loadRepositoryTemplates(templatesParentRepositoryPath);
    result.addAll(repositoryTemplates);
    if (result.size() == 0)
    {
        result.add(NULL_SYSTEM_TEMPLATE);
    }
    super.setAllowedValues(result);
    return result;
}
 
Example #4
Source File: StringLengthConstraint.java    From alfresco-data-model with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void evaluateSingleValue(Object value)
{
    // ensure that the value can be converted to a String
    String checkValue = null;
    try
    {
        checkValue = DefaultTypeConverter.INSTANCE.convert(String.class, value);
    }
    catch (TypeConversionException e)
    {
        throw new ConstraintException(ERR_NON_STRING, value);
    }
    
    // Check that the value length
    int length = checkValue.length();
    if (length > maxLength || length < minLength)
    {
        if (length > 20)
        {
            checkValue = checkValue.substring(0, 17) + "...";
        }
        throw new ConstraintException(ERR_INVALID_LENGTH, checkValue, minLength, maxLength);
    }
}
 
Example #5
Source File: NumericRangeConstraint.java    From alfresco-data-model with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void evaluateSingleValue(Object value)
{
    // ensure that the value can be converted to a double
    double checkValue = Double.NaN;
    try
    {
        checkValue = DefaultTypeConverter.INSTANCE.doubleValue(value);
    }
    catch (NumberFormatException e)
    {
        throw new ConstraintException(ERR_NON_NUMERIC, value);
    }
    
    // Infinity and NaN cannot match
    if (Double.isInfinite(checkValue) || Double.isNaN(checkValue))
    {
        throw new ConstraintException(ERR_OUT_OF_RANGE, checkValue, minValue, maxValue);
    }
    
    // Check that the value is in range
    if (checkValue > maxValue || checkValue < minValue)
    {
        throw new ConstraintException(ERR_OUT_OF_RANGE, checkValue, minValue, maxValue);
    }
}
 
Example #6
Source File: RegexConstraint.java    From alfresco-data-model with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void evaluateSingleValue(Object value)
{
    // convert the value to a String
    String valueStr = DefaultTypeConverter.INSTANCE.convert(String.class, value);
    Matcher matcher = patternMatcher.matcher(valueStr);
    boolean matches = matcher.matches();
    if (matches != requiresMatch)
    {
        // Look for a message corresponding to this constraint name
        String messageId = CONSTRAINT_REGEX_MSG_PREFIX + getShortName();
        if (I18NUtil.getMessage(messageId, value) != null)
        {
            throw new ConstraintException(messageId, value);
        }
        // Otherwise, fall back to a generic (but unfriendly) message
        else if (requiresMatch)
        {
            throw new ConstraintException(RegexConstraint.CONSTRAINT_REGEX_NO_MATCH, value, expression);
        }
        else
        {
            throw new ConstraintException(RegexConstraint.CONSTRAINT_REGEX_MATCH, value, expression);
        }
    }
}
 
Example #7
Source File: ConstraintsTest.java    From alfresco-data-model with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Fails on everything but String values, which pass.
 * Null values cause runtime exceptions and all other failures are by
 * DictionaryException.
 */
@Override
protected void evaluateSingleValue(Object value)
{
    if (value == null)
    {
        throw new NullPointerException("Null value in dummy test");
    }
    else if (value instanceof String)
    {
        tested.add(value);
    }
    else
    {
        throw new ConstraintException("Non-String value");
    }
}
 
Example #8
Source File: ListOfValuesConstraint.java    From alfresco-data-model with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see org.alfresco.repo.dictionary.constraint.AbstractConstraint#evaluateSingleValue(java.lang.Object)
 */
@Override
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 (!allowedValuesSet.contains(valueStr))
        {
            throw new ConstraintException(ERR_INVALID_VALUE, value);
        }
    }
    else
    {
        if (!allowedValuesUpperSet.contains(valueStr.toUpperCase()))
        {
            throw new ConstraintException(ERR_INVALID_VALUE, value);
        }
    }
}
 
Example #9
Source File: ConstraintsTest.java    From alfresco-data-model with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * ensure that the default handling of checks on collections will work
 */ 
public void testCollections() throws Exception
{
    DummyConstraint constraint = new DummyConstraint();
    constraint.initialize();
    
    assertEquals("DummyConstraint type should be 'org.alfresco.repo.dictionary.constraint.ConstraintsTest$DummyConstraint'", 
                "org.alfresco.repo.dictionary.constraint.ConstraintsTest$DummyConstraint", 
                constraint.getType());
    assertNotNull("DummyConstraint should not have empty parameters", constraint.getParameters());
    assertEquals("DummyConstraint should not have empty parameters", 0, constraint.getParameters().size());
    
    List<Object> dummyObjects = new ArrayList<Object>(3);
    dummyObjects.add("ABC");   // correct
    dummyObjects.add("DEF");   // correct
    dummyObjects.add(this);    // NO
    try
    {
        constraint.evaluate(dummyObjects);
        fail("Failed to detected constraint violation in collection");
    }
    catch (ConstraintException e)
    {
        // expected
        checkI18NofExceptionMessage(e);
    }
    // check that the two strings were properly dealt with
    assertEquals("String values not checked", 2, constraint.tested.size());
}
 
Example #10
Source File: ConstraintsTest.java    From alfresco-data-model with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void evaluate(Constraint constraint, Object value, boolean expectFailure) throws Exception
{
    try
    {
        constraint.evaluate(value);
        if (expectFailure)
        {
            // it should have failed
            fail("Failure did not occur: \n" +
                    "   constraint: " + constraint + "\n" +
                    "   value: " + value);
        }
    }
    catch (ConstraintException e)
    {
        // check if we expect an error
        if (expectFailure)
        {
            // expected - check message I18N
            checkI18NofExceptionMessage(e);
        }
        else
        {
            // didn't expect it
            throw e;
        }
    }
}
 
Example #11
Source File: TestCustomConstraint.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void evaluateSingleValue(Object value)
{
    String checkValue = DefaultTypeConverter.INSTANCE.convert(String.class, value);

    if (checkValue.contains("#"))
    {
        throw new ConstraintException("The value must not contain '#'");
    }
}
 
Example #12
Source File: DirectoryAnalyserImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean isMetadataValid(ImportableItem importableItem)
{
    if (!importableItem.getHeadRevision().metadataFileExists())
    {
        return true;
    }
    
    if (metadataLoader != null)
    {
        MetadataLoader.Metadata result = new MetadataLoader.Metadata();
        metadataLoader.loadMetadata(importableItem.getHeadRevision(), result);
        
        Map<QName, Serializable> metadataProperties = result.getProperties();
        for (QName propertyName : metadataProperties.keySet())
        {
            PropertyDefinition propDef = dictionaryService.getProperty(propertyName);
            if (propDef != null)
            {
                for (ConstraintDefinition constraintDef : propDef.getConstraints())
                {
                    Constraint constraint = constraintDef.getConstraint();
                    if (constraint != null)
                    {
                        try
                        {
                            constraint.evaluate(metadataProperties.get(propertyName));
                        }
                        catch (ConstraintException e)
                        {
                            if (log.isWarnEnabled())
                            {
                                log.warn("Skipping file '" + FileUtils.getFileName(importableItem.getHeadRevision().getContentFile())
                                   +"' with invalid metadata: '" + FileUtils.getFileName(importableItem.getHeadRevision().getMetadataFile()) + "'.", e);
                            }
                            return false;
                        }
                    }
                }
            }
        }
    }
    
    return true;
}
 
Example #13
Source File: ActivitiPropertyConverter.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Sets Default Properties of Task
 * 
 * @param task
 *            task instance
 */
public void setDefaultTaskProperties(DelegateTask task)
{
    TypeDefinition typeDefinition = typeManager.getFullTaskDefinition(task);
    // Only local task properties should be set to default value
    Map<QName, Serializable> existingValues = getTaskProperties(task, typeDefinition, true);
    Map<QName, Serializable> defaultValues = new HashMap<QName, Serializable>();

    Map<QName, PropertyDefinition> propertyDefs = typeDefinition.getProperties();

    // for each property, determine if it has a default value
    for (Map.Entry<QName, PropertyDefinition> entry : propertyDefs.entrySet())
    {
        QName key = entry.getKey();
        String defaultValue = entry.getValue().getDefaultValue();
        if (defaultValue != null && existingValues.get(key) == null)
        {
            defaultValues.put(key, defaultValue);
        }
    }

    // Special case for property priorities
    PropertyDefinition priorDef = propertyDefs.get(WorkflowModel.PROP_PRIORITY);
    Serializable existingValue = existingValues.get(WorkflowModel.PROP_PRIORITY);
    try
    {
    	if(priorDef != null) {
         for (ConstraintDefinition constraintDef : priorDef.getConstraints())
         {
             constraintDef.getConstraint().evaluate(existingValue);
         }
    	}
    }
    catch (ConstraintException ce)
    {
        if(priorDef != null) {
            Integer defaultVal = Integer.valueOf(priorDef.getDefaultValue());
            if (logger.isDebugEnabled())
            {
                logger.debug("Task priority value ("+existingValue+") was invalid so it was set to the default value of "+defaultVal+". Task:"+task.getName());
            }
            defaultValues.put(WorkflowModel.PROP_PRIORITY, defaultVal);
        }
    }    
    
    // Special case for task description default value
    String description = (String) existingValues.get(WorkflowModel.PROP_DESCRIPTION);
    if (description == null || description.length() == 0)
    {
        //Try the localised task description first
        String processDefinitionKey = ((ProcessDefinition) ((TaskEntity)task).getExecution().getProcessDefinition()).getKey();
        description = factory.getTaskDescription(typeDefinition, factory.buildGlobalId(processDefinitionKey), null, task.getTaskDefinitionKey());
        if (description != null && description.length() > 0) {
            defaultValues.put(WorkflowModel.PROP_DESCRIPTION,  description);
        } else {
            String descriptionKey = factory.mapQNameToName(WorkflowModel.PROP_WORKFLOW_DESCRIPTION);
            description = (String) task.getExecution().getVariable(descriptionKey); 
            if (description != null && description.length() > 0) {
                defaultValues.put(WorkflowModel.PROP_DESCRIPTION,  description); 
            } else {
                defaultValues.put(WorkflowModel.PROP_DESCRIPTION,  task.getName());
            }
        }
         
    }

    // Assign the default values to the task
    if (defaultValues.size() > 0)
    {
        setTaskProperties(task, defaultValues);
    }
}