1 package org.objectweb.petals.demo.mortgage.workflow.sdk; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.IOException ; 5 import java.util.List ; 6 import java.util.logging.Logger ; 7 8 import javax.jbi.messaging.DeliveryChannel; 9 import javax.xml.parsers.DocumentBuilder ; 10 import javax.xml.parsers.DocumentBuilderFactory ; 11 import javax.xml.parsers.ParserConfigurationException ; 12 13 import org.objectweb.petals.component.common.HandlingException; 14 import org.objectweb.petals.demo.mortgage.workflow.orchestration.DetailedMortgageProfiling; 15 import org.objectweb.petals.demo.mortgage.workflow.orchestration.MortgageProfiling; 16 import org.objectweb.petals.demo.mortgage.workflow.orchestration.MortgageRequest; 17 import org.objectweb.petals.demo.mortgage.workflow.orchestration.MortgageResponse; 18 import org.objectweb.petals.demo.mortgage.workflow.orchestration.SimpleMortgageProfiling; 19 import org.objectweb.petals.tools.jbicommon.util.XMLUtil; 20 import org.w3c.dom.Document ; 21 import org.w3c.dom.Node ; 22 import org.xml.sax.SAXException ; 23 24 public class MortgageWorkflowImpl implements MortgageWorkflow { 25 26 private Logger logger; 27 28 private DeliveryChannel channel; 29 30 public MortgageWorkflowImpl(Logger logger, DeliveryChannel channel) { 31 super(); 32 this.logger = logger; 33 this.channel = channel; 34 } 35 36 public String orchestrate(String endpointName, String content) 37 throws HandlingException { 38 39 42 MortgageRequest request = retrieveRequestParameters(content); 43 44 47 MortgageResponse response = executeWorkflow(endpointName, request); 48 49 return createContentFromMortgageResponse(response); 50 } 51 52 63 protected MortgageRequest retrieveRequestParameters(String stringContent) 64 throws HandlingException { 65 66 String address = ""; 67 String autoPayment = ""; 68 String creditCards = ""; 69 String firstName = ""; 70 String insurance = ""; 71 String lastName = ""; 72 String otherPayments = ""; 73 String propertyTaxes = ""; 74 String salary = ""; 75 76 System.out.println(stringContent); 77 78 Document doc = parseXml(stringContent); 79 Node requestValues = doc.getDocumentElement().getFirstChild(); 80 81 List <Node > values = XMLUtil.getNodeChildren(requestValues); 82 for (Node node : values) { 83 if ("address".equalsIgnoreCase(node.getLocalName())) { 84 address = node.getTextContent(); 85 } else if ("autoPayment".equalsIgnoreCase(node.getLocalName())) { 86 autoPayment = node.getTextContent(); 87 } else if ("creditCards".equalsIgnoreCase(node.getLocalName())) { 88 creditCards = node.getTextContent(); 89 } else if ("firstName".equalsIgnoreCase(node.getLocalName())) { 90 firstName = node.getTextContent(); 91 } else if ("insurance".equalsIgnoreCase(node.getLocalName())) { 92 insurance = node.getTextContent(); 93 } else if ("lastName".equalsIgnoreCase(node.getLocalName())) { 94 lastName = node.getTextContent(); 95 } else if ("otherPayments".equalsIgnoreCase(node.getLocalName())) { 96 otherPayments = node.getTextContent(); 97 } else if ("propertyTaxes".equalsIgnoreCase(node.getLocalName())) { 98 propertyTaxes = node.getTextContent(); 99 } else if ("salary".equalsIgnoreCase(node.getLocalName())) { 100 salary = node.getTextContent(); 101 } 102 } 103 104 MortgageRequest request = new MortgageRequest(firstName, lastName, 105 address, new Float (salary).floatValue(), new Float ( 106 propertyTaxes).floatValue(), new Float (insurance) 107 .floatValue(), new Float (autoPayment).floatValue(), 108 new Float (creditCards).floatValue(), new Float (otherPayments) 109 .floatValue()); 110 111 return request; 112 } 113 114 private Document parseXml(final String xmlString) throws HandlingException { 115 116 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory 117 .newInstance(); 118 docBuilderFactory.setNamespaceAware(true); 119 120 DocumentBuilder docBuilder = null; 121 122 try { 123 docBuilder = docBuilderFactory.newDocumentBuilder(); 124 } catch (ParserConfigurationException pce) { 125 throw new HandlingException("Bad XML parser configuration", pce); 126 } 127 128 Document document = null; 129 130 try { 131 document = docBuilder.parse(new ByteArrayInputStream (xmlString 132 .getBytes())); 133 } catch (SAXException saxe) { 134 throw new HandlingException( 135 "JBI descriptor is not well formed XML", saxe); 136 } catch (IOException ioe) { 137 throw new HandlingException("Can't read JBI descriptor", ioe); 138 } 139 return document; 140 } 141 142 155 private MortgageResponse executeWorkflow(String endpointName, 156 MortgageRequest request) throws HandlingException { 157 MortgageResponse response = null; 158 159 MortgageProfiling profiling = null; 160 if (MortgageWorkflowEngine.ENDPOINT1.equals(endpointName)) { 161 profiling = new SimpleMortgageProfiling(channel, logger); 162 } else if (MortgageWorkflowEngine.ENDPOINT2.equals(endpointName)) { 163 profiling = new DetailedMortgageProfiling(channel, logger); 164 } else { 165 throw new HandlingException( 166 "Given endpoint doesn't exist for MortgageWorkflow"); 167 } 168 response = profiling.orchestrate(request); 169 170 return response; 171 } 172 173 182 protected String createContentFromMortgageResponse(MortgageResponse response) { 183 184 StringBuffer soap = new StringBuffer (); 185 soap 186 .append("<orchestrateResponse xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"); 187 soap 188 .append("<orchestrateReturn xmlns:tns0=\"http://orchestration.workflow.mortgage.demo.petals.objectweb.org\" xsi:type=\"tns0:MortgageResponse\">"); 189 soap.append("<firstName xsi:type=\"xsd:string\">" 190 + response.getFirstName() + "</firstName>"); 191 soap.append("<lastName xsi:type=\"xsd:string\">" 192 + response.getLastName() + "</lastName>"); 193 soap.append("<mortgageRate xsi:type=\"xsd:float\">" 194 + response.getMortgageRate() + "</mortgageRate>"); 195 soap.append("</orchestrateReturn>"); 196 soap.append("</orchestrateResponse>"); 197 198 return soap.toString(); 199 } 200 201 } 202 | Popular Tags |