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(); 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.dictionary.types.period.Years;
import org.alfresco.repo.forms.DataTypeParameters; import org.alfresco.repo.forms.DataTypeParameters;
import org.alfresco.service.cmr.repository.PeriodProvider; 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. * 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 * Returns the valid period options as a JSONArray of JSONObject's.
* for the property
* *
* @see org.alfresco.repo.forms.DataTypeParameters#getParametersAsJSONString() * @see org.alfresco.repo.forms.DataTypeParameters#getParametersAsJSON()
* @return A JSON string representing the period options for the property * @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) for (PeriodProvider pp : this.providers)
{ {
if (isFirst == false)
{
builder.append(",\n");
}
else
{
isFirst = false;
}
boolean hasExpression = !(pp.getExpressionMutiplicity().equals(PeriodProvider.ExpressionMutiplicity.NONE)); boolean hasExpression = !(pp.getExpressionMutiplicity().equals(PeriodProvider.ExpressionMutiplicity.NONE));
builder.append("{ \"type\" : \"").append(pp.getPeriodType()).append("\","); JSONObject period = new JSONObject();
builder.append(" \"label\" : \"").append(getPeriodTypeLabel(pp)).append("\","); period.put("type", pp.getPeriodType());
builder.append(" \"hasExpression\" : ").append(hasExpression); period.put("label", getPeriodTypeLabel(pp));
period.put("hasExpression", hasExpression);
if (hasExpression) if (hasExpression)
{ {
builder.append(", \"expressionMandatory\" : ").append( period.put("expressionMandatory",
pp.getExpressionMutiplicity().equals(PeriodProvider.ExpressionMutiplicity.MANDATORY)).append(","); pp.getExpressionMutiplicity().equals(PeriodProvider.ExpressionMutiplicity.MANDATORY));
builder.append(" \"expressionType\" : \"").append(pp.getExpressionDataType().toPrefixString()).append("\","); period.put("expressionType", pp.getExpressionDataType().toPrefixString());
builder.append(" \"defaultExpression\" : \"").append(pp.getDefaultExpression()).append("\""); period.put("defaultExpression", pp.getDefaultExpression());
}
builder.append(" }");
} }
builder.append("\n]"); periods.put(period);
return builder.toString();
} }
}
/* catch (JSONException je)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{ {
// NOTE: Until the script and REST API for the form service is // return an empty array
// made a little less generic toString() will be called for these periods = new JSONArray();
// objects, we'll therefore temporarily return the result of }
// getParametersAsJSONString() from here, eventually the js/ftl
// will call it directly. return periods;
return getParametersAsJSONString();
} }
protected String getPeriodTypeLabel(PeriodProvider pp) protected String getPeriodTypeLabel(PeriodProvider pp)