Section |
---|
|
Column |
---|
| Include Page |
---|
| DAS Java Subproject Menu |
---|
| DAS Java Subproject Menu |
---|
|
|
Column |
---|
| Starting with DASThis |
|
...
page provides links to DAS samples and explains a simple "how to" and gives step-by-step instructions to check one of the DAS features - ColumnConverter. Other DAS features can be tried on similar lines. Many sample examples demonstrating different DAS features are available on https://svn.apache.org/repos/asf/incubator/tuscany/java/das/samples e.g. - companyweb-webapp - tomcat based web sample
- customer - J2SE based standalone application
Check readme files e.g. For companyweb https://svn.apache.org/repos/asf/incubator/tuscany/java/das/samples/company-webapp/readme.htm for basic steps on how to get the sample running. Once the sample is running, parts of sample can be modified based on the requirement or new code can be added from-scratch. Many features supported by DAS are listed in User Guide - http://incubator.apache.org/tuscany/rdb-das-user-guide.html This document details the changes needed to different part of the web sample (companyweb) to experiment with more features of DAS than just basic CRUD.Let us take example of checking how to use ColumnConverters. For details on feature testing, check Tests under https://svn.apache.org/repos/asf/incubator/tuscany/java/das/rdb/src/test/ 1> As a first step you need to implement Converter interface in a class to achieve the required column conversion. Check org.apache.tuscany.das.rdb.test.mappings.StringObfuscationConverter from the svn repository for example of converter. Make your converter class available in (Tomcat root)webapps/companyweb-webapp/WEB-INF/classes. 2> DAS functions based on external Configuration, e.g. CompanyConfig.xml file in companyweb sample. The purpose of this Config file to supply information for DataSource connection, Commands (SQL) (that DAS can execute against Database) and Database schema like - Table/Columns, Relationship and so forth. Please check Architecture Guide for complete details about Config.xsd () at http://incubator.apache.org/tuscany/rdb-das-architecture-guide.html. Modify Config to get converter working. The <ConnectionInfo> element should match the resource name from server.xml for DataSource.Add a <Table> which has a column with converter class name of the class you just created. Add a <Command> with kind="Select" for this table/column e.g. No Format |
---|
<Command name="getFirstCustomer" SQL="Select * from CUSTOMER where ID = 1 |
|
|
A. Initial Setup
- Install Tomcat(latest version) and MySQL(latest version). Please indicate which version of the Tomcat and MySQL is this tested with?
- Download dependencies and libraries Please add a link to this information. Which dependencies and libraries?
- Create companyweb directory in (Tomcat root)/webapps/. EX: (Tomcat root)/webapps/companyweb
- Create the directory (Tomcat root)/webapps/companyweb/WEB-INF/lib and copy the libraries into it. As an alternative, you can copy the libraries into (Tomcat root)/common/lib directory.
- The required libraries are:
i) common-(latest version).jar
ii) ecore-(latest version).jar
iii) ecore-change-(latest version).jar
iv) ecore-xmi-(lateste version).jar
v) log4j-(latest version).jar
vi) sdo-api-xxx.jar
vii) tuscany-das-rdb-xxx.jar
viii) tuscany-sdo-xxx.jar
ix) xsd-(latest version).jar
x) mysql-connector-java-(latest version).jar -> This is the JDBC connector, It'll be used to connect to Mysql database
B. Creating CompanyWeb Database
After setting up your enviroment, the next step will be to create the databse where the DAS will interact with.
- Run the following commands in the MySQL Command Shell
create database companyweb; -> This command will create the CompanyWeb database
use companyweb; -> Set the shell to work with companyweb database - Create a text file and name it as "comapnyweb.sql", you should insert the following lines in this file
No Format |
---|
CREATE TABLE EMPLOYEE (
ID INTEGER NOT NULL AUTO_INCREMENT, NAME VARCHAR(30), SN VARCHAR(10), MANAGER SMALLINT, DEPARTMENTID INTEGER, PRIMARY KEY (ID)
);
CREATE TABLE DEPARTMENT (
ID INTEGER NOT NULL AUTO_INCREMENT, NAME VARCHAR(30), LOCATION VARCHAR(30), NUMBER VARCHAR(10), COMPANYID INTEGER, EOTM INTEGER, PRIMARY KEY (ID)
);
CREATE TABLE COMPANY (
ID INTEGER NOT NULL AUTO_INCREMENT, NAME VARCHAR(30), PRIMARY KEY (ID)
);
CREATE UNIQUE INDEX SQL060217085530980 ON COMPANY (ID ASC);
CREATE UNIQUE INDEX SQL060217085531710 ON DEPARTMENT (ID ASC);
INSERT INTO COMPANY VALUES (51, "ACME Publishing");
INSERT INTO COMPANY VALUES (52, "Do-rite plumbing");
INSERT INTO COMPANY VALUES (53, "MegaCorp");
|
- Run the following command in the Mysql Shell to execute the script that creates tables, constraints, etc.
Code Block |
---|
source (path)/companyweb.sql
|
C. Creating XML configuration file
Would be nice to explain what this file is used for
...
...
...
...
<Column columnName="ID" primaryKey |
|
|
...
...
...
...
D. Creating the class and jsp which manages the DAS/SDO features
What do you mean by a class manages DAS/SDO features? Do you mean, a class that manipulates or uses DAS/SDO features?
- Create the file CompanyClient.java in the directory (Tomcat root)/webapps/companyweb/WEB-INF/classes/org/apache/tuscany/samples/das/companyweb
- Write the following code in the file :
It would be helpful to explain what the purpose of this code is. I would suggest you move this code to another page since it is so long and instead have a link on this page
No Format |
---|
package LASTNAME" converterClassName="org.apache.tuscany |
| .samples.companyweb;
import java.ioInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import org.apache.tuscany.das.rdb.Command;
import org.apache.tuscany.das.rdb.DAS;
import commonj.sdo.DataObject;
public class CompanyClient {
private Random generator = new Random();
private DAS das = DAS.FACTORY.createDAS(getConfig("CompanyConfig.xml"));
public final List getCompanies() {
Command read = das.getCommand("all companies");
DataObject root = read.executeQuery();
return root.getList("COMPANY");
}
public final List getCompaniesWithDepartments() {
Command read = das.getCommand("all companies and departments");
DataObject root = read.executeQuery();
return root.getList("COMPANY");
}
public final List getDepartmentsForCompany(int id) {
Command read = das.getCommand("all departments for company");
read.setParameter(1, new Integer(id));
DataObject root = read.executeQuery();
return root.getList("COMPANY");
}
public final void addDepartmentToFirstCompany() {
Command read = das.getCommand("all companies and departments");
DataObject root = read.executeQuery();
DataObject firstCustomer = root.getDataObject("COMPANY[1]");
DataObject newDepartment = root.createDataObject("DEPARTMENT");
newDepartment.setString("NAME", "Default Name");
firstCustomer.getList("departments").add(newDepartment);
das.applyChanges(root);
}
public final void deleteDepartmentsFromFirstCompany() {
// This section gets the ID of the first Company just so I can
// demonstrate a parameterized command next
Command readAll = das.getCommand("all companies and departments");
DataObject root = readAll.executeQuery();
int idOfFirstCustomer = root.getInt("COMPANY[1]/ID");
System.out.println("ID of first company is: " + idOfFirstCustomer);
// Read a specific company based on the known ID
Command readCust = das.getCommand("company by id with departments");
readCust.setParameter(1, new Integer(idOfFirstCustomer));
root = readCust.executeQuery();
// Delete all the comany's departments from the graph
DataObject firstCustomer = root.getDataObject("COMPANY[1]");
// Shallow copy of list for deleting. This is required to avoid the
// dreaded
// ConcurrentModificationException since #delete operation also removes
// from the original list
List allDepartments = new ArrayList(firstCustomer.getList("departments"));
Iterator i = allDepartments.iterator();
DataObject department;
while (i.hasNext()) {
department = (DataObject) i.next();
System.out.println("Deleting department named: " + department.getString("NAME"));
department.delete();
}
das.applyChanges(root);
}
public final void changeFirstCompanysDepartmentNames() {
// This section gets the ID of the first Company just so I can
// demonstrate a parameterized command next
Command readAll = das.getCommand("all companies and departments");
DataObject root = readAll.executeQuery();
int idOfFirstCustomer = root.getInt("COMPANY[1]/ID");
System.out.println("ID of first company is: " + idOfFirstCustomer);
// Read a specific company based on the known ID
Command readCust = das.getCommand("company by id with departments");
readCust.setParameter(1, new Integer(idOfFirstCustomer));
root = readCust.executeQuery();
// Modify all the comany's department names
DataObject firstCustomer = root.getDataObject("COMPANY[1]");
Iterator i = firstCustomer.getList("departments").iterator();
DataObject department;
while (i.hasNext()) {
department = (DataObject) i.next();
System.out.println("Modifying department: " + department.getString("NAME"));
department.setString("NAME", getRandomDepartmentName());
}
das.applyChanges(root);
}
public void releaseResources() {
das.releaseResources();
}
// Utilities
private String getRandomDepartmentName() {
int number = generator.nextInt(1000) + 1;
return "Dept-" + number;
}
private InputStream getConfig(String fileName) {
return getClass().getClassLoader().getResourceAsStream(fileName);
}
}
|
- Create following Company.jsp file under (Tomcat root)/webapps/companyweb.
No Format |
---|
<html>
<head>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
import="org.apache.tuscany.samples.das.companyweb.CompanyClient"
import="commonj.sdo.*"
%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Company Test</title>
</head>
<body>
<H2>Tuscany DAS Companies WEB Example</H2>
<form>
<input type="submit" id="doFill" name="doFill" value="All Companies">
<input type="submit" id="doFillAll" name="doFillAll" value="All Companies/Departments">
<input type="submit" id="doAddDepartment" name="doAddDepartment" value="Add department to first company">
<input type="submit" id="doChangeDepartmentNames" name="doChangeDepartmentNames" value="Change Company(1) Dept names">
<input type="submit" id="doDeleteDepartments" name="doDeleteDepartments" value="Delete Company(1) Depts">
<hr>
<!-- Do Fill -->
<%if(request.getParameter("doFill") != null){%>
<table border>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<%
CompanyClient companyClient = new CompanyClient();
java.util.Iterator i = companyClient.getCompaniesWithDepartments().iterator();
while (i.hasNext()) {
DataObject company = (DataObject)i.next();
%>
<tr>
<td><%=company.getInt("ID")%></td>
<td><%=company.getString("NAME")%></td>
<tr>
<%
}
companyClient.releaseResources();
%>
</tbody>
</table>
<%}%>
<!-- Do Add Department -->
<%
if(request.getParameter("doAddDepartment") != null){
CompanyClient companyClient = new CompanyClient();
companyClient.addDepartmentToFirstCompany();
companyClient.releaseResources();
}
%>
<!-- Do Delete Departments from first company -->
<%
if(request.getParameter("doDeleteDepartments") != null){
CompanyClient companyClient = new CompanyClient();
companyClient.deleteDepartmentsFromFirstCompany();
companyClient.releaseResources();
}
%>
<!-- Do Change First Company's Department Names -->
<%
if(request.getParameter("doChangeDepartmentNames") != null){
CompanyClient companyClient = new CompanyClient();
companyClient.changeFirstCompanysDepartmentNames();
companyClient.releaseResources();
}
%>
<!-- Do FillAll -->
<%if(request.getParameter("doFill") == null) {%>
<table border>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Department_ID</th>
<th>Department_Name</th>
</tr>
</thead>
<tbody>
<%
CompanyClient companyClient = new CompanyClient();
java.util.Iterator i = companyClient.getCompaniesWithDepartments().iterator();
while (i.hasNext()) {
DataObject company = (DataObject)i.next();
%>
<tr>
<td><%=company.getInt("ID")%></td>
<td><%=company.getString("NAME")%></td>
<tr>
<%
java.util.Iterator j = company.getList("departments").iterator();
while (j.hasNext()) {
DataObject department = (DataObject)j.next();
%>
<tr>
<td></td><td></td><td><%=department.getInt("ID")%></td>
<td><%=department.getString("NAME")%></td>
<tr>
<%
}
%>
<%
}
companyClient.releaseResources();
%>
</tbody>
</table>
<%}%>
</form>
</body>
</html>
|
E. Creating web and context configuration files supporting Tomcat
...
rdb.test.mappings.StringObfuscationConverter"/>
</Table>
|
3> Now the code and config setup is complete. What remains is calling the new command from .jsp and servlet. Follow the technique similar to CompanyClient.java and Company.jsp to call the new command. The returned results will verify that the converter is called and the column value from database is converted based on converter logic. 4> For more details check the test cases from ConverterTests in https://svn.apache.org/repos/asf/incubator/tuscany/java/das/rdb/src/test/. Assumption: Tomcat version 5.5.* or 6.0.10 , Derby version 10.1.2.1. Troubleshooting Checkpoints: Check the following and if still having issues , report at http://incubator.apache.org/tuscany/issue-tracking.html - All libraries are present in (Tomcat root)/webapps/companyweb/WEB-INF/lib (these libs can
be taken from companyweb sample or can be downloaded from web) i) common-(latest version).jar ii) ecore-(latest version).jar iii) ecore-change-(latest version).jar iv) ecore-xmi-(latest version).jar v) log4j-(latest version).jar vi) sdo-api-xxx.jar vii) tuscany-das-rdb-xxx.jar viii) tuscany-sdo-xxx.jar ix) xsd-(latest version).jar x) derby driver jar - or whichever database you are planning to use - The Database exists with required tables and data.
- WEB-INF/web.xml has entry under welcome-file-list for required .jsp like -
No Format |
---|
<web-app>
<display-name>Tuscany DAS sample Company WEB</display-name>
<welcome-file-list id="WelcomeFileList">
<welcome-file>Company.jsp</welcome-file>
</welcome-file-list>
</web-app>
|
|
|
...
...
...
- has entry for the datasource the sample is using like -
No Format |
---|
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/DAS Stand alone app" debug="5" reloadable="true" crossContext="true">
<Manager pathname=""/>
<ResourceLink name="jdbc/dastest" global="jdbc/dastest" type="javax.sql.DataSource" />
</Context>
|
|
|
...
...
...
- has entry similar to below for the datasource -
No Format |
---|
<Resource name="jdbc/dastest"
type="javax.sql.DataSource" auth="Container"
description=" |
|
|
...
...
Company sample"
maxActive="100" maxIdle="30" maxWait="10000"
username="" password=""
driverClassName=" |
|
|
...
...
...
EmbeddedDriver"
url="jdbc: |
|
|
...
F. Run the sample companyweb
...
derby:c:\apache-tomcat-5.5.20\Databases/dastest;create=true"/>
|
|
|