From 4439e6a3c8d6a045c27ddf840ab591db176bce19 Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@jalin.de>
Date: Thu, 20 Dec 2012 16:09:53 +0100
Subject: [PATCH] hsscript returns regular javascript obejcts on hsadmin server calls

---
 hsarjcli/src/de/hsadmin/jscli/Main.java          |   12 ++++--
 hsarjcli/src/de/hsadmin/jscli/ScriptClient.java  |   38 ++++++++++++++++++-
 hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java |   34 ++++++++--------
 3 files changed, 61 insertions(+), 23 deletions(-)

diff --git a/hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java b/hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java
index 96580ca..2f221fb 100644
--- a/hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java
+++ b/hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java
@@ -8,6 +8,23 @@
 	
 	private int indent = 1;
 
+	public String format(Object object) {
+		if (object == null) return "";
+		if (object instanceof List<?>) {
+			return formatList((List<?>) object);
+		}
+		if (object instanceof Map<?, ?>) {
+			return formatMap((Map<?, ?>) object);
+		}
+		if (object instanceof String) {
+			return formatString((String) object);
+		}
+		if (object instanceof Object[]) {
+			return formatArr((Object[]) object);
+		}
+		return "an instance of " + object.getClass().getCanonicalName();
+	}
+
 	public String formatMap(Map<?, ?> map) {
 		StringBuffer result = new StringBuffer();
 		result.append('{');
@@ -59,23 +76,6 @@
 		}
 		result.append(']');
 		return result.toString();
-	}
-	
-	public String format(Object object) {
-		if (object == null) return "";
-		if (object instanceof List<?>) {
-			return formatList((List<?>) object);
-		}
-		if (object instanceof Map<?, ?>) {
-			return formatMap((Map<?, ?>) object);
-		}
-		if (object instanceof String) {
-			return formatString((String) object);
-		}
-		if (object instanceof Object[]) {
-			return formatArr((Object[]) object);
-		}
-		return "an instance of " + object.getClass().getCanonicalName();
 	}
 	
 	private void incr() {
diff --git a/hsarjcli/src/de/hsadmin/jscli/Main.java b/hsarjcli/src/de/hsadmin/jscli/Main.java
index 131047e..3452ee5 100644
--- a/hsarjcli/src/de/hsadmin/jscli/Main.java
+++ b/hsarjcli/src/de/hsadmin/jscli/Main.java
@@ -20,11 +20,13 @@
 			String file = cmdParser.getFile();
 			if (file != null && file.length() > 0) {
 				if ("-".equals(file)) {
-					console.println(formatter.format(scriptClient.execute(new InputStreamReader(System.in))));
+					scriptClient.execute(new InputStreamReader(System.in));
+					console.println(formatter.format(scriptClient.getLastRpcResult()));
 				} else {
 					File fileHandle = new File(file);
 					try {
-						console.println(formatter.format(scriptClient.execute(new FileReader(fileHandle))));
+						scriptClient.execute(new FileReader(fileHandle));
+						console.println(formatter.format(scriptClient.getLastRpcResult()));
 					} catch (FileNotFoundException e) {
 						System.err.println("File not found: " + file);
 					}
@@ -32,13 +34,15 @@
 			}
 			String expr = cmdParser.getExpression();
 			if (expr != null && expr.length() > 0) {
-				console.println(formatter.format(scriptClient.execute(expr)));
+				scriptClient.execute(expr);
+				console.println(formatter.format(scriptClient.getLastRpcResult()));
 			}
 			if (cmdParser.isInteractive()) {
 				String command = console.readInput();
 				while (!("bye".equals(command.trim()) || "exit".equals(command.trim()) || "quit".equals(command.trim()))) {
 					try {
-						console.println(formatter.format(scriptClient.execute(command)));
+						scriptClient.execute(command);
+						console.println(formatter.format(scriptClient.getLastRpcResult()));
 					} catch (Exception e) {
 						console.println("Error: " + e.getLocalizedMessage() + "\n");
 					}
diff --git a/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java b/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java
index adfc9ad..6bb0540 100644
--- a/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java
+++ b/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java
@@ -23,8 +23,16 @@
 		try {
 			engine.put("casgrantingticket", grantingTicket);
 			engine.put("xmlrpcclient", rpcClient);
+			engine.put("xmlrpcLastResult", null);
 			engine.eval("importClass(java.util.ArrayList);");
 			engine.eval("importClass(java.util.HashMap);");
+			engine.eval("function hsaParseParam(val) { " +
+					"if (val instanceof java.util.List) return val;" + 
+					"if (val instanceof java.util.Map) return val;" + 
+					"if (typeof val === 'object' && val.constructor === Array) { res = hsaParseParamArray(val); } " +
+					"else if (typeof val === 'object') { res = hsaParseParamObject(val); }; " +
+					"return res; " +
+					"}");
 			engine.eval("function hsaParseParamArray(o) { " +
 					"var lst = new ArrayList(); " +
 					"var val = ''; " +
@@ -45,6 +53,25 @@
 					"  hsh.put(key, val); " +
 					"}; " +
 					"return hsh; " +
+					"}");
+			engine.eval("function hsaToNativeJSObject(val) { " +
+					"if (val instanceof java.util.List) {" +
+				    "    var res = [];" +
+				    "    for (i = 0; i < val.size(); i++) {" +
+				    "        res[i] = hsaToNativeJSObject(val.get(i));" +
+				    "    }" +
+				    "    return res;" +
+				    "}" +
+				    "if (val instanceof java.util.Map) {" +
+				    "    var res = {};" +
+				    "    var iter = val.keySet().iterator();" +
+				    "    while (iter.hasNext()) {" +
+				    "        var key = iter.next();" +
+				    "        res[key] = hsaToNativeJSObject(val.get(key));" +
+				    "    }" +
+				    "    return res;" +
+				    "}" +
+				    "return val;" +
 					"};");
 		} catch (ScriptException e) {
 			throw new JSCliException(e);
@@ -71,8 +98,9 @@
 								"params.add(casgrantingticket.getTicket()); " + 
 								"if (typeof json === 'undefined') { json = { where:{}, set:{} } };" +
 								"if (fct == 'update' || fct == 'add') { params.add(hsaParseParamObject(json['set'])); }; " + 
-								"if (fct == 'update' || fct == 'delete' || fct == 'search') { params.add(hsaParseParamObject(json['where'])); }; " + 
-								"return xmlrpcclient.execute(mod + '.' + fct, params); " + 
+								"if (fct == 'update' || fct == 'delete' || fct == 'search') { params.add(hsaParseParamObject(json['where'])); }; " +
+								"xmlrpcLastResult = xmlrpcclient.execute(mod + '.' + fct, params);" +
+								"return hsaToNativeJSObject(xmlrpcLastResult); " + 
 								"};");
 				} catch (ScriptException e) {
 					e.printStackTrace();
@@ -94,6 +122,7 @@
 
 	public Object execute(String snippet) throws JSCliException {
 		try {
+			engine.put("xmlrpcLastResult", null);
 			return engine.eval(snippet);
 		} catch (ScriptException e) {
 			throw new JSCliException(e);
@@ -102,10 +131,15 @@
 	
 	public Object execute(Reader rd) throws JSCliException {
 		try {
+			engine.put("xmlrpcLastResult", null);
 			return engine.eval(rd);
 		} catch (ScriptException e) {
 			throw new JSCliException(e);
 		}
 	}
 	
+	public Object getLastRpcResult() {
+		return engine.get("xmlrpcLastResult");
+	}
+	
 }

--
Gitblit v1.9.0-SNAPSHOT