Searching a folder using an SQL string
An object of the ODFolder class represents a Content Manager OnDemand folder. An object of the ODHit class represents a Content Manager OnDemand document. You use methods from these classes to search through a folder using an SQL string.
About this task
The following example uses ODFolder methods to open the specified folder, search the folder with the specified SQL string, and close the folder. This example uses ODHit methods to display the number of items that match the query and to display the document list.
This
example demonstrates the following ODFolder methods:
- getName
- getDescription
- getDisplayOrder
- setApplGroupForSearchWithSQL
- setMaxHits
- search
- close
This example demonstrates the following ODHit methods:
- getDisplayValue
- getDisplayValues
- getDocType
- getMimeType
- getDocLocation
- getDocId
This example also uses ODServer methods to prepare for
logon, open the specified folder, and log off. This example demonstrates
the following ODServer methods:
- initialize
- logon
- openFolder
- logoff
- terminate
This example uses the following runtime parameters:
- Server name
- Port
- User Id
- Password
- Folder name
- SQL string
- Max hits
- Date 1 (optional)
- Date 2 (optional)
- Date format (optional)
- Application group (optional)
Example of searching a folder using an SQL string:
import java.util.*;
import java.io.*;
import com.ibm.edms.od.*;
public class TcSQLSearch
{
public static void main ( String argv[] )
{
ODServer odServer;
ODFolder odFolder;
ODHit odHit;
Enumeration values_enum;
Vector hits;
String[] display_crit;
String header, line1, line2, hit_value, useable_value;
boolean mismatch_detected = false;
int j, k, opr;
//----------
// If too few parameters, display syntax and get out
//----------
if ( argv.length < 6)
{
System.out.println( "usage: java TcSQLSearch <server> <port> <userid> <password> <folder> <SQL>
<MaxHits (-1 to use default)> <opt begin Date> <opt end Date>
<opt Date Format> <opt ApplGrpName>" );
return;
}
try
{
//----------
// Set the stage
//----------
System.out.println( "Testcase TCSQLSearch started." );
System.out.println( "This testcase should:" );
System.out.println( " Logon to the specified server" );
System.out.println( " Open the specified folder" );
System.out.println( " Display the folder name and description" );
System.out.println( " Set the application group (if specified)" );
System.out.println( " Search the folder using specified Sql" );
System.out.println( " Display search message (if any)" );
System.out.println( " Display the number of hits" );
System.out.println( " Display the hitlist with each hit using 3 lines:" );
System.out.println( " 1. The hit values returned by the ODHit.getDisplayValue method" );
System.out.println( " 2. The hit values returned by the ODHit.getDisplayValues method" );
System.out.println( " 3. The doc type, mime type, doc location, and doc id values" );
System.out.println( "" );
System.out.println( "Ensure that lines 1 and 2 of the hitlist are the same and that the" );
System.out.println( "hitlist values are the same as those displayed using the Windows Client." );
System.out.println( "If arswww.props is restricting the number of hits, there may be fewer" );
System.out.println( "hits than displayed using the Windows Client." );
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( "TcSQLSearch.java" );
odServer.setPort(Integer.parseInt(argv[1]));
System.out.println( "Logging on to " + argv[0] + " server with user " + argv[2] + "..." );
odServer.logon( argv[0], argv[2], argv[3]);
//----------
// Open the specified folder and find the requested criteria
//----------
System.out.println( "Opening " + argv[4] + " folder..." );
odFolder = odServer.openFolder( argv[4] );
System.out.println( "Name='" + odFolder.getName( ) + "' Desc='" + odFolder.getDescription( ) + "'" );
System.out.println( "Getting " + argv[5] + " criteria..." );
//----------
// Search with Application Group limiters if specified
//----------
if(argv.length == 10 )
{
System.out.println("**Search is limited to Application Group " + argv[8]);
odFolder.setApplGroupForSearchWithSQL(argv[9]);
}
//Set MaxHits
int maxHits = Integer.parseInt(argv[6]);
if (maxHits != -1)
{
System.out.println("Setting MaxHITS to " + maxHits + ".");
odFolder.setMaxHits(maxHits);
}
else
System.out.println("No MaxHits passed in. Will use the default, or the Folder defined max,
which ever is less.");
//----------
// Search with date limiters if specified
//----------
if(argv.length >= 9 ) //call search(sql,date1,date2,date format)
{
String date1 = argv[7];
if(date1.compareTo(" ") != 0) //Only go in here if date was actually spec'd
{
System.out.println("Searching " + argv[4] +" with Date Range limits of " + argv[7] +
" and " + argv[8]);
System.out.println("Date format is " + argv[9]);
hits = odFolder.search(argv[5],
argv[7], //date1
(argv.length > 9) ? argv[8] : " ", //date2
(argv.length >= 10) ? argv[9] : " ", //date format
null); // null
}
else
{
//----------
// Search the folder
//----------
System.out.println( " Searching " + argv[4] + "with SQL " + argv[5] + "..." );
hits = odFolder.search(argv[5] );
}
}
else if(argv.length > 7 && argv.length < 10 ) //call search(sql,date1,date2)
{
String date1 = argv[7];
if(date1.compareTo(" ") != 0) //Only go in here if date was actually spec'd
{
System.out.println("Searching " + argv[4] +" with Date Range limits of " + argv[7] +
" and " +argv[8]);
hits = odFolder.search(argv[5],
argv[7], //date1
(argv.length > 9) ? "" : argv[8]); //date2
}
else //call search(sql)
{
//----------
// Search the folder
//----------
System.out.println( " Searching " + argv[4] + "with SQL " + argv[5] + "..." );
hits = odFolder.search(argv[5] );
}
}
else //call search(sql)
{
//----------
// Search the folder
//----------
System.out.println( " Searching " + argv[4] + "with SQL " + argv[5] + "..." );
hits = odFolder.search(argv[5] );
}
//----------
// Display the hits
//----------
System.out.println(" Number of Hits Found = " + hits.size());
mismatch_detected = false;
if ( hits != null && hits.size( ) > 0 )
{
display_crit = odFolder.getDisplayOrder( );
header = " ";
for( j = 0; j < display_crit.length; j++ )
header = header + display_crit[j] + "--";
System.out.println( " ------------------------------------------------" );
System.out.println( header + " (from ODHit.getDisplayValue method)" );
System.out.println( header + " (from ODHit.getDisplayValues method)" );
System.out.println( " DocType--MimeType--DocLocation--DocId" );
System.out.println( " ------------------------------------------------" );
for ( j = 0; j < hits.size( ); j++ )
{
odHit = (ODHit)hits.elementAt( j );
line1 = " ";
for ( k = 0; k < display_crit.length; k++ )
{
hit_value = odHit.getDisplayValue( display_crit[k] );
useable_value = ( hit_value.equals( "" ) ) ? " " : hit_value;
line1 = line1 + useable_value + "--";
}
System.out.println( line1 );
line2 = " ";
for ( values_enum = odHit.getDisplayValues( ); values_enum.hasMoreElements( ); )
{
hit_value = (String)values_enum.nextElement( );
useable_value = ( hit_value.equals( "" ) ) ? " " : hit_value;
line2 = line2 +useable_value + "--";
}
System.out.println( line2 );
System.out.println( " " + getDocTypeString( odHit.getDocType( ) ) +
"--" + odHit.getMimeType( ) +
"--" + getLocationString( odHit.getDocLocation( ) ) +
"--" + odHit.getDocId( ) );
if ( !line1.equals( line2 ) )
mismatch_detected = true;
}
}
//----------
// Cleanup
//----------
odFolder.close( );
odServer.logoff( );
odServer.terminate( );
System.out.println( "" );
System.out.println( "---------------------------------------------------" );
System.out.println( "" );
System.out.println( "Testcase TcSQLSearch completed - analyze if required" );
System.out.println( "" );
if ( mismatch_detected )
{
System.out.println( "*** At least one mismatch was found between" );
System.out.println( "*** lines 1 and 2 of a hit" );
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( );
}
}
static String getOperatorName( int oper )
{
String str;
switch( oper )
{
case ODConstant.OPEqual:
str = "Equals";
break;
case ODConstant.OPNotEqual:
str = "Not Equal";
break;
case ODConstant.OPLessThan:
str = "Less Than";
break;
case ODConstant.OPLessThanEqual:
str = "Less Than or Equal";
break;
case ODConstant.OPGreaterThan:
str = "Greater Than";
break;
case ODConstant.OPGreaterThanEqual:
str = "Greather Than or Equal";
break;
case ODConstant.OPIn:
str = "In";
break;
case ODConstant.OPNotIn:
str = "Not In";
break;
case ODConstant.OPLike:
str = "Like";
break;
case ODConstant.OPNotLike:
str = "Not Like";
break;
case ODConstant.OPBetween:
str = "Between";
break;
case ODConstant.OPNotBetween:
str = "Not Between";
break;
default:
str = "Operator unknown";
break;
}
return str;
}
static String getDocTypeString( char type )
{
String str;
switch( type )
{
case ODConstant.FileTypeAFP:
str = "AFP";
break;
case ODConstant.FileTypeBMP:
str = "BMP";
break;
case ODConstant.FileTypeEMAIL:
str = "EMAIL";
break;
case ODConstant.FileTypeGIF:
str = "GIF";
break;
case ODConstant.FileTypeJFIF:
str = "JFIF";
break;
case ODConstant.FileTypeLINE:
str = "LINE";
break;
case ODConstant.FileTypeMETA:
str = "META";
break;
case ODConstant.FileTypeNONE:
str = "NONE";
break;
case ODConstant.FileTypePCX:
str = "PCX";
break;
case ODConstant.FileTypePDF:
str = "PDF";
break;
case ODConstant.FileTypePNG:
str = "PNG";
break;
case ODConstant.FileTypeSCS:
str = "SCS";
break;
case ODConstant.FileTypeTIFF:
str = "TIFF";
break;
case ODConstant.FileTypeUSRDEF:
str = "USRDEF";
break;
default:
str = "*** Invalid Doc Type ***";
break;
}
return str;
}
static String getLocationString( int loc )
{
String str;
switch( loc )
{
case ODConstant.DocLocationCache:
str = "Cache";
break;
case ODConstant.DocLocationArchive:
str = "Archive";
break;
case ODConstant.DocLocationExternal:
str = "External";
break;
case ODConstant.DocLocationUnknown:
str = "Unknown";
break;
default:
str = "*** Invalid Doc Location ***";
break;
}
return str;
}
}