- Refinements to Javascript/Freemarker Node conversions

- Removed hard-coded conversions from Web Scripts
- example category search web script aligned with documentation

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5940 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
David Caruana
2007-06-13 17:23:47 +00:00
parent 80939c8187
commit 2794a59085
5 changed files with 114 additions and 83 deletions

View File

@@ -72,7 +72,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess
private static final String SCRIPT_ROOT = "_root";
/** Base Value Converter */
private ValueConverter valueConverter = new ReturnValueConverter();
private ValueConverter valueConverter = new ValueConverter();
/** Store into which to resolve cm:name based script paths */
private StoreRef storeRef;
@@ -510,19 +510,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess
Object result = cx.evaluateString(scope, script, "AlfrescoScript", 1, null);
// extract java object result if wrapped by Rhino
if (result instanceof Serializable)
{
result = valueConverter.convertValueForRepo((Serializable)result);
}
else if (result instanceof Wrapper)
{
result = ((Wrapper)result).unwrap();
}
else if (result instanceof NativeArray)
{
result = Context.jsToJava(result, Object[].class);
}
result = valueConverter.convertValueForRepo((Serializable)result);
return result;
}
catch (Throwable err)
@@ -564,30 +552,5 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess
}
return newModel;
}
/**
* Value conversion for handling Javascript return values.
*/
public class ReturnValueConverter extends ValueConverter
{
/**
* Convert an object from any script wrapper value to a valid repository serializable value.
* This includes converting JavaScript Array objects to Lists of valid objects.
*
* @param value Value to convert from script wrapper object to repo serializable value
*
* @return valid repo value
*/
public Serializable convertValueForRepo(Serializable value)
{
if (value instanceof Wrapper ||
value instanceof ScriptableObject ||
value instanceof Serializable[])
{
value = super.convertValueForRepo(value);
}
return value;
}
}
}

View File

@@ -28,7 +28,9 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.AssociationRef;
@@ -37,6 +39,7 @@ import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.QName;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.IdScriptableObject;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
@@ -153,26 +156,50 @@ public class ValueConverter
// set using a JavaScript Array object
ScriptableObject values = (ScriptableObject)value;
if (value instanceof NativeArray)
if (value instanceof IdScriptableObject)
{
// convert JavaScript array of values to a List of Serializable objects
Object[] propIds = values.getIds();
List<Serializable> propValues = new ArrayList<Serializable>(propIds.length);
for (int i=0; i<propIds.length; i++)
if (value instanceof NativeArray)
{
// work on each key in turn
Object propId = propIds[i];
// we are only interested in keys that indicate a list of values
if (propId instanceof Integer)
// convert JavaScript array of values to a List of Serializable objects
Object[] propIds = values.getIds();
List<Serializable> propValues = new ArrayList<Serializable>(propIds.length);
for (int i=0; i<propIds.length; i++)
{
// get the value out for the specified key
Serializable val = (Serializable)values.get((Integer)propId, values);
// recursively call this method to convert the value
propValues.add(convertValueForRepo(val));
// work on each key in turn
Object propId = propIds[i];
// we are only interested in keys that indicate a list of values
if (propId instanceof Integer)
{
// get the value out for the specified key
Serializable val = (Serializable)values.get((Integer)propId, values);
// recursively call this method to convert the value
propValues.add(convertValueForRepo(val));
}
}
value = (Serializable)propValues;
}
else
{
// convert JavaScript map to values to a Map of Serializable objects
Object[] propIds = values.getIds();
Map<String, Serializable> propValues = new HashMap<String, Serializable>(propIds.length);
for (int i=0; i<propIds.length; i++)
{
// work on each key in turn
Object propId = propIds[i];
// we are only interested in keys that indicate a list of values
if (propId instanceof String)
{
// get the value out for the specified key
Serializable val = (Serializable)values.get((String)propId, values);
// recursively call this method to convert the value
propValues.put((String)propId, convertValueForRepo(val));
}
}
value = (Serializable)propValues;
}
value = (Serializable)propValues;
}
else
{