//****************************************************************************************
// File:          PLSQLCommands.java
// Description:   Demo of PLSQL Commands with Java Access Provider
//                Describes how to execute basic blocks of PL/SQL code
// Created by:    Kapila
//****************************************************************************************

import ifs.fnd.ap.Record;
import ifs.fnd.ap.RecordAttribute;
import ifs.fnd.ap.PlsqlCommand;
import ifs.fnd.ap.PlsqlBaseMethodCommand;
import ifs.fnd.ap.PlsqlBaseMethodAction;
import ifs.fnd.ap.PlsqlBaseMethodType;
import ifs.fnd.ap.Server;
import ifs.fnd.ap.APException;

public abstract class PLSQLCommands {
   private static Server srv;
   
   public static void main(String [] pars) {
      try {
         if(pars.length != 3) {
            System.out.println("Syntax : java PLSQLCommands [connectstring] [identity] [password]");
            System.out.println();
            return;
         }
         
         // Create a server
         srv = new Server();
         srv.setConnectionString(pars[0]);
         srv.setCredentials(pars[1], pars[2]);
         
         System.out.println();
         System.out.println("\n***** IFSAPP Description *****");
         GetUserDescription();
         System.out.println("\n***** Prepare Profile *****");
         PrepareProfile();
      } 
      catch(APException err) {
         err.printStackTrace(System.out);
      }
   }
   
   // This sample shows how to call a package method
   private static void GetUserDescription()  throws APException {
      PlsqlCommand cmd = new PlsqlCommand(srv, ":DESC := FND_USER_API.GET_DESCRIPTION(:IDENTITY)");
      cmd.getBindVariables().add("IDENTITY", "IFSAPP");
      cmd.getBindVariables().add("DESC", "");
      
      cmd.execute();
      
      System.out.println((String)cmd.getBindVariables().findValue("DESC"));
      System.out.println();
   }
   
   // This sample shows how to call a LU standard method "New Prepare"
   private static void PrepareProfile() throws APException {
      // The row needs to have OBJID, and OBJVERSION
      Record profile = new Record("PROFILE");
      profile.add("OBJID", "");
      profile.add("OBJVERSION", "");
      // I also add OWNER since I want to receive the prepared value
      profile.add("OWNER", "");
      
      PlsqlBaseMethodCommand cmd = new PlsqlBaseMethodCommand(srv, PlsqlBaseMethodType.NEW, "FNDRR_CLIENT_PROFILE_API", "New__", profile, PlsqlBaseMethodAction.PREPARE);
      
      cmd.execute();
      
      // Check prepared value OWNER
      RecordAttribute owner = profile.find("OWNER");
      if(owner!=null && owner.hasValue())
         System.out.println("OWNER set in Prepare_Insert to " + (String)owner.getValue());
      else
         System.out.println("OWNER set in Prepare_Insert to null");
      System.out.println();
   }
   
}