KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > opc > workflowmanager > WorkFlowManagerBean


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;
39
40 import java.util.*;
41
42 import javax.jms.*;
43 import javax.ejb.*;
44 import javax.naming.*;
45 import javax.ejb.Timer JavaDoc;
46
47 import com.sun.j2ee.blueprints.opc.workflowmanager.handlers.*;
48 import com.sun.j2ee.blueprints.processmanager.ejb.*;
49 import com.sun.j2ee.blueprints.processmanager.manager.ejb.*;
50 import com.sun.j2ee.blueprints.servicelocator.*;
51 import com.sun.j2ee.blueprints.servicelocator.ejb.*;
52 import com.sun.j2ee.blueprints.opc.JNDINames;
53
54 /**
55  * This is the work flow manager that controls
56  * work flow within the Order Processing Center
57  */

58 public class WorkFlowManagerBean implements MessageDrivenBean, MessageListener, TimedObject {
59      
60     private MessageDrivenContext context;
61     private POHandler poHandler;
62     private InvoiceHandler invHandler;
63     private ProcessManagerLocal pm;
64   
65     public void setMessageDrivenContext(MessageDrivenContext context) {
66         this.context=context;
67     }
68       
69     public void ejbCreate() {
70         try{
71             poHandler = new POHandler();
72             invHandler = new InvoiceHandler();
73             ServiceLocator sl = new ServiceLocator();
74       ProcessManagerLocalHome pmlh = (ProcessManagerLocalHome)
75                                           sl.getLocalHome(JNDINames.PM_EJB);
76             pm = pmlh.create();
77         } catch (HandlerException he){
78             throw new EJBException(he.getMessage());
79         } catch (ServiceLocatorException se) {
80       throw new EJBException(se.getMessage());
81   } catch (Exception JavaDoc exe) {
82       throw new EJBException(exe.getMessage());
83   }
84     }
85       
86     public void onMessage(Message message) {
87         try{
88             /*
89              * For now, just call the handlers
90              * This will change as the state machine is implemented
91              */

92             String JavaDoc docType = message.getStringProperty(JNDINames.DOC_TYPE);
93             if(docType.equals(JNDINames.PO_DOCUMENT)) {
94                 poHandler.handle(message);
95             } else if(docType.equals(JNDINames.INVOICE_DOCUMENT)) {
96                 invHandler.handle(message);
97                 createStatusUpdateTimer();
98             }
99         } catch (HandlerException he){
100             throw new EJBException(he);
101         } catch (JMSException exe) {
102             throw new EJBException(exe);
103         }
104     }
105     
106     private void createStatusUpdateTimer() {
107         try{
108
109             TimerService timerService = context.getTimerService();
110              
111             //check if a timer already exists
112
if ((timerService.getTimers()).isEmpty()) {
113                 Context ic = new InitialContext();
114       
115                 //create an interval timer to update the order status
116
int expiration = (((Integer JavaDoc) ic.lookup(JNDINames.TIMER_EXPIRATION)).intValue()) * 60000;
117           int interval = (((Integer JavaDoc) ic.lookup(JNDINames.TIMER_INTERVAL)).intValue()) * 60000;
118                 Timer JavaDoc timer = timerService.createTimer(expiration, interval, "OPC order update timer");
119             }
120         } catch (Exception JavaDoc exe) {
121             throw new EJBException(exe);
122         }
123     }
124
125     public void ejbTimeout(Timer JavaDoc timer) {
126         try{
127
128             //check the status of all the orders that are submitted to suppliers
129
Collection ordersList = pm.getOrdersByStatus(OrderStatusNames.SUBMITTED);
130             Iterator iter = ordersList.iterator();
131             while (iter.hasNext()) {
132                 ManagerLocal mgr = (ManagerLocal) iter.next();
133                 String JavaDoc orderID = mgr.getOrderId();
134
135                 //change status to completed if all the three supplier orders are completed
136
pm.updateStatusToCompleted(orderID);
137
138                 //send the order completed mail
139
if(pm.getOrderStatus(orderID).equals(OrderStatusNames.COMPLETED)){
140                     invHandler.sendOrderCompletedMail(orderID);
141                 }
142             }
143         } catch (Exception JavaDoc exe) {
144             throw new EJBException(exe);
145         }
146     }
147     
148     public void ejbRemove() {}
149 }
150
151
Popular Tags