Powered By Blogger

Feb 15, 2014

Implementing a Data Service via WSO2 Developer Studio

This post describes the basic steps needed to implement a Data Service. As prerequisites you need to download and install WSO2 Developer Studio.

Scenario:

Let's assume you need to implement a simple login. In that case we are going to assume we have a MySQL database as follows with sample data.

Database name : TestUserDatabase
Table Name       : TestUser



  • Navigate to File -> New -> Maven Multi Module Project and click on Next.


  • Then provide relevant information and click Finish
  • Right click on the created project New -> Data Service Project and select option ' Create New Data Service' and click Next.
  • Then fill relevant information as below


  • In the next wizard, provide necessary data source configuration as listed below 

Database Engine: MySQL
Driver Class : com.mysql.jdbc.Driver
JDBC URL : jdbc:mysql://localhost:3306/TestUserDatabase
Username : [username according to MySQL credentials]
Password: [password according to MySQL credentials]



  • If you open the created UserData.dbs it will be displayed as below




  •  Now we want to add a select query to select the users in the TestUser table. Right click on the UserData and then select 'Add Query'
  • Then you can see a the query has been added as below
  • Now go to the properties window and add below information
  • Now right click on the added query and select 'Add Sql'
  • Then add below sql query in the window provided to insert SQL statement

  • Then we need to add input and output mapping, in order to do so right click on the query and select 'Add Input Mapping'
  • Then go to the properties window and add two params for UName and password as shown below, one after the other
  • Then right click on the query and then select 'Add Output Mapping' and go to the properties window and add following
  • Then right click on the created output mapping 'Users' and select  Add Output Mapping -> Add Element then add two elements for UName and password.


  • Finally we need to add an operation. Right click on the UserData and then select 'Add Operation' go to the properties window and add following
  • Then expand the created query 'GetUsers' and select call-query and in the properties window for the attribute 'href' give the query-id we have given for the select query (SelectUsers)
  • Then right click on the created call- query and select Add Input Mapping and go to the properties window and add following. This is the value that we are using for WHERE clause in the query. Since we need two values as UName and password, we have to add two input mappings

  • Final view would be as follows
  • Final configuration of the UserData.dbs is as follows

<data name="UserData" serviceGroup="UserData" serviceNamespace="org.user.com">
    <description/>
    <config id="test">
        <property name="org.wso2.ws.dataservice.user">root</property>
        <property name="org.wso2.ws.dataservice.password">root</property>
        <property name="org.wso2.ws.dataservice.protocol">jdbc:mysql://localhost:3306/TestUserDatabase</property>
        <property name="org.wso2.ws.dataservice.driver">com.mysql.jdbc.Driver</property>
    </config>
    <query id="SelectUsers" input-event-trigger="" output-event-trigger="" useConfig="test" keyColumns="">
        <sql dialect="">SELECT * FROM TestUser WHERE UName=? and password=?</sql>
        <param name="UName" ordinal="1" paramType="SCALAR" sqlType="STRING" type="IN"/>
        <param name="password" ordinal="2" paramType="SCALAR" sqlType="STRING" structType="" type="IN"/>
        <result defaultNamespace="" element="Users" rowName="user">
            <element column="UName" name="UName" xsdType="xs:string"/>
            <element column="password" name="password" xsdType="xs:string" query-param=""/>
        </result>
    </query>
    <operation name="GetUsers">
        <call-query href="SelectUsers">
            <with-param name="UName" query-param="UName"/>
            <with-param name="password" query-param=""/>
        </call-query>
    </operation>
</data>

No comments:

Post a Comment