1 22 package org.objectweb.petals.demo.mortgage.workflow.orchestration; 23 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 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 ; 32 import javax.xml.transform.Source ; 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 44 public class SimpleMortgageProfiling implements MortgageProfiling { 45 46 public final static String MORTGAGE_SERVICE = "ProfilerImplService"; 47 48 public final static String MORTGAGE_SERVICE_OP = "evaluateSimpleProfile"; 49 50 public final static String OK_PROFILE = "yes"; 51 52 protected DeliveryChannel channel; 53 54 protected Logger l; 55 56 protected MortgageProfilingUtil profilingUtil; 57 58 public SimpleMortgageProfiling() { 59 60 } 61 62 public SimpleMortgageProfiling(DeliveryChannel channel, Logger 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 80 l.info("##### First step : Evalute Mortgage Profile #####"); 81 String 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 91 l.info("##### Second Step : Evaluate Mortgage Rate #####"); 92 rate = profilingUtil.doEvalMortgageRate(profile); 93 } 94 95 98 response = profilingUtil.createMortgageResponse(rate, request); 99 l 100 .info("##### Last Step : Send the response to the Mortgage requester#####"); 101 102 105 profilingUtil.doSendSummary(response); 106 107 return response; 108 } 109 110 protected Source createInMessageContent(float salary, float propertyTaxes, 111 float insurance, float autoPayment, float creditCards, 112 float otherPayments) throws PEtALSComponentSDKException { 113 114 StringBuffer soap = new StringBuffer (); 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 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 137 Source source = createInMessageContent(salary, propertyTaxes, 138 insurance, autoPayment, creditCards, otherPayments); 139 140 143 msg = channel.createExchangeFactory().createInOutExchange(); 144 msg.setService(new QName (MortgageProfilingUtil.DEFAULT_NS, 145 MORTGAGE_SERVICE)); 146 msg.setOperation(new QName (MORTGAGE_SERVICE_OP)); 147 148 151 NormalizedMessage inNM = msg.createMessage(); 152 inNM.setContent(source); 153 ((InOut) msg).setInMessage(inNM); 154 155 158 channel.sendSync(msg); 159 } catch (Exception e) { 160 throw new HandlingException("Error evaluating profile", e); 161 } 162 165 NormalizedMessage outNM = ((InOut) msg).getOutMessage(); 166 String 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 |