KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xpetstore > services > order > ejb > OrderProcessorMDB


1 package xpetstore.services.order.ejb;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import javax.ejb.ActivationConfigProperty JavaDoc;
7 import javax.ejb.MessageDriven JavaDoc;
8
9 import javax.jms.Message JavaDoc;
10 import javax.jms.MessageListener JavaDoc;
11 import javax.jms.ObjectMessage JavaDoc;
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 /**
30  *
31  * @ ejb.bean
32  * name="OrderProcessor"
33  * acknowledge-mode="Auto-acknowledge"
34  * destination-type="javax.jms.Queue"
35  * subscription-durability="Durable"
36  * transaction-type="Container"
37  * @ ejb.transaction
38  * type="Required"
39  * @ ejb.ejb-ref
40  * ejb-name="Order"
41  * view-type="local"
42  * ref-name="ejb/OrderLocal"
43  * @ ejb.resource-ref
44  * res-ref-name="${jndi.queue.ConnectionFactory}"
45  * res-type="javax.jms.QueueConnectionFactory"
46  * res-auth="Container"
47  * jndi-name="${orion.queue.ConnectionFactory}"
48  * @ ejb.resource-ref
49  * res-ref-name="${jndi.queue.mail}"
50  * res-type="javax.jms.Queue"
51  * res-auth="Container"
52  * jndi-name="${orion.queue.mail}"
53  *
54  * @ jboss.destination-jndi-name
55  * name="${jboss.queue.order}"
56  * @ jboss.resource-ref
57  * res-ref-name="${jndi.queue.ConnectionFactory}"
58  * jndi-name="${jboss.queue.ConnectionFactory}"
59  * @ jboss.resource-ref
60  * res-ref-name="${jndi.queue.mail}"
61  * jndi-name="${jboss.queue.mail}"
62  *
63  * @ weblogic.message-driven
64  * destination-jndi-name="${weblogic.queue.order}"
65  * @ weblogic.resource-description
66  * res-ref-name="${jndi.queue.ConnectionFactory}"
67  * jndi-name="${weblogic.queue.ConnectionFactory}"
68  * @ weblogic.resource-description
69  * res-ref-name="${jndi.queue.mail}"
70  * jndi-name="${weblogic.queue.mail}"
71  *
72  * @ orion.bean
73  * connection-factory-location="${orion.queue.ConnectionFactory}"
74  * destination-location="${orion.queue.order}"
75  */

76 @MessageDriven JavaDoc(name="OrderProcessor", activationConfig =
77 {
78       @ActivationConfigProperty JavaDoc(propertyName="destinationType", propertyValue="javax.jms.Queue"),
79       @ActivationConfigProperty JavaDoc(propertyName="destination", propertyValue="queue/order")
80 })
81 public class OrderProcessorMDB
82    implements MessageListener JavaDoc
83 {
84    @PersistenceContext
85    private EntityManager manager;
86
87     /**
88      * This method expect a <code>javax.jms.ObjectMessage</code> that
89      * contains the orderUId as the message object
90      */

91     public void onMessage( Message JavaDoc recvMsg )
92     {
93         Integer JavaDoc orderUId = null;
94         Order order = null;
95
96         try
97         {
98             Debug.print( "OrderProcessorMDB.onMessage(" + recvMsg + ")" );
99
100             /* Get the order to proceed */
101             ObjectMessage JavaDoc msg = ( ObjectMessage JavaDoc ) recvMsg;
102             orderUId = ( Integer JavaDoc ) msg.getObject( );
103             order = manager.find( Order.class, orderUId );
104
105             /* Proceed the order */
106             proceedPayment( order );
107             proceedOrder( order );
108         }
109         catch ( CreditCardException c )
110         {
111             try
112             {
113                 cancelOrder( order );
114             }
115             catch ( Exception JavaDoc e )
116             {
117                 Debug.print( "Unable to cancel the order[" + orderUId + "]", e );
118             }
119         }
120         catch ( Exception JavaDoc 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 JavaDoc
131     {
132         order.changeStatus( OrderStatus.TO_DELIVER );
133         notifyCustomer( order );
134     }
135
136     private void cancelOrder( Order order )
137         throws Exception JavaDoc
138     {
139         order.changeStatus( OrderStatus.CANCELLED );
140         notifyCustomer( order );
141     }
142
143     private void notifyCustomer( Order order )
144         throws Exception JavaDoc
145     {
146         Customer customer = order.getCustomer( );
147         if ( customer == null)
148         {
149             Debug.print( "No customer to notify" );
150             return;
151         }
152          
153         String JavaDoc to = customer.getEmail( );
154         String JavaDoc subject = "[Petstore] Order Confirmation";
155         String JavaDoc body = toHtml( order );
156         JMSUtil.sendToJMSQueue( JNDINames.QUEUE_MAIL, new Email( to, subject, body ), false );
157     }
158
159     private String JavaDoc toHtml( Order order )
160     {
161         StringBuffer JavaDoc buff = new StringBuffer JavaDoc( );
162
163         /* Id/Status */
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         /* Shipping/Bill address Address */
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         /* Items */
182         Collection JavaDoc 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'>&nbsp;</th>" );
190         buff.append( "</tr>" );
191
192         for ( Iterator JavaDoc 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 JavaDoc toHtml( OrderItem orderItem )
208     {
209         StringBuffer JavaDoc buff = new StringBuffer JavaDoc( );
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