Added handling for colons,underscores in field names during post

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@13312 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Neil McErlean
2009-02-23 09:25:49 +00:00
parent 3762bb104b
commit aea17ab58f
4 changed files with 59 additions and 25 deletions

View File

@@ -6,10 +6,13 @@ function main()
var ta_id = url.templateArgs['id']; var ta_id = url.templateArgs['id'];
var ta_path = url.templateArgs['path']; var ta_path = url.templateArgs['path'];
logger.log("ta_storeType = " + ta_storeType); if (logger.isLoggingEnabled())
logger.log("ta_storeId = " + ta_storeId); {
logger.log("ta_id = " + ta_id); logger.log("ta_storeType = " + ta_storeType);
logger.log("ta_path = " + ta_path); logger.log("ta_storeId = " + ta_storeId);
logger.log("ta_id = " + ta_id);
logger.log("ta_path = " + ta_path);
}
var formUrl = ''; var formUrl = '';
// The template argument 'path' only appears in the second URI template. // The template argument 'path' only appears in the second URI template.
@@ -22,16 +25,23 @@ function main()
{ {
formUrl = ta_storeType + '://' + ta_storeId + '/' + ta_id; formUrl = ta_storeType + '://' + ta_storeId + '/' + ta_id;
} }
logger.log("formUrl = " + formUrl);
if (logger.isLoggingEnabled())
{
logger.log("formUrl = " + formUrl);
}
var formScriptObj = formService.getForm(formUrl); var formScriptObj = formService.getForm(formUrl);
if (formScriptObj == null) if (formScriptObj == null)
{ {
var message = "The form for item \"" + formUrl + "\" could not be found."; if (logger.isWarnLoggingEnabled())
logger.log(message); {
status.setCode(404, message); var message = "The form for item \"" + formUrl + "\" could not be found.";
return; logger.warn(message);
status.setCode(404, message);
return;
}
} }
var formModel = {}; var formModel = {};

View File

@@ -10,18 +10,26 @@ function main()
// The template argument 'path' only appears in the second URI template. // The template argument 'path' only appears in the second URI template.
if (ta_path != null) if (ta_path != null)
{ {
//TODO Need to test this path. nodeRef = ta_path;
nodeRef = ta_path;
} }
else else
{ {
nodeRef = ta_storeType + '://' + ta_storeId + '/' + ta_id; nodeRef = ta_storeType + '://' + ta_storeId + '/' + ta_id;
} }
logger.log("POST request received for nodeRef: " + nodeRef); if (logger.isLoggingEnabled())
{
logger.log("POST request received for nodeRef: " + nodeRef);
}
// TODO: check the given nodeRef is real // TODO: check the given nodeRef is real
// persist the submitted data using the most appropriate data set // persist the submitted data using the most appropriate data set
if (typeof formdata !== "undefined") if (typeof formdata !== "undefined")
{ {
@@ -30,22 +38,28 @@ function main()
model.data = formdata; model.data = formdata;
// Note: This formdata is org/alfresco/web/scripts/servlet/FormData.java // Note: This formdata is org/alfresco/web/scripts/servlet/FormData.java
logger.log("Saving form with formdata, " + formdata.fields.length + " fields."); if (logger.isLoggingEnabled())
//TODO At this point, for multipart, the field names are e.g. prop_cm_name {
logger.log("Saving form with formdata, " + formdata.fields.length + " fields.");
}
// N.B. This repoFormData is a different FormData class to that used above. // N.B. This repoFormData is a different FormData class to that used above.
var repoFormData = new Packages.org.alfresco.repo.forms.FormData(); var repoFormData = new Packages.org.alfresco.repo.forms.FormData();
for (var i = 0; i < formdata.fields.length; i++) for (var i = 0; i < formdata.fields.length; i++)
{ {
repoFormData.addData(formdata.fields[i].name, formdata.fields[i].value); // Replace the first 2 underscores with colons.
var alteredName = formdata.fields[i].name.replaceFirst("_", ":").replaceFirst("_", ":");
repoFormData.addData(alteredName, formdata.fields[i].value);
} }
//TODO How to handle false booleans? They are omitted from POST
formService.saveForm(nodeRef, repoFormData); formService.saveForm(nodeRef, repoFormData);
} }
else else
{ {
logger.log("Saving form with args = " + args); if (logger.isLoggingEnabled())
{
logger.log("Saving form with args = " + args);
}
formService.saveForm(nodeRef, args); formService.saveForm(nodeRef, args);
} }

View File

@@ -17,19 +17,27 @@ function main()
nodeRef = ta_storeType + '://' + ta_storeId + '/' + ta_id; nodeRef = ta_storeType + '://' + ta_storeId + '/' + ta_id;
} }
logger.log("POST request received for nodeRef: " + nodeRef); if (logger.isLoggingEnabled())
{
logger.log("JSON POST request received for nodeRef: " + nodeRef);
}
//TODO Add check whether nodeRef exists.
if (typeof json !== "undefined") if (typeof json !== "undefined")
{ {
logger.log("Saving form with json = " + json);
// At this point the field names are e.g. prop_cm_name // At this point the field names are e.g. prop_cm_name
// and there are some extra values - hidden fields? These are fields from YUI's datepicker(s) // and there are some extra values - hidden fields? These are fields from YUI's datepicker(s)
// e.g. "template_x002e_form-ui_x002e_form-test_prop_my_date-entry":"2/19/2009" // e.g. "template_x002e_form-ui_x002e_form-test_prop_my_date-entry":"2/19/2009"
//TODO Need to remove the extra fields.
} }
else else
{ {
logger.log("json object was undefined."); if (logger.isWarnLoggingEnabled())
{
logger.warn("json object was undefined.");
}
status.setCode(501, message);
return; return;
} }
@@ -37,12 +45,14 @@ function main()
var jsonKeys = json.keys(); var jsonKeys = json.keys();
for ( ; jsonKeys.hasNext(); ) for ( ; jsonKeys.hasNext(); )
{ {
// Replace the first 2 underscores with colons.
var nextKey = jsonKeys.next(); var nextKey = jsonKeys.next();
repoFormData.addData(nextKey, json.get(nextKey)); var alteredKey = nextKey.replaceFirst("_", ":").replaceFirst("_", ":");
repoFormData.addData(alteredKey, json.get(nextKey));
} }
formService.saveForm(nodeRef, repoFormData); formService.saveForm(nodeRef, repoFormData);
model.message = "Successfully updated node " + nodeRef; model.message = "Successfully updated node " + nodeRef;
} }

View File

@@ -39,7 +39,7 @@ public class TestFormRestAPI_JsonPost extends AbstractTestFormRestApi
private static final String PROP_CM_DESCRIPTION = "prop_cm_description"; private static final String PROP_CM_DESCRIPTION = "prop_cm_description";
private static final String APPLICATION_JSON = "application/json"; private static final String APPLICATION_JSON = "application/json";
public void testSubmitJsonPostRequest() throws IOException, JSONException public void testSimpleJsonPostRequest() throws IOException, JSONException
{ {
// Retrieve and store the original property value. // Retrieve and store the original property value.
Serializable originalDescription = Serializable originalDescription =