The framework contains support Java functions to help you with generating and parsing TreeGrid XML data and reading it from and upating it to database in server side script.
You need not use this framework, it contains only helper functions to simplify writting your code.
The framework is located in single file TreeGridFramework.jsp and can be included into your JSP pages by standard include mechanism <%@include file="TreeGridFramework.jsp"%>.
Remember, if you are generating XML files, be careful to not generate any white space before the first < character.
Parsing uploaded XML
Specific functions for parsing TreeGrid output XML changes
DocumentparseXML (StringXML)
Returns XML Document from String XML.
Returns null when XML is empty or not valid. XML is complete data XML uploaded by TreeGrid.
Element[]getChanges (StringXML)
Element[]getChanges (DocumentXML)
Returns all changed rows - array of XML <I> elements in <Changes> tag. XML is complete data XML uploaded by TreeGrid.
intgetPagePos (StringXML)
intgetPagePos (DocumentXML)
Returns page number (<B Pos='000'/>) from input XML. By this number is page identified for server paging. XML is complete data XML uploaded by TreeGrid.
StringgetPageId (StringXML)
StringgetPageId (DocumentXML)
Returns page id attribute (<B id='xxx'/>) from input XML. By this id is row identified to get its children when server child paging is used. XML is complete data XML uploaded by TreeGrid.
String[]getSortCols (StringXML)
String[]getSortCols (DocumentXML)
Returns array of columns according to is grid sorted.
Returns null if there are no columns. XML is complete data XML uploaded by TreeGrid.
int[]getSortTypes (StringXML)
int[]getSortTypes (DocumentXML)
Returns array of sorting types for columns according to is grid sorted.
Returns null if there are no columns. XML is complete data XML uploaded by TreeGrid.
String[]getGroupCols (StringXML)
String[]getGroupCols (DocumentXML)
Returns array of columns according to is grid grouped.
Returns null if there are no columns. XML is complete data XML uploaded by TreeGrid.
int[]getGroupTypes (StringXML)
int[]getGroupTypes (DocumentXML)
Returns array of grouping types for columns according to is grid grouped.
Returns null if there are no columns. XML is complete data XML uploaded by TreeGrid.
booleanisAttribute (ElementI,
Stringname, Stringvalue)
Compares value of row I attribute name with given value.
booleanisDeleted (ElementI)
booleanisAdded (ElementI)
booleanisChanged (ElementI)
booleanisMoved (ElementI)
Tests if given row I has set the flag. For Moved tests equality to "2" (moved to another parent).
booleangetParameter (HttpServletRequestrequest,Stringname)
Returns request parameter value, for null returns "".
Generating SQL commands
Universal functions for generating SQL commands from strings or values in XML attributes
StringtoSQL (Stringvalue)
Returns string in quotes (') and with doubled all quotes.
StringtoSQL (ElementI, Stringname)
Returns value of XML attribute in quotes (') and with doubled all quotes.
If attribute does not exist, returns two single quotes ('').
StringtoSQLUpdateString (Stringname, Stringvalue)
Returns one item for UPDATE command, for string types. Returns "name='value',".
If value is null, returns empty string.
StringtoSQLUpdateString (Stringname, ElementI, StringattributeName)
Returns one item for UPDATE command, for string types. Returns "name='I.getAttribute(attributeName)',". For example "A='Peter''s bug',".
If attribute does not exist, returns empty string.
StringtoSQLUpdateNumber (Stringname, Stringvalue)
Returns one item for UPDATE command, for number types. Returns "name=value,".
If value is null or empty string, returns empty string.
StringtoSQLUpdateNumber (Stringname, ElementI, StringattributeName)
Returns one item for UPDATE command, for number types. Returns "name=I.getAttribute(attributeName),". For example "A=3,".
If attribute does not exist, returns empty string.
StringtrimSQL (Stringvalue)
Deletes all commas on the end of string value. Useful when building comma separated list in loop, call it after list is built to strip ending comma(s).
StringtoSQLInsert (ElementI,
String[]attrNames, boolean[]attrIsString)
Returns values of row (XML element) I attributes in attrNames, separated by comma.
The string never ends by comma.
If the attribute does not exists, it is not included.
If attrIsString[n] is true, it encloses attribute value by ' '.
This function can be used for SQL INSERT command generating. Example:
Element I; // I is XML for example "<I id='7' Project='Main' Resource='This' Week='4' Hours='13'/>
String[] Names = {"Project","Resource","Week","Hours"};
boolean[] IsString = {false,true,true,false,false};
String SQL = "INSERT INTO TableData(ID,Project,Resource,Week,Hours) VALUES(" + getAttributes(I,Names,IsString) + ")";
StringtoSQLUpdate (ElementI,
String[]attrNames, String[]outNames,
boolean[]attrIsString)
Returns row (XML element) I attribute values in format "outNames[0]=value_of_attrNames[0],outNames[1]=value_of_attrNames[1], ...".
The string never ends by comma.
If the attribute does not exists, it is not included.
If attrIsString[n] is true, it encloses attribute value by ' '.
This function can be used for SQL UPDATE command generating. Example:
Element I; // I is XML for example "<I id='7' Project='Main' Hours='13'/>
String[] Names = {"Project","Resource","Week","Hours"};
boolean[] IsString = {true,true,false,false};
String SQL = "UPDATE TableData SET " + getAttributes(I,Names,Names,IsString) + " WHERE ID=" + getAttribute(I,"id");
Updating changes in XML to database
Advanced specific functions for updating changes got in TreeGrid XML to database table.
booleansaveTree (StatementCmd,
Stringtable, StringidCol,
StringparentCol, StringparentBody)
throwsSQLException
Saves changes in XML to database table by SQL statement Cmd. idCol is database table column name where is stored id attribute. parentCol is database table column name where is stored Parent attribute, for Parent<-Child relation. In Parent column the row has value of id column of parent row => All parent's children have its id in their Parent column. bodyParent is value of Parent for new added root rows.
This function expects that all other attribute names are the same as column names in database table.
Creating XML
Universal functions for creating XML.
StringtoXMLString (Stringvalue)
Returns value with replaced & ' < by XML entities
StringfromXMLString (Stringvalue)
Returns value with replaced XML entities by & ' < " > characters.
StringtoHTMLString (Stringvalue)
Returns value with replaced & " by XML entities.
Useful for direct writing generated XML to hidden input value on HTML page. Don't forget to use double quotes to enclose the string.
StringtoXML (Stringvalue)
voidtoXML (StringBufferS, Stringvalue)
Returns value in single quotes (') with replaced XML entities by & ' < characters.
StringtoXML (shortvalue)
StringtoXML (intvalue)
StringtoXML (longvalue)
StringtoXML (booleanvalue)
StringtoXML (floatvalue)
StringtoXML (doublevalue)
Returns value in single quotes (').
StringtoXML (Stringname, Stringvalue)
voidtoXML (StringBufferS, Stringname, Stringvalue)
Returns " value='value'", value with replaced XML entities by & ' < characters.
voidtoXML (StringBufferS, Stringname, ResultSetR, StringcolumnName)
Returns " name='R.getString(columnName)'", value with replaced XML entities by & ' < characters.
ResultSet must be opened and cursor must be on some row.
StringgetRowXML (ResultSetR, String[]names, Stringend) throwsSQLException
Returns XML representation of one row with given attribute names and their values in array or from ResultSet R in columns colNames or colIndexes or in columns in order according to names. end is the string appended after row, for rows with children use ">", without children "/>" (default), or you can set own attributes.
Does not move cursor in ResultSet.
Remember when colIndexes are used, columns are indexed from 1 !
Example:
String[] Names = {"id","Project","Resource","Week","Hours"};
String[] Values = {"7", "Main","This","4","13"};
String XML = getRowXML(Names,Values); // The result will be: "<I id='7' Project='Main' Resource='This' Week='4' Hours='13'/>"
StringgetTableXML (ResultSetR, String[]names) throwsSQLException
Returns complete (include <Grid> tag) XML representation of all rows with given attribute names and their values from ResultSet R in columns colNames or colIndexes or in columns in order according to names.
Remember when colIndexes are used, columns are indexed from 1 !
Example result:
StringgetTableXML (ResultSetR, StringidCol) throwsSQLException
Returns complete (include <Grid> tag) XML representation of all rows with given attribute names and their values from ResultSet R in the same named attributes.
Remember, some SQL databases have case insensitive column names and TreeGrid XML has always case-sensitive names. idCol is name of database column that will be stored to id attribute.
StringgetTreeXML (StatementCmd,
Stringtable, String[]names,
StringbodyParent,
StringheadParent, StringfootParent,
booleandeep) throwsSQLException
Returns complete (include <Grid> tag) XML representation of all rows with given attribute names
and their values from ResultSet R in columns indexed as attribute names
or when used colNames returns only columns in this array in the same order as names attributes.
It is used for generating tree from one table that has parent->id relation. table is name of database table
names are TreeGrid attribute names in order in that are filled from columns from database table
colNames are names of columns in database used instead of indexes. Must be in the same order as names array. Can contain only some columns from database table. names must contain name "id", this is identity attribute to unique identify row when uploading and in tree. names must also contain name "Parent", this is column related to id in Parent<-Child.
In Parent column the row has value of id column of parent row => All parent's children have its id in their Parent column.
For deep==false reads only one level of tree, for server side child paging.
StringgetTreeXML (StatementCmd,
Stringtable, String[]names,
StringparentValbooleandeep) throwsSQLException
Returns partial XML, returns children of row with id = parentVal => returns all rows that have in their Parent column value parentVal.
Other parameters are the same as in previous function getTreeXML.
Support for examples
StatementgetHsqlStatement (HttpServletRequestrequest,
JspWriterout, StringdbPath,
Stringuser, Stringpass)
throwsException
Creates SQL statement for SQL database HSQLDB that is used for examples.
HSQLDB is simple database in text files without need of any driver instalation. Just copy file hsqldb.jar file to your shared lib directory and restart http server. request and out are just the variables in main servlet functions. dbPath is path and name of database file(s), relative to location of the script file. user and pass are login information to database.
If database driver cannot be executed, because hsqldb.jar file is missing or not accessible it writes this information to output, closes it and raises Exception.