package com.sun.j2ee.blueprints.supplier.orderfulfillment.ejb;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Date;
import java.util.HashMap;
import java.util.StringTokenizer;
import javax.ejb.EJBException;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.jms.JMSException;
import com.sun.j2ee.blueprints.xmldocuments.tpa.TPAInvoiceXDE;
import com.sun.j2ee.blueprints.xmldocuments.XMLDocumentException;
import com.sun.j2ee.blueprints.supplierpo.ejb.SupplierOrder;
import com.sun.j2ee.blueprints.supplierpo.ejb.SupplierOrderLocal;
import com.sun.j2ee.blueprints.supplierpo.ejb.SupplierOrderLocalHome;
import com.sun.j2ee.blueprints.lineitem.ejb.LineItemLocal;
import com.sun.j2ee.blueprints.lineitem.ejb.LineItemLocalHome;
import com.sun.j2ee.blueprints.lineitem.ejb.LineItem;
import com.sun.j2ee.blueprints.supplier.inventory.ejb.InventoryLocal;
import com.sun.j2ee.blueprints.supplier.inventory.ejb.InventoryLocalHome;
import com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException;
import com.sun.j2ee.blueprints.servicelocator.ejb.ServiceLocator;
import com.sun.j2ee.blueprints.processmanager.ejb.OrderStatusNames;
public class OrderFulfillmentFacadeEJB implements SessionBean {
private TPAInvoiceXDE invoiceXDE;
private TPASupplierOrderXDE supplierOrderXDE;
private SupplierOrderLocalHome supplierOrderLocalHome;
private InventoryLocalHome invHome;
private SessionContext sc;
public OrderFulfillmentFacadeEJB() {}
public void ejbCreate() throws CreateException {
try {
ServiceLocator serviceLocator = new ServiceLocator();
invoiceXDE
= new TPAInvoiceXDE(serviceLocator.getUrl(JNDINames.XML_ENTITY_CATALOG_URL),
serviceLocator.getBoolean(JNDINames.XML_XSD_VALIDATION_INVOICE));
supplierOrderXDE
= new TPASupplierOrderXDE(serviceLocator.getUrl(JNDINames.XML_ENTITY_CATALOG_URL),
serviceLocator.getBoolean(JNDINames.XML_VALIDATION_SUPPLIER_ORDER),
serviceLocator.getString(JNDINames.XML_SUPPLIER_ORDER_SCHEMA));
supplierOrderLocalHome
= (SupplierOrderLocalHome) serviceLocator.getLocalHome(JNDINames.PO_EJB);
invHome
= (InventoryLocalHome) serviceLocator.getLocalHome(JNDINames.INV_EJB);
} catch (XMLDocumentException xe) {
throw new EJBException(xe);
} catch (ServiceLocatorException se) {
throw new EJBException(se);
}
}
public void ejbPostCreate() throws CreateException {}
public void setSessionContext(SessionContext sc) {
this.sc = sc;
}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
private boolean checkInventory(LineItemLocal item) {
try {
InventoryLocal inv = invHome.findByPrimaryKey(item.getItemId());
if(inv.getQuantity() < item.getQuantity())
return(false);
inv.reduceQuantity(item.getQuantity());
} catch (FinderException fe) {
return(false);
}
return(true);
}
private String createInvoice(SupplierOrderLocal po, HashMap newLis)
throws XMLDocumentException {
invoiceXDE.newDocument();
invoiceXDE.setOrderId(po.getPoId());
invoiceXDE.setUserId("Dear PetStore Customer");
Date poDate = new Date(po.getPoDate());
invoiceXDE.setOrderDate(poDate);
Date curDate = new Date();
invoiceXDE.setShippingDate(curDate);
Collection items = po.getLineItems();
Iterator it = items.iterator();
while((it != null) && (it.hasNext())) {
LineItemLocal anItem = (LineItemLocal)it.next();
if(newLis.containsKey(anItem.getItemId())) {
invoiceXDE.addLineItem(anItem.getCategoryId(),
anItem.getProductId(),
anItem.getItemId(),
anItem.getLineNumber(),
anItem.getQuantity(),
anItem.getUnitPrice());
}
}
String invDoc = null;
invDoc = invoiceXDE.getDocumentAsString();
return(invDoc);
}
private String processAnOrder(SupplierOrderLocal po)
throws XMLDocumentException {
boolean allItemsAvailable = true;
boolean invoiceReqd = false;
String invoiceXml = null;
HashMap items = new HashMap();
Collection liColl = po.getLineItems();
Iterator liIt = liColl.iterator();
while((liIt != null) && (liIt.hasNext())) {
LineItemLocal li = (LineItemLocal) liIt.next();
if(li.getQuantity() == li.getQuantityShipped())
continue;
if(!checkInventory(li)) {
allItemsAvailable = false;
continue;
}
li.setQuantityShipped(li.getQuantity());
items.put(li.getItemId(), OrderStatusNames.COMPLETED);
invoiceReqd = true;
} if(allItemsAvailable)
po.setPoStatus(OrderStatusNames.COMPLETED);
if(invoiceReqd) {
try {
invoiceXml = (createInvoice(po, items));
} catch (XMLDocumentException xe) {
System.out.println("OrderFulfillmentFacade**" + xe);
return null;
}
}
return invoiceXml;
}
public String processPO(String poXmlDoc) throws CreateException,
XMLDocumentException {
String invoiceXml = null;
SupplierOrderLocal order = null;
SupplierOrder supplierOrder = null;
supplierOrderXDE.setDocument(poXmlDoc);
supplierOrder = supplierOrderXDE.getSupplierOrder();
order = supplierOrderLocalHome.create(supplierOrder);
invoiceXml = processAnOrder(order);
return invoiceXml;
}
public Collection processPendingPO() throws FinderException {
ArrayList invoices = new ArrayList();
Collection coll =
supplierOrderLocalHome.findOrdersByStatus(OrderStatusNames.PENDING);
if(coll != null) {
Iterator it = coll.iterator();
while((it!=null) && (it.hasNext())) {
SupplierOrderLocal order = (SupplierOrderLocal) it.next();
String newInvoice = null;
try {
newInvoice = processAnOrder(order);
} catch (XMLDocumentException xe) {
System.out.println("OrderFulfillmentFacade:" + xe);
}
if(newInvoice != null) {
invoices.add(newInvoice);
}
} }
return invoices;
}
}