package com.sun.j2ee.blueprints.opc.customerrelations.ejb;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.MessageListener;
import javax.xml.transform.dom.DOMSource;
import com.sun.j2ee.blueprints.mailer.ejb.Mail;
import com.sun.j2ee.blueprints.xmldocuments.XMLDocumentException;
import com.sun.j2ee.blueprints.purchaseorder.ejb.PurchaseOrderLocal;
import com.sun.j2ee.blueprints.purchaseorder.ejb.PurchaseOrderLocalHome;
import com.sun.j2ee.blueprints.opc.ejb.TPAInvoiceXDE;
import com.sun.j2ee.blueprints.opc.transitions.MailInvoiceTransitionDelegate;
import com.sun.j2ee.blueprints.processmanager.transitions.*;
import com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException;
import com.sun.j2ee.blueprints.servicelocator.ejb.ServiceLocator;
public class MailInvoiceMDB implements MessageDrivenBean, MessageListener {
private static final String MAIL_SUBJECT = "Java Pet Store Order Shipped: ";
private static final String INVOICE_STYLE_SHEET = "/com/sun/j2ee/blueprints/opc/rsrc/xsl/PartialInvoice.xsl";
private Context context;
private MessageDrivenContext mdc = null;
private boolean sendConfirmationMail = false;
private MailContentXDE mailContentXDE;
private TransitionDelegate transitionDelegate;
private TPAInvoiceXDE invoiceXDE = null;
private PurchaseOrderLocalHome poHome;
public MailInvoiceMDB() {
}
public void ejbCreate() {
try {
ServiceLocator serviceLocator = new ServiceLocator();
sendConfirmationMail = serviceLocator.getBoolean(JNDINames.SEND_CONFIRMATION_MAIL);
invoiceXDE = new TPAInvoiceXDE(serviceLocator.getUrl(JNDINames.XML_ENTITY_CATALOG_URL),
serviceLocator.getBoolean(JNDINames.XML_VALIDATION_INVOICE),
serviceLocator.getBoolean(JNDINames.XML_XSD_VALIDATION));
poHome = (PurchaseOrderLocalHome)serviceLocator.getLocalHome(JNDINames.PURCHASE_ORDER_EJB);
transitionDelegate = new MailInvoiceTransitionDelegate();
transitionDelegate.setup();
mailContentXDE = new MailContentXDE(INVOICE_STYLE_SHEET);
} catch (ServiceLocatorException se) {
throw new EJBException(se);
} catch (TransitionException te) {
throw new EJBException(te);
} catch (MailContentXDE.FormatterException exception) {
throw new EJBException(exception);
} 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();
if (sendConfirmationMail) {
String xmlMail = doWork(recdText);
doTransition(xmlMail);
}
} catch(XMLDocumentException xde) {
throw new EJBException(xde);
} catch(TransitionException te) {
throw new EJBException(te);
} catch (JMSException je) {
throw new EJBException(je);
} catch (MailContentXDE.FormatterException mfe) {
throw new EJBException(mfe);
} catch (FinderException fe) {
throw new EJBException(fe);
}
}
public void setMessageDrivenContext(MessageDrivenContext mdc) {
this.mdc = mdc;
}
public void ejbRemove() {
}
private String doWork(String xmlMessage) throws JMSException,
XMLDocumentException,
MailContentXDE.FormatterException,
FinderException {
invoiceXDE.setDocument(xmlMessage);
PurchaseOrderLocal po = poHome.findByPrimaryKey(invoiceXDE.getOrderId());
String emailAddress = po.getPoEmailId();
String subject = MAIL_SUBJECT + invoiceXDE.getOrderId();
mailContentXDE.setDocument(invoiceXDE.getDocument());
mailContentXDE.setLocale(LocaleUtil.getLocaleFromString(po.getPoLocale()));
String message = mailContentXDE.getDocumentAsString();
Mail mailMsg = new Mail(emailAddress, subject, message);
return mailMsg.toXML();
}
private void doTransition(String xmlMail) throws TransitionException {
TransitionInfo info = new TransitionInfo(xmlMail);
transitionDelegate.doTransition(info);
}
}