Example |
- Sample XML Project
- Sample MySQL DDL Output
- Sample CustomerInformation Table DAO Pattern Output
- Sample CustomerInformation DTO output
This section provides an xml sample project.
Sample XML Project
<PROJECT NAME="Customer Order System">
<PATTERN NAME='value.classpath' VALUE='com.mycompany.values'/>
<PATTERN NAME='stream.classpath' VALUE='com.mycompany.stream'/>
<PATTERN NAME='stream.proto_version' VALUE='1'/>
<PATTERN NAME='dao.classpath' VALUE='com.mycompany.dao'/>
<SECTION NAME="SECTION 1" MODULE="MULTI"
DEFAULT_PACKAGE="com.my.company.db"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="/dev/emeraldjb/schemas/emeraldjb.xsd">
<HEADER>
<DEVELOPER>John Doe</DEVELOPER>
<FILE_HEADER>
* Copyright (c) 2003 by Company, Inc.
</FILE_HEADER>
</HEADER>
<ENTITY NAME="CUSTOMER_INFORMATION">
<JAVADOC>
<DESCRIPTION>Table that contains all customers in the system.</DESCRIPTION>
<SINCE>1.0</SINCE>
</JAVADOC>
<MEMBER_SPEC NAME="CUSTOMER_ID" FROM_SEQ='CUSTOMER_ID_SEQ' TYPE='int' NULL_ALLOWED='FALSE'>
<JAVADOC>
<DESCRIPTION>
The id assigned to every customer.
</DESCRIPTION>
<DEPRECATED>CUSTINDEX has been replaced by CUSTOMER_ID</DEPRECATED>
<SEE>com.mycompany.CustomerInformation#getCustomerId()</SEE>
<SINCE>1.1</SINCE>
</JAVADOC>
</MEMBER_SPEC>;
<MEMBER NAME="ACTIVE" TYPE='boolean' DEFAULT='1' NULL_ALLOWED='FALSE'/>
<MEMBER NAME="FIRST_NAME" COL_LEN='75' TYPE='string' NULL_ALLOWED='FALSE'/>
<MEMBER NAME="LAST_NAME" COL_LEN='75' TYPE='string' NULL_ALLOWED='FALSE'/>
<MEMBER NAME="ADDRESS" COL_LEN='175' TYPE='string' NULL_ALLOWED='FALSE'/>
<MEMBER NAME="ACCOUNT_ACTIVATION_DATE" TYPE='date'/>
<MEMBER NAME="ACCOUNT_DEACTIVATION_DATE" TYPE='date' NULL_ALLOWED='FALSE'/>
<PRIMARY_KEY NAME="customer_info_pk" COLS='CUSTOMER_ID'/>
</ENTITY>
<ENTITY NAME="ITEM">
<MEMBER NAME="ITEM_ID" FROM_SEQ='ITEM_ID_SEQ' TYPE='int' NULL_ALLOWED='FALSE'/>
<MEMBER NAME="ITEM_DESCRIPTION" TYPE='String' COL_LEN="255" NULL_ALLOWED='FALSE'/>
<MEMBER NAME="PRICE" TYPE='float' NULL_ALLOWED='FALSE'/>
<PRIMARY_KEY COLS='ITEM_ID' NAME="item_pk"/>
</ENTITY>
<ENTITY NAME="CUSTOMER_ORDER">
<MEMBER NAME="ORDER_ID" FROM_SEQ='ORDER_ID_SEQ' TYPE='int' NULL_ALLOWED='FALSE'/>
<MEMBER NAME="ORDER_DATE" TYPE='timestamp' NULL_ALLOWED='FALSE'/>
<MEMBER NAME="CUSTOMER_ID" TYPE='int' NULL_ALLOWED='FALSE'/>
<PRIMARY_KEY COLS='ORDER_ID' NAME="order_pk"/>
<FINDER NAME="getOrdersByCustomerId" >
<PARAMS>int customerId</PARAMS>
<SQL>
WHERE
ORDER_ID = '%$P(customerId)%'
</SQL>
</FINDER>
</ENTITY>
<ENTITY NAME="ORDER_ITEM">
<JAVADOC>
<DESCRIPTION>Table that contains all order items in the system.</DESCRIPTION>
<SINCE>1.0</SINCE>
</JAVADOC>
<MEMBER NAME="ORDER_ITEM_ID" FROM_SEQ='ORDER_ITEM_ID_SEQ' TYPE='int'
NULL_ALLOWED='FALSE'/>
<MEMBER NAME="ORDER_ID" TYPE='int' NULL_ALLOWED='FALSE'/>
<PRIMARY_KEY COLS='ORDER_ITEM_ID' NAME="order_item_pk"/>
<FINDER NAME="getItemsByOrderId">
<PARAMS>int orderId</PARAMS>
<SQL>
WHERE
ORDER_ID = '%$P(orderId)%'
</SQL>
<JAVADOC>
<DESCRIPTION>
Returns all items by order id.
</DESCRIPTION>
</JAVADOC>
</FINDER>
</ENTITY>
<NUGGET NAME="CustomerOrderNugget">
<JAVADOC>
<DESCRIPTION>
The orders a customer has placed.
</DESCRIPTION>
</JAVADOC>
<FROM>CUSTOMER_INFORMATION c, ITEM i, ORDER_ITEM oi, CUSTOMER_ORDER o</FROM>
<FIELD_SPEC NAME="customerId" TYPE='int' SRC='c.CUSTOMER_ID' >
<JAVADOC>
<DESCRIPTION>
The customer id form the CUSTOMER_INFORMATION table.
</DESCRIPTION>
<SINCE>1.1</SINCE>
</JAVADOC>
</FIELD_SPEC>
<FIELD NAME="firstName" TYPE='String' SRC='c.FIRST_NAME' />
<FIELD NAME="lastName" TYPE='String' SRC='c.LAST_NAME' />
<FIELD NAME="itemId" TYPE='int' SRC='i.ITEM_ID' />
<FIELD NAME="itemDescription" TYPE='String' SRC='i.ITEM_DESCRIPTION' />
<FIELD NAME="orderDate" TYPE='DATE' SRC='o.ORDER_DATE' />
<FINDER NAME="findByCustomerId" >
<PARAMS>int customerId</PARAMS>
<SQL>
WHERE
c.CUSTOMER_ID = '%$P(customerId)%'
AND o.CUSTOMER_ID = c.CUSTOMER_ID
AND oi.ORDER_ID = o.ORDER_ID
AND i.ITEM_ID = oi.ITEM_ID
ORDER BY o.ORDER_DATE
</SQL>
<JAVADOC>
<DESCRIPTION>
Returns all items a customer has ordered by customer id.
</DESCRIPTION>
</JAVADOC>
</FINDER>
</NUGGET>
</SECTION>
</PROJECT>
Sample MySQL DDL Output
CREATE TABLE CUSTOMER_INFORMATION ( CUSTOMER_ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, ACTIVE SMALLINT NOT NULL DEFAULT 1, FIRST_NAME VARCHAR(75) NOT NULL, LAST_NAME VARCHAR(75) NOT NULL, ADDRESS VARCHAR(175) NOT NULL, ACCOUNT_ACTIVATION_DATE DATE, ACCOUNT_DEACTIVATION_DATE DATE NOT NULL ); CREATE TABLE ITEM ( ITEM_ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, ITEM_DESCRIPTION BLOB NOT NULL, PRICE FLOAT NOT NULL ); CREATE TABLE CUSTOMER_ORDER ( ORDER_ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, ORDER_DATE TIMESTAMP NOT NULL, CUSTOMER_ID INTEGER NOT NULL ); CREATE TABLE ORDER_ITEM ( ORDER_ITEM_ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, ORDER_ID INTEGER NOT NULL );
Sample CustomerInformation Table DAO Pattern Output
package com.mycompany.dao;
import java.util.*;
import java.util.logging.*;
import java.sql.*;
import com.emeraldjb.runtime.*;
import com.mycompany.values.*;
public class CustomerInformationMysqlDAO extends DaoBase implements CustomerInformationDAO
{
protected Logger daoLogger =
Logger.getLogger("com.mycompany.dao.CustomerInformationMysqlDAO");
public CustomerInformationValues insert(CustomerInformationValues values) throws SQLException
{
Connection conn = ConnectionManager.getConnection(getDbAlias());
try
{
values = insert(values, conn);
}
finally
{
if (conn!=null) conn.close();
}
return values;
}
public CustomerInformationValues insert(CustomerInformationValues values, Connection conn) throws SQLException
{
if (values==null) throw new SQLDataException("Values object cannot be null.");
if (conn==null) throw new SQLDataException("Connection object cannot be null.");
int count = 0;
PreparedStatement stmnt = null;
String query =
"INSERT INTO CUSTOMER_INFORMATION "
+"SET "
+"ACTIVE= ? "
+",FIRST_NAME= ? "
+",LAST_NAME= ? "
+",ADDRESS= ? "
+",ACCOUNT_ACTIVATION_DATE= ? "
+",ACCOUNT_DEACTIVATION_DATE= ? ";
String loggedParams="";
boolean wantLogging = daoLogger.isLoggable(Level.FINEST);
try
{
stmnt = conn.prepareStatement(query);
if (wantLogging) loggedParams+= "[1]->["+ (values.getActive()?"true":"false")+"]";
stmnt.setBoolean(1, values.getActive());
if (wantLogging) loggedParams+= "[2]->["+ (values.getFirstName()!=null && values.getFirstName().length()>80?values.getFirstName().substring(0,80)+"(truncated to 80 chars)":values.getFirstName())+"]";
if (values.getFirstName()!=null) {
stmnt.setString(2, values.getFirstName());
} else {
stmnt.setNull(2, java.sql.Types.VARCHAR);
}
if (wantLogging) loggedParams+= "[3]->["+ (values.getLastName()!=null && values.getLastName().length()>80?values.getLastName().substring(0,80)+"(truncated to 80 chars)":values.getLastName())+"]";
if (values.getLastName()!=null) {
stmnt.setString(3, values.getLastName());
} else {
stmnt.setNull(3, java.sql.Types.VARCHAR);
}
if (wantLogging) loggedParams+= "[4]->["+ (values.getAddress()!=null && values.getAddress().length()>80?values.getAddress().substring(0,80)+"(truncated to 80 chars)":values.getAddress())+"]";
if (values.getAddress()!=null) {
stmnt.setString(4, values.getAddress());
} else {
stmnt.setNull(4, java.sql.Types.VARCHAR);
}
if (wantLogging) loggedParams+= "[5]->["+ values.getAccountActivationDate()+"]";
if (values.getAccountActivationDate()!=null) {
stmnt.setDate(5, values.getAccountActivationDate());
} else {
stmnt.setNull(5, java.sql.Types.DATE);
}
if (wantLogging) loggedParams+= "[6]->["+ values.getAccountDeactivationDate()+"]";
if (values.getAccountDeactivationDate()!=null) {
stmnt.setDate(6, values.getAccountDeactivationDate());
} else {
stmnt.setNull(6, java.sql.Types.DATE);
}
if (wantLogging) {
daoLogger.logp(Level.FINEST,"CUSTOMER_INFORMATION","insert",
"Query: "+query+"\n"+"Params: "+loggedParams);
}
count = stmnt.executeUpdate();
} finally {
stmnt.close();
}
if (count>0) { // NativeSequencesPattern
// Retrieve autogen primary key value
Statement s_id = null;
String id_query="";
ResultSet rs_id = null;
try {
s_id = conn.createStatement();
id_query="select LAST_INSERT_ID()";
rs_id = s_id.executeQuery(id_query);
if (rs_id.next()) {
values.setCustomerId(rs_id.getInt(1));
}
rs_id.close();
} finally {
if (s_id!=null) s_id.close();
}
} // end of NativeSequencesPattern
return values;
}
public int update(CustomerInformationValues values) throws SQLException
{
Connection conn = ConnectionManager.getConnection(getDbAlias());
int count = 0;
try
{
count = update(values, conn);
}
finally
{
if (conn!=null) conn.close();
}
return count;
}
public int update(Collection valuesCollection) throws SQLException
{
Connection conn = ConnectionManager.getConnection(getDbAlias());
int count = 0;
try
{
Iterator it = valuesCollection.iterator();
while (it.hasNext())
{
CustomerInformationValues values = (CustomerInformationValues)it.next();
count += update(values, conn);
}
}
finally
{
if (conn!=null) conn.close();
}
return count;
}
public int update(CustomerInformationValues values, Connection conn) throws SQLException
{
if (values==null) throw new SQLDataException("Values object cannot be null.");
if (conn==null) throw new SQLException("Connection object cannot be null.");
int count = 0;
PreparedStatement stmnt = null;
String query =
"UPDATE CUSTOMER_INFORMATION "
+"SET "
+"ACTIVE= ? "
+",FIRST_NAME= ? "
+",LAST_NAME= ? "
+",ADDRESS= ? "
+",ACCOUNT_ACTIVATION_DATE= ? "
+",ACCOUNT_DEACTIVATION_DATE= ? "
+"WHERE "
+"CUSTOMER_ID= ? ";
String loggedParams="";
boolean wantLogging = daoLogger.isLoggable(Level.FINEST);
try
{
stmnt = conn.prepareStatement(query);
if (wantLogging) loggedParams+= "[1]->["+ (values.getActive()?"true":"false")+"]";
stmnt.setBoolean(1, values.getActive());
if (wantLogging) loggedParams+= "[2]->["+ (values.getFirstName()!=null && values.getFirstName().length()>80?values.getFirstName().substring(0,80)+"(truncated to 80 chars)":values.getFirstName())+"]";
if (values.getFirstName()!=null) {
stmnt.setString(2, values.getFirstName());
} else {
stmnt.setNull(2, java.sql.Types.VARCHAR);
}
if (wantLogging) loggedParams+= "[3]->["+ (values.getLastName()!=null && values.getLastName().length()>80?values.getLastName().substring(0,80)+"(truncated to 80 chars)":values.getLastName())+"]";
if (values.getLastName()!=null) {
stmnt.setString(3, values.getLastName());
} else {
stmnt.setNull(3, java.sql.Types.VARCHAR);
}
if (wantLogging) loggedParams+= "[4]->["+ (values.getAddress()!=null && values.getAddress().length()>80?values.getAddress().substring(0,80)+"(truncated to 80 chars)":values.getAddress())+"]";
if (values.getAddress()!=null) {
stmnt.setString(4, values.getAddress());
} else {
stmnt.setNull(4, java.sql.Types.VARCHAR);
}
if (wantLogging) loggedParams+= "[5]->["+ values.getAccountActivationDate()+"]";
if (values.getAccountActivationDate()!=null) {
stmnt.setDate(5, values.getAccountActivationDate());
} else {
stmnt.setNull(5, java.sql.Types.DATE);
}
if (wantLogging) loggedParams+= "[6]->["+ values.getAccountDeactivationDate()+"]";
if (values.getAccountDeactivationDate()!=null) {
stmnt.setDate(6, values.getAccountDeactivationDate());
} else {
stmnt.setNull(6, java.sql.Types.DATE);
}
if (wantLogging) loggedParams+= "[7]->["+ Integer.toString(values.getCustomerId())+"]";
stmnt.setInt(7, values.getCustomerId());
if (wantLogging) {
daoLogger.logp(Level.FINEST,"CUSTOMER_INFORMATION","update",
"Query: "+query+"\n"+"Params: "+loggedParams);
}
count = stmnt.executeUpdate();
}
finally
{
if (stmnt!=null)stmnt.close();
}
return count;
}
public int update(Collection valuesCollection, Connection conn) throws SQLException
{
int count = 0;
Iterator it = valuesCollection.iterator();
while (it.hasNext())
{
CustomerInformationValues values = (CustomerInformationValues)it.next();
count += update(values, conn);
}
return count;
}
public int delete(CustomerInformationValues values) throws SQLException
{
int count = 0;
Connection conn = ConnectionManager.getConnection(getDbAlias());
try
{
count = delete(values, conn);
}
finally
{
if (conn!=null) conn.close();
}
return count;
}
public int delete(CustomerInformationValues values, Connection conn) throws SQLException
{
int count = 0;
PreparedStatement stmnt = null;
String query =
"DELETE FROM CUSTOMER_INFORMATION "
+"WHERE "
+"CUSTOMER_ID= ? ";
stmnt = conn.prepareStatement(query);
String loggedParams="";
boolean wantLogging = daoLogger.isLoggable(Level.FINEST);
if (wantLogging) loggedParams+= "[1]->["+ Integer.toString(values.getCustomerId())+"]";
stmnt.setInt(1, values.getCustomerId());
try
{
if (wantLogging) {
daoLogger.logp(Level.FINEST,"CUSTOMER_INFORMATION","delete",
"Query: "+query+"\n"+"Params: "+loggedParams);
}
count = stmnt.executeUpdate();
stmnt.close();
}
finally
{
if (stmnt!=null)stmnt.close();
}
return count;
}
public CustomerInformationValues findByPrimaryKey(int pk) throws SQLException
{
Connection conn = ConnectionManager.getConnection(getDbAlias());
PreparedStatement stmnt = null;
ResultSet rs = null;
CustomerInformationValues values = null;
String query =
"SELECT CUSTOMER_ID "
+",ACTIVE "
+",FIRST_NAME "
+",LAST_NAME "
+",ADDRESS "
+",ACCOUNT_ACTIVATION_DATE "
+",ACCOUNT_DEACTIVATION_DATE "
+"FROM CUSTOMER_INFORMATION "
+"WHERE "
+"CUSTOMER_ID= ? ";
String loggedParams="";
boolean wantLogging = daoLogger.isLoggable(Level.FINEST);
try
{
stmnt = conn.prepareStatement(query);
stmnt.setInt(1, pk);
if (wantLogging) {
daoLogger.logp(Level.FINEST,"CUSTOMER_INFORMATION","findByPrimaryKey",
"Query: "+query+"\n"+"Params: "+loggedParams);
}
rs = stmnt.executeQuery();
while(rs.next())
{
values = loadFromResultSet(rs);
break; // pick the first element
}
rs.close();
stmnt.close();
}
finally
{
if (rs!=null) rs.close();
if (stmnt!=null)stmnt.close();
if (conn!=null)conn.close();
}
return values;
}
protected CustomerInformationValues loadFromResultSet(ResultSet rs) throws SQLException
{
if (rs==null) throw new SQLException("ResultSet object cannot be null.");
CustomerInformationValues values = new CustomerInformationValues();
// iterate through the result set.
values.setCustomerId(rs.getInt(1));
values.setActive(rs.getBoolean(2));
values.setFirstName(rs.getString(3));
if (rs.wasNull()) values.setFirstName((String)null);
values.setLastName(rs.getString(4));
if (rs.wasNull()) values.setLastName((String)null);
values.setAddress(rs.getString(5));
if (rs.wasNull()) values.setAddress((String)null);
values.setAccountActivationDate(rs.getDate(6));
if (rs.wasNull()) values.setAccountActivationDate((java.sql.Date)null);
values.setAccountDeactivationDate(rs.getDate(7));
if (rs.wasNull()) values.setAccountDeactivationDate((java.sql.Date)null);
return values;
}
}
Sample CustomerInformation DTO output
package com.mycompany.values;
public class CustomerInformationDto extends DaoValues
{
protected int customerId;
protected boolean active=true;
protected String firstName;
protected String lastName;
protected String address;
protected java.sql.Date accountActivationDate;
protected java.sql.Date accountDeactivationDate;
public int getCustomerId()
{
return customerId;
}
public void setCustomerId(int customerId)
{
this.customerId=customerId;
}
public boolean getActive()
{
return active;
}
public void setActive(boolean active)
{
this.active=active;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName=lastName;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address=address;
}
public java.sql.Date getAccountActivationDate()
{
return accountActivationDate;
}
public void setAccountActivationDate(java.sql.Date accountActivationDate)
{
this.accountActivationDate=accountActivationDate;
}
public java.sql.Date getAccountDeactivationDate()
{
return accountDeactivationDate;
}
public void setAccountDeactivationDate(java.sql.Date accountDeactivationDate)
{
this.accountDeactivationDate=accountDeactivationDate;
}
/**
* This renders the object to string.
*/
public String toString() {
StringBuffer ret = new StringBuffer(getClass().getName()+":");
ret.append(" [CUSTOMER_ID->"+Integer.toString(getCustomerId())+"]");
ret.append(" [ACTIVE->"+(getActive()?"true":"false")+"]");
ret.append(" [FIRST_NAME->"+getFirstName()+"]");
ret.append(" [LAST_NAME->"+getLastName()+"]");
ret.append(" [ADDRESS->"+(getAddress()!=null && getAddress().length()>80?getAddress().substring(0,80)+"(truncated to 80 chars)":getAddress())+"]");
ret.append(" [ACCOUNT_ACTIVATION_DATE->"+getAccountActivationDate()+"]");
ret.append(" [ACCOUNT_DEACTIVATION_DATE->"+getAccountDeactivationDate()+"]");
return ret.toString();
}
/**
* This copies the input objects members into this object.
* @param sourceObject The object to copy FROM.
*/
public void copy(CustomerInformationDto sourceObject) {
setCustomerId(sourceObject.getCustomerId());
setActive(sourceObject.getActive());
setFirstName(sourceObject.getFirstName());
setLastName(sourceObject.getLastName());
setAddress(sourceObject.getAddress());
setAccountActivationDate(sourceObject.getAccountActivationDate());
setAccountDeactivationDate(sourceObject.getAccountDeactivationDate());
}
}

