Copyright 2009,2010, Lapetus Systems Ltd. (All rights reserved)

com.lapetus_ltd.api.db.control
Class TLptsRowSetOutputListener

java.lang.Object
  extended by com.lapetus_ltd.api.db.control.TLptsRowSetOutputListener
All Implemented Interfaces:
ILptsFactoryRowSetListener

public class TLptsRowSetOutputListener
extends java.lang.Object
implements ILptsFactoryRowSetListener

Class Description :

This class provides the ability to store the output from a rowset execution (either simple or dynamic with sub-sets)
which can then be output to a file or used to transfer information (across the net for instance).

There are 4 options for processing the rowset information:
The information can be output to xml, which can be written to file or retrieved from this class
as an object of type XLptsXmlRowSetType.


$LastChangedRevision: 1210 $
$LastChangedDate:: 2010-11-29 15:22:58#$


Nested Class Summary
static class TLptsRowSetOutputListener.OUTPUT_TYPE
           
 
Constructor Summary
TLptsRowSetOutputListener(java.lang.String fileName, TLptsRowSetOutputListener.OUTPUT_TYPE outputType, java.lang.String charset)
          Construct this listener for writing rowset output to a data, debug or XML file.
 
Method Summary
 void closeOutputFile()
          Closes the output files for XML, debug and data output.
 TLptsXmlRowSetType getXmlRowSet()
          Gets the RowSet XML data.
 boolean processNewRowSetRows(TLptsRowSetEvent rowSetEvent)
          Implementation of ILptsFactoryRowSetListener.
 void rowEvent(TLptsRowEvent rowEvent)
          Implementation of ILptsFactoryRowSetListener.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TLptsRowSetOutputListener

public TLptsRowSetOutputListener(java.lang.String fileName,
                                 TLptsRowSetOutputListener.OUTPUT_TYPE outputType,
                                 java.lang.String charset)
Construct this listener for writing rowset output to a data, debug or XML file.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : No

Notes :
If dynamic statement processing has been declared, then XML output is required so that the structure
and hierarchy can be captured and transported. This is demonstrated in the example below.

Example :

 String path = TLptsFileUtil.createDirectoryPath(TLptsFileUtil.getCurrentDirectory() + "test");
 TLptsFactoryRowSet.addListener(rowSetOutputXmlListener = new TLptsRowSetOutputListener(path + "resultset.xml",
                                                                                        TLptsRowSetOutputListener.OUTPUT_TYPE.OUTPUT_TO_XML));
 

Parameters:
fileName - Supply a file name to write XML or text output. Null means output to stdout.
outputType - The types are follows: TLptsRowSetOutputListener.OUTPUT_TYPE.OUTPUT_TO_XML, TLptsRowSetOutputListener.OUTPUT_TYPE.DEBUG_OUTPUT, TLptsRowSetOutputListener.OUTPUT_TYPE.DATA_OUTPUT, The names are pretty self explanatory. If any of this options are provided with a null file name then the output is sent to std out, with the exception of OUTPUT_TO_XML. The XML output can be retrieved using the function getXmlRowSet(), if OUTPUT_TO_XML is specified.
charset - This is the charset to be used for processing the file. It should be the same as the statement charset.
Method Detail

closeOutputFile

public void closeOutputFile()
Closes the output files for XML, debug and data output.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A

Notes : This is normally done at the end of processing of the primary rowset.

Example :


 private class RowSetListener implements ILptsFactoryRowSetListener
 {
   public boolean processNewRowSetRows(TLptsRowSetEvent rowSetEvent)
   {
     return true;
   }
   public void rowEvent(final TLptsRowEvent rowEvent)
   {
     if (rowEvent.getEventType() == TLptsRowEvent.EVENT_TYPE.PROCESSING_COMPLETE ||
         rowEvent.getEventType() == TLptsRowEvent.EVENT_TYPE.EXECUTION_INTERRUPTED ||
         rowEvent.getEventType() == TLptsRowEvent.EVENT_TYPE.ERROR_OCCURRED)
      closeOutputFile();
   }
 }
 


getXmlRowSet

public TLptsXmlRowSetType getXmlRowSet()
Gets the RowSet XML data.

Thread Safe : Yes

Spawns its own Thread : No

May Return NULL : N/A

Notes :

Example :


 public void addMultipleRows()
 {
   TLptsXmlRowSetType xmlRowSetType = new TLptsXmlRowSetType();

   // we declare the lists for the data infusion
   List columnNames = new LinkedList();
   columnNames.add("COLUMN_NAME_1");
   columnNames.add("COLUMN_NAME_2");

   List objectList1 = new LinkedList();
   objectList1.add("My String for column 1 - Level 1");
   objectList1.add(123);
   List byteList1 = new LinkedList();
   byteList1.add(TLptsBytesUtil.string2Bytes("My String for column 1 - Level 1"));
   byteList1.add(TLptsBytesUtil.int2Bytes(123));

   List objectList2 = new LinkedList();
   objectList2.add("My String for column 1 - Level 2");
   objectList2.add(456);
   List byteList2 = new LinkedList();
   byteList2.add(TLptsBytesUtil.string2Bytes("My String for column 1 - Level 2"));
   byteList2.add(TLptsBytesUtil.int2Bytes(456));

   List objectList3 = new LinkedList();
   objectList3.add("My String for column 1 - Level 3");
   objectList3.add(789);
   List byteList3 = new LinkedList();
   byteList3.add(TLptsBytesUtil.string2Bytes("My String for column 1 - Level 3"));
   byteList3.add(TLptsBytesUtil.int2Bytes(789));

   List sqlTypeList = new LinkedList();
   sqlTypeList.add(java.sql.Types.VARCHAR);
   sqlTypeList.add(java.sql.Types.INTEGER);

   TLptsXmlRowSetTableType table1 = new TLptsXmlRowSetTableType("Table1",columnNames);
   TLptsXmlRowSetTableType table2 = new TLptsXmlRowSetTableType("Table2",columnNames);
   TLptsXmlRowSetTableType table3 = new TLptsXmlRowSetTableType("Table3",columnNames);

   TLptsXmlRowSetSingleRowType level1Row = new TLptsXmlRowSetSingleRowType(1,
                                                                           objectList1,
                                                                           sqlTypeList,
                                                                           byteList1 );
   table1.addRowListItem(level1Row);

   TLptsXmlRowSetSingleRowType level2Row = new TLptsXmlRowSetSingleRowType(1,
                                                                           objectList2,
                                                                           sqlTypeList,
                                                                           byteList2 );
   table2.addRowListItem(level2Row);

   table2.addRowListItem(new TLptsXmlRowSetSingleRowType(1,
                                                         objectList2,
                                                         sqlTypeList,
                                                         byteList2 ));
   // add the sub row to the 2nd level row
   table3.addRowListItem(new TLptsXmlRowSetSingleRowType(1,
                                                         objectList3,
                                                         sqlTypeList,
                                                         byteList3 ));

   // add the sub row to the top level row
   level1Row.addSubTableListItem(table2);
   level2Row.addSubTableListItem(table3);

   xmlRowSetType.setPrimaryTable(table1);
   printSingleRowSetList(xmlRowSetType.getPrimaryTable().getRowListItem(), 1);
 }


 public void printSingleRowSetList(java.util.List singleRowList, int level)
 {
   for (XLptsXmlRowSetSingleRowType rssrt : singleRowList)
   {
     String preamble = "";
     for (int i = 1;i < level;i++)
       preamble += " ";
     preamble += "=> ";
     System.out.print(preamble);

     for (XLptsXmlRowSetRowObjectType rsro : rssrt.getRowObjectListItem())
       System.out.print(rsro.getString() + " ");
     System.out.println();

     for (XLptsXmlRowSetTableType tableItem : rssrt.getSubTableListItem())
       printSingleRowSetList(tableItem.getRowListItem(), level + 1);
   }
 }

 RUN RESULT :
 => My String for column 1 - Level 1 123
  => My String for column 1 - Level 2 456
   => My String for column 1 - Level 3 789
  => My String for column 1 - Level 2 456

 

Returns:
The XML data structure with all the rows, sub-tables and sub-rows.

processNewRowSetRows

public boolean processNewRowSetRows(TLptsRowSetEvent rowSetEvent)
Implementation of ILptsFactoryRowSetListener.

Specified by:
processNewRowSetRows in interface ILptsFactoryRowSetListener
Parameters:
rowSetEvent - A new row or null if there is an error.
Returns:
True if we want the rows to be processed by the rowset factory. If we return true, we should not perform actions on the rowset as the row cursor will be moved by TLptsFactoryRowSet.

rowEvent

public void rowEvent(TLptsRowEvent rowEvent)
Implementation of ILptsFactoryRowSetListener.

Specified by:
rowEvent in interface ILptsFactoryRowSetListener
Parameters:
rowEvent - This is the new row. The event type indicates what kind of action it is.


Copyright 2009,2010, Lapetus Systems Ltd. (All rights reserved)