KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jms.*;
41
42 import com.sun.j2ee.blueprints.opc.invoice.*;
43 import com.sun.j2ee.blueprints.opc.*;
44 import com.sun.j2ee.blueprints.processmanager.ejb.*;
45 import com.sun.j2ee.blueprints.servicelocator.*;
46 import com.sun.j2ee.blueprints.servicelocator.ejb.*;
47 import com.sun.j2ee.blueprints.opc.purchaseorder.ejb.*;
48 import com.sun.j2ee.blueprints.opc.utils.*;
49 import com.sun.j2ee.blueprints.opc.JNDINames;
50 import com.sun.j2ee.blueprints.opc.mailer.*;
51
52 /**
53  * This is the Invoice handler that gets called by the
54  * OPC Work Flow Manager. This handler calls the
55  * workers that process the invoice
56  */

57 public class InvoiceHandler {
58     
59     private ProcessManagerLocal processManager;
60     private ServiceLocator sl;
61     
62     public InvoiceHandler() throws HandlerException {
63         try{
64             sl = new ServiceLocator();
65             ProcessManagerLocalHome pmHome =
66                 (ProcessManagerLocalHome)sl.getLocalHome(JNDINames.PM_EJB);
67             processManager = pmHome.create();
68         } catch (Exception JavaDoc exe) {
69             System.err.println(exe);
70             throw new HandlerException("OPC Exception creating InvoiceHandler");
71         }
72     }
73     
74     public void handle(Message message) throws HandlerException {
75         Invoice invoice = null;
76         String JavaDoc inv = null;
77         String JavaDoc opcPoID = null;
78         
79         //extract the Invoice from the message
80
try {
81             if(message instanceof TextMessage){
82                 TextMessage txtMsg = (TextMessage) message;
83                 inv = txtMsg.getText();
84             }
85             if(inv != null){
86                 invoice = Invoice.fromXML(inv);
87                 String JavaDoc supplierID = invoice.getSupplierId();
88                 opcPoID = invoice.getOpcPoId();
89                 String JavaDoc invStat = invoice.getStatus();
90                 if(supplierID.equals(JNDINames.ACTIVITY_INVOICE))
91                     processManager.updateActivityOrderStatus(opcPoID, invStat);
92                 if(supplierID.equals(JNDINames.LODGING_INVOICE))
93                     processManager.updateLodgingOrderStatus(opcPoID, invStat);
94                 if(supplierID.equals(JNDINames.AIRLINE_INVOICE))
95                     processManager.updateAirlineOrderStatus(opcPoID, invStat);
96            }
97         } catch (XMLException exe) {
98             System.err.println(exe);
99             //call process manager and set error status
100
try{
101                 /*
102                  * The status is set to invoice xml error, indicating that an error
103                  * occurred while deserializing the invoice. Advanced error handling
104                  * will be done for a later release, with the status having additional
105                  * information about the supplier(lodging, activity, or airline)
106                  */

107                 processManager.updateStatus(opcPoID,OrderStatusNames.INVOICE_XML_ERROR);
108                 processManager.updateOrderErrorStatus(opcPoID, true);
109             } catch(Exception JavaDoc xe){
110                 System.err.println(xe);
111             }
112         } catch (Exception JavaDoc exe) {
113             System.err.println(exe);
114             throw new HandlerException("OPC Exception handling invoice");
115         }
116     }
117    
118     public void sendOrderCompletedMail(String JavaDoc orderID) throws HandlerException{
119         boolean sendMail = sl.getBoolean(JNDINames.SEND_MAIL);
120         try{
121             //get email id and call CRM
122
if(sendMail){
123                 PurchaseOrderLocalHome poHome =
124                   (PurchaseOrderLocalHome)sl.getLocalHome(JNDINames.PO_EJB);
125                 PurchaseOrderLocal poLocal = poHome.findByPrimaryKey(orderID);
126                 String JavaDoc msg = "Your order (# " + orderID + " ) has been completed.";
127                 msg += " Thank you for shopping with us and we hope to see you again soon";
128                 Mail mail = new Mail(poLocal.getEmailId(),
129                        " Your Adventure Builder order has been completed ", msg);
130                 String JavaDoc xmlMail = mail.toXML();
131                 JMSUtils.sendMessage(JNDINames.CRM_MDB_QUEUE,
132                                 JNDINames.DOC_TYPE, JNDINames.MAIL_DOCUMENT, xmlMail);
133             }
134         } catch (Exception JavaDoc exe) {
135             System.err.println(exe);
136             throw new HandlerException("OPC Exception sending mail");
137         }
138
139     }
140 }
141
Popular Tags