package examples.extending;

/*                                         
 *  T H E                                     Copyright (c) The Theory Center,
 *                                            Inc. 1997-1999. All Rights Re-
 * |||||  ||                                  served. Unpublished rights re-
 *   ||   ||                                  served under the copyright laws
 *   ||   ||||    |||    |||   |||||  |  ||   of the United States. The soft-
 *   ||   ||  |  |   |  ||  |  ||  |  |  ||   ware contained on this media is
 *   ||   ||  |  |||||  ||  |  ||     |  ||   proprietary to and embodies the
 *   ||   ||  |  |      ||  |  ||      ||||   confidential technology of
 *   ||   ||  |   |||    |||   ||         |   The Theory Center. The possession
 *                                     ||||   or receipt of this information
 *                                            does not convey any right to dis-
 *  |||                   ||                  close its contents, reproduce it,
 * ||  |                  ||                  or use,  or license the use, for
 * ||      |||   ||||    ||||   |||   |||||   manufacture or sale, the informa-
 * ||     |   |  ||  |    ||   |   |  ||  |   tion or anything described
 * ||     |||||  ||  |    ||   |||||  ||      therein. Any use, disclosure, or
 * ||  |  |      ||  |    ||   |      ||      reproduction without The Theory
 *  |||    |||   ||  |    ||    |||   ||      Center's prior written permission
 *                                            is strictly prohibited.
 *
 *
 * $JustDate:  8/03/99 $ $Revision: 12 $
 */

import java.rmi.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import theory.smart.axiom.units.*;
import theory.smart.axiom.util.*;
import theory.smart.axiom.messaging.*;
import theory.smart.axiom.contact.*;
import theory.smart.axiom.workflow.*;
import theory.smart.axiom.accounting.*;

/**
 * This example demonstrates extending a Business Smart Component.
 *
 * For this example, we have taken the
 * theory.smart.axiom.util.AlphaNumericSequencer eBSC and extended it so it
 * produces the sequencer string differently.  On the new bean, the
 * counter portion of the sequencer string is in hexadecimal notation.
 * Pay careful attention to what class or interface each of the four
 * class files (interface, home, impl) extends.
 *
 * @see   theory.smart.axiom.util.AlphaNumericSequencer
 * @see   examples.extending.AlphaNumericSequencerExtension
 * @see   theory.smart.ebusiness.item.Item
 *  */
public class ExtendingExample
{
   static Context ctx = null;
   static String user = null;
   static String password = null;
   static String url = "t3://localhost:7501";

   public static final String HOME_NAME = "examples.extending.AlphaNumericSequencerExtension";

   /**
	* ExtendingExample constructor.
	*/
   public ExtendingExample()
   {
	  super();
   }   
   /**
	* Exit program, displaying the exception
	*/
   public void exitExample(String message, Exception e)
   {
	  if (message != null)
	  {
		 System.out.println("Example exited: " + message);
	  }
	  if (e != null)
	  {
		 System.out.println("Exception thrown: " + e);
		 e.printStackTrace(System.out);
	  }
   }   
   /**
	* If the sequencer already exists in DB, this method gets it, otherwise it creates
	* a new sequencer in DB.
	*
	* @param	sequencerKey java.lang.String
	* @return	examples.extending.AlphaNumericSequencerExtension
	*/
   public AlphaNumericSequencerExtension findOrCreateSequencer(String sequencerKey)
   {
	  AlphaNumericSequencerExtensionHome sequencerEHome = null;
	  AlphaNumericSequencerExtension sequencerE = null;
	  
	  // get sequencer, or create it if it does no exist
	  AlphaNumericSequencerExtensionPk sequencerEPk = new AlphaNumericSequencerExtensionPk(sequencerKey);
	  try
	  {
		 // if sequencer exists, get it.
		 sequencerEHome = (AlphaNumericSequencerExtensionHome) getInitialContext().lookup(HOME_NAME);
		 sequencerE = (AlphaNumericSequencerExtension) sequencerEHome.findByPrimaryKey(sequencerEPk);
		 System.out.println("Found sequencer " + sequencerKey + ", counter is at: " + sequencerE.getCounter());
		 System.out.println("");
	  }
	  catch (FinderException fe)
	  {
		 try
		 {
	    // not in the database, so create a new one.
			System.out.println("Creating sequencer <" + sequencerKey + ">");
	    sequencerE = sequencerEHome.create(sequencerEPk);
			// set the prefix, suffix, step
			sequencerE.setPrefix("hex-");
	    sequencerE.setSuffix("-extendedSequencer");
	    sequencerE.setStep(64);
		 }
		 catch (CreateException ce)
		 {
	    exitExample("Failed to create a sequencer", ce);
		 }
		 catch (RemoteException re)
		 {
	    exitExample("Remote Exception is thrown", re);
		 }
	  }
	  catch (Exception e)
	  {
		 exitExample("General Exception thrown", e);
	  }
	  
	  return sequencerE;
   }      
   /**
	* Gets an initial context for the current user, password and url.
	*
	* @return		Context
	* @exception	java.lang.Exception if there is
	*              an error in getting the Contex
	*/
   static public Context getInitialContext() throws Exception
   {
	  if (ctx == null)
	  {
		 Properties p = new Properties();
		 p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
		 p.put(Context.PROVIDER_URL, url);
		 if (user != null)
		 {
	    p.put(Context.SECURITY_PRINCIPAL, user);
	    if (password == null)
			   password = "";
	    p.put(Context.SECURITY_CREDENTIALS, password);
		 }
		 ctx = new InitialContext(p);
	  }
	  return ctx;
   }   
   public static void main(String argv[])
   {
	  System.out.println("----------ExtendingExample running---------");
	  System.out.println("");
	  String key = null;
	  if ((argv.length > 0) && (argv[0] != null))
	  {
		 key = argv[0];
	  } else {
		 key = "ExtendingExampleSequencer";
	  }
	  
	  new ExtendingExample().run(key);
   }         
   /**
	* gets a sequencer and displays a 10 of the strings in a row.
	*
	* @param	argv java.lang.String
	*/
   public boolean run(String sequencerKey)
   {
	  System.out.println("Extending example is using sequencer key: " + sequencerKey);
	  AlphaNumericSequencerExtension seq = findOrCreateSequencer(sequencerKey);
	  
	  //got a sequencer - now display a few
	  for (int i = 0; i < 10; i++)
	  {
		 try
		 {
			String s = seq.getNextValue();
			System.out.println("The next sequence string is: " + s);
		 }
		 catch (RemoteException re)
		 {
			exitExample("Failed to get next value of the sequencer", re);
		 }
	  }
	  System.out.println("");
	  

	  System.out.println("Example exits normally");
	  return true;
   }            
}