ALF-10143 Now Form processors convert property values from String to the appropriate type as specified by the property's DataTypeDefinition when the form is persisted.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@31096 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
N Smith
2011-10-10 15:15:25 +00:00
parent 8f3116a501
commit 717e89267e
4 changed files with 46 additions and 16 deletions

View File

@@ -72,14 +72,19 @@ public class MockClassAttributeDefinition implements PropertyDefinition, Associa
public static MockClassAttributeDefinition mockPropertyDefinition(QName name, QName dataTypeName)
{
MockClassAttributeDefinition mock = new MockClassAttributeDefinition(name);
mockDataTypeName(dataTypeName, mock);
mockDataTypeName(mock, dataTypeName, null);
return mock;
}
public static MockClassAttributeDefinition mockPropertyDefinition(QName name, QName dataTypeName, String defaultValue)
{
return mockPropertyDefinition(name, dataTypeName, null, defaultValue);
}
public static MockClassAttributeDefinition mockPropertyDefinition(QName name, QName dataTypeName, Class<?> typeClass, String defaultValue)
{
MockClassAttributeDefinition mock = new MockClassAttributeDefinition(name);
mockDataTypeName(dataTypeName, mock);
mockDataTypeName(mock, dataTypeName, typeClass);
mock.defaultValue = defaultValue;
return mock;
}
@@ -94,7 +99,7 @@ public class MockClassAttributeDefinition implements PropertyDefinition, Associa
boolean multiValued)
{
MockClassAttributeDefinition mock = new MockClassAttributeDefinition(name, title, description, isProtected);
mockDataTypeName(dataTypeName, mock);
mockDataTypeName(mock, dataTypeName, null);
mock.defaultValue = defaultValue;
mock.mandatory = Mandatory;
mock.multiValued = multiValued;
@@ -123,9 +128,13 @@ public class MockClassAttributeDefinition implements PropertyDefinition, Associa
return mock;
}
private static void mockDataTypeName(QName dataTypeName, MockClassAttributeDefinition mock)
private static void mockDataTypeName(MockClassAttributeDefinition mock, QName dataTypeName, Class<?> javaClass)
{
when(mock.dataType.getName()).thenReturn(dataTypeName);
if (javaClass!=null)
{
when(mock.dataType.getJavaClassName()).thenReturn(javaClass.getName());
}
}
private static void mockTargetClassName(QName targetClassName, MockClassAttributeDefinition mock)

View File

@@ -90,6 +90,9 @@ public class PropertyFieldProcessor extends QNameFieldProcessor<PropertyDefiniti
return propDef;
}
/**
* {@inheritDoc}
*/
@Override
public Field makeField(PropertyDefinition propDef, Object value, FieldGroup group)
{

View File

@@ -319,6 +319,16 @@ public class TaskFormProcessorTest extends TestCase
assertEquals(value, actualProperties.get(DESC_NAME));
}
public void testPersistConvertsPropertyValueToCorrectType()
{
String fieldName = PROP_PRIORITY.toPrefixString(namespaceService);
String dataKey = makeDataKeyName(fieldName);
String value = "2"; // String value for property of type Integer!
processPersist(dataKey, value);
assertEquals(2, actualProperties.get(PROP_PRIORITY));
}
public void testPersistPropertyWith_() throws Exception
{
String fieldName = PROP_WITH_.toPrefixString(namespaceService);
@@ -671,6 +681,13 @@ public class TaskFormProcessorTest extends TestCase
PropertyDefinition pckgItemAction = MockClassAttributeDefinition.mockPropertyDefinition(pckgItemActionGroup,
textType, "read_package_item_actions");
properties.put(pckgItemActionGroup, pckgItemAction);
// Add a priority property
QName priorityName = PROP_PRIORITY;
PropertyDefinition priorityDef =
MockClassAttributeDefinition.mockPropertyDefinition(priorityName, DataTypeDefinition.INT, Integer.class, "0");
properties.put(priorityName, priorityDef);
return properties;
}

View File

@@ -27,6 +27,7 @@ import java.util.List;
import org.alfresco.repo.forms.FormException;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.extensions.surf.util.I18NUtil;
@@ -48,19 +49,20 @@ public class TypedPropertyValueGetter
return null;
}
Serializable typedValue = null;
// before persisting check data type of property
if (propDef.isMultiValued())
{
typedValue = processMultiValuedType(value);
return processMultiValuedType(value);
}
else if (isBooleanProperty(propDef))
if (isBooleanProperty(propDef))
{
typedValue = processBooleanValue(value);
return processBooleanValue(value);
}
else if (isLocaleProperty(propDef))
{
typedValue = processLocaleValue(value);
return processLocaleValue(value);
}
else if (value instanceof String)
{
@@ -68,24 +70,23 @@ public class TypedPropertyValueGetter
// make sure empty strings stay as empty strings, everything else
// should be represented as null
if (valStr.length() == 0 && !isTextProperty(propDef))
if (isTextProperty(propDef))
{
// Do nothing, leave typedValue as null.
return valStr;
}
else
if(valStr.isEmpty())
{
typedValue = valStr;
return null;
}
}
else if (value instanceof Serializable)
if (value instanceof Serializable)
{
typedValue = (Serializable) value;
return (Serializable) DefaultTypeConverter.INSTANCE.convert(propDef.getDataType(), value);
}
else
{
throw new FormException("Property values must be of a Serializable type! Value type: " + value.getClass());
}
return typedValue;
}
private boolean isTextProperty(PropertyDefinition propDef)