UniDataSet

From Pickwiki
Jump to navigationJump to search

Both the readField and readNamedField methods of UniFile can be passed a list of fields in the form of a UniDynArray. For readField, you pass a list of field numbers. for readNamedField, you pass a list of field names.

[I'd have to double check but I'm fairly sure that ONLY DATA FIELDS work here. No I-Descriptors.]

When combined with a UniDataSet, which allows you to specify a list of record keys, you can pick and choose exactly what data you want to retrieve.

An example follows.


import asjava.uniclientlibs.*;
import asjava.uniobjects.*;

/**
 *@author     Wendy Smoak (wsmoak@asu.edu)
 */
public class [[TestUniDataSet]]
{

   /**
    *  The main program for the [[TestUniDataSet]] class
    *
    *@param  args  The command line arguments
    */
   public static void main( String[] args ) throws Exception
   {
      [[UniSession]] uSession = openConnection();

      [[UniFile]] uFile = uSession.open( "PERSON" );
      System.out.println( uFile );

      //add record keys to the [[UniDataSet]]
      [[UniDataSet]] u[[InputDataSet]] = new [[UniDataSet]]();
      u[[InputDataSet]].insert( "0452360" );
      u[[InputDataSet]].append( "0235876" );
      u[[InputDataSet]].append( "0501415" );
      u[[InputDataSet]].append( "0459760" );
      u[[InputDataSet]].append( "0227728" );

      [[UniDynArray]] fieldArray;
      [[UniDataSet]] u[[OutputDataSet]];

      //choose the field names you want to see in the output
      fieldArray = new [[UniDynArray]]();
      fieldArray.replace( 1, "FIRST.NAME" );
      fieldArray.replace( 2, "MIDDLE.NAME" );
      fieldArray.replace( 3, "LAST.NAME" );
      fieldArray.replace( 4, "BIRTH.DATE" );

      System.out.println( "\nThe fieldArray with field names: " + fieldArray );
      
      u[[OutputDataSet]] = uFile.read[[NamedField]]( u[[InputDataSet]], fieldArray.toString() );

      for ( int i = 0; i < u[[OutputDataSet]].get[[RowCount]](); i++ ) {
         System.out.println( "Row = " + u[[OutputDataSet]].get[[UniString]]( i ) );
      }

      //choose the field numbers you want to see in the output
      fieldArray = new [[UniDynArray]]();
      fieldArray.replace( 1, "1" );  /[[/LAST]].NAME is field 1
      fieldArray.replace( 2, "3" );  /[[/FIRST]].NAME is field 3
      fieldArray.replace( 3, "4" );  /[[/MIDDLE]].NAME is field 4

      System.out.println( "\nThe fieldArray with field names: " + fieldArray );
      
      u[[OutputDataSet]] = uFile.read[[NamedField]]( u[[InputDataSet]], fieldArray.toString() );

      for ( int i = 0; i < u[[OutputDataSet]].get[[RowCount]](); i++ ) {
         System.out.println( "Row = " + u[[OutputDataSet]].get[[UniString]]( i ) );
      }

      fieldArray = new [[UniDynArray]]();
      fieldArray.replace( 1, "3" );  /[[/FIRST]].NAME is field 3
      fieldArray.replace( 2, "4" );  /[[/MIDDLE]].NAME is field 4
      fieldArray.replace( 3, "1" );  /[[/LAST]].NAME is field 1

      System.out.println( "\nThe fieldArray with field numbers: " + fieldArray );

      u[[OutputDataSet]] = uFile.readField( u[[InputDataSet]], fieldArray.toString() );

      for ( int i = 0; i < u[[OutputDataSet]].get[[RowCount]](); i++ ) {
         System.out.println( "Row = " + u[[OutputDataSet]].get[[UniString]]( i ) );
      }

      uSession.disconnect( );

   }


   /**
    *  Creates a connected [[UniSession]]
    */
   private static [[UniSession]] openConnection()
       throws [[UniSessionException]]
   {
      [[UniSession]] uSession = new [[UniSession]]();

      uSession.set[[HostName]]( "my.host.name" );
      uSession.set[[UserName]]( "user" );
      uSession.setPassword( "pass" );
      uSession.set[[AccountPath]]( "/path/to/data" );
      uSession.set[[ConnectionString]]( "udcs" );

      uSession.connect();

      return uSession;
   }

}