1 package xpetstore.services.order.ejb; 2 3 import java.util.Collection ; 4 import java.util.Iterator ; 5 6 import javax.ejb.ActivationConfigProperty ; 7 import javax.ejb.MessageDriven ; 8 9 import javax.jms.Message ; 10 import javax.jms.MessageListener ; 11 import javax.jms.ObjectMessage ; 12 import javax.persistence.EntityManager; 13 import javax.persistence.PersistenceContext; 14 15 import xpetstore.domain.customer.ejb.Customer; 16 import xpetstore.domain.order.ejb.Order; 17 import xpetstore.domain.order.ejb.OrderItem; 18 19 import xpetstore.domain.order.model.OrderStatus; 20 21 import xpetstore.services.mail.model.Email; 22 import xpetstore.services.order.exceptions.CreditCardException; 23 24 import xpetstore.util.Debug; 25 import xpetstore.util.JMSUtil; 26 import xpetstore.util.JNDINames; 27 28 29 76 @MessageDriven (name="OrderProcessor", activationConfig = 77 { 78 @ActivationConfigProperty (propertyName="destinationType", propertyValue="javax.jms.Queue"), 79 @ActivationConfigProperty (propertyName="destination", propertyValue="queue/order") 80 }) 81 public class OrderProcessorMDB 82 implements MessageListener 83 { 84 @PersistenceContext 85 private EntityManager manager; 86 87 91 public void onMessage( Message recvMsg ) 92 { 93 Integer orderUId = null; 94 Order order = null; 95 96 try 97 { 98 Debug.print( "OrderProcessorMDB.onMessage(" + recvMsg + ")" ); 99 100 101 ObjectMessage msg = ( ObjectMessage ) recvMsg; 102 orderUId = ( Integer ) msg.getObject( ); 103 order = manager.find( Order.class, orderUId ); 104 105 106 proceedPayment( order ); 107 proceedOrder( order ); 108 } 109 catch ( CreditCardException c ) 110 { 111 try 112 { 113 cancelOrder( order ); 114 } 115 catch ( Exception e ) 116 { 117 Debug.print( "Unable to cancel the order[" + orderUId + "]", e ); 118 } 119 } 120 catch ( Exception e ) 121 { 122 Debug.print( "Unable to proceed the order[" + orderUId + "]", e ); 123 } 124 } 125 126 private void proceedPayment( Order order ) 127 throws CreditCardException {} 128 129 private void proceedOrder( Order order ) 130 throws Exception 131 { 132 order.changeStatus( OrderStatus.TO_DELIVER ); 133 notifyCustomer( order ); 134 } 135 136 private void cancelOrder( Order order ) 137 throws Exception 138 { 139 order.changeStatus( OrderStatus.CANCELLED ); 140 notifyCustomer( order ); 141 } 142 143 private void notifyCustomer( Order order ) 144 throws Exception 145 { 146 Customer customer = order.getCustomer( ); 147 if ( customer == null) 148 { 149 Debug.print( "No customer to notify" ); 150 return; 151 } 152 153 String to = customer.getEmail( ); 154 String subject = "[Petstore] Order Confirmation"; 155 String body = toHtml( order ); 156 JMSUtil.sendToJMSQueue( JNDINames.QUEUE_MAIL, new Email( to, subject, body ), false ); 157 } 158 159 private String toHtml( Order order ) 160 { 161 StringBuffer buff = new StringBuffer ( ); 162 163 164 buff.append( "<table border='1' width='100%'>" ); 165 buff.append( "<tr><td width='10%'><b>Order ID:</b></td><td>" + order.getOrderUId( ) + "</td></tr>" ); 166 buff.append( "<tr><td width='10%'><b>Status:</b></td><td>" + order.getStatus( ) + "</td></tr>" ); 167 buff.append( "</table>" ); 168 169 170 buff.append( "<table border='1' width='100%'><tr>" ); 171 buff.append( "<td width='10%' valign='top'><b>Address:</b></td>" ); 172 buff.append( "<td>" ); 173 buff.append( order.getStreet1( ) + "<br>" ); 174 buff.append( order.getStreet2( ) + "<br>" ); 175 buff.append( order.getCity( ) + "," + order.getState( ) + "<br>" ); 176 buff.append( order.getZipcode( ) + "<br>" ); 177 buff.append( order.getCountry( ) ); 178 buff.append( "</td>" ); 179 buff.append( "</tr></table>" ); 180 181 182 Collection orderItems = order.getOrderItems( ); 183 buff.append( "<table border='1' width='100%'>" ); 184 buff.append( "<tr>" ); 185 buff.append( "<th bgcolor='#c0c0c0'>ID</td>" ); 186 buff.append( "<th bgcolor='#c0c0c0'>Description</th>" ); 187 buff.append( "<th bgcolor='#c0c0c0'>Unit Price</th>" ); 188 buff.append( "<th bgcolor='#c0c0c0'>Quantity</th>" ); 189 buff.append( "<th bgcolor='#c0c0c0'> </th>" ); 190 buff.append( "</tr>" ); 191 192 for ( Iterator it = orderItems.iterator( ); it.hasNext( ); ) 193 { 194 OrderItem orderItem = ( OrderItem ) it.next( ); 195 buff.append( toHtml( orderItem ) ); 196 } 197 198 buff.append( "<tr>" ); 199 buff.append( "<td colspan=4 align=right bgcolor='#c0c0c0'>TOTAL:</td>" ); 200 buff.append( "<td bgcolor='#c0c0c0'><b>" + order.calculateTotal( ) + "<b></td>" ); 201 buff.append( "</tr>" ); 202 buff.append( "</table>" ); 203 204 return buff.toString( ); 205 } 206 207 private String toHtml( OrderItem orderItem ) 208 { 209 StringBuffer buff = new StringBuffer ( ); 210 211 buff.append( "<tr>" ); 212 buff.append( "<td>" + orderItem.getItem( ).getItemId( ) + "</td>>" ); 213 buff.append( "<td>" + orderItem.getItem( ).getDescription( ) + "<br>" ); 214 buff.append( "<td>" + orderItem + "</td>" ); 215 buff.append( "<td>" + orderItem + "</td>" ); 216 buff.append( "<td>" + orderItem.calculateSubTotal( ) + "</td>" ); 217 buff.append( "</tr>" ); 218 219 return buff.toString( ); 220 } 221 } 222 | Popular Tags |