Retrieving a document
You use methods from the ODServer, ODFolder, and ODHit classes to help you retrieve a specific document from a folder.
About this task
The example shows the following tasks:
- Logging on to the specified server
- Opening the specified folder
- Searching the folder by using the default criteria
- Displaying the number of hits
- Retrieving the document for the first hit by using
ODHit.retrieve
, which outputs the document data to a byte array - Retrieving the document again by using
ODHit.getDocument()
, which also outputs the document data to a byte array - Verifying whether the document is AFP, and if so, calling
ODHit.getResources()
to retrieve the AFP resource file
This example demonstrates the following ODServer methods:
- initialize
- logon
- openFolder
- logoff
- terminate
- search
- close
- retrieve
- getDocument
- getResources
- docRequiresResources
This example uses the following runtime parameters:
- Server name
- User Id
- Password
- Folder name
Example of retrieving a document:
import java.util.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.ibm.edms.od.*;
public class TcRetrieve
{
public static void main ( String argv[] )
{
ODServer odServer;
ODFolder odFolder;
ODHit odHit;
Vector<ODHit> hits;
byte[] data_from_hit;
byte[] data_from_server;
byte[] res_from_server;
Path file_retr, file_getdoc, file_getres;
int getDocResSize = 0;
//----------
// If too few parameters, display syntax and get out
//----------
if ( argv.length < 4 )
{
System.out.println( "usage: java TcRetrieve <server> <userid> <password> <folder> " );
return;
}
try
{
//----------
// Set the stage
//----------
System.out.println( "This testcase should:" );
System.out.println( " Logon to the specified server" );
System.out.println( " Open the specified folder" );
System.out.println( " Search the folder using the default criteria" );
System.out.println( " Display the number of hits" );
System.out.println( " Display the MIME and document type" );
System.out.println( " Retrieve the data for the first hit using ODHit.retrieve" );
System.out.println( " Retrieve the data for the first hit using ODHit.getDocument" );
System.out.println( " For AFP data, retrieve the resources for the first hit using ODHit.getResources" );
System.out.println( " Display length of data retrieved from each method" );
System.out.println( " Compare the lengths and data retrieved from each method" );
System.out.println( " Display the result of the comparisons" );
System.out.println( "" );
System.out.println( "---------------------------------------------------" );
System.out.println( "" );
//----------
// Logon to specified server
//----------
ODConfig odConfig = new ODConfig();
if(odConfig != null)
{
odServer = new ODServer(odConfig );
odServer.initialize( "TcRetrieve.java" );
System.out.println( "Logging on to " + argv[0] + "..." );
odServer.logon( argv[0], argv[1], argv[2] );
//----------
// Open the specified folder and search with the default criteria
//----------
System.out.println( "Opening " + argv[3] + " folder..." );
odFolder = odServer.openFolder( argv[3] );
System.out.println( "Searching with default criteria..." );
hits = odFolder.search( );
System.out.println( "Number of hits: " + hits.size( ) );
//----------
// Do some retrieves and comparisons
//----------
if ( hits.size( ) > 0 )
{
odHit = (ODHit)hits.elementAt( 0 );
System.out.println( " View Mime Type = " + odHit.getMimeType());
System.out.println( " Doc Type = " + odHit.getDocType());
System.out.println( "Retrieving data from first hit using ODHit.retrieve..." );
data_from_hit = odHit.retrieve(ODConstant.NATIVE);
file_retr = Paths.get("data.retrieve.native");
Files.write(file_retr, data_from_hit);
System.out.println( "Retrieving data from first hit using ODHit.getDocument/getResources..." );
data_from_server = odHit.getDocument();
file_getdoc = Paths.get("data.getdocument");
Files.write(file_getdoc, data_from_hit);
//----------
// Alternatively, use the ODHit.getDocument( String filename, boolean allsegments ) method
// to have ODWEK write the document data directly to file above
//----------
if( odHit.docRequiresResources() )
{
try{
res_from_server = odHit.getResources();
file_getres = Paths.get("res.afp");
Files.write(file_getres, res_from_server);
//----------
// Alternatively, use the ODHit.getResources( String filename ) method
// to have ODWEK write the resource data directly to file above
//----------
getDocResSize = data_from_server.length + res_from_server.length;
}
catch(ODException E)
{
System.out.println("Error retrieving resources for this document.");
getDocResSize = data_from_server.length;
}
}
else
getDocResSize = data_from_server.length ;
System.out.println( "Number of bytes retrieved from:" );
System.out.println( " ODHit.retrieve = " + data_from_hit.length );
System.out.println( " ODHit.getDocument (+getResource) = " + getDocResSize );
if ( data_from_hit.length == getDocResSize )
System.out.println( "SUCCESS - ODHit.retrieve vs. ODHit.getDoc: Length of data matches" );
else
System.out.println( "*** ODHit.retrieve vs. ODHit.getDoc: Length mismatch" );
}
else
System.out.println( "There is no document to retrieve. Zero hits returned" );
//----------
// Cleanup
//----------
odFolder.close( );
odServer.logoff( );
odServer.terminate( );
System.out.println( "" );
System.out.println( "---------------------------------------------------" );
System.out.println( "" );
System.out.println( "Testcase completed - analyze the result of the comparisons" );
System.out.println( "" );
}
}
catch ( ODException e )
{
System.out.println( "ODException: " + e );
System.out.println( " id = " + e.getErrorId( ) );
System.out.println( " msg = " + e.getErrorMsg( ) );
e.printStackTrace( );
}
catch ( Exception e2 )
{
System.out.println( "exception: " + e2 );
e2.printStackTrace( );
}
}
}