Showing posts with label Oracle Service Bus. Show all posts
Showing posts with label Oracle Service Bus. Show all posts

Wednesday, November 19, 2014

วิธีการเปลี่ยนแปลงค่า Atomic value ของ Element ในตัวแปร Body




ในตัวอย่างจะส่งค่า Inbound มาเป็น <a>111</a> และต้องการเปลี่ยนแปลงค่า 111 เป็นค่าของ Process ID ซึ่งได้จากการ Generate เองหรือได้จากการประยุกค์ใช้กับ Service Callout เพื่อให้ Service อื่น Generate ให้ได้เช่นกัน


Message Flow



































Assign Process ID stage:

Assign =>
<ins:processId xmlns:ins="http://test_processid/ins">
<ins1:returnCode xmlns:ins1="http://test_processid/ins1">0000020141119144</ins1:returnCode>
</ins:processId>



to variable => processid

*** สามารถเป็น Response Document Variable ของ Service Callout ได้

Replace Body variable:

Replace => //proc1:a
In XPath Expression Editor, add User Defined Namespaces
Prefix
URI
proc1 http://test_processid/proc1

in variable => body

with => <proc1:a>{$processid//ins1:returnCode/text()}</proc1:a>
In XPath Expression Editor, add User Defined Namespaces
Prefix
URI
proc1 http://test_processid/proc1
ins1 http://test_processid/ins1

and Replace entire node



Testing

Request document =
<proc1:pos xmlns:proc1="http://test_processid/proc1">
<proc1:a>111</proc1:a>
<proc1:b>222</proc1:b>
<proc1:c>333</proc1:c>
</proc1:pos>

Response document =
<proc1:pos xmlns:proc1="http://test_processid/proc1">
<proc1:a>0000020141119144</proc1:a>
<proc1:b>222</proc1:b>
<proc1:c>333</proc1:c>

</proc1:pos>

Friday, May 2, 2014

การสร้าง OSB กับ Web Services


Pre-requisites



Tools

  • สร้างด้วย Oracle Service Bus Console หรือ
  • สร้างด้วย OEPE (Eclipse IDE)


Instruction

Oracle Service Bus Console



Create Resources เลือก Resource from URL ตามรูป


ใส่ URL ของ Web Services ที่ต้องการ , ตั้งชื่อ และเลือก Resource Type เป็น WSDL จากนั้นกด Next
กด Import และ Activate ได้เลย


สร้าง Business Service จาก WSDL resource ที่ import มา


ปรากฏ Wizard ดังรูปข้างล่าง ให้เลือก Create a New Service => WSDL Web Service => Browse…


ให้เลือก Ports name=GlobalWeatherSoap
จากนั้นกด Next


จากนั้นกด Last>> เลยก็ได้ ถ้าไม่แก้ไขอะไรเพิ่มเติม
จากนั้นกด Save


สร้าง Proxy Service


ปรากฏหน้า Wizard  ให้เลือก Create From Existing Services => Business Service => Browse…


เลือก Business Service ที่ได้สร้างไว้ก่อนหน้านี้


จากนั้นกด Last>> และกด Save ในหน้าจอ ตามลำดับ
ถึงขั้นตอนนี้เราก็ได้ Service Inbound ของ OSB ที่เราต้องการแล้ว ต่อไปคือการ ทดสอบใช้งาน
ให้ไปที่ Proxy Service ที่เราได้สร้างไว้   กดที่ Icon “Lunch Test Console” ตามรูป
จะปรากฏหน้าจอ Test Console เนื่องจาก Web Service ตัวนี้มี 2 Operationห ให้เลือก Operations ตามรูปก่อน   จากนั้น Request Document จะเปลี่ยนไปเป็นตามรูป ให้ ทดสอบใส่ข้อมูล CityName=Don Muang และ CountryName=Thailand   และสุดท้ายก็กด Execute ที่ Icon ด้านบน


ได้ผลลัพธ์ ตามรูป สังเกต Response Document จะมีข้อมูลเกี่ยวกับสภาพอากาศของสถานที่ที่เราเรียกดูไป ซึ่งได้แก่ ดอนเมือง ประเทศไทย


Wednesday, September 18, 2013

การใช้งาน Message Format Language (MFL) และ File Transport



ตัวอย่าง
  • Mapping non-XML to XML
    • อ่าน CSV file แล้ว Insert เข้า Database (File transport - Proxy Service)
    • ส่ง Payload เป็น CSV แล้ว insert เข้า Database
  • Mapping XML to non-XML
    • Query database แล้ว save เป็น CSV file (File transport - Business Service)

ก่อนตัวอย่างทั้งหมด ให้สร้าง MFL file (Mapping file คล้าย XSL กับ XQ)
From
To
File name
non-XML
XML
EmployeeMessageFormat.mfl
XML
non-XML
EmployeeMessageFormat.mfl
** ในตัวอย่างสามารถใช้ file เดียวกันได้เลย
OEPE:
1) New MFL…
แก้ XML document encoding เป็น UTF-8


2) เพิ่มกลุ่มของ message เพราะอาจส่งข้อมูล Employee มากกว่า 1 คน
3) เพิ่ม Fields ที่ต้องใช้ได้แก่
a) field name ‘title’ ระบุเป็น Child ของ Group

b) field name ‘firstname’
c) field name ‘surname’
d) field name ‘ouid’
e) field name ‘birthdate’
f) field name ‘nationid’ ซึ่งเป็น field สุดท้าย ให้ปิดท้ายด้วย \n (ขึ้นบรรทัดใหม่)

4) ทดสอบ MFL transformation ด้วย CSV  
**** ข้อควรระวัง บรรทัดสุดท้ายต้องเป็นบรรทัดว่าง ด้วยการกด ENTER ไม่เช่นนั้นจะไม่สามารถ Transform CSV นั้นได้
นาย;สมชาย;แซ่ลี้;10;2013-09-16T18:01:33;1112222233333
นาง;สมศรี;วงศ์ไทย;10;2013-09-16T18:04:55;2112222233333
นาย;สมศักดิ์;จันดี;10;2013-09-16T18:09:01;3112222233333
นาง;สมหญิง;เก่งงาน;10;2013-09-16T18:13:10;4112222233333

*
กรณีไม่ \n หรือมีมากกว่า 1 จะเกิด Error





หลังจากได้ MFL file แล้วให้สร้าง XQuery transformation
สำหรับตัวอย่างที่ 1 อ่าน CSV file แล้ว Insert เข้า Database
Source
Target
File name
EmployeeMessageFormat
MergeEmployeeService1
EmployeeMessageFormat_To_MergeEmployeeService1.xq

สำหรับตัวอย่าง 3 Query database แล้ว save เป็น CSV file
Source
Target
File name
QueryEmployeeService1
EmployeeMessageFormat
QueryEmployeeService1_To_EmployeeMessageFormat.xq
WriteCSVService1
QueryEmployee
WriteCSVService1_To_QueryEmployee.xq

OEPE:
EmployeeMessageFormat_To_MergeEmployeeService1.xq
(:: pragma bea:mfl-element-parameter parameter="$employeeMessageFormat1" type="EmployeeMessageFormat@" location="../mfl/EmployeeMessageFormat.mfl" ::)
(:: pragma bea:global-element-return element="ns0:EmployeeBakCollection" location="../../Adapter/xsd/MergeEmployeeService1_table.xsd" ::)


declare namespace ns0 = "http://xmlns.oracle.com/pcbpel/adapter/db/top/MergeEmployeeService1";
declare namespace xf = "http://tempuri.org/DatabaseTX1/Service/xq/EmployeeMessageFormat_To_MergeEmployeeService1/";
declare namespace funcSQL = "http://www.v-smart.com/function/xq/funcSQL";


declare function funcSQL:nextVal() as xs:decimal {
let $val := fn-bea:execute-sql('jdbc/OSBDemoXE','value','SELECT EMP_SEQ.NEXTVAL FROM DUAL')
return $val
};


declare function xf:EmployeeMessageFormat_To_MergeEmployeeService1($employeeMessageFormat1 as element())
   as element(ns0:EmployeeBakCollection) {
       <ns0:EmployeeBakCollection>
           {
               for $Employee in $employeeMessageFormat1/Employee
               return
                   <ns0:EmployeeBak>
                   <ns0:id>{ funcSQL:nextVal() }</ns0:id>
                   <ns0:firstname>{ data($Employee/firstname) }</ns0:firstname>
                   <ns0:surname>{ data($Employee/surname) }</ns0:surname>
                   <ns0:ouId>{ xs:decimal($Employee/ouid/text()) }</ns0:ouId>
                   <ns0:birthDate>{ xs:dateTime($Employee/birthdate/text()) }</ns0:birthDate>
                   <ns0:nationalId>{ data($Employee/nationid) }</ns0:nationalId>
                       
                   </ns0:EmployeeBak>
           }
       </ns0:EmployeeBakCollection>
};


declare variable $employeeMessageFormat1 as element() external;


xf:EmployeeMessageFormat_To_MergeEmployeeService1($employeeMessageFormat1)


QueryEmployeeService1_To_EmployeeMessageFormat.xq
(:: pragma bea:global-element-parameter parameter="$queryEmployeeService1OutputCollection1" element="ns0:QueryEmployeeService1OutputCollection" location="../../Adapter/xsd/QueryEmployeeService1.xsd" ::)
(:: pragma bea:mfl-element-return type="EmployeeMessageFormat@" location="../mfl/EmployeeMessageFormat.mfl" ::)


declare namespace ns0 = "http://xmlns.oracle.com/pcbpel/adapter/db/QueryEmployeeService1";
declare namespace xf = "http://tempuri.org/DatabaseTX1/Service/xq/QueryEmployeeService1_To_EmployeeMessageFormat/";


declare function xf:QueryEmployeeService1_To_EmployeeMessageFormat($queryEmployeeService1OutputCollection1 as element(ns0:QueryEmployeeService1OutputCollection))
   as element() {
       <EmployeeMessageFormat>
           {
               for $QueryEmployeeService1Output in $queryEmployeeService1OutputCollection1/ns0:QueryEmployeeService1Output
               return
                   <Employee>
                    <title/>
                       <firstname>{ data($QueryEmployeeService1Output/ns0:EMP_FIRSTNAME) }</firstname>
                       <surname>{ data($QueryEmployeeService1Output/ns0:EMP_SURNAME) }</surname>
                       <ouid>{ xs:string($QueryEmployeeService1Output/ns0:OU_ID) }</ouid>
                       <birthdate>{ xs:string($QueryEmployeeService1Output/ns0:BIRTH_DATE) }</birthdate>
                       <nationid>{ data($QueryEmployeeService1Output/ns0:NATIONAL_ID) }</nationid>
                   </Employee>
           }
       </EmployeeMessageFormat>
};


declare variable $queryEmployeeService1OutputCollection1 as element(ns0:QueryEmployeeService1OutputCollection) external;


xf:QueryEmployeeService1_To_EmployeeMessageFormat($queryEmployeeService1OutputCollection1)


WriteCSVService1_To_QueryEmployee.xq
xquery version "1.0" encoding "Cp1252";
(:: pragma  parameter="$anyType1" type="xs:anyType" ::)
(:: pragma bea:global-element-return element="ns0:QueryEmployeeService1Input" location="../../Adapter/xsd/QueryEmployeeService1.xsd" ::)


declare namespace xf = "http://tempuri.org/DatabaseTX1/Service/xq/WriteCSVService1_To_QueryEmployee/";
declare namespace ns0 = "http://xmlns.oracle.com/pcbpel/adapter/db/QueryEmployeeService1";


declare function xf:WriteCSVService1_To_QueryEmployee($anyType1 as element(*))
   as element(ns0:QueryEmployeeService1Input) {
       <ns0:QueryEmployeeService1Input/>
};


declare variable $anyType1 as element(*) external;


xf:WriteCSVService1_To_QueryEmployee($anyType1)






*********** เริ่มต้น
ตัวอย่างที่ 1 อ่าน CSV file แล้ว Insert เข้า Database
สร้าง Proxy Service และกำหนดให้ Proxy service อ่าน CSV file จาก disk เพื่อนำมาเป็น Inbound ของ Service
แก้ไข default Service type เป็น Message Service
แก้ไขเป็น Text
แก้ไข Protocol = file และกำหนด Endpoint เป็น Directory เก็บ CSV file
กำหนด
File mask = *.csv (filter ชื่อ file ต่อท้ายด้วย .csv)
Polling interval = 10 (อ่าน file จาก Directory ทุกๆ 10 วินาที)
Read limit = 1 (หากเจอ file มากกว่า 1 file จะเปิดอ่านที่ละ file)
Uncheck Sort By Arrival (ให้อ่าน file ที่มาก่อนหรือไม่)
Post Read Action = archive (หากอ่าน file เสร็จให้ move ไปที่ Archive Directory)
Request Encoding = UTF-8 (default)
จากนั้นระบุ Stage, Archive และ Error Directory

สร้าง Message Flow
Transform_To_XML stage
Inside stage



Merge stage หรือ Insert เข้า Database
Inside stage

Completed Message Flow
ตัวอย่างที่ 2 ส่ง Payload เป็น CSV format แล้ว insert เข้า Database
สร้าง Proxy Service กำหนดให้รับ XML Payload ที่มี Value เป็น CSV string ตัวอย่าง Payload ที่ใช้ในการทดสอบ ได้แก่
<employee>นาย;สมชาย;แซ่ลี้;10;2013-09-16T18:01:33;1112222233333
นาง;สมศรี;วงศ์ไทย;10;2013-09-16T18:04:55;2112222233333
นาย;สมศักดิ์;จันดี;10;2013-09-16T18:09:01;3112222233333
นาง;สมหญิง;เก่งงาน;10;2013-09-16T18:13:10;4112222233333
</employee>

Transform_To_XML1 stage
Inside stage

*** Recommendation: OSB treats "$body/*[1]" as a special XPath that can be evaluated without invoking the XQuery engine. This is faster than specifying an absolute path pointing to the child of $body. A general XPath like "$body/Order" must be evaluated by the XQuery engine before the primary transformation resource is executed.

Merge1 stage จะเหมือนกับ Merge stage ในตัวอย่างที่ 1 เพราะมันจะต่างแค่ Stage ที่รับ Inbound และ MFL เท่านั้น











ตัวอย่างที่ 3 Query database แล้ว save เป็น CSV file
ก่อนอื่นให้สร้าง Business Service สำหรับ Write file operation ก่อน

สร้าง Proxy Service กำหนดให้ Query Database แล้ว Save เป็น CSV file ลง Disk

QueryEmployee Stage
Inside stage
Transform_To_CSV stage
Inside stage
WriteCSVFile stage
Inside stage
Completed Message Flow
ตัวอย่าง Output file คือ
test2013-09-17T10:07:00.407-07:00.txt