Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/DBWS/DBWSIntermediateAttribute"

Line 22: Line 22:
 
In the <tt>eclipselink-dbwsutils.jar</tt>, the built-in transformers form a [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern <i>Chain-of-Responsibility</i>]
 
In the <tt>eclipselink-dbwsutils.jar</tt>, the built-in transformers form a [http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern <i>Chain-of-Responsibility</i>]
 
[[Image:NCTChainOfResponsibility.png|650px]]
 
[[Image:NCTChainOfResponsibility.png|650px]]
<br clear="all"/>
+
<br/>
 +
<br/>
 
In the <tt>o.e.p.tools.dbws.NamingConventionTransformer</tt> services file, the transformer chain is listed in order:
 
In the <tt>o.e.p.tools.dbws.NamingConventionTransformer</tt> services file, the transformer chain is listed in order:
 
<source lang="text" enclose="div">
 
<source lang="text" enclose="div">
Line 38: Line 39:
 
</source>
 
</source>
  
=== Changing an <element-tag> to an "attrib
+
=== User-Provided NamingConventionTransformer ===
It is possible to change an <element-tag> to an "attribute" with a custom transformer:
+
It is possible to change an <element-tag> to an "attribute" with a custom transformer. The user sub-classes <tt>o.e.p.tools.dbws.DefaultNamingConventionTransformer</tt>. For example, for the table
 +
<source lang=sql>
 +
CREATE TABLE IF NOT EXISTS SIMPLE_TABLE (
 +
  ID NUMERIC NOT NULL,
 +
  NAME VARCHAR(25),
 +
  SINCE DATE,
 +
  PRIMARY KEY (ID)
 +
)
 +
</source>
 +
The following <code>NamingConventionTransformer<code> will:
 +
* name the schema element '<tt>simple</tt>'
 +
* make the element names lowercase
 +
* have the <tt>ID</tt> field represented as an attribute, and
 +
* skip the <tt>SINCE</tt> field
 +
<source lang=java5>
 +
import org.eclipse.persistence.tools.dbws.DefaultNamingConventionTransformer;
 +
import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.ATTRIBUTE;
 +
import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.ELEMENT;
 +
import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.NONE;
 +
 
 +
public class MyNamingConventionTransformer extends DefaultNamingConventionTransformer {
 +
 
 +
    @Override
 +
    public String generateSchemaAlias(String tableName) {
 +
        return super.generateSchemaAlias("simple");
 +
    }
 +
 
 +
    @Override
 +
    public String generateElementAlias(String originalElementName) {
 +
        return super.generateElementAlias(originalElementName.toLowerCase());
 +
    }
 +
 
 +
    @Override
 +
    public ElementStyle styleForElement(String elementName) {
 +
        if ("id".equalsIgnoreCase(elementName)) {
 +
            return ATTRIBUTE;
 +
        }
 +
        else if ("since".equalsIgnoreCase(elementName)) {
 +
            return NONE;
 +
        }
 +
        return ELEMENT;
 +
    }
 +
}
 +
</source>

Revision as of 13:20, 3 June 2009

Naming Convention for schema elements

As seen in the Basic Table example, the DBWSBuilder-generated eclipselink-dbws-schema.xsd file derives <element-tag> names from the Database table metadata:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 >
  <xsd:complexType name="empType">
    <xsd:sequence>
      <xsd:element name="empno" type="xsd:int" xsi:nil="false"/>
      <xsd:element name="ename" type="xsd:string" xsi:nil="true"/>
      <xsd:element name="job" type="xsd:string" xsi:nil="true"/>
      <xsd:element name="mgr" type="xsd:int" minOccurs="0" xsi:nil="true"/>
      <xsd:element name="hiredate" type="xsd:dateTime" xsi:nil="true"/>
      <xsd:element name="sal" type="xsd:decimal" xsi:nil="true"/>
      <xsd:element name="comm" type="xsd:int" minOccurs="0" xsi:nil="true"/>
      <xsd:element name="deptno" type="xsd:int" xsi:nil="true"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

Converting the database metadata into the names of <element-tag>'s is the job of o.e.p.tools.dbws.NamingConventionTransformer's

public interface NamingConventionTransformer {
 
    public enum ElementStyle {
        ELEMENT, ATTRIBUTE, NONE
    };
 
    public String generateSchemaName(String tableName);
 
    public String generateElementAlias(String originalElementName);
 
    public ElementStyle styleForElement(String originalElementName);
}

In the eclipselink-dbwsutils.jar, the built-in transformers form a Chain-of-Responsibility NCTChainOfResponsibility.png

In the o.e.p.tools.dbws.NamingConventionTransformer services file, the transformer chain is listed in order:

eclipselink-dbwsutils.jar
|  
+---META-INF
|   |   MANIFEST.MF
|   |  
|   \---services
|           org.eclipse.persistence.tools.dbws.NamingConventionTransformer
            contains 3 lines:
              org.eclipse.persistence.tools.dbws.ToLowerTransformer
              org.eclipse.persistence.tools.dbws.TypeSuffixTransformer
              org.eclipse.persistence.tools.dbws.SQLX2003Transformer

User-Provided NamingConventionTransformer

It is possible to change an <element-tag> to an "attribute" with a custom transformer. The user sub-classes o.e.p.tools.dbws.DefaultNamingConventionTransformer. For example, for the table

CREATE TABLE IF NOT EXISTS SIMPLE_TABLE (
  ID NUMERIC NOT NULL,
  NAME VARCHAR(25),	
  SINCE DATE, 
  PRIMARY KEY (ID) 
)

The following NamingConventionTransformer<code> will:

  • name the schema element 'simple'
  • make the element names lowercase
  • have the ID field represented as an attribute, and
  • skip the SINCE field
import org.eclipse.persistence.tools.dbws.DefaultNamingConventionTransformer;
import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.ATTRIBUTE;
import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.ELEMENT;
import static org.eclipse.persistence.tools.dbws.NamingConventionTransformer.ElementStyle.NONE;
 
public class MyNamingConventionTransformer extends DefaultNamingConventionTransformer {
 
    @Override
    public String generateSchemaAlias(String tableName) {
        return super.generateSchemaAlias("simple");
    }
 
    @Override
    public String generateElementAlias(String originalElementName) {
        return super.generateElementAlias(originalElementName.toLowerCase());
    }
 
    @Override
    public ElementStyle styleForElement(String elementName) {
        if ("id".equalsIgnoreCase(elementName)) {
            return ATTRIBUTE;
        }
        else if ("since".equalsIgnoreCase(elementName)) {
            return NONE;
        }
        return ELEMENT;
    }
}