本文提出了加快基于 XML 的面向服務(wù)的體系結(jié)構(gòu)(Service Oriented Architecture,SOA)應(yīng)用程序表示開發(fā)的解決方案系列文章的第 2 部分。這種解決方案包括 Eclipse 功能,可以生成靜態(tài)類型的用于 XSD Schema 的服務(wù)對象數(shù)據(jù)(Service Data Object,SDO),并提供了在表示元素數(shù)據(jù)與 XML 數(shù)據(jù)相互轉(zhuǎn)換中使用 SDO 的運行框架。第 2 部分增強了在第 1 部分中開發(fā)的解決方案。
引言
這個由五部分組成的系列文章提出了能夠加快基于 XML 的 SOA 應(yīng)用程序表示開發(fā)的解決方案,這種解決方案包括 Eclipse 功能,可以生成靜態(tài)類型的用于 XSD Schema 的服務(wù)對象數(shù)據(jù)(Service Data Object,SDO),并提供了在表示元素數(shù)據(jù)與 XML 數(shù)據(jù)相互轉(zhuǎn)換中使用 SDO 的運行框架。在該系列文獻(xiàn)的 第 1 部分 中,我們經(jīng)歷了通過使用提供的插件集來向每個指定的 XML schema 中的服務(wù)傳遞信息的簡單 JSF 應(yīng)用程序的整個開發(fā)過程,我們使用的應(yīng)用程序場景進(jìn)行了一次服務(wù)調(diào)用——發(fā)出一個 XML 請求及收到一個 XML 響應(yīng)。這個應(yīng)用程序說明了主從復(fù)合結(jié)構(gòu)的數(shù)據(jù)對象視圖,連同分頁及分類的能力。
在第 2 部分中,我們將擴展在 第 1 部分 中開發(fā)的解決方案,使之包括創(chuàng)建、更新和刪除功能,包括用本地附加的變量及基本的轉(zhuǎn)換來自定義生成 SDO。該文章將舉例說明多 schema 模型、對于單一頁面中多個請求及響應(yīng)的場景,并且說明綁定 XML 數(shù)據(jù)的 JSF 下拉式控制。
IBM? Rational? Application Developer V6 以及 Websphere? Application Server V6 需要利用這種轉(zhuǎn)換功能。本文假定讀者是精通 Rational Application Developer 的 JSF 頁面設(shè)計師。
安裝 XSD SDO 轉(zhuǎn)換功能
為了安裝 XSD SDO 的轉(zhuǎn)換功能,請按照 第 1 部分 中的詳細(xì)步驟操作。
場景
我們的場景擴展了第 1 部分 中開發(fā)的保險應(yīng)用程序,使之包括創(chuàng)建、更新以及刪除功能,本地附加的變量,基本的轉(zhuǎn)換和 下拉式綁定的功能。在該場景中:
xsd_sdo_soa_part2_listings.zip
清單 1. BrokerService.xsd
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http:///xyz.brokerservice.ecore" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http:///xyz.brokerservice.ecore"> <xsd:annotation> <xsd:documentation xml:lang="en"> Broker Service Schema for xyz.com. Copyright 2004 ibm.com. All rights reserved. </xsd:documentation> </xsd:annotation> <xsd:element name="brokerService" type="brokerServiceType"/> <xsd:complexType name="brokerServiceType"> <xsd:sequence> <xsd:element minOccurs="0" name="broker" type="brokerType"/> <xsd:element minOccurs="0" name="error" type="errorType"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="brokerType"> <xsd:sequence> <xsd:element minOccurs="0" ref="firstName"/> <xsd:element minOccurs="0" ref="lastName"/> <xsd:element minOccurs="0" name="loginName" type="xsd:string"/> <xsd:element minOccurs="0" name="loginPassword" type="xsd:string"/> <xsd:element maxOccurs="unbounded" minOccurs="0" name="client" type="clientType"/> </xsd:sequence> <xsd:attribute name="brokerId" type="xsd:string" use="required"/> </xsd:complexType> <xsd:complexType name="clientType"> <xsd:sequence> <xsd:element minOccurs="0" ref="firstName"/> <xsd:element minOccurs="0" ref="lastName"/> <xsd:element minOccurs="0" name="dateOfBirth" type="xsd:date"/> <xsd:element minOccurs="0" name="currentAddress" type="addressType"/> <xsd:element minOccurs="0" name="permanentAddress" type="addressType"/> <xsd:element maxOccurs="unbounded" minOccurs="0" name="policy" type="policyType"/> </xsd:sequence> <xsd:attribute name="clientId" type="xsd:string" use="required"/> </xsd:complexType> <xsd:complexType name="addressType"> <xsd:sequence> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="state" type="xsd:string"/> <xsd:element name="zip" type="xsd:string"/> <xsd:element name="country" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="policyType"> <xsd:sequence> <xsd:element name="policyName" type="xsd:string"/> <xsd:element name="policyStartDate" type="xsd:date"/> <xsd:element name="policyEndDate" type="xsd:date"/> <xsd:element name="policyAmount" type="xsd:string"/> <xsd:element name="policyDescription" type="xsd:string"/> </xsd:sequence> <xsd:attribute name="policyId" type="xsd:string" use="required"/> </xsd:complexType> <xsd:complexType name="errorType"> <xsd:sequence> <xsd:element name="errorCode" type="xsd:string"/> <xsd:element name="errorDescription" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:element name="firstName" type="xsd:string"/> <xsd:element name="lastName" type="xsd:string"/> </xsd:schema>
清單 2. ValidCodesService.xsd
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http:///xyz.validcodesservice.ecore" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http:///xyz.validcodesservice.ecore"> <xsd:annotation> <xsd:documentation xml:lang="en"> Valid Codes Service Schema for xyz.com. Copyright 2004 ibm.com. All rights reserved. </xsd:documentation> </xsd:annotation> <xsd:element name="validCodesService" type="validCodesServiceType"/> <xsd:complexType name="validCodesServiceType"> <xsd:sequence> <xsd:element minOccurs="0" name="stateList" type="stateListType"/> <xsd:element minOccurs="0" name="policyCodeList" type="policyCodeListType"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="stateListType"> <xsd:sequence> <xsd:element maxOccurs="unbounded" minOccurs="0" name="state" type="stateType"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="stateType"> <xsd:sequence> <xsd:element name="shortName" type="xsd:string"/> <xsd:element name="fullName" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="policyCodeListType"> <xsd:sequence> <xsd:element maxOccurs="unbounded" minOccurs="0" name="policyCode" type="policyCodeType"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="policyCodeType"> <xsd:sequence> <xsd:element name="shortName" type="xsd:string"/> <xsd:element name="fullName" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
BrokerUpdate 場景
該場景為現(xiàn)有的 brokerupdate.jsp JavaServer Faces JSP 頁增添了新的功能。該頁已經(jīng)發(fā)送請求以獲得代理人的詳細(xì)資料(第 1 部分 中的 brokerDetailRequest.xml),并且接收到了包含代理人的所有客戶和他們的策略的響應(yīng)信息(第 1 部分中的brokerDetailResponse.xml)。該頁也將會調(diào)用有效的代碼服務(wù)來檢索有效的代碼值作為一個 XML 響應(yīng)(清單 5 中的 allValidCodesResponse.xml),同時也能夠創(chuàng)建、更新和刪除策略,還能夠演示除了用于基本轉(zhuǎn)換的來自已經(jīng)生成的 SDO 的行為之外的操作行為。轉(zhuǎn)換作為 brokerUpdateRequest.xml (清單 3)發(fā)送到該服務(wù),對操作成功或者失敗的響應(yīng)作為brokerUpdateResponse.xml (清單 4)接收。 brokersummary.jsp 頁在檢索來自服務(wù)更新后的改動之后將顯示出來。
brokerupdate.jsp
brokerDetailRequest.xml
brokerDetailResponse.xml
allValidCodesResponse.xml
brokerUpdateRequest.xml
brokerUpdateResponse.xml
brokersummary.jsp
Listing 3. brokerUpdateRequest.xml
<?xml version="1.0" encoding="UTF-8"?> <brokerService xmlns="http:///xyz.brokerservice.ecore" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <broker brokerId="000-00-9999"> <firstName>Narinder</firstName> <lastName>Makin</lastName> <client clientId="001-00-9999"> <firstName>Dan</firstName> <lastName>Moore</lastName> <dateOfBirth>1967-08-13</dateOfBirth> <currentAddress> <street>113 Oak Pine St.</street> <city>Santa Clara</city> <state>LA</state> <zip>91929</zip> <country>US</country> </currentAddress> <permanentAddress> <street>123 Demi Lane</street> <city>Cary</city> <state>NC</state> <zip>22999</zip> <country>US</country> </permanentAddress> <policy policyId="L000000000"> <policyName>Life</policyName> <policyStartDate>2004-01-01</policyStartDate> <policyEndDate>2005-01-01</policyEndDate> <policyAmount>200000.00</policyAmount> <policyDescription>Life Insurance</policyDescription> </policy> <policy policyId="H000000000"> <policyName>House</policyName> <policyStartDate>2004-01-01</policyStartDate> <policyEndDate>2005-01-01</policyEndDate> <policyAmount>20000.00</policyAmount> <policyDescription>Home Insurance</policyDescription> </policy> <policy policyId="C000000001"> <policyName>Car 1</policyName> <policyStartDate>2004-01-01</policyStartDate> <policyEndDate>2005-01-01</policyEndDate> <policyAmount>15000.00</policyAmount> <policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription> </policy> <policy policyId="C000000002"> <policyName>Car 2</policyName> <policyStartDate>2004-01-01</policyStartDate> <policyEndDate>2005-01-01</policyEndDate> <policyAmount>5000.00</policyAmount> <policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription> </policy> <policy policyId="B000000002"> <policyName>Restaurant</policyName> <policyStartDate>2004-01-01</policyStartDate> <policyEndDate>2005-01-01</policyEndDate> <policyAmount>25000.00</policyAmount> <policyDescription>Business Insurance - Restaurant</policyDescription> </policy> <policy policyId="B000000003"> <policyName>Golf Course</policyName> <policyStartDate>2004-01-01</policyStartDate> <policyEndDate>2005-01-01</policyEndDate> <policyAmount>25000.00</policyAmount> <policyDescription>Second Business Insurance - Golf Course</policyDescription> </policy> </client> <client clientId="002-00-9999"> <firstName>Tom</firstName> <lastName>Cross</lastName> <dateOfBirth>1970-11-11</dateOfBirth> <currentAddress> <street>113 Duke St.</street> <city>Shelton</city> <state>CT</state> <zip>08989</zip> <country>US</country> </currentAddress> <permanentAddress> <street>123 Lex Lane</street> <city>Fairfield</city> <state>NY</state> <zip>09833</zip> <country>US</country> </permanentAddress> <policy policyId="L100000000"> <policyName>Life</policyName> <policyStartDate>2004-01-01</policyStartDate> <policyEndDate>2005-01-01</policyEndDate> <policyAmount>100000.00</policyAmount> <policyDescription>Life Insurance</policyDescription> </policy> <policy policyId="H100000000"> <policyName>House</policyName> <policyStartDate>2004-01-01</policyStartDate> <policyEndDate>2005-01-01</policyEndDate> <policyAmount>2000.00</policyAmount> <policyDescription>Home Insurance</policyDescription> </policy> <policy policyId="C100000001"> <policyName>Car 1</policyName> <policyStartDate>2004-01-01</policyStartDate> <policyEndDate>2005-01-01</policyEndDate> <policyAmount>2000.00</policyAmount> <policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription> </policy> </client> </broker> </brokerService>
清單 4. brokerUpdateResponse.xml
<?xml version="1.0" encoding="UTF-8"?> <brokerService xmlns="http:///xyz.brokerservice.ecore" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <broker brokerId="099-99-9999"/> <error> <errorCode>0</errorCode> <errorDescription>Success: The changes have been successfully applied to the policy.</errorDescription> </error> </brokerService>
清單 5. allValidCodesResponse.xml
<?xml version="1.0" encoding="UTF-8"?> <validCodesService xmlns="http:///xyz.validcodesservice.ecore" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <stateList> <state> <shortName>CT</shortName> <fullName>Connecticut</fullName> </state> <state> <shortName>LA</shortName> <fullName>Los Angelas</fullName> </state> <state> <shortName>NC</shortName> <fullName>North Carolina</fullName> </state> <state> <shortName>CA</shortName> <fullName>California</fullName> </state> <state> <shortName>NH</shortName> <fullName>New Hampshire</fullName> </state> <state> <shortName>VA</shortName> <fullName>Virginia</fullName> </state> </stateList> <policyCodeList> <policyCode> <shortName>H</shortName> <fullName>House</fullName> </policyCode> <policyCode> <shortName>C</shortName> <fullName>Car</fullName> </policyCode> <policyCode> <shortName>B</shortName> <fullName>Business</fullName> </policyCode> </policyCodeList> </validCodesService>
應(yīng)用程序設(shè)計流
圖 4 描繪了用于該場景的應(yīng)用程序設(shè)計流。
運行時架構(gòu)
一旦頁請求到來,就會發(fā)生以下事件 (圖 5):
實現(xiàn)解決方案
在下面的章節(jié)中詳細(xì)描述了實現(xiàn)該解決方案的步驟:
將項目轉(zhuǎn)換 xsd_sdo_soa_xml_tutorial.zip 下載文件 導(dǎo)入到 Application Developer 的工作區(qū)中。因此,下面的項目將被導(dǎo)入(圖 6):
xsd_sdo_soa_xml_tutorial.zip
BrokerService.xsd
BrokerUpdateRoot.java
BrokerSummaryRoot.java
在 Application Developer 中,在 ValidCodesService.xsd 上單擊右鍵,然后選擇 Create SDO Package(圖 7)。
因此,將產(chǎn)生如圖 8 所示的情景。
該包的名稱來自 targetNamespace,它在 XSD 聲明的 schema 中定義(圖 9)。
用戶可以修改已經(jīng)生成的 SDO,使之包含特定的變量或者行為。當(dāng)再次生成該 SDO 以用于同一個 schema 的命名空間時,任何沒有使用 @generated 標(biāo)簽注解的方法或者變量將不能被替換。用戶也可以將這樣的文件標(biāo)記為 non-derived 類型,以便項目 "clean" 時刪除這些文件。
@generated
xyz.brokerservice.PolicyType
/** * Get the value for selected */ boolean getSelected(); /** * Set the value for selected */ void setSelected(boolean selected);
xyz.brokerservice.impl.PolicyTypeImpl
/** * add selected attribute as local variable. */ protected boolean selected = false; /** * Get the value for selected */ public boolean getSelected(){ return selected; } /** * Set the value for selected */ public void setSelected(boolean selected){ this.selected = selected; }
xyz.brokerservice.ClientType
/** * TotalPolicyAmount is computed by summation of the client's Policy Amounts */ double getTotalPolicyAmount();
xyz.brokerservice.impl.ClientTypeImpl
/** * totalPolicyAmount is computed by summation of the client's Policy Amounts */ public double getTotalPolicyAmount(){ double totalPolicyAmount = 0.00; for(int index=0; index < getPolicy().size(); ++index){ PolicyType policyType = (PolicyType)getPolicy().get(index); if(policyType.getPolicyAmount() != null){ double temp = 0.00; try{ temp = Double.parseDouble(policyType.getPolicyAmount()); }catch(NumberFormatException ex){ } totalPolicyAmount += temp; } } return totalPolicyAmount; }
ValidCodesServiceRoot.java 文件包含了計劃命名空間 URI 的注冊,用于有效代碼服務(wù)的屬性和它的獲取/設(shè)置方法。通過調(diào)用這些有效的代碼服務(wù)來慢慢地裝載所有有效的代碼。在 brokerservice.root 包中添加 ValidCodesServiceRoot.java Java 類:
ValidCodesServiceRoot.java
brokerservice.root
package brokerservice.root; import org.eclipse.emf.ecore.EPackage; import proxy.ValidCodesServiceProxy; import xyz.validcodesservice.DocumentRoot; import xyz.validcodesservice.ValidCodesServiceType; import xyz.validcodesservice.ValidcodesservicePackage; import dw.ibm.etools.xsd.sdo.xmltransformservice.XMLTransformServiceFactory; public class ValidCodesServiceRoot extends BaseRoot{ protected ValidCodesServiceType validCodesServiceRoot; static{ EPackage.Registry.INSTANCE.put( ValidcodesservicePackage.eINSTANCE.getNsURI() , ValidcodesservicePackage.eINSTANCE); } protected void loadValidCodesResponse(String response){ DocumentRoot docRoot= (DocumentRoot) XMLTransformServiceFactory.INSTANCE.load(response); validCodesServiceRoot = docRoot.getValidCodesService(); } public ValidCodesServiceType getValidCodesServiceRoot() { if(validCodesServiceRoot == null){ preLoadValidCodes(); } return validCodesServiceRoot; } public void setValidCodesServiceRoot( ValidCodesServiceType validCodesServicetRoot) { this.validCodesServiceRoot = validCodesServicetRoot; } protected void preLoadValidCodes(){ String response = ValidCodesServiceProxy.invoke( ValidCodesServiceProxy.ALL_REQUEST); loadValidCodesResponse(response); } }
更新 brokerservice.root 包中現(xiàn)有的BrokerUpdateRoot.java Java 類 ,該包提供了相關(guān)的下載文件,或者更新帶有以下附加物的文件:
public void applyPolicyChanges(){ //unset all the features that are empty String xmlData = XMLTransformServiceFactory.INSTANCE.convert((DataObject)brokerServiceRoot, true); String response = BrokerServiceProxy.invoke(xmlData, BrokerServiceProxy.BROKERUPDATE_REQUEST); loadBrokerDetailUpdateResponse(response); }
protected void loadBrokerDetailUpdateResponse(String response){ DocumentRoot docRoot= (DocumentRoot) XMLTransformServiceFactory.INSTANCE.load(response); BrokerServiceType brokerdetailUpdateRespRoot = docRoot.getBrokerService(); //set the error type brokerServiceRoot.setError(brokerdetailUpdateRespRoot.getError()); }
public void addNewPolicy(ClientType clientType){ clientType.createPolicy(); }
public void deleteSelectedPolicy(ClientType clientType){ for(int index=clientType.getPolicy().size()-1; index >= 0; --index){ PolicyType policyType = ((PolicyType)clientType.getPolicy().get(index)); if(policyType.getSelected()){ clientType.getPolicy().remove(policyType); } } }
編輯 brokerupdate.jsp 的 Brokerupdate.java 頁代碼文件,并添加下列方法作為命令行為來使用。(這些方法中使用的控制數(shù)據(jù)訪問器在下一步中定義。)
Brokerupdate.java
BrokerUpdateRoot.class
public String doAddNewPolicyAction(){ this.getVarBrokerUpdateRootBean().addNewPolicy((ClientType)getTable1().getRowData()); //returning empty string re-displays the page with same data binding return ""; }
public String doDeleteSelectedPolicyAction(){ this.getVarBrokerUpdateRootBean().deleteSelectedPolicy((ClientType)getTable1().getRowData()); return ""; }
public String doUpdatePolicyAction(){ return ""; }
public String doApplyPolicyChangesAction(){ this.getVarBrokerUpdateRootBean().applyPolicyChanges(); this.sessionScope.remove("varBrokerUpdateRootBean"); //a navigation rule is defined for this return return "applyChanges"; }
http://localhost:9080/XYZInsuranceWeb/faces/brokerupdate.jsp
結(jié)束語
在該系列文章的第 2 部分中,我們描述了加速開發(fā)用于基于 XML 的應(yīng)用程序的 JSF 的解決方案。這個解決方案提供了 Eclipse 功能以及框架工具,在設(shè)計時可用其將 XML Schema 轉(zhuǎn)換成包括靜態(tài)服務(wù)對象數(shù)據(jù)的 Java 包,在運行時可用其將 XML 實例文件與這些 SDO 相互轉(zhuǎn)化。SDO 包裝成 JavaBean,連同 JavaServer Faces 一起被用于表示開發(fā)。本文也提供了擴展保險應(yīng)用程序的基本場景所需的通用步驟,該應(yīng)用程序包括新建、更新以及刪除的功能,本地附加的變量,基本的轉(zhuǎn)換和下拉式綁定。這些增強實現(xiàn)了單一頁面中多 schema 模型和多個請求及響應(yīng)的場景。
在該系列文章中將介紹:
下載
posted on 2006-04-17 03:17 wsdfsdf 閱讀(282) 評論(0) 編輯 收藏 引用 所屬分類: 技術(shù)文章
Powered by: C++博客 Copyright © wsdfsdf