Final minor forms refactor with the way data type parameters are handled

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14705 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gavin Cornwell
2009-06-12 20:06:48 +00:00
parent e9dfb3f64b
commit 60cd434285
2 changed files with 43 additions and 49 deletions

View File

@@ -42,9 +42,15 @@ public interface DataTypeParameters
public Object getParameters();
/**
* Returns the parameters in a REST API friendly manner i.e. as JSON.
* Returns the parameters represented as JSON.
* <p>
* Implementations can use whatever JSON libraries they
* desire, the only rule is that the object returned must
* toString() to either a JSON array or JSON object i.e.
* [...] or {...}
* </p>
*
* @return JSON String representing the data type parameters
* @return JSON Object representing the parameters
*/
public String getParametersAsJSONString();
public Object getParametersAsJSON();
}

View File

@@ -44,6 +44,9 @@ import org.alfresco.repo.dictionary.types.period.XMLDuration;
import org.alfresco.repo.dictionary.types.period.Years;
import org.alfresco.repo.forms.DataTypeParameters;
import org.alfresco.service.cmr.repository.PeriodProvider;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Represents the parameters for the period data type.
@@ -93,60 +96,45 @@ public class PeriodDataTypeParameters implements DataTypeParameters, Serializabl
}
/**
* Retrieves a JSON array representing the valid period options
* for the property
* Returns the valid period options as a JSONArray of JSONObject's.
*
* @see org.alfresco.repo.forms.DataTypeParameters#getParametersAsJSONString()
* @return A JSON string representing the period options for the property
* @see org.alfresco.repo.forms.DataTypeParameters#getParametersAsJSON()
* @return A JSONArray object holding JSONObject's representing the
* period definitions
*/
public String getParametersAsJSONString()
public Object getParametersAsJSON()
{
StringBuilder builder = new StringBuilder("[\n");
JSONArray periods = new JSONArray();
boolean isFirst = true;
try
{
for (PeriodProvider pp : this.providers)
{
if (isFirst == false)
{
builder.append(",\n");
}
else
{
isFirst = false;
}
boolean hasExpression = !(pp.getExpressionMutiplicity().equals(PeriodProvider.ExpressionMutiplicity.NONE));
builder.append("{ \"type\" : \"").append(pp.getPeriodType()).append("\",");
builder.append(" \"label\" : \"").append(getPeriodTypeLabel(pp)).append("\",");
builder.append(" \"hasExpression\" : ").append(hasExpression);
JSONObject period = new JSONObject();
period.put("type", pp.getPeriodType());
period.put("label", getPeriodTypeLabel(pp));
period.put("hasExpression", hasExpression);
if (hasExpression)
{
builder.append(", \"expressionMandatory\" : ").append(
pp.getExpressionMutiplicity().equals(PeriodProvider.ExpressionMutiplicity.MANDATORY)).append(",");
builder.append(" \"expressionType\" : \"").append(pp.getExpressionDataType().toPrefixString()).append("\",");
builder.append(" \"defaultExpression\" : \"").append(pp.getDefaultExpression()).append("\"");
}
builder.append(" }");
period.put("expressionMandatory",
pp.getExpressionMutiplicity().equals(PeriodProvider.ExpressionMutiplicity.MANDATORY));
period.put("expressionType", pp.getExpressionDataType().toPrefixString());
period.put("defaultExpression", pp.getDefaultExpression());
}
builder.append("\n]");
return builder.toString();
periods.put(period);
}
/*
* @see java.lang.Object#toString()
*/
@Override
public String toString()
}
catch (JSONException je)
{
// NOTE: Until the script and REST API for the form service is
// made a little less generic toString() will be called for these
// objects, we'll therefore temporarily return the result of
// getParametersAsJSONString() from here, eventually the js/ftl
// will call it directly.
return getParametersAsJSONString();
// return an empty array
periods = new JSONArray();
}
return periods;
}
protected String getPeriodTypeLabel(PeriodProvider pp)