package com.mahindrabt.lsm.registration;

import java.rmi.RemoteException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.naming.directory.*;
import javax.naming.*;
import java.sql.SQLException;
import java.util.Hashtable;


/** this is a Bean of the Login data entity bean
  * which will do the LDAP writing and reading  for the attributes of
  * login userid and password
  * @ author  CSI
  */

public class LDAPBean implements EntityBean
{

   public String uid = "";
   public String password = "";
   private static   String MY_SEARCHBASE ="ou=LAMCOU,o=mahindrabt.com";
   private static	String MY_FILTER = "";
   private EntityContext ctx;
   private final String MGR_DN = "cn=Directory Manager";
   private final String MGR_PW = "adminadmin";
   private final String MGR_URL = "ldap://10.5.3.43:389";
   private final String MGR_CTX ="com.netscape.jndi.ldap.LdapContextFactory";
   private DirContext initctx;

  public String getPassword()
  {
  	System.out.println("in getPassword() method ");
  	return password;
  }// end of getPassword() method
  public void setPassword(String password)
  {
  	System.out.println("in setPassword() method   ");
  	this.password = password;
  }// end of setPassword() method

    public  String ejbCreate(String uid, String password) throws CreateException
  {
  	System.out.println("in ejbCreate() method ");
	String dn = "lsmapplicationuserid="+ uid +", ou = LAMCOU, o = mahindrabt.com";
	System.out.println("distinguished name is"+ dn);
	this.uid = uid;
	this.password = password;
	DirContext initctx = null;
	System.out.println("reached DirContext assignment");
	try
	{
		System.out.println(" inside try of ejbCreate() method");
		initctx= getConnection();
		System.out.println("getConnection() method executed");
		Attribute objclasses = new BasicAttribute("objectclass");
		objclasses.add("lsmregobject");
		Attribute lsmpassword = new BasicAttribute("lsmpassword",password);
		Attributes orig = new BasicAttributes();
		orig.put(objclasses);
		orig.put(lsmpassword);
		initctx.createSubcontext(dn,orig);
	}
	catch(Exception e)
	{
		System.out.println("in ejbCreate() exception occured ");
		e.printStackTrace();
	}
	return uid;
  }//end of ejbCreate() method

  public void ejbPostCreate(String uid,String password)
  {
  	System.out.println("in ejbpostcreate() method");
  }//end of ejbPostCreate() method

  public void ejbActivate()
  {
  	System.out.println("in ejbActivate() method");
  }//end of ejbActivate() method

  public void ejbPassivate()
  {
  	System.out.println("in ejbPassivate() method");
  }//end of ejbPassivate() method

  public void ejbLoad()
  {
  	System.out.println("in ejbLoad() method");
	this.uid = (String) ctx.getPrimaryKey();
	String dn="lsmapplicationuserid=" + uid + ",ou = LAMCOU,o= mahindrabt.com";
	System.out.println("in ejbLoad() :" + uid);
	DirContext initctx = null;
	try
	{
		System.out.println(" inside  try  of ejbLoad() method");
		initctx = getConnection();
		Attributes attrs = initctx.getAttributes(dn);
		this.password=(String)attrs.get("lsmpassword").get();
		System.out.println("in ejbLoad() : "+password);
	}
	catch(Exception e)
	{
		System.out.println("in ejbLoad exception");
		e.printStackTrace();
	}
  }//end of ejbLoad() method

	public void ejbRemove()
	{
		System.out.println("in ejbRemove() method");
	}// end of ejbRemove() method

	public void ejbStore()
	{
		System.out.println("in ejbStore() method");
		String dn="lsmapplicationuserid="+uid+",ou = LAMCOU,o= mahindrabt.com";
	    DirContext initctx = null;
		try
		{   System.out.println("in ejbStore() try");
			initctx = getConnection();
			System.out.println("connection object found");

			ModificationItem[] mods= new ModificationItem[1];
			Attribute mod1 =new BasicAttribute("lsmpassword", password);
			System.out.println("new atrribute for password created ");

			mods[0] = new ModificationItem(initctx.REPLACE_ATTRIBUTE,mod1);
			System.out.println("password replace happened ");
			initctx.modifyAttributes(dn,mods);
			System.out.println("modify attributes successful ");
		}
		catch(Exception e)
		{
			System.out.println("in ejbStore() exception");
			e.printStackTrace();
		}
	}//end of ejbStore() method

	public void unsetEntityContext()
	{
		System.out.println("in unsetEntityContext() method");
		this.ctx = null;
	}//end of unsetEntityContext() method

	public void setEntityContext(EntityContext ctx)
	{
		System.out.println("in setEntityContext() method");
		this.ctx = ctx;
	}//end of setEntityContext() method

	public String ejbFindByPrimaryKey(String uid) //throws ObjectNotFoundException
	{

	try
		{
		    System.out.println(" in ejbFindByPrimaryKey() method ");
			return uid;
		}
		catch(Exception e)
		{   System.out.println(" in ejbFindByPrimaryKey Exception ");
			throw new EJBException (e);
		}
	}//end of ejbFindByPrimaryKey() method

	private DirContext getConnection()
	{
		try
		{
			System.out.println(" inside try of getConnection method");
			Hashtable env = new Hashtable();
			env.put(Context.INITIAL_CONTEXT_FACTORY,MGR_CTX);
			env.put(Context.PROVIDER_URL,MGR_URL);
			env.put(Context.SECURITY_PRINCIPAL,MGR_DN);
			env.put(Context.SECURITY_CREDENTIALS,MGR_PW);
			if(initctx == null)
				initctx = new InitialDirContext(env);
			System.out.println("dircontext got");

		}
		catch(Exception e)
		{
			System.out.println("error occured in getConnection() method");
			e.printStackTrace();

		}
	return initctx;

	}// end of getConnection() method
}


