KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > demo > mortgage > workflow > orchestration > SimpleMortgageProfiling


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: SimpleMortgageProfiling.java 154 7 juin 2006 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.demo.mortgage.workflow.orchestration;
23
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.jbi.messaging.DeliveryChannel;
28 import javax.jbi.messaging.InOut;
29 import javax.jbi.messaging.MessageExchange;
30 import javax.jbi.messaging.NormalizedMessage;
31 import javax.xml.namespace.QName JavaDoc;
32 import javax.xml.transform.Source JavaDoc;
33
34 import org.objectweb.petals.component.common.HandlingException;
35 import org.objectweb.petals.component.common.PEtALSComponentSDKException;
36 import org.objectweb.petals.component.common.util.SourceHelper;
37
38 /**
39  * Workflow for simple mortgage
40  *
41  * @author ofabre - EBM Websourcing
42  *
43  */

44 public class SimpleMortgageProfiling implements MortgageProfiling {
45
46     public final static String JavaDoc MORTGAGE_SERVICE = "ProfilerImplService";
47
48     public final static String JavaDoc MORTGAGE_SERVICE_OP = "evaluateSimpleProfile";
49
50     public final static String JavaDoc OK_PROFILE = "yes";
51
52     protected DeliveryChannel channel;
53
54     protected Logger JavaDoc l;
55
56     protected MortgageProfilingUtil profilingUtil;
57
58     public SimpleMortgageProfiling() {
59
60     }
61
62     public SimpleMortgageProfiling(DeliveryChannel channel, Logger JavaDoc l) {
63         super();
64         this.channel = channel;
65         this.l = l;
66         profilingUtil = new MortgageProfilingUtil(channel, l);
67     }
68
69     public MortgageResponse orchestrate(MortgageRequest request)
70             throws HandlingException {
71         MortgageResponse response = null;
72
73         l.info("#################################################");
74         l.info("#### PROCESSING A SIMPLE MORTGAGE RATE EVALUATION");
75         l.info("#################################################");
76
77         /*
78          * Evaluate Mortgage Profile
79          */

80         l.info("##### First step : Evalute Mortgage Profile #####");
81         String JavaDoc profile = doEvalProfile(request.getSalary(), request
82                 .getPropertyTaxes(), request.getInsurance(), request
83                 .getAutoPayment(), request.getCreditCards(), request
84                 .getOtherPayments());
85
86         float rate = -1;
87         if (OK_PROFILE.equals(profile)) {
88             /*
89              * Evaluate Mortgage rate
90              */

91             l.info("##### Second Step : Evaluate Mortgage Rate #####");
92             rate = profilingUtil.doEvalMortgageRate(profile);
93         }
94
95         /*
96          * Create Mortgage Response
97          */

98         response = profilingUtil.createMortgageResponse(rate, request);
99         l
100                 .info("##### Last Step : Send the response to the Mortgage requester#####");
101
102         /*
103          * Send a summary of the profiling
104          */

105         profilingUtil.doSendSummary(response);
106
107         return response;
108     }
109
110     protected Source JavaDoc createInMessageContent(float salary, float propertyTaxes,
111             float insurance, float autoPayment, float creditCards,
112             float otherPayments) throws PEtALSComponentSDKException {
113
114         StringBuffer JavaDoc soap = new StringBuffer JavaDoc();
115         soap.append("<ns1:" + MORTGAGE_SERVICE_OP
116                 + " xmlns:ns1=\"http://petals.objectweb.org/\">");
117         soap.append("<salary>" + salary + "</salary>");
118         soap.append("<propertyTaxes>" + propertyTaxes + "</propertyTaxes>");
119         soap.append("<insurance>" + insurance + "</insurance>");
120         soap.append("<autoPayment>" + autoPayment + "</autoPayment>");
121         soap.append("<creditCards>" + creditCards + "</creditCards>");
122         soap.append("<otherPayments>" + otherPayments + "</otherPayments>");
123         soap.append("</ns1:" + MORTGAGE_SERVICE_OP + ">");
124
125         return SourceHelper.createSource(soap.toString());
126     }
127
128     protected String JavaDoc doEvalProfile(float salary, float propertyTaxes,
129             float insurance, float autoPayment, float creditCards,
130             float otherPayments) throws HandlingException {
131
132         MessageExchange msg = null;
133         try {
134             /*
135              * Create message content from given data
136              */

137             Source JavaDoc source = createInMessageContent(salary, propertyTaxes,
138                     insurance, autoPayment, creditCards, otherPayments);
139
140             /*
141              * Create a new message exchange for MortgageService
142              */

143             msg = channel.createExchangeFactory().createInOutExchange();
144             msg.setService(new QName JavaDoc(MortgageProfilingUtil.DEFAULT_NS,
145                     MORTGAGE_SERVICE));
146             msg.setOperation(new QName JavaDoc(MORTGAGE_SERVICE_OP));
147
148             /*
149              * Create the inMessage to send
150              */

151             NormalizedMessage inNM = msg.createMessage();
152             inNM.setContent(source);
153             ((InOut) msg).setInMessage(inNM);
154
155             /*
156              * Send the message exchange
157              */

158             channel.sendSync(msg);
159         } catch (Exception JavaDoc e) {
160             throw new HandlingException("Error evaluating profile", e);
161         }
162         /*
163          * Get response from EvalRate service
164          */

165         NormalizedMessage outNM = ((InOut) msg).getOutMessage();
166         String JavaDoc response = "ProfileNotDefined";
167         if (outNM != null) {
168             try {
169                 response = SourceHelper.createString(outNM.getContent());
170             } catch (PEtALSComponentSDKException e) {
171                 throw new HandlingException(e);
172             }
173             if (response.indexOf("return") > -1) {
174                 response = response.substring(response.indexOf("<return>") + 8,
175                         response.indexOf("</return>"));
176                 l.log(Level.INFO, response);
177             }
178         } else {
179             throw new HandlingException(
180                     "Mortgage profiler web service response is null");
181         }
182         l.info("##### Evaluated profile : " + response + " #####");
183         return response;
184     }
185
186 }
187
Popular Tags