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

View File

@@ -90,6 +90,9 @@ public class PropertyFieldProcessor extends QNameFieldProcessor<PropertyDefiniti
return propDef; return propDef;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public Field makeField(PropertyDefinition propDef, Object value, FieldGroup group) 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)); 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 public void testPersistPropertyWith_() throws Exception
{ {
String fieldName = PROP_WITH_.toPrefixString(namespaceService); String fieldName = PROP_WITH_.toPrefixString(namespaceService);
@@ -671,6 +681,13 @@ public class TaskFormProcessorTest extends TestCase
PropertyDefinition pckgItemAction = MockClassAttributeDefinition.mockPropertyDefinition(pckgItemActionGroup, PropertyDefinition pckgItemAction = MockClassAttributeDefinition.mockPropertyDefinition(pckgItemActionGroup,
textType, "read_package_item_actions"); textType, "read_package_item_actions");
properties.put(pckgItemActionGroup, pckgItemAction); 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; return properties;
} }

View File

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