KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > demo > mortgage > workflow > sdk > MortgageWorkflowImpl


1 package org.objectweb.petals.demo.mortgage.workflow.sdk;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.logging.Logger JavaDoc;
7
8 import javax.jbi.messaging.DeliveryChannel;
9 import javax.xml.parsers.DocumentBuilder JavaDoc;
10 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
11 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
21 import org.w3c.dom.Node JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 public class MortgageWorkflowImpl implements MortgageWorkflow {
25
26     private Logger JavaDoc logger;
27
28     private DeliveryChannel channel;
29
30     public MortgageWorkflowImpl(Logger JavaDoc logger, DeliveryChannel channel) {
31         super();
32         this.logger = logger;
33         this.channel = channel;
34     }
35
36     public String JavaDoc orchestrate(String JavaDoc endpointName, String JavaDoc content)
37             throws HandlingException {
38
39         /*
40          * retrieve request parameters
41          */

42         MortgageRequest request = retrieveRequestParameters(content);
43
44         /*
45          * execute workflow to process
46          */

47         MortgageResponse response = executeWorkflow(endpointName, request);
48
49         return createContentFromMortgageResponse(response);
50     }
51
52     /**
53      * Build a MortgageRequest bo from information contained into the String
54      * content (a Soap representation of the MortgageRequest).
55      *
56      * @param content
57      * the String content (a Soap representation of the
58      * MortgageRequest)
59      * @return a MortgageRequest bo. Can't be null.
60      * @throws HandlingException
61      * @throws Exception
62      */

63     protected MortgageRequest retrieveRequestParameters(String JavaDoc stringContent)
64             throws HandlingException {
65
66         String JavaDoc address = "";
67         String JavaDoc autoPayment = "";
68         String JavaDoc creditCards = "";
69         String JavaDoc firstName = "";
70         String JavaDoc insurance = "";
71         String JavaDoc lastName = "";
72         String JavaDoc otherPayments = "";
73         String JavaDoc propertyTaxes = "";
74         String JavaDoc salary = "";
75         
76         System.out.println(stringContent);
77
78         Document JavaDoc doc = parseXml(stringContent);
79         Node JavaDoc requestValues = doc.getDocumentElement().getFirstChild();
80
81         List JavaDoc<Node JavaDoc> values = XMLUtil.getNodeChildren(requestValues);
82         for (Node JavaDoc 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 JavaDoc(salary).floatValue(), new Float JavaDoc(
106                         propertyTaxes).floatValue(), new Float JavaDoc(insurance)
107                         .floatValue(), new Float JavaDoc(autoPayment).floatValue(),
108                 new Float JavaDoc(creditCards).floatValue(), new Float JavaDoc(otherPayments)
109                         .floatValue());
110
111         return request;
112     }
113
114     private Document JavaDoc parseXml(final String JavaDoc xmlString) throws HandlingException {
115
116         DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory
117                 .newInstance();
118         docBuilderFactory.setNamespaceAware(true);
119
120         DocumentBuilder JavaDoc docBuilder = null;
121
122         try {
123             docBuilder = docBuilderFactory.newDocumentBuilder();
124         } catch (ParserConfigurationException JavaDoc pce) {
125             throw new HandlingException("Bad XML parser configuration", pce);
126         }
127
128         Document JavaDoc document = null;
129
130         try {
131             document = docBuilder.parse(new ByteArrayInputStream JavaDoc(xmlString
132                     .getBytes()));
133         } catch (SAXException JavaDoc saxe) {
134             throw new HandlingException(
135                     "JBI descriptor is not well formed XML", saxe);
136         } catch (IOException JavaDoc ioe) {
137             throw new HandlingException("Can't read JBI descriptor", ioe);
138         }
139         return document;
140     }
141
142     /**
143      * Execute the workflow depending on the endpoint information : detailed
144      * workflow for "detailedMortgageProfiler" endpoint and simple workflow for
145      * "simpleMortgageProfiler" endpoint.
146      *
147      * @param endpointName
148      * name of the targeted endpoint
149      * @param request
150      * MortgageRequest bo
151      * @return MortgageResponse bo. Can't be null.
152      * @throws Exception
153      * if given endpoint doesn't exist for MortgageWorkflow service
154      */

155     private MortgageResponse executeWorkflow(String JavaDoc 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     /**
174      * Create a String content from the MortgageResponse. This String handles a
175      * Soap representation of the MortgageResponse bo.
176      *
177      * @param response
178      * the MortgageResponse bo.
179      * @return the String handling the Soap representation of the
180      * MortgageResponse. Can't be null.
181      */

182     protected String JavaDoc createContentFromMortgageResponse(MortgageResponse response) {
183
184         StringBuffer JavaDoc soap = new StringBuffer JavaDoc();
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