org.alfresco.service.cmr.repository.datatype.TypeConversionException Java Examples

The following examples show how to use org.alfresco.service.cmr.repository.datatype.TypeConversionException. 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: RemoteCredentialsServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
private BaseCredentialsInfo loadCredentials(String remoteSystem, NodeRef remoteSystemNodeRef, NodeRef credentialsNodeRef)
{
    QName type = nodeService.getType(credentialsNodeRef);
    RemoteCredentialsInfoFactory factory = credentialsFactories.get(type);
    if (factory == null)
    {
        throw new TypeConversionException("No Factory registered for type " + type);
    }
    
    // Wrap as an object
    return factory.createCredentials(
            type, credentialsNodeRef,
            remoteSystem, remoteSystemNodeRef,
            nodeService.getProperties(credentialsNodeRef)
    );
}
 
Example #4
Source File: BaseNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Checks that unconvertable property values cannot be persisted.
 */
@Test
public void testAR782() throws Exception
{
    Map<QName, Serializable> properties = nodeService.getProperties(rootNodeRef);
    // Set usr:accountExpiryDate correctly
    properties.put(ContentModel.PROP_ACCOUNT_EXPIRY_DATE, new Date());
    nodeService.setProperties(rootNodeRef, properties);
    try
    {
        // Set usr:accountExpiryDate using something that can't be converted to a Date
        properties.put(ContentModel.PROP_ACCOUNT_EXPIRY_DATE, "blah");
        nodeService.setProperties(rootNodeRef, properties);
        fail("Failed to catch type conversion issue early.");
    }
    catch (TypeConversionException e)
    {
        // Expected
    }
}
 
Example #5
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 #6
Source File: RemoteCredentialsServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private BaseCredentialsInfo createCredentials(String remoteSystem, NodeRef remoteSystemNodeRef, BaseCredentialsInfo credentials)
{
    if (credentials.getNodeRef() != null)
    {
        throw new IllegalArgumentException("Cannot create Credentials which have already been persisted!");
    }
    
    // Check we know about the type
    RemoteCredentialsInfoFactory factory = credentialsFactories.get(credentials.getCredentialsType());
    if (factory == null)
    {
        throw new TypeConversionException("No Factory registered for type " + credentials.getCredentialsType());
    }
    
    // Build the properties
    Map<QName,Serializable> properties = RemoteCredentialsInfoFactory.FactoryHelper.getCoreCredentials(credentials);
    properties.putAll( factory.serializeCredentials(credentials) );
    
    // Generate a name for it, which will be unique and doesn't need updating
    QName name = QName.createQName(GUID.generate()); 
    
    // Add the node
    NodeRef nodeRef = nodeService.createNode(
            remoteSystemNodeRef, RemoteCredentialsModel.ASSOC_CREDENTIALS,
            name, credentials.getCredentialsType(), properties
    ).getChildRef();
    
    if (logger.isDebugEnabled())
        logger.debug("Created new credentials at " + nodeRef + " for " + remoteSystem + " in " + 
                     remoteSystemNodeRef + " of " + credentials);
    
    // Return the new object
    return factory.createCredentials(
            credentials.getCredentialsType(), nodeRef,
            remoteSystem, remoteSystemNodeRef,
            nodeService.getProperties(nodeRef)
    );
}
 
Example #7
Source File: RemoteCredentialsServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public BaseCredentialsInfo updateCredentials(BaseCredentialsInfo credentials)
{
    if (credentials.getNodeRef() == null)
    {
        throw new IllegalArgumentException("Cannot update Credentials which haven't been persisted yet!");
    }
    
    RemoteCredentialsInfoFactory factory = credentialsFactories.get(credentials.getCredentialsType());
    if (factory == null)
    {
        throw new TypeConversionException("No Factory registered for type " + credentials.getCredentialsType());
    }
    
    // Grab the current set of properties
    Map<QName,Serializable> oldProps = nodeService.getProperties(credentials.getNodeRef());
    
    // Overwrite them with the credentials ones
    Map<QName,Serializable> props = new HashMap<QName,Serializable>(oldProps);
    props.putAll( RemoteCredentialsInfoFactory.FactoryHelper.getCoreCredentials(credentials) );
    props.putAll( factory.serializeCredentials(credentials) );
    
    // Store
    nodeService.setProperties(credentials.getNodeRef(), props);
    
    // For now, return as-is
    return credentials;
}
 
Example #8
Source File: NodePropertyHelper.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Helper method to convert the <code>Serializable</code> value into a full, persistable {@link NodePropertyValue}.
 * <p>
 * Where the property definition is null, the value will take on the {@link DataTypeDefinition#ANY generic ANY}
 * value.
 * <p>
 * Collections are NOT supported. These must be split up by the calling code before calling this method. Map
 * instances are supported as plain serializable instances.
 * 
 * @param propertyDef the property dictionary definition, may be null
 * @param value the value, which will be converted according to the definition - may be null
 * @return Returns the persistable property value
 */
public NodePropertyValue makeNodePropertyValue(PropertyDefinition propertyDef, Serializable value)
{
    // get property attributes
    final QName propertyTypeQName;
    if (propertyDef == null) // property not recognised
    {
        // allow it for now - persisting excess properties can be useful sometimes
        propertyTypeQName = DataTypeDefinition.ANY;
    }
    else
    {
        propertyTypeQName = propertyDef.getDataType().getName();
    }
    try
    {
        NodePropertyValue propertyValue = null;
        propertyValue = new NodePropertyValue(propertyTypeQName, value);

        // done
        return propertyValue;
    }
    catch (TypeConversionException e)
    {
        throw new TypeConversionException(
                "The property value is not compatible with the type defined for the property: \n" +
                "   property: " + (propertyDef == null ? "unknown" : propertyDef) + "\n" +
                "   value: " + value + "\n" +
                "   value type: " + value.getClass(),
                e);
    }
}
 
Example #9
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 #10
Source File: CustomModelNamedValue.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
private String convertToString(Object value)
{
    try
    {
        return DefaultTypeConverter.INSTANCE.convert(String.class, value);
    }
    catch (TypeConversionException e)
    {
        throw new InvalidArgumentException("Cannot convert to string '" + value + "'.");
    }
}