Updating a document

You use methods from the ODServer, ODFolder, ODHit, and ODCriteria classes to find specific documents in a folder, retrieve those documents, and update database values.

About this task

The following example demonstrates how to update a document.

This example uses ODServer, ODFolder, and ODCriteria methods to do the following tasks:
  • connect to a server using the specified user ID and password
  • Open the specified folder
  • Set the search values for two search fields
  • Set the Date search field to null
  • Search the folder
For the document that matches the query, ODHit methods are then used to update one or more database values.
This example demonstrates the following ODServer methods:
  • initialize
  • logon
  • openFolder
  • logoff
  • terminate
This example demonstrates the following ODFolder methods:
  • getDisplayOrder
  • getCriteria
  • search
  • close
This example demonstrates the following ODCriteria methods:
  • setOperator
This example demonstrates the following ODHit methods:
  • getDisplayValue
  • getDocId
  • updateValuesForHit
This example uses the following runtime parameters:
  • Server name
  • Port
  • User Id
  • Password
  • Folder name
  • Criteria name 1
  • Search value 1
  • Criteria name 2
  • Search value 2
  • New search value to replace search value 2
Example of updating a document:
import java.util.*;
import java.io.*;
import com.ibm.edms.od.*;

public class TcUpdate
{
  public static void main ( String argv[] )
  {
    ODServer odServer;
    ODFolder odFolder;
    ODCriteria odCrit, odCrit2;
    String criteria1;
    String criteria2;
    String search_value1;
    String search_value2;
    String replacement_value;

    //----------
    // If too few parameters, display syntax and get out
    //----------
    if ( argv.length < 9 )
    {
            System.out.println( "usage: java TcUpdate <server> <port> <userid> <password> <folder> 
                                   <criteria1> <value1> <criteria2> <value2> <new value2>" );
      return;
    }

    try
    {
      System.out.println( "Testcase TcUpdate 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( "  Set the search values" );
      System.out.println( "  Search the folder" );
      System.out.println( "  For the first hit, change the value of criteria 2 to the new value" );
      System.out.println( "  Set the search values" );
      System.out.println( "  Change the value of criteria 2 to the original value" );
      System.out.println( "" );
      System.out.println( "---------------------------------------------------" );
      System.out.println( "" );


      //----------
      //Setup ODConfig object using defaults and initialize ODServer.
      //----------
      ODConfig odConfig = new ODConfig();
      if(odConfig != null)
      {
         odServer = new ODServer(odConfig);
         odServer.initialize( "TcUpdate.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 set the requested criteria
         //----------
         criteria1 = argv[5];
         criteria2 = argv[7];
         search_value1 = argv[6];
         search_value2 = argv[8];
         replacement_value = argv[9];
         System.out.println( "Opening " + argv[4] + " folder..." );
         odFolder = odServer.openFolder( argv[4] );

         ArrayList allCrit =odFolder.getCriteriaSortOrder();
         /*Clear all default search values*/
         for (int a =0; a < allCrit.size(); a++)
         {
             odCrit = (ODCriteria)allCrit.get(a);
             odCrit.setSearchValues("","");
         }
         odCrit = odFolder.getCriteria( criteria1 );
         if(odCrit == null)
	        System.out.println("ERROR: Search Criteria Not Found [" + criteria1 + "]");
         else
         {
            odCrit.setOperator( ODConstant.OPEqual );
            odCrit.setSearchValue( search_value1 );
         }
         odCrit2 = odFolder.getCriteria( criteria2 );
         if(odCrit2 == null)
           System.out.println("ERROR: Search Criteria Not Found [" + criteria2 + "]");
         else
         {
            odCrit2.setOperator( ODConstant.OPEqual );
            odCrit2.setSearchValue( search_value2 );
         }
         //----------
         // Search the folder
         //----------
         if (odCrit != null && odCrit2 != null)
         {
                System.out.println( "Change " + criteria2 + " from original value " + search_value2 +
                                            " to " + replacement_value  );

                UpdateHits(odFolder, criteria1, search_value1, criteria2, search_value2, replacement_value);

                //
                //----------
                // Change values back to original
                //----------
                System.out.println( "Change " + criteria2 + " from new value " + replacement_value +
                                            " to "  + search_value2  );
                odCrit2.setSearchValue(replacement_value);

                UpdateHits(odFolder, criteria1, search_value1, criteria2, replacement_value, search_value2);

         }
         //----------
         // Cleanup
         //----------
         odFolder.close( );
         odServer.logoff( );
         odServer.terminate( );
         System.out.println( "" );
         System.out.println( "---------------------------------------------------" );
         System.out.println( "" );
         System.out.println( "Testcase TcUpdate completed," );
         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( );
    }
  }

  public static void UpdateHits(ODFolder odFolder1, String crit1, String value1, String crit2, 
                                   String value2, String new_value)
  {
      ODHit odHit;
      Hashtable hash;
      Vector hits;
      String[] display_crit;
      String line;
      int j;

      try
      {
             System.out.println( "  Searching for " + crit1 + " = " + value1 + " and " + crit2 + " = " + value2 + "..." );
             hits = odFolder1.search( );

            //----------
            // If there was at least one hit
            //----------
            if ( hits != null && hits.size( ) > 0 )
            {
              //----------
              // Display the values for the first hit
              //----------
              System.out.println( "    For first hit:" );
              odHit = (ODHit)hits.elementAt( 0 );
              System.out.println( "      DOCID = "+ odHit.getDocId() );
              System.out.println("      Application Group Name = " + odHit.getApplGrpName());
              line = "      ";
              display_crit = odFolder1.getDisplayOrder( );
              for( j = 0; j < display_crit.length; j++ )
                line = line + display_crit[j] + " ";
              System.out.println( line );
              line = "      ";
              for ( j = 0; j < display_crit.length; j++ )
                line = line + odHit.getDisplayValue( display_crit[j] ) + " ";
              System.out.println( line );

              //----------
              // Create a hash table of existing critera/value pairs, except for critera 2
	           // which will be set to the new value. Update the hit values
              //----------
              System.out.println( "    Replacing " + crit2 + " = " + value2 + " with " + crit2 + " = " + new_value );
              hash = new Hashtable( );
              for ( j = 0; j < display_crit.length; j++ )
              {
                if ( display_crit[j].equals( crit2 ) )
		             hash.put( display_crit[j], new_value );
		          else
		          hash.put( display_crit[j], odHit.getDisplayValue( display_crit[j] ) );
              }
              odHit.updateValuesForHit( hash );
              System.out.println("New display values as set in ODHit");
              odHit = (ODHit)hits.elementAt( 0 );
              line = "      ";
              System.out.println( "      DOCID = "+ odHit.getDocId() );
              display_crit = odFolder1.getDisplayOrder( );
              for( j = 0; j < display_crit.length; j++ )
                 line = line + display_crit[j] + " ";
              System.out.println( line );
              line = "      ";
              for ( j = 0; j < display_crit.length; j++ )
                  line = line + odHit.getDisplayValue( display_crit[j] ) + " ";
              System.out.println( line );
            }
	         else
              System.out.println( "There were no hits" );
      }

      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( );
      }
  }
}