package com.ibm.websphere.userprofile.beans;

import com.ibm.servlet.personalization.userprofile.UserProfileKey;

//import com.ibm.websphere.userprofile.beans.Legacy;
/******************************************************************
*                                                                                                                                  *
* This is an example of extending the base userprofile EJB.                                            *
*                                                                                                                                  *
* In this example, you use our base demographic properties                                          *
* and add some columns which are defined in you legacy database                                *
* table. You then define an EJB to access that columns.                                                 *
*                                                                                                                                  *
* For instance, if you have columns for "primaryKey" and                                              *
* "cellPhone" in your legacy database table, then define an                                             *
* EJB named"Legacy" which maps to those fields.                                                         *
*                                                                                                                                  *
*******************************************************************/

public class UPBaseChildBean extends com.ibm.servlet.personalization.userprofile.UPBaseBean {

    //Define a reference to the Legacy Bean
    protected Legacy legacy;
 

    //Override ejbCreate
    public void ejbCreate(String primaryKey) {
        this.userName=primaryKey;
        this.legacyPrimayKey=primaryKey;

        //Create row in the legacy database
        legacy = getBeanReference(true);

        //if(legacy == null)
         //   throw new javax.ejb.CreateException("Already Exsists");
    }
 
 

    public void ejbPassivate() throws java.rmi.RemoteException{
        legacyPrimayKey = null;
        legacy = null;
        super.ejbPassivate();
    }
 

    public void ejbRemove() throws java.rmi.RemoteException{
        try {

            legacy = getBeanReference(false);
            legacy.remove();
            legacy = null;
            super.ejbRemove();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
 
 

    public void ejbActivate() throws java.rmi.RemoteException{
        UserProfileKey key =    (UserProfileKey) entityContext.getPrimaryKey();
        userName = key.userName;
        legacyPrimayKey = userName;
    }
 
 

    //Methods mapping to legacy table(bean)
    public String getCellPhone() {

        legacy = getBeanReference(false);

        try {

            return legacy.getCellPhone();
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 

    public void setCellPhone(String cell) {

        legacy = getBeanReference(false);

        try {
            legacy.setCellPhone(cell);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
 

    //create: true   for creating
    //        false for finding the existing

    private Legacy getBeanReference(boolean create) {
        if(legacy != null)
            return legacy;

        if(legacyPrimayKey == null)
            legacyPrimayKey = userName;

        try {

            java.util.Properties p = new java.util.Properties();
            p.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,"com.ibm.ejs.ns.jndi.CNInitialContextFactory");

            //If your legacy EJB representing Userprofile is residing on
            //different host, specify the provider URL while getting IntialContext.
            //p.put(java.naming.provider.url,"iiop://legacyhost.com:900");
            javax.naming.InitialContext ic = new javax.naming.InitialContext();
            Object obj =  ic.lookup("LegacyHome");

            if(obj != null) {
                LegacyHome legacyHome = (LegacyHome) javax.rmi.PortableRemoteObject.narrow(obj,LegacyHome.class);
                if(create)
                    legacy =   legacyHome.create(new LegacyKey(legacyPrimayKey));
                else
                    legacy = legacyHome.findByPrimaryKey(new LegacyKey(legacyPrimayKey));
            }
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }

        return legacy;

    }

}