KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > opc > workflowmanager > handlers > POHandler


1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
25 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
27 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
28 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
30 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that Software is not designed, licensed or intended
34 * for use in the design, construction, operation or maintenance of
35 * any nuclear facility.
36 */

37
38 package com.sun.j2ee.blueprints.opc.workflowmanager.handlers;
39
40 import java.rmi.*;
41
42 import javax.jms.*;
43 import javax.ejb.*;
44
45 import com.sun.j2ee.blueprints.opc.purchaseorder.*;
46 import com.sun.j2ee.blueprints.opc.orderreceiver.*;
47 import com.sun.j2ee.blueprints.opc.JNDINames;
48 import com.sun.j2ee.blueprints.processmanager.ejb.*;
49 import com.sun.j2ee.blueprints.servicelocator.*;
50 import com.sun.j2ee.blueprints.servicelocator.ejb.*;
51 import com.sun.j2ee.blueprints.opc.financial.*;
52 import com.sun.j2ee.blueprints.opc.utils.*;
53 import com.sun.j2ee.blueprints.opc.mailer.*;
54
55 /**
56  * This is the PO handler that gets called by the
57  * OPC Work Flow Manager. The handler first calls
58  * the PO receiver that persists the PO. Then
59  * it makes a call to the Credit Card Verifier
60  * and then the CRM component. Finally it calls the
61  * order filler component.
62  */

63 public class POHandler {
64     
65     private ProcessManagerLocal processManager;
66     private ServiceLocator sl;
67     private POReceiver poReceiver;
68     private CreditCardVerifier cardVerifier;
69     
70     public POHandler()throws HandlerException {
71         try{
72             sl = new ServiceLocator();
73             cardVerifier = new CreditCardVerifier();
74             poReceiver = new POReceiver();
75             ProcessManagerLocalHome pmHome = (ProcessManagerLocalHome)sl.getLocalHome(JNDINames.PM_EJB);
76             processManager = pmHome.create();
77         } catch (Exception JavaDoc exe) {
78             System.err.println(exe);
79             throw new HandlerException("OPC Exception creating POHandler");
80         }
81     }
82     
83     public void handle(Message message) throws HandlerException {
84
85         PurchaseOrder po = null;
86         String JavaDoc poID = null;
87         String JavaDoc emailID = null;
88         boolean sendMail = sl.getBoolean(JNDINames.SEND_MAIL);
89         
90         //extract the PO from the message
91
try {
92             if(message instanceof ObjectMessage){
93                 ObjectMessage objMsg = (ObjectMessage) message;
94                 po = (PurchaseOrder)objMsg.getObject();
95             }
96             if(po != null){
97                 poID = po.getPoId();
98                 emailID = po.getEmailId();
99                 //persist the PO
100
poReceiver.persistPO(po);
101                 String JavaDoc lodgOrderStatus = po.getLodging() == null
102                                          ? OrderStatusNames.COMPLETED
103                                          : OrderStatusNames.PENDING;
104                 String JavaDoc actyOrderStatus = po.getActivities().length == 0
105                                          ? OrderStatusNames.COMPLETED
106                                          : OrderStatusNames.PENDING;
107                 String JavaDoc airlineOrderStatus = (po.getDepartureFlightInfo() == null)
108                                             && (po.getReturnFlightInfo() == null)
109                                             ? OrderStatusNames.COMPLETED
110                                             : OrderStatusNames.PENDING;
111                 processManager.createManager(poID, OrderStatusNames.PENDING,
112                                             actyOrderStatus, airlineOrderStatus,
113                                             lodgOrderStatus);
114            
115                 //next call Credit Card Verifier
116
String JavaDoc creditCardXML = po.getCreditCard().toXML();
117                 boolean ccStatus = cardVerifier.verifyCreditCard(creditCardXML);
118
119                 //change status and submit the order to order filler if credit
120
//card is approved
121
if (ccStatus){
122                     processManager.updateStatus(poID, OrderStatusNames.APPROVED);
123                     //call CRM
124
if(sendMail){
125                         String JavaDoc subject = "Your Adventure Builder order has been approved";
126                         String JavaDoc msg = "Your order (# " + poID + " ) has been approved.";
127                         msg += " Thank you for shopping with us and we hope to see you again soon";
128                         sendMail(emailID, subject, msg);
129                     }
130                     //next call order filler
131
if (JMSUtils.sendMessage(JNDINames.ORDER_FILLER_MDB_QUEUE,
132                                          JNDINames.DOC_TYPE, JNDINames.PO_DOCUMENT,
133                                          (Object JavaDoc)po) == false){
134                         processManager.updateStatus(poID, OrderStatusNames.ORDER_FILLER_ERROR);
135                         processManager.updateOrderErrorStatus(poID, true);
136                     } else {
137                         processManager.updateStatus(poID, OrderStatusNames.SUBMITTED);
138                     }
139                 } else {
140                     processManager.updateStatus(poID, OrderStatusNames.DENIED);
141                     //call CRM
142
if(sendMail){
143                         String JavaDoc subject = "Your Adventure Builder order has been denied";
144                         String JavaDoc msg = "Your order (# " + poID + " ) has been denied. ";
145                         msg += " Thank you for shopping with us and we hope to see you again soon";
146                         sendMail(emailID, subject, msg);
147                     }
148                 }
149             }
150         } catch (CreateException ce) {
151             //call CRM to notify the customer
152
if(sendMail){
153                 String JavaDoc subject = "Problems processing your Adventure Builder order";
154                 String JavaDoc msg = "We had problems processing your Adventure Builder order.";
155                 msg += " Please resubmit the order";
156                 sendMail(emailID, subject, msg);
157             }
158         } catch (RemoteException re) {
159             //call process manager and set error status
160
try{
161                 processManager.updateStatus(poID,OrderStatusNames.PAYMENT_PROCESSING_ERROR);
162                 processManager.updateOrderErrorStatus(poID, true);
163             } catch(FinderException fe){
164                 System.err.println(fe);
165             }
166         } catch (Exception JavaDoc exe) {
167             System.err.println(exe);
168             throw new HandlerException("OPC Exception handling PO");
169         }
170     }
171     
172     private void sendMail(String JavaDoc emailID, String JavaDoc subject, String JavaDoc msg){
173         Mail mail = new Mail(emailID, subject, msg);
174         String JavaDoc xmlMail = mail.toXML();
175         JMSUtils.sendMessage(JNDINames.CRM_MDB_QUEUE,
176                             JNDINames.DOC_TYPE, JNDINames.MAIL_DOCUMENT,
177                             xmlMail);
178     }
179 }
180
Popular Tags