/*
 * @copyright(disclaimer)                                                     
 *                                                                            *
 * DISCLAIMER OF WARRANTIES.                                                  *
 *                                                                            *
 * The following IBM Content Manager Enterprise Edition code is sample code   *
 * created by IBM Corporation. IBM grants you a nonexclusive copyright        *
 * license to use this sample code example to generate similar function       *
 * tailored to your own specific needs. This sample code is not part of any   *
 * standard IBM product and is provided to you solely for the purpose of      *
 * assisting you in the development of your applications. This example has    *
 * not been thoroughly tested under all conditions. IBM, therefore cannot     *
 * guarantee nor may you imply reliability, serviceability, or function of    *
 * these programs. The code is provided "AS IS", without warranty of any      *
 * kind. IBM shall not be liable for any damages arising out of your or any   *
 * other parties use of the sample code, even if IBM has been advised of the  *
 * possibility of such damages. If you do not agree with these terms, do not  *
 * use the sample code.                                                       *
 *                                                                            *
 * Licensed Materials - Property of IBM                                       *
 * 5724-B19, 5697-H60                                                         *
 * � Copyright IBM Corp. 1994, 2013 All Rights Reserved.                      *
 *                                                                            *
 * US Government Users Restricted Rights - Use, duplication or disclosure     *
 * restricted by GSA ADP Schedule Contract with IBM Corp.                     *
 *                                                                            *
 * @endCopyright                                                              
 */

// sample connect/execute/disconnect to IP

/*
 * Filename       : TCallbackExecuteIP.java
 * Description    : Connect to IP Datastore.
 *                  Create query string.
 *                  Specify to retrieve a max result of 5 items.
 *                  Get the result in a resultset cursor.
 *                  Loop thru results.
 *                  Display documents found and their parts and attributes.
 *                  Disconnect from IP Datastore.
 */

import com.ibm.mm.sdk.common.*;
import com.ibm.mm.sdk.server.*;
import java.io.*;
import java.lang.*;
import java.util.*;

// TCallbackExecuteIP class
public class TCallbackExecuteIP implements DKConstantIP
{


  // Main method
  public static void main(String argv[])
  {

   // Indicate this is a Console Application on Windows NT/95 for error
   // message display in dos console code page. If this was a Windows
   // GUI (Graphic User Interface) Application this would not be necessary
   // because DK_CM_SS_WINDOWS is the default.
   DKEnvironment.setSubSystem(DK_CM_SS_CONSOLE);

    DKDatastoreIP dsIP = null;
    try {
      //*** for input parameters
      String libSrv = new String(""),   // library server
             userid = new String(""),     //userid
         pw = new String(""); // pw

      //--------------------------------------------------------------
      // Checking for input parameters
      //--------------------------------------------------------------
      if (argv.length < 3)
      {
       System.out.println("Invoke Syntax: " );
       System.out.println("  java TCallbackExecuteIP " +
        "<libSrv> <userid> <pw>" );
       return;
      }
      else
      {
       libSrv = argv[0];
       userid = argv[1];
       pw = argv[2];
      }

    dsIP = new DKDatastoreIP();
    dkResultSetCursor pCur = null;
    DKNVPair parms[] = null;

    String s = new String( "APPL=01;FAFIP=9.67.43.83;"
                              + "IODMIP=9.67.43.83;FAFPORT=3061;"
                              + "IODMPORT=3082;FAFPROT=4000;IODMPROT=4000;"
                              + "FAFSITE=CS61" );

    System.out.println("connecting to datastore");
    dsIP.connect(libSrv,userid,pw,s);
    System.out.println("datastore connected libSrv " + libSrv + " userid " +
                        userid );
    // prepare the parametric query string
    String cmd = "SEARCH=(ENTITY=DOCUMENT," +
                 "MAX_RESULTS=5," +
                 "COND=(FolderId == \"IPSAMP\") );" +
                 "OPTION=(CONTENT=YES)";

    TCallbackIP cbkIP = new TCallbackIP();
    InputStreamReader isreader = new InputStreamReader(System.in);

    System.out.println("query string " + cmd);
    System.out.println("executing query");
    Date date;
    date  = new Date();
    System.out.println("before query time " + date.toString());
    dsIP.executeWithCallback(cmd,DK_CM_PARAMETRIC_QL_TYPE,parms,(dkCallback)cbkIP);
    System.out.println("datastore executed query");

    boolean isDone = false;
    DKException ckExc = null;
    while (isDone == false)
    {
     System.out.println("press <enter> to continue...");
     isreader.read();
     isDone = cbkIP.isDone();
    }

    ckExc = cbkIP.getException();
    if (ckExc != null)
    {
     System.out.println("Exception name " + ckExc.name());
     System.out.println("Exception message " + ckExc.getMessage());
     System.out.println("Exception error code " + ckExc.errorCode());
     System.out.println("Exception error state " + ckExc.errorState());
     ckExc.printStackTrace();
     cbkIP.resetException(); // reset since it is not needed any more
    }    

    System.out.println("query results processed");
    dsIP.disconnect();
    System.out.println("datastore disconnected");
    }
    catch (DKException exc)
    {
     System.out.println("Exception name " + exc.name());
     System.out.println("Exception message " + exc.getMessage());
     System.out.println("Exception error code " + exc.errorCode());
     System.out.println("Exception error state " + exc.errorState());
     exc.printStackTrace();
    }
    catch (Exception exc)
    {
     System.out.println("Exception message " + exc.getMessage());
     exc.printStackTrace();
    }

  }
}