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

@@ -24,9 +24,16 @@
*/
package org.alfresco.repo.forms;
import java.util.Collection;
import java.util.List;
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.
*
@@ -34,6 +41,9 @@ import java.util.Map;
*/
public class PropertyFieldDefinition extends FieldDefinition
{
/** Logger */
private static Log logger = LogFactory.getLog(PropertyFieldDefinition.class);
protected String dataType;
protected DataTypeParameters dataTypeParams;
protected boolean mandatory = false;
@@ -175,7 +185,7 @@ public class PropertyFieldDefinition extends FieldDefinition
public class FieldConstraint
{
protected String type;
protected Map<String, String> params;
protected Map<String, Object> params;
/**
* Constructs a FieldConstraint
@@ -183,7 +193,7 @@ public class PropertyFieldDefinition extends FieldDefinition
* @param type The type of 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();
this.type = type;
@@ -206,9 +216,51 @@ public class PropertyFieldDefinition extends FieldDefinition
* @return Map of parameters for the constraint or null if
* there are no parameters
*/
public Map<String, String> getParams()
public Map<String, Object> getParameters()
{
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;
}
}
}