Lab B.4: Add to the function Library
From LongJump Support Wiki
Revision as of 22:08, 19 October 2012 by imported>Aeric (Text replace - 'functionParams' to 'requestParams')
Deprecated
- Goals
-
- Write a function to display values returned in a hashmap
- Execute the function from a JSP page
- View results in the debug log
- Prerequisites
-
- ...
Exercise
List Values Returned in HashMap
Note: It may not be possible to write a good version of this function with the current implementation of Parameters. Values appear to be passed as strings, using Parameters.add(key-string, value-string). The calling class is then required to know the types and get the appropriate a conversion using getDate(), getBoolean(), getInteger(), or getFloat().
- 1. Simple display (Integers and Strings only)
Map<String,Object> map = Functions.getLoggedInUserInfo(); for (Map.Entry<String, Object> item : map.entrySet()) { Functions.debug( item.getKey() + ":" + item.getValue().toString() ); }
- 2. Display arbitrary objects (requires explode function)
Map<String,Object> map = Functions.getLoggedInUserInfo(); for (Map.Entry<String, Object> item : map.entrySet()) { if ((item.getValue() instanceof String) || (item.getValue() instanceof Integer)) { String value = item.getValue().toString(); Functions.debug(item.getKey() + ":" + value); } else { Object obj = item.getValue(); String obj_name = obj.getClass().getName(); Functions.debug(item.getKey()+":"+obj_name); Parameters params = Functions.getParametersInstance(); params.add("level",1); params.add("object", obj); Functions.exec("explode", params); } }
- Notes:
- Hover over a debug line to display a long output string
- Learn more: requestParams Object
- Notes:
- 3. Explode function
//Functions.debug("Function: explode"); //Params: // "object" => Object to display // { "level" } => Optional indent level //Notes: // * Indents 2 spaces for each level of depth. // * Initial indent level defaults to zero Object obj = requestParams.getObject("object"); int level = requestParams.getInteger("level", 0); //Default level = 1 String indent = ""; for (int i=0; i<level; i++) { indent = indent + " "; } Functions.debug(indent + "LIST OF FIELDS"); // FOR ALL FIELDS { /* if ((field instanceof String) { Functions.debug(indent + obj); } else if ((field instanceof Integer) || (field instanceof Date) || (field instanceof Float)) || (field instanceof Boolean)) { Functions.debug(indent + obj.toString()); } else { String field_class = field.getClass().getName(); Functions.debug(indent + field_class + ":"); Parameters params = Functions.getParametersInstance(); params.add("level",level+1); params.add("object", field); Functions.exec("explode", params); } } */
- Notes:
- Variables cannot be declared using the reflection classes "Class" or "Method". Methods which return those values can be invoked, however. So we can do what we want to do--we just can't use intermediate variables to do it. (Hence the long line ....)
- Notes:
- 4. Do foo
class foo implements bar { }
Do Y
stuff...
- Next
- --Lab X.N: To Be Done--