package com.sun.j2ee.blueprints.supplier.processpo.ejb;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.MessageListener;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.ejb.FinderException;
import com.sun.j2ee.blueprints.xmldocuments.XMLDocumentException;
import com.sun.j2ee.blueprints.xmldocuments.tpa.TPAInvoiceXDE;
import com.sun.j2ee.blueprints.supplier.orderfulfillment.ejb.OrderFulfillmentFacadeLocalHome;
import com.sun.j2ee.blueprints.supplier.orderfulfillment.ejb.OrderFulfillmentFacadeLocal;
import com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException;
import com.sun.j2ee.blueprints.servicelocator.ejb.ServiceLocator;
import com.sun.j2ee.blueprints.processmanager.transitions.*;
public class SupplierOrderMDB implements MessageDrivenBean, MessageListener {
private Context context;
private MessageDrivenContext mdc = null;
private OrderFulfillmentFacadeLocal poProcessor = null;
private TransitionDelegate transitionDelegate;
public SupplierOrderMDB() {}
public void ejbCreate() {
try {
ServiceLocator serviceLocator = new ServiceLocator();
OrderFulfillmentFacadeLocalHome ref = (OrderFulfillmentFacadeLocalHome)
serviceLocator.getLocalHome(JNDINames.ORDERFACADE_EJB);
poProcessor = ref.create();
String tdClassName = serviceLocator.getString(JNDINames.TRANSITION_DELEGATE__SUPPLIER_ORDER);
TransitionDelegateFactory tdf = new TransitionDelegateFactory();
transitionDelegate = tdf.getTransitionDelegate(tdClassName);
transitionDelegate.setup();
} catch (CreateException ce) {
throw new EJBException(ce);
} catch (TransitionException te) {
throw new EJBException(te);
} catch (ServiceLocatorException se) {
throw new EJBException(se);
}
}
public void onMessage(Message recvMsg) {
try {
String messageID = recvMsg.getJMSMessageID();
TextMessage recdTM = (TextMessage)recvMsg;
String recdText = recdTM.getText();
String invoice = doWork(recdText);
if(invoice!=null) {
doTransition(invoice);
} } catch (TransitionException te) {
throw new EJBException(te);
} catch (CreateException ce) {
throw new EJBException(ce);
} catch (XMLDocumentException xe) {
throw new EJBException(xe);
} catch (JMSException je) {
throw new EJBException(je);
}
}
public void setMessageDrivenContext(MessageDrivenContext mdc) {
this.mdc = mdc;
}
public void ejbRemove() {}
private String doWork(String xmlMessage) throws
CreateException, XMLDocumentException {
String invoice = null;
invoice = poProcessor.processPO(xmlMessage);
return invoice;
}
private void doTransition(String xmlMessage) throws TransitionException {
TransitionInfo info = new TransitionInfo(xmlMessage);
transitionDelegate.doTransition(info);
}
}