Searching a folder

An object of the ODFolder class represents a Content Manager OnDemand folder. An object of the ODCriteria class represents the search criteria for 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.

About this task

The following example uses ODFolder methods to open the specified folder, display the folder name, description, display order and search criteria, search the folder, and close the folder. This example uses ODCriteria methods to set the current search operand and search values. This example uses ODHit methods to do the following tasks:
  • Get the display values for the document
  • Get the document type
  • Get a persistent identifier for the document
  • Get the document location
  • Get the MIME content type for the document
This example demonstrates the following ODFolder methods:
  • close
  • getName
  • getDescription
  • getDisplayOrder
  • getCriteria
  • getSearchMessage
  • search
  • setMaxHits
This example demonstrates the following ODCriteria methods:
  • getName
  • setOperator
  • setSearchValue
  • setSearchValues
This example demonstrates the following ODHit methods:
  • getDisplayValue
  • getDisplayValues
  • getDocType
  • getMimeType
  • getDocLocation
  • getDocId
Note: If you extract and reload the same document, the system generates a new DocID for the document, and the old DocID does not reference the new document.
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
  • logoff
  • logon
  • openFolder
  • terminate
This example uses the following runtime parameters:
  • Criteria name
  • Folder name
  • Maximum hits
  • Operator (must be one of eq, ne, lt, le, gt, ge, in, ni, li, nl, be, nb)
  • Password
  • Port
  • Search value 1
  • (optional) Search value 2
  • Server name
  • User Id
Important: The number of hits might be restricted by the MAXHITS specified in the ODConfig or passed to the search.
Example of searching a folder:
import java.util.*;
import java.io.*;
import com.ibm.edms.od.*;

public class TcSearch
{
    public static void main ( String argv[] )
    {
        ODServer odServer;
        ODFolder odFolder;
        ODCriteria odCrit;
        ODHit odHit;
        Enumeration values_enum;
        Vector hits;
        String[] display_crit;
        String server, userid, password, folder, directory;
        String crit = "", operator = "", value1 = "", value2 = "";
        String header, line1, line2, hit_value, useable_value;
        boolean mismatch_detected, use_default_values;
        int j, k, opr;
        int port, maxHits;

        //----------
        // If too few parameters, display syntax and get out
        //----------
        if ( argv.length <  10 && argv.length != 6 )
        {
            System.out.println( "usage: java TcSearch <server> <port> <userid> <password> <folder> " );
            System.out.println( "         <MaxHits (-1 to use default)> <criteria> <opr> <value1> <value2>" );
            System.out.println( "       or, to use deafult search criteria" );
            System.out.println( "       java TcSearch <server> <port> <userid> <password> <folder>" );
            System.out.println( "         <MaxHits (-1 to use default)> " );
            return;
        }

        try
        {
            //----------
            // Set the stage
            //----------
            System.out.println( "Testcase TcSearch 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( "  If specified:" );
            System.out.println( "    Get the specified criteria" );
            System.out.println( "    Set the operator" );
            System.out.println( "    Set the operand(s)" );
            System.out.println( "  Search the folder" );
            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( "" );
            System.out.println( "---------------------------------------------------" );
            System.out.println( "" );

            //----------
            // Logon to specified server
            //----------
            use_default_values = argv.length == 6;
            server   = argv[0];
            port     = Integer.parseInt(argv[1]);
            userid   = argv[2];
            password = argv[3];
            folder   = argv[4];
            maxHits  = Integer.parseInt(argv[5]);
            crit      = argv[6];
            operator  = argv[7];
            value1    = argv[8];
            value2    = argv[9];

            ODConfig odConfig = new ODConfig();
            if(odConfig != null)
            {
                odServer = new ODServer(odConfig );
                odServer.initialize(  "TcSearch.java" );
                odServer.setPort(port);
                System.out.println( "Logging on to " + server  + " server with user " + userid + "..." );
                odServer.logon( server, userid, password);

                //----------
                // Open the specified folder
                //----------
                System.out.println( "Opening " + folder + " folder..." );
                odFolder = odServer.openFolder( folder );

                System.out.println( "Name='" + odFolder.getName( ) + "' Desc='" + odFolder.getDescription( ) + "'" );

                //----------
                // If we are not using the default search values:
                //----------
                if ( !use_default_values )
                {
                    //----------
                    // Find the requested criteria
                    //----------
                    System.out.println( "Getting " + crit + " criteria..." );
                    odCrit = odFolder.getCriteria( crit );
                    if ( odCrit == null )
                    {                    
                        System.out.println( "  *** " + crit + " criteria does not exist - " );
                        System.out.println( "  NullPointerException will be reported" );
                    }
                    //----------
                    // Convert the operator parameter to the internal operator value and set
                    // the criteria operator
                    //----------
                    System.out.println( "Setting operator to " + operator + "..." );
                    if ( operator.equals( "eq" ) )
                        opr = ODConstant.OPEqual;
                    else if ( operator.equals( "ne" ) )
                        opr = ODConstant.OPNotEqual;
                    else if ( operator.equals( "lt" ) )
                        opr = ODConstant.OPLessThan;
                    else if ( operator.equals( "le" ) )
                        opr = ODConstant.OPLessThanEqual;
                    else if ( operator.equals( "gt" ) )
                        opr = ODConstant.OPGreaterThan;
                    else if ( operator.equals( "ge" ) )
                        opr = ODConstant.OPGreaterThanEqual;
                    else if ( operator.equals( "in" ) )
                        opr = ODConstant.OPIn;
                    else if ( operator.equals( "ni" ) )
                        opr = ODConstant.OPNotIn;
                    else if ( operator.equals( "li" ) )
                        opr = ODConstant.OPLike;
                    else if ( operator.equals( "nl" ) )
                        opr = ODConstant.OPNotLike;
                    else if ( operator.equals( "be" ) )
                        opr = ODConstant.OPBetween;
                    else if ( operator.equals( "nb" ) )
                        opr = ODConstant.OPNotBetween;
                    else
                        opr = -1;

                    System.out.println( "Setting operand(s)..." );
                    odCrit.setOperator( opr );

                    //----------
                    // Set the search values
                    //----------
                    if ( opr == ODConstant.OPBetween || opr == ODConstant.OPNotBetween )
                    {
                        odCrit.setSearchValues( value1, value2 );
                        System.out.println( "  " + odCrit.getName( ) + " " + getOperatorName( opr ) + " " + value1);
                        System.out.println( " and " + value2 );
                    }
                    else
                    {
                        odCrit.setSearchValue( value1 );
                        System.out.println( "  " + odCrit.getName( ) + " " + getOperatorName( opr ) + " " + value1 );
                    }
                }

                //----------
                // Set Max Hits limit if specified
                //----------
                if (maxHits != -1)
                {
                    System.out.println("Setting MaxHITS to " + maxHits + ".");
                    odFolder.setMaxHits(maxHits);
                }
                else
                {
                    System.out.println("No MaxHits passed in. Will use the MaxHits in ODConfig, ");
                    System.out.println("or the Folder defined max, whichever is less.");
                }

                //----------
                // Search the folder
                //----------
                System.out.println( "  Searching " + folder );
                System.out.println( ( use_default_values ? " using default values" : "" ) + "..." );

                long startTime  = System.currentTimeMillis();
                hits = odFolder.search( );
                long estimatedTime = System.currentTimeMillis() - startTime;
                System.out.println( "Elapsed Search Time: " +  estimatedTime + " ms");
                System.out.println( "    Search message: " + odFolder.getSearchMessage( ) );
                System.out.println( "    Number of hits: " + hits.size( ) );

                //----------
                // Display the hits
                //----------
                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 TcSearch 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;
    }
}