package ordering.ejb;

 

import javax.ejb.*;

import ordering.common.model.OrderingModel;

import java.util.ArrayList;

import java.sql.Connection;

import ordering.common.vo.PurchaseOrderVO;

import ordering.common.exceptions.ModelAccessException;

import ordering.common.exceptions.IntegrityException;

import java.sql.SQLException;

import java.util.Collection;

import ordering.common.exceptions.NotFoundException;

import java.util.Iterator;

import ordering.common.vo.PODescriptorVO;

import ordering.custom.Constants;

import ordering.common.vo.InvoiceDescriptorVO;

import ordering.common.vo.InvoiceVO;

import ordering.common.vo.AddressVO;

import ordering.common.vo.POLineItemVO;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import ordering.common.vo.InvoiceLineItemVO;

 

public class OrderingServiceEJBBean implements SessionBean {

       SessionContext sessionContext;

       SequenceGeneratorLocalHome sequenceHome;

       private ordering.ejb.PurchaseOrderEJBHome poHome;

       private ordering.ejb.InvoiceEJBHome invoiceHome;

 

       public void ejbCreate() throws CreateException {

         /**@todo Complete this method*/

       }

       public void ejbRemove() {

         /**@todo Complete this method*/

       }

       public void ejbActivate() {

         /**@todo Complete this method*/

       }

       public void ejbPassivate() {

         /**@todo Complete this method*/

       }

       public void setSessionContext(SessionContext sessionContext) {

         this.sessionContext = sessionContext;

         this.poHome = getPurchaseOrderHome();

         this.invoiceHome = getInvoiceHome();

         this.sequenceHome = getSequenceHome();

       }

 

    private static PurchaseOrderEJBHome getPurchaseOrderHome()

    {

        try

        {

            return (PurchaseOrderEJBHome)

                    new InitialContext().lookup("PurchaseOrderEJB");

        }

        catch(NamingException ne)

        {

            throw new EJBException();

        }

    }

 

    private static InvoiceEJBHome getInvoiceHome()

    {

        try

        {

            return (InvoiceEJBHome)

                    new InitialContext().lookup("InvoiceEJB");

        }

        catch(NamingException ne)

        {

            throw new EJBException();

        }

    }

 

       private static SequenceGeneratorLocalHome getSequenceHome()

       {

           try

           {

               return (SequenceGeneratorLocalHome)

                       new InitialContext().lookup("SequenceGeneratorBeanLocal");

           }

           catch(NamingException ne)

           {

               throw new EJBException();

           }

       }

 

       public void addPurchaseOrder(PurchaseOrderVO po)

              throws ModelAccessException, IntegrityException

       {

           try

           {

               System.out.println(po.getPoId());

               PurchaseOrderEJB poEJB = poHome.createUsingVO(po);

               System.out.println(po.getPoId());

           }

           catch (CreateException ce)

           {

               throw new ModelAccessException(ce.getMessage());

           }

       }

 

       public Collection getPODescriptors(int partnerId, String status)

              throws ModelAccessException, NotFoundException

       {

           Collection descs = new ArrayList();

           try {

               Collection poEJBS = poHome.findByStatus(status);

               Iterator iterator = poEJBS.iterator();

 

               while(iterator.hasNext()){

 

                   PurchaseOrderEJB poEJB = (PurchaseOrderEJB)iterator.next();

                   if(poEJB.getStatus().equals(status) || status.equals(ordering.custom.Constants.STATUS_ALL))

                   {

                       PurchaseOrderVO po = new PurchaseOrderVO();

                       po.setPoId(poEJB.getPoID());

                       po.setPoNumber(poEJB.getNumber());

                       po.setBillTo(new AddressVO(

                         poEJB.getBill_to_name(),

                               poEJB.getBill_to_address(),

                               poEJB.getBill_to_city(),

                               poEJB.getBill_to_state(),

                               poEJB.getBill_to_zip(),

                               poEJB.getBill_to_country()

                               ));

                       po.setShipTo(new AddressVO(

                               poEJB.getShip_to_name(),

                               poEJB.getShip_to_address(),

                               poEJB.getShip_to_city(),

                               poEJB.getShip_to_state(),

                               poEJB.getShip_to_zip(),

                               poEJB.getShip_to_country()

                               ));

                       po.setNote(poEJB.getNote());

                       po.setDate(poEJB.getDate());

                       po.setPaymentMethod(poEJB.getPayment_method());

                       po.setTotalCost(poEJB.getTotal_cost());

                       po.setStatus(poEJB.getStatus());

                       descs.add(po.getDescriptorVO());

                   }

               }

           }

           catch (FinderException e)

           {

 

           }

           return descs;

       }

 

       public PurchaseOrderVO getPurchaseOrder(int id) throws ModelAccessException, NotFoundException

       {

           PurchaseOrderVO po = new PurchaseOrderVO();

           Collection poLineItemVOs = new ArrayList();

           try {

               PurchaseOrderEJBPK pk = new PurchaseOrderEJBPK ();

               pk.poID = id;

               PurchaseOrderEJB poEJB = (PurchaseOrderEJB)poHome.findByPrimaryKey(pk);

               Collection lineItemEJBs = poEJB.getPoLineItems();

 

               po.setPoId(poEJB.getPoID());

               po.setPoNumber(poEJB.getNumber());

               po.setBillTo(new AddressVO(

                       poEJB.getBill_to_name(),

                       poEJB.getBill_to_address(),

                       poEJB.getBill_to_city(),

                       poEJB.getBill_to_state(),

                       poEJB.getBill_to_zip(),

                       poEJB.getBill_to_country()

                       ));

               po.setShipTo(new AddressVO(

                       poEJB.getShip_to_name(),

                       poEJB.getShip_to_address(),

                       poEJB.getShip_to_city(),

                       poEJB.getShip_to_state(),

                       poEJB.getShip_to_zip(),

                       poEJB.getShip_to_country()

                       ));

               po.setNote(poEJB.getNote());

               po.setDate(poEJB.getDate());

               po.setPaymentMethod(poEJB.getPayment_method());

               po.setTotalCost(poEJB.getTotal_cost());

               po.setStatus(poEJB.getStatus());

 

               Iterator iter = lineItemEJBs.iterator();

               while (iter.hasNext())

               {

                 POLineItemEJB itemEJB = (POLineItemEJB) iter.next();

                 POLineItemVO poLineItem = new POLineItemVO(

                         itemEJB.getId(),

                         poEJB.getPoID(),

                         itemEJB.getItem_number(),

                         itemEJB.getItem_description(),

                         itemEJB.getQuantity(),

                         itemEJB.getUnit_price(),

                         itemEJB.getLine_total()

                         );

                 poLineItemVOs.add(poLineItem);

               }

               po.setLineItems(poLineItemVOs);

           }

           catch (FinderException e) {

           }

           return po;

       }

 

       public void createInvoice(InvoiceVO invoiceVO)

                throws ModelAccessException, IntegrityException

       {

           try

               {

 

                 InvoiceEJB invoiceEJB = invoiceHome.createUsingVO(invoiceVO);

 

               }

               catch (CreateException ce)

               {

                 throw new ModelAccessException(ce.getMessage());

               }

 

       }

 

       public void acceptPurchaseOrder(int id) throws ModelAccessException, NotFoundException

       {

           Collection invoiceLineItemVOs = new ArrayList();

           InvoiceVO invoiceVO= new InvoiceVO();

           try

           {

               PurchaseOrderEJBPK pk = new PurchaseOrderEJBPK ();

               pk.poID = id;

 

               PurchaseOrderEJB poEJB = (PurchaseOrderEJB)poHome.findByPrimaryKey(pk);

               poEJB.setStatus(Constants.STATUS_ACCEPTED);

               Collection lineItemEJBs = poEJB.getPoLineItems();

               int invoiceID = newId("Invoice");

               invoiceVO.setInvoiceId(invoiceID);

               invoiceVO.setInvoiceNumber(""+invoiceID);

               invoiceVO.setPoId(poEJB.getPoID());

               invoiceVO.setBillTo(new AddressVO(

                       poEJB.getBill_to_name(),

                       poEJB.getBill_to_address(),

                       poEJB.getBill_to_city(),

                       poEJB.getBill_to_state(),

                       poEJB.getBill_to_zip(),

                       poEJB.getBill_to_country()

                       ));

               invoiceVO.setShipTo(new AddressVO(

                       poEJB.getShip_to_name(),

                       poEJB.getShip_to_address(),

                       poEJB.getShip_to_city(),

                       poEJB.getShip_to_state(),

                       poEJB.getShip_to_zip(),

                       poEJB.getShip_to_country()

                       ));

               invoiceVO.setNote(poEJB.getNote());

               invoiceVO.setDate(poEJB.getDate());

               invoiceVO.setPaymentMethod(poEJB.getPayment_method());

               invoiceVO.setTotalCost(poEJB.getTotal_cost());

               invoiceVO.setStatus(Constants.STATUS_PENDING);

 

               Iterator iter = lineItemEJBs.iterator();

               while (iter.hasNext())

               {

                 POLineItemEJB itemEJB = (POLineItemEJB) iter.next();

                 InvoiceLineItemVO invoiceLineItem = new InvoiceLineItemVO(

                         newId("InvoiceLineItem"),

                         invoiceVO.getInvoiceId(),

                         itemEJB.getItem_number(),

                         itemEJB.getItem_description(),

                         itemEJB.getQuantity(),

                         itemEJB.getUnit_price(),

                         itemEJB.getLine_total()

                         );

                 invoiceLineItemVOs.add(invoiceLineItem);

               }

               invoiceVO.setLineItems(invoiceLineItemVOs);

               createInvoice(invoiceVO);

           }

           catch (Exception e)

           {

 

           }

       }

 

       public void rejectPurchaseOrder(int id) throws ModelAccessException, NotFoundException

       {

           PurchaseOrderVO po = getPurchaseOrder(id);

           po.setStatus(Constants.STATUS_REJECTED);

       }

 

 

       public Collection getInvoiceDescriptors(int partnerId,String status)

               throws ModelAccessException,NotFoundException

       {

           Collection descs = new ArrayList();

           try {

               Collection invoiceEJBS = invoiceHome.findByStatus(status);

               Iterator iterator = invoiceEJBS.iterator();

 

                      while(iterator.hasNext()){

 

                             InvoiceEJB invoiceEJB = (InvoiceEJB)iterator.next();

                             if(invoiceEJB.getStatus().equals(status) || status.equals(ordering.custom.Constants.STATUS_ALL))

                             {

                                    InvoiceVO invoice = new InvoiceVO();

                                    invoice.setInvoiceId(invoiceEJB.getInvoiceID());

                                    invoice.setPoId(invoiceEJB.getPoID());

                                    invoice.setInvoiceNumber(invoiceEJB.getNumber());

                                    invoice.setBillTo(new AddressVO(

                                                  invoiceEJB.getBill_to_name(),

                                                  invoiceEJB.getBill_to_address(),

                                                  invoiceEJB.getBill_to_city(),

                                                  invoiceEJB.getBill_to_state(),

                                                  invoiceEJB.getBill_to_zip(),

                                                  invoiceEJB.getBill_to_country()

                                                  ));

                                    invoice.setShipTo(new AddressVO(

                                                  invoiceEJB.getShip_to_name(),

                                                  invoiceEJB.getShip_to_address(),

                                                  invoiceEJB.getShip_to_city(),

                                                  invoiceEJB.getShip_to_state(),

                                                  invoiceEJB.getShip_to_zip(),

                                                  invoiceEJB.getShip_to_country()

                                                  ));

                                    invoice.setNote(invoiceEJB.getNote());

                                    invoice.setDate(invoiceEJB.getDate());

                                    invoice.setPaymentMethod(invoiceEJB.getPayment_method());

                                    invoice.setTotalCost(invoiceEJB.getTotal_cost());

                                    invoice.setStatus(invoiceEJB.getStatus());

                                    descs.add(invoice.getDescriptorVO());

                             }

                      }

               }

               catch (FinderException e)

               {

 

               }

               return descs;

        }

 

        public InvoiceVO getInvoice(int id) throws ModelAccessException,NotFoundException

        {

               InvoiceVO invoice = new InvoiceVO();

               Collection invoiceLineItemVOs = new ArrayList();

               try {

                      InvoiceEJBPK pk = new InvoiceEJBPK ();

                      pk.invoiceID = id;

                      InvoiceEJB invoiceEJB = (InvoiceEJB)invoiceHome.findByPrimaryKey(pk);

                      Collection lineItemEJBs = invoiceEJB.getInvoiceLineItems();

 

                      invoice.setPoId(invoiceEJB.getPoID());

                      invoice.setInvoiceNumber(invoiceEJB.getNumber());

                      invoice.setBillTo(new AddressVO(

                                    invoiceEJB.getBill_to_name(),

                                    invoiceEJB.getBill_to_address(),

                                    invoiceEJB.getBill_to_city(),

                                    invoiceEJB.getBill_to_state(),

                                    invoiceEJB.getBill_to_zip(),

                                    invoiceEJB.getBill_to_country()

                                    ));

                      invoice.setShipTo(new AddressVO(

                                    invoiceEJB.getShip_to_name(),

                                    invoiceEJB.getShip_to_address(),

                                    invoiceEJB.getShip_to_city(),

                                    invoiceEJB.getShip_to_state(),

                                    invoiceEJB.getShip_to_zip(),

                                    invoiceEJB.getShip_to_country()

                                    ));

                      invoice.setNote(invoiceEJB.getNote());

                      invoice.setDate(invoiceEJB.getDate());

                      invoice.setPaymentMethod(invoiceEJB.getPayment_method());

                      invoice.setTotalCost(invoiceEJB.getTotal_cost());

                      invoice.setStatus(invoiceEJB.getStatus());

 

                      Iterator iter = lineItemEJBs.iterator();

                      while (iter.hasNext())

                      {

                        InvoiceLineItemEJB itemEJB = (InvoiceLineItemEJB) iter.next();

                        InvoiceLineItemVO invoiceLineItem = new InvoiceLineItemVO(

                                      itemEJB.getId(),

                                      invoiceEJB.getPoID(),

                                      itemEJB.getItem_number(),

                                      itemEJB.getItem_description(),

                                      itemEJB.getQuantity(),

                                      itemEJB.getUnit_price(),

                                      itemEJB.getLine_total()

                                      );

                        invoiceLineItemVOs.add(invoiceLineItem);

                      }

                      invoice.setLineItems(invoiceLineItemVOs);

               }

               catch (FinderException e) {

               }

               return invoice;

        }

 

        public void saveInvoice(InvoiceVO invoice) throws ModelAccessException, IntegrityException{}

        public void sendInvoice(int id) throws ModelAccessException{}

 

        public Collection buildSampleData()

        {

       //   int IDGEN_START = (int)System.currentTimeMillis();

       //   int idgen = IDGEN_START;

 

          Collection poList = new ArrayList();

          AddressVO address = new AddressVO("SomeGreatCompany","1 Elm","Chihuahua","NA","12345","Neverland");

          for(int i=1; i <= 20; i++){

 

                 PurchaseOrderVO po = new PurchaseOrderVO();

 

                 int poID = newId("PurchaseOrder");

                 po.setPoId(poID);

                 po.setPoNumber(String.valueOf("PO"+poID));

                 po.setDate(new java.util.Date());

 

                 po.setTotalCost(i*10.0);

                 po.setBillTo(address);

                 po.setShipTo(address);

 

                 po.setStatus(Constants.STATUS_PENDING);

 

                 ArrayList lineItems = new ArrayList();

                 for (int j=1; j<=5; j++) {

                        int poLineItemID = newId("POLineItem");

                        System.out.println("i,j,poId,polineitemid: "+i+"---"+j+"---"+poID+"----"+poLineItemID);

                        lineItems.add(new POLineItemVO(poLineItemID, poID, String.valueOf(poLineItemID), "uom", j,j*1.2f, j*j*1.2f));

                 }

                 po.setLineItems(lineItems);

 

                 try {

                      poHome.createUsingVO(po);

                 } catch (Exception e) {

                        System.out.println(e.getMessage());

                 }

 

          }

          return poList;

        }

 

        public int newId(String sequence_name) {

         try {

              SequenceGeneratorLocal seq = sequenceHome.create();

              return seq.nextSequenceNumber(sequence_name);

         }

         catch (Exception ex) {

              throw new EJBException("Error generating id for : " + sequence_name);

         }

       }

 

 

}