package com.sun.j2ee.blueprints.opc.ejb;
import java.net.URL;
import javax.ejb.EJBException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.Context;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import com.sun.j2ee.blueprints.purchaseorder.ejb.PurchaseOrderHelper;
import com.sun.j2ee.blueprints.purchaseorder.ejb.PurchaseOrderLocalHome;
import com.sun.j2ee.blueprints.purchaseorder.ejb.PurchaseOrderLocal;
import com.sun.j2ee.blueprints.xmldocuments.XMLDocumentException;
import com.sun.j2ee.blueprints.processmanager.ejb.ProcessManagerLocalHome;
import com.sun.j2ee.blueprints.processmanager.ejb.ProcessManagerLocal;
import com.sun.j2ee.blueprints.processmanager.ejb.OrderStatusNames;
import com.sun.j2ee.blueprints.opc.transitions.*;
import com.sun.j2ee.blueprints.processmanager.transitions.*;
import com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException;
import com.sun.j2ee.blueprints.servicelocator.ejb.ServiceLocator;
public class InvoiceMDB implements MessageDrivenBean, MessageListener {
private Context context;
private MessageDrivenContext mdc;
private ProcessManagerLocal processManager;
private PurchaseOrderLocalHome poHome;
private TPAInvoiceXDE invoiceXDE;
private TransitionDelegate transitionDelegate;
public InvoiceMDB() {
}
public void ejbCreate() {
try {
ServiceLocator serviceLocator = new ServiceLocator();
poHome = (PurchaseOrderLocalHome)serviceLocator.getLocalHome(JNDINames.PURCHASE_ORDER_EJB);
ProcessManagerLocalHome pmlh = (ProcessManagerLocalHome)serviceLocator.getLocalHome(JNDINames.PROCESS_MANAGER_EJB);
processManager = pmlh.create();
URL entityCatalogURL = serviceLocator.getUrl(JNDINames.XML_ENTITY_CATALOG_URL);
boolean validateXmlInvoice = serviceLocator.getBoolean(JNDINames.XML_VALIDATION_INVOICE);
invoiceXDE = new TPAInvoiceXDE(entityCatalogURL, validateXmlInvoice,
serviceLocator.getBoolean(JNDINames.XML_XSD_VALIDATION));
transitionDelegate = new InvoiceTD();
transitionDelegate.setup();
} catch (TransitionException te) {
throw new EJBException(te);
} catch (ServiceLocatorException se) {
throw new EJBException(se);
} catch (CreateException ce) {
throw new EJBException(ce);
} catch (XMLDocumentException xde) {
throw new EJBException(xde);
}
}
public void onMessage(Message recvMsg) {
TextMessage recdTM = null;
String recdText = null;
try {
recdTM = (TextMessage)recvMsg;
recdText = recdTM.getText();
String joinMessage = doWork(recdText);
if (joinMessage != null) {
doTransition(joinMessage);
}
} catch(TransitionException te) {
throw new EJBException(te);
} catch(XMLDocumentException xde) {
throw new EJBException(xde);
} catch (JMSException je) {
throw new EJBException(je);
} catch (FinderException fe) {
throw new EJBException(fe);
}
}
public void setMessageDrivenContext(MessageDrivenContext mdc) {
this.mdc = mdc;
}
public void ejbRemove() {
}
private String doWork(String xmlInvoice) throws XMLDocumentException, FinderException {
String completedOrder = null;
PurchaseOrderHelper poHelper = new PurchaseOrderHelper();
invoiceXDE.setDocument(xmlInvoice);
PurchaseOrderLocal po = poHome.findByPrimaryKey(invoiceXDE.getOrderId());
boolean orderDone = poHelper.processInvoice(po, invoiceXDE.getLineItemIds());
if(orderDone) {
processManager.updateStatus(invoiceXDE.getOrderId(), OrderStatusNames.COMPLETED);
completedOrder = invoiceXDE.getOrderId();
} else {
processManager.updateStatus(invoiceXDE.getOrderId(), OrderStatusNames.SHIPPED_PART);
}
return completedOrder;
}
private void doTransition(String completedOrder) throws TransitionException {
TransitionInfo info = new TransitionInfo(completedOrder);
transitionDelegate.doTransition(info);
}
}