KickJava   Java API By Example, From Geeks To Geeks.

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


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: DetailedMortgageProfiling.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 detailed mortgage
40  *
41  * @author ofabre - EBM Websourcing
42  *
43  */

44 public class DetailedMortgageProfiling implements MortgageProfiling {
45
46     public final static String JavaDoc MORTGAGE_SERVICE = "ProfilerImplService";
47
48     public final static String JavaDoc MORTGAGE_SERVICE_OP = "evaluateDetailedProfile";
49
50     protected DeliveryChannel channel;
51
52     protected Logger JavaDoc l;
53
54     protected MortgageProfilingUtil profilingUtil;
55
56     public DetailedMortgageProfiling() {
57         super();
58     }
59
60     /**
61      * Build a new DetailedMortgageProfiling setted with the given
62      * ComponentContext and Logger. Set a new MortgageProfilingUtil.
63      *
64      * @param ctx
65      * the ComponentContext of the MortgageWorkflow service
66      * @param l
67      * a logger
68      */

69     public DetailedMortgageProfiling(DeliveryChannel channel, Logger JavaDoc l) {
70         super();
71         this.channel = channel;
72         this.l = l;
73         profilingUtil = new MortgageProfilingUtil(channel, l);
74
75     }
76
77     /**
78      * Launch the detailed mortgage workflow : evaluate the profile, evaluate
79      * the mortgage rate and create the MortgageResponse bo
80      *
81      * @param request
82      * the MortgageRequest bo. Must be non null.
83      * @return a MortgageResponse bo. Can't be null
84      */

85     public MortgageResponse orchestrate(MortgageRequest request)
86             throws HandlingException {
87         MortgageResponse response = null;
88
89         if (request == null) {
90             throw new HandlingException("MortgageRequest must not be null !");
91         }
92
93         l.info("###################################################");
94         l.info("#### PROCESSING A DETAILED MORTGAGE RATE EVALUATION");
95         l.info("###################################################");
96
97         /*
98          * Evaluate Mortgage Profile
99          */

100         l.info("##### First step : Evalute Mortgage Profile #####");
101         String JavaDoc profile = doEvalProfile(request.getSalary(), request
102                 .getPropertyTaxes(), request.getInsurance(), request
103                 .getAutoPayment(), request.getCreditCards(), request
104                 .getOtherPayments());
105
106         /*
107          * Evaluate Mortgage rate
108          */

109         l.info("##### Second Step : Evaluate Mortgage Rate #####");
110         if (profilingUtil == null) {
111             profilingUtil = new MortgageProfilingUtil(channel, l);
112         }
113         float rate = profilingUtil.doEvalMortgageRate(profile);
114
115         /*
116          * Create Mortgage Response
117          */

118         response = profilingUtil.createMortgageResponse(rate, request);
119         l
120                 .info("##### Last Step : Send the response to the Mortgage requester#####");
121
122         /*
123          * Send a summary of the profiling
124          */

125         profilingUtil.doSendSummary(response);
126
127         return response;
128     }
129
130     /**
131      * Create a new Source content that handles a Soap representation of the
132      * Profiler service's evaluation method call from given customer personal
133      * information
134      *
135      * @param salary
136      * The salary of the customer
137      * @param propertyTaxes
138      * Property Taxes of the customer
139      * @param insurance
140      * Amount of insurance of the customer
141      * @param autoPayment
142      * All the auto payment of the customer
143      * @param creditCards
144      * Amount of the credit cards of the customer
145      * @param otherPayments
146      * Other payments (credits, subscriptions...) of the customer
147      * @return a new Source content that handles a Soap representation of the
148      * Profiler service's evaluation method call
149      * @throws PEtALSComponentSDKException
150      */

151     protected Source JavaDoc createInMessageContent(float salary, float propertyTaxes,
152             float insurance, float autoPayment, float creditCards,
153             float otherPayments) throws PEtALSComponentSDKException {
154
155         StringBuffer JavaDoc soap = new StringBuffer JavaDoc();
156         soap.append("<ns1:" + MORTGAGE_SERVICE_OP
157                 + " xmlns:ns1=\"http://petals.objectweb.org/\">");
158         soap.append("<salary>" + salary + "</salary>");
159         soap.append("<propertyTaxes>" + propertyTaxes + "</propertyTaxes>");
160         soap.append("<insurance>" + insurance + "</insurance>");
161         soap.append("<autoPayment>" + autoPayment + "</autoPayment>");
162         soap.append("<creditCards>" + creditCards + "</creditCards>");
163         soap.append("<otherPayments>" + otherPayments + "</otherPayments>");
164         soap.append("</ns1:" + MORTGAGE_SERVICE_OP + ">");
165
166         return SourceHelper.createSource(soap.toString());
167     }
168
169     /**
170      * Launch the mortgage profile evaluation : create a Soap content with given
171      * data, create and send a new MessageExchange handling this content to the
172      * Profiler service and finaly process the response.
173      *
174      * @param salary
175      * @param propertyTaxes
176      * @param insurance
177      * @param autoPayment
178      * @param creditCards
179      * @param otherPayments
180      * @return
181      * @throws Exception
182      */

183     protected String JavaDoc doEvalProfile(float salary, float propertyTaxes,
184             float insurance, float autoPayment, float creditCards,
185             float otherPayments) throws HandlingException {
186
187         MessageExchange msg = null;
188         try {
189             /*
190              * Create message content from given data
191              */

192             Source JavaDoc source = createInMessageContent(salary, propertyTaxes,
193                     insurance, autoPayment, creditCards, otherPayments);
194
195             /*
196              * Create a new message exchange for the Profiler Service
197              */

198             msg = channel.createExchangeFactory().createInOutExchange();
199             msg.setService(new QName JavaDoc(MortgageProfilingUtil.DEFAULT_NS,
200                     MORTGAGE_SERVICE));
201             msg.setOperation(new QName JavaDoc(MORTGAGE_SERVICE_OP));
202
203             /*
204              * Create the inMessage to send
205              */

206             NormalizedMessage inNM = msg.createMessage();
207             inNM.setContent(source);
208             ((InOut) msg).setInMessage(inNM);
209
210             /*
211              * Send the message exchange
212              */

213             channel.sendSync(msg);
214         } catch (Exception JavaDoc e) {
215             throw new HandlingException("Error evaluating profile", e);
216         }
217         /*
218          * Get response from EvalRate service
219          */

220         NormalizedMessage outNM = ((InOut) msg).getOutMessage();
221         String JavaDoc response = "ProfileNotDefined";
222         if (outNM != null) {
223             try {
224                 response = SourceHelper.createString(outNM.getContent());
225             } catch (PEtALSComponentSDKException e) {
226                 throw new HandlingException(e);
227             }
228             if (response.indexOf("return") > -1) {
229                 response = response.substring(response.indexOf("<return>") + 8,
230                         response.indexOf("</return>"));
231                 l.log(Level.INFO, response);
232             }
233         } else {
234             throw new HandlingException(
235                     "Mortgage profiler web service response is null");
236         }
237         l.info("##### Evaluated profile : " + response + " #####");
238         return response;
239     }
240
241 }
242
Popular Tags