Last refactor of forms API before Labs release - FieldConstraints API now receives and returns a Map<String, Object> rather than Map<String, String>, a method to return the parameters as JSON has also been added and is used in the REST API. This means that parameters go all the way to client side validation handlers in their native form.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14727 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gavin Cornwell
2009-06-15 23:32:40 +00:00
parent 97e28176a0
commit 4ed7957b63
6 changed files with 64 additions and 30 deletions

View File

@@ -39,7 +39,7 @@ public interface DataTypeParameters
* *
* @return An Object representing the data type parameters * @return An Object representing the data type parameters
*/ */
public Object getParameters(); public Object getAsObject();
/** /**
* Returns the parameters represented as JSON. * Returns the parameters represented as JSON.
@@ -52,5 +52,5 @@ public interface DataTypeParameters
* *
* @return JSON Object representing the parameters * @return JSON Object representing the parameters
*/ */
public Object getParametersAsJSON(); public Object getAsJSON();
} }

View File

@@ -324,7 +324,7 @@ public class FormServiceImplTest extends BaseAlfrescoSpringTest
assertEquals("Expecting 1 constraint for cm:name", 1, constraints.size()); assertEquals("Expecting 1 constraint for cm:name", 1, constraints.size());
FieldConstraint constraint = constraints.get(0); FieldConstraint constraint = constraints.get(0);
assertEquals("Expecting name of constraint to be 'REGEX'", "REGEX", constraint.getType()); assertEquals("Expecting name of constraint to be 'REGEX'", "REGEX", constraint.getType());
Map<String, String> params = constraint.getParams(); Map<String, Object> params = constraint.getParameters();
assertNotNull("Expecting constraint parameters", params); assertNotNull("Expecting constraint parameters", params);
assertEquals("Expecting 2 constraint parameters", 2, params.size()); assertEquals("Expecting 2 constraint parameters", 2, params.size());
assertNotNull("Expecting an 'expression' constraint parameter", params.get("expression")); assertNotNull("Expecting an 'expression' constraint parameter", params.get("expression"));

View File

@@ -24,9 +24,16 @@
*/ */
package org.alfresco.repo.forms; package org.alfresco.repo.forms;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/** /**
* A property field definition. * A property field definition.
* *
@@ -34,6 +41,9 @@ import java.util.Map;
*/ */
public class PropertyFieldDefinition extends FieldDefinition public class PropertyFieldDefinition extends FieldDefinition
{ {
/** Logger */
private static Log logger = LogFactory.getLog(PropertyFieldDefinition.class);
protected String dataType; protected String dataType;
protected DataTypeParameters dataTypeParams; protected DataTypeParameters dataTypeParams;
protected boolean mandatory = false; protected boolean mandatory = false;
@@ -175,7 +185,7 @@ public class PropertyFieldDefinition extends FieldDefinition
public class FieldConstraint public class FieldConstraint
{ {
protected String type; protected String type;
protected Map<String, String> params; protected Map<String, Object> params;
/** /**
* Constructs a FieldConstraint * Constructs a FieldConstraint
@@ -183,7 +193,7 @@ public class PropertyFieldDefinition extends FieldDefinition
* @param type The type of the constraint * @param type The type of the constraint
* @param params Map of parameters for the constraint * @param params Map of parameters for the constraint
*/ */
public FieldConstraint(String type, Map<String, String> params) public FieldConstraint(String type, Map<String, Object> params)
{ {
super(); super();
this.type = type; this.type = type;
@@ -206,9 +216,51 @@ public class PropertyFieldDefinition extends FieldDefinition
* @return Map of parameters for the constraint or null if * @return Map of parameters for the constraint or null if
* there are no parameters * there are no parameters
*/ */
public Map<String, String> getParams() public Map<String, Object> getParameters()
{ {
return this.params; return this.params;
} }
/**
* Returns the paramters for the constraint as a JSONObject
*
* @return JSONObject representation of the parameters
*/
@SuppressWarnings("unchecked")
public JSONObject getParametersAsJSON()
{
JSONObject result = null;
if (this.params != null)
{
result = new JSONObject();
for (String name : this.params.keySet())
{
try
{
Object value = this.params.get(name);
if (value instanceof Collection)
{
// if the value is a Collection add to JSONObject as a JSONArray
result.put(name, new JSONArray((Collection)value));
}
else
{
result.put(name, value);
}
}
catch (JSONException je)
{
// just log a warning
if (logger.isWarnEnabled())
logger.warn("Failed to add constraint parameter '" + name +
"' to JSON object.", je);
}
}
}
return result;
}
} }
} }

View File

@@ -624,26 +624,8 @@ public class NodeFormProcessor extends FilteredFormProcessor
for (ConstraintDefinition constraintDef : constraints) for (ConstraintDefinition constraintDef : constraints)
{ {
Constraint constraint = constraintDef.getConstraint(); Constraint constraint = constraintDef.getConstraint();
Map<String, String> fieldConstraintParams = null;
Map<String, Object> constraintParams = constraint.getParameters();
if (constraintParams != null)
{
// TODO: Just return the param value object, don't convert to String
fieldConstraintParams = new HashMap<String, String>(constraintParams.size());
for (String name : constraintParams.keySet())
{
Object paramValue = constraintParams.get(name);
if (paramValue instanceof List)
{
paramValue = makeListString((List)paramValue);
}
fieldConstraintParams.put(name, paramValue.toString());
}
}
FieldConstraint fieldConstraint = fieldDef.new FieldConstraint( FieldConstraint fieldConstraint = fieldDef.new FieldConstraint(
constraint.getType(), fieldConstraintParams); constraint.getType(), constraint.getParameters());
fieldConstraints.add(fieldConstraint); fieldConstraints.add(fieldConstraint);
} }

View File

@@ -75,10 +75,10 @@ public class PeriodDataTypeParameters implements DataTypeParameters, Serializabl
* Retrieves a List of PeriodProvider objects representing * Retrieves a List of PeriodProvider objects representing
* the valid period options for the property * the valid period options for the property
* *
* @see org.alfresco.repo.forms.DataTypeParameters#getParameters() * @see org.alfresco.repo.forms.DataTypeParameters#getAsObject()
* @return List of PeriodProvider objects * @return List of PeriodProvider objects
*/ */
public Object getParameters() public Object getAsObject()
{ {
return this.providers; return this.providers;
} }
@@ -86,11 +86,11 @@ public class PeriodDataTypeParameters implements DataTypeParameters, Serializabl
/** /**
* Returns the valid period options as a JSONArray of JSONObject's. * Returns the valid period options as a JSONArray of JSONObject's.
* *
* @see org.alfresco.repo.forms.DataTypeParameters#getParametersAsJSON() * @see org.alfresco.repo.forms.DataTypeParameters#getAsJSON()
* @return A JSONArray object holding JSONObject's representing the * @return A JSONArray object holding JSONObject's representing the
* period definitions * period definitions
*/ */
public Object getParametersAsJSON() public Object getAsJSON()
{ {
JSONArray periods = new JSONArray(); JSONArray periods = new JSONArray();

View File

@@ -86,7 +86,7 @@ function testGetFormForContentNode()
test.assertEquals(1, constraints.size()); test.assertEquals(1, constraints.size());
var constraint = constraints.get(0); var constraint = constraints.get(0);
test.assertEquals("REGEX", constraint.type); test.assertEquals("REGEX", constraint.type);
var params = constraint.params; var params = constraint.parameters;
test.assertNotNull(params, "params should not be null."); test.assertNotNull(params, "params should not be null.");
test.assertEquals(2, params.length); test.assertEquals(2, params.length);
test.assertNotNull(params["expression"], "params['expression'] should not be null."); test.assertNotNull(params["expression"], "params['expression'] should not be null.");