private static JsonNode storageCommand(JsonNode command, JsonCallback callback) throws CommandException {
JsonNode moduleId = command.get(ScriptCommand.ID);
if (moduleId==null||!moduleId.isTextual()){
throw new CommandParsingException(command,"error parsing module id");
}
String id = moduleId.asText();
JsonNode params = command.get(ScriptCommand.PARAMS);
if (params == null||!params.isObject()){
throw new CommandParsingException(command,"error parsing params");
}
JsonNode actionNode = params.get("action");
if (actionNode==null||!actionNode.isTextual()){
throw new CommandParsingException(command,"error parsing action");
}
String action=actionNode.asText();
ODocument result;
if ("get".equals(action)){
try {
result = ScriptingService.getStore(id);
} catch (ScriptException e) {
//should never happen
throw new CommandExecutionException(command,"script does not exists");
}
} else if ("set".equals(action)){
JsonNode args = params.get("args");
if (args==null){
args = NullNode.getInstance();
}
if (!args.isObject()||!args.isNull()){
throw new CommandExecutionException(command,"Stored values should be objects or null");
}
try {
result = ScriptingService.resetStore(id,args);
} catch (ScriptException e) {
throw new CommandExecutionException(command,"script does not exists");
}
} else if ("swap".equals(action)){
if (callback==null) throw new CommandExecutionException(command,"missing function callback");
try {
result = ScriptingService.swap(id,callback);
} catch (ScriptException e) {
throw new CommandExecutionException(command,e.getMessage(),e);
}
} else if ("trade".equals(action)){
if (callback==null) throw new CommandExecutionException(command,"missing function callback");
try {
result = ScriptingService.trade(id,callback);
} catch (ScriptException e) {
throw new CommandExecutionException(command,e.getMessage(),e);
}
} else {
throw new CommandParsingException(command,"unknown action: "+action);
}
if (result == null){
return NullNode.getInstance();
} else {
String s = result.toJSON();