/*
 * @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                                                              
 */

/*
 * Description    : Execute a parametric query against an ImagePlus/390 server
 *                  Search for documents in a folder IPSAMP.
 *
 */

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

public class TClientExecuteIP 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);

      if (argv.length < 3)
      {
         System.out.println("Invoke Syntax: " );
         System.out.println("  java TClientExecuteIP <libsrv> <userid> <pw>" );
         return;
      }

      String libSrv = argv[0];
      String userid = argv[1];
      String pw     = argv[2];

      DKDatastoreIP dsIP = null;
      try
      {
         System.out.println(" *** ImagePlus Connector *** ");
         dsIP = new DKDatastoreIP();
         System.out.println("---Connecting to ImagePlus server---");

         String connectString = new String("APPL=01;FAFIP=9.67.43.83;IODMIP=9.67.43.83;FAFPORT=3061;IODMPORT=3082;FAFPROT=4000;IODMPROT=4000;FAFSITE=CS61");
         dsIP.connect(libSrv, userid, pw, connectString);
         System.out.println("datastore ImagePlus connected");

         String pcmd = null;
         DKDDO pDDO = null;
         int i = 0;
         short j = 0;
         short k = 0;
         int resultCounter = 0;
         int numDataItems = 0;
         Object a = null;
         dkCollection pCol = null;
         String strDataName;
         dkIterator pIter = null;
         dkDataObjectBase pDO = null;
         dkXDO pXDO = null;
         DKPidXDO pidXDO = null;
         String pidString;
         DKPidXDOIP pidXDOIP = null;

         // prepare the parametric query string
         pcmd = "SEARCH=( ENTITY=DOCUMENT,"
                       + "MAX_RESULTS=10,"
                       + "COND=(FolderId == \"IPSAMP\") );"
              + "OPTION=(CONTENT=YES)";

         DKParametricQuery pq = (DKParametricQuery) dsIP.createQuery( pcmd
                                                                  , DK_PARAMETRIC_QL_TYPE
                                                                  , null);
         System.out.println("---Executing query against ImagePlus server---");
         System.out.println(" parametric query string " + pq.queryString() );


         //do PQ only:
         DKResultSetCursorIP resCur = (DKResultSetCursorIP)dsIP.execute( pq );
         for ( pDDO = resCur.fetchNext()
             ; resCur.isValid() && !resCur.isEnd()
             ; pDDO = resCur.fetchNext() )
         {
            if (pDDO != null)
            {
               // pDDO.retrieve();  // used when CONTENT=NO
            }
            System.out.println("=====> Item " + ++resultCounter + " <=====");
            i++;
            numDataItems = pDDO.dataCount();
            k = pDDO.propertyId(DK_PROPERTY_ITEM_TYPE);
            if (k > 0)
            {
               Short sVal = (Short)pDDO.getProperty(k);
               j = sVal.shortValue();
               switch (j)
               {
                  case DK_DOCUMENT :
                  {
                     System.out.println("Item is document ");
                     break;
                  }
                  case DK_FOLDER :
                  {
                     System.out.println("Item is folder ");
                     break;
                  }
               }  // end switch...
            }  // end valid property

            System.out.println("Pid                  : " + pDDO.getPidObject().getPrimaryId());
            System.out.println("Number of Data Items : " + numDataItems);
            for (j = 1; j <= numDataItems; j++)
            {
               a = pDDO.getData(j);
               strDataName = pDDO.getDataName(j);
               System.out.println(j + ": Attribute Name   : " + strDataName );

               if (a instanceof Integer)
               {
                  System.out.println("   Integer Value    : " + a);
               }
               else if (a instanceof Short)
               {
                  System.out.println("   Short Value      : " + a);
               }
               else if (a instanceof DKTimestamp)
               {
                  System.out.println("   Timestamp value  : " + a);
               }
               else if (a instanceof DKDate)
               {
                  System.out.println("   Date value       : " + a);
               }
               else if (a instanceof String)
               {
                  System.out.println("   String Value     : " + a);
               }
               else if (a instanceof dkCollection)
               {
                  System.out.println("   Collection Values: ");
                  pCol = (dkCollection)a;
                  pIter = pCol.createIterator();
                  i = 0;
                  while (pIter.more() == true)
                  {
                     i++;
                     a = pIter.next();
                     pDO = (dkDataObjectBase)a;
                     if (pDO.protocol() == DK_PDDO)
                     {
                        System.out.println( "    " + i + ": DKDDO primary id  = "
                                          + ((DKDDO)pDO).getPid().getPrimaryId());
                     }
                     else if (pDO.protocol() == DK_XDO)
                     {
                        pXDO = (dkXDO)pDO;

                        pidXDOIP = (DKPidXDOIP)pXDO.getPidObject();
                        pidString = pidXDOIP.getPrimaryId();
                        System.out.println( "    " + i + ": DKXDOIP primary id = "
                                          + pidString );

                        pidXDOIP = (DKPidXDOIP) pXDO.getPidObject();
                        System.out.println( "                   Part # = "
                                          + pidXDOIP.getPartId() );

                        pidXDOIP = (DKPidXDOIP) pXDO.getPidObject();
                        System.out.println( "                Part Type = "
                                          + pXDO.getAffiliatedType() );

                        pXDO.retrieve();

                        if ( !pXDO.isNull() )
                        {
                           byte[] tmpIn;
                           tmpIn = new byte[3];
                           System.out.print("View the document? (y/n)");
                           System.in.read( tmpIn );
                           if (  tmpIn[0] == 'y'
                              || tmpIn[0] == 'Y' )
                           {
                              pXDO.open();
                           }
                        }
                     }
                  } // end while
               } // end if collection
               else if (a != null)
               {
                  System.out.println("   Attribute Value  : " + a.toString());
               }
               else
               {
                  System.out.println("   Attribute Value  : <null>");
               }
            }  // endfor
         }  // end forCursor
         resCur.destroy();

         dsIP.disconnect();
         System.out.println("datastore ImagePlus disconnected");
         dsIP.destroy();
      }
      catch (DKException exc)
      {
         try
         {
            if ( dsIP != null )
               dsIP.destroy();
         }
         catch (Exception e)
     	  {
     	     e.printStackTrace();
     	  }
         System.out.println("Exception name " + exc.name());
         System.out.println("Exception message " + exc.getMessage());
         exc.printStackTrace();
      }
      catch (Exception exc)
      {
         try
         {
            if ( dsIP != null )
               dsIP.destroy();
      	  }
     	  catch (Exception e)
     	  {
     	     e.printStackTrace();
      	  }
         System.out.println("Exception message " + exc.getMessage());
         exc.printStackTrace();
      }
   }
}