package com.oreilly.jent.transactions.cmtd;

/**
 * In general, you may use the code in this book in your programs and 
 * documentation. You do not need to contact us for permission unless 
 * you're reproducing a significant portion of the code. For example, 
 * writing a program that uses several chunks of code from this book does 
 * not require permission. Selling or distributing a CD-ROM of examples 
 * from O'Reilly books does require permission. Answering a question by 
 * citing this book and quoting example code does not require permission. 
 * Incorporating a significant amount of example code from this book into 
 * your product's documentation does require permission.
 * 
 * We appreciate, but do not require, attribution. An attribution usually 
 * includes the title, author, publisher, and ISBN. For example: 
 * 
 *   "Java Enterprise in a Nutshell, Third Edition, 
 *    by Jim Farley and William Crawford 
 *    with Prakash Malani, John G. Norman, and Justin Gehtland. 
 *    Copyright 2006 O'Reilly Media, Inc., 0-596-10142-2."
 *  
 *  If you feel your use of code examples falls outside fair use or the 
 *  permission given above, feel free to contact us at 
 *  permissions@oreilly.com.
 */

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;

import com.oreilly.jent.transactions.domain.Person;
import com.oreilly.jent.transactions.domain.PersonHome;
import com.oreilly.jent.transactions.domain.PersonStatus;
import com.oreilly.jent.transactions.domain.PersonStatusHome;
import com.oreilly.jent.transactions.domain.PersonStatusValue;
import com.oreilly.jent.transactions.domain.PersonValue;

public class ManagerCMTDBean implements SessionBean {
    protected static Logger sLogger = Logger.getLogger(ManagerCMTDBean.class.getName());
    protected SessionContext mSessionContext;

    public void ejbCreate() {

    }

    public PersonValue createPersonAndStatusNew(
        PersonValue aPersonValue,
        PersonStatusValue aPersonStatusValue) throws RemoteException {
        aPersonValue = 
            ((ManagerCMTD) mSessionContext.getEJBObject()).createPersonNewTransaction(
                aPersonValue);
        aPersonStatusValue = createPersonStatus(aPersonValue, 
                                                aPersonStatusValue);
        return aPersonValue;
    }


    public PersonValue createPersonAndStatus(
        PersonValue aPersonValue,
        PersonStatusValue aPersonStatusValue) {
        aPersonValue = createPersonNewTransaction(aPersonValue);
        aPersonStatusValue = createPersonStatus(aPersonValue, 
                                                aPersonStatusValue);
        return aPersonValue;
    }

    protected PersonStatusValue createPersonStatus(
        PersonValue aPersonValue,
        PersonStatusValue aPersonStatusValue
    ) {
        try {
             InitialContext aInitialContext = new InitialContext();
             PersonHome aPersonHome = (PersonHome) aInitialContext.lookup("java:comp/env/ejb/Person");
             Person aPerson = aPersonHome.findByPrimaryKey(aPersonValue.getPersonId());

             PersonStatusHome aPersonStatusHome = (PersonStatusHome) aInitialContext.lookup(
                 "java:comp/env/ejb/PersonStatus"
             );
             PersonStatus aPersonStatus = aPersonStatusHome.create(aPerson);
             aPersonStatus.setStatus(aPersonStatusValue.getStatus());
             aPersonStatus.setStatusComment(aPersonStatusValue.getStatusComment());

             aPersonStatusValue.setId(aPersonStatus.getPersonStatusId());

             aPersonValue.setPersonStatusValue(aPersonStatusValue);
             return aPersonStatusValue;
         } catch (Exception e) {
             String aMessage = "exception occured in manager cmtd bean. create person and status";
             sLogger.log(Level.SEVERE, aMessage, e);
             throw new EJBException (aMessage, e);
         }
    }


    public PersonValue createPersonNewTransaction(PersonValue aPersonValue) {
        return createPerson(aPersonValue);
    }

    public PersonValue createPersonSameTransaction(PersonValue aPersonValue) {
        return createPerson(aPersonValue);
    }

    protected PersonValue createPerson(PersonValue aPersonValue) {
        sLogger.info("5 creating person in manager cmtd bean ...");

        try {
            InitialContext aInitialContext = new InitialContext();
            PersonHome aPersonHome = (PersonHome) aInitialContext.lookup("java:comp/env/ejb/Person");
            Person p = aPersonHome.create(aPersonValue.getEmail(), aPersonValue.getPassword());
            p.setWorkPhone(aPersonValue.getWorkPhone());
            p.setHomePhone(aPersonValue.getHomePhone());

            sLogger.info("Created a person with id : " + p.getPersonId());
            aPersonValue.setPersonId(p.getPersonId());
        } catch (Exception e) {
            String aMessage = "exception occured in manager cmtd bean.createPersonNewTransaction ";
            sLogger.log(Level.SEVERE, aMessage, e);
            throw new EJBException(aMessage, e);
        }

        return aPersonValue;
    }

    public void setSessionContext(SessionContext aSessionContext) throws EJBException, RemoteException {
        mSessionContext = aSessionContext;
    }

    public void ejbRemove() throws EJBException, RemoteException {
    }

    public void ejbActivate() throws EJBException, RemoteException {
    }

    public void ejbPassivate() throws EJBException, RemoteException {
    }

    public Collection getAllPersons() {
        sLogger.info("creating person in manager cmtd bean. find all person...");
        Collection c = new ArrayList();
        try {
            InitialContext aInitialContext = new InitialContext();
            PersonHome aPersonHome = (PersonHome) aInitialContext.lookup("java:comp/env/ejb/Person");
            Collection aPersonCollection = aPersonHome.findAll();
            Iterator i = aPersonCollection.iterator();
            while (i.hasNext()) {
                Person aPerson = (Person) i.next();
                c.add(toValue(aPerson));
            }
        } catch (Exception e) {
            String aMessage = "exception occured in manager cmtd bean.find all person";
            sLogger.log(Level.SEVERE, aMessage, e);
            throw new EJBException(aMessage, e);
        }
        return c;
    }

    protected PersonValue toValue(Person aPerson) {
        PersonValue aPersonValue = new PersonValue();
        if (aPerson != null) {
            aPersonValue.setEmail(aPerson.getEmail());
            aPersonValue.setFAX(aPerson.getFAX());
            aPersonValue.setHomePhone(aPerson.getHomePhone());
            //aPersonValue.setPassword(aPerson.getPassword());
            aPersonValue.setPersonId(aPerson.getPersonId());
            aPersonValue.setWorkPhone(aPerson.getWorkPhone());

            aPersonValue.setPersonStatusValue(toValue(aPerson.getPersonStatus()));
        }
        return aPersonValue;
    }

    protected PersonStatusValue toValue(PersonStatus aLocal) {
        PersonStatusValue aValue = new PersonStatusValue();
        if (aLocal != null) {
            aValue.setId(aLocal.getPersonStatusId());
            aValue.setStatus(aLocal.getStatus());
            aValue.setStatusComment(aLocal.getStatusComment());
        }
        return aValue;
    }

}

