|
Back to the other templates.
Template Files:
The target language is Java and the template script language is also Java.
You can download the entire set or view each file separately
at the end of this page.
Purpose:
This template set allows you to construct basic data container objects. A data container
object is a convenience class for grouping data fields that belong together. These data fields might map to
database tables or be useful for RMI or CORBA method calls. In the most basic form they are
simply a collection of get and set methods and a list of member variables. Of course,
practical use of such objects usually means extending them in some way.
Design:
The basic data container objects are very simple in form: A set of private data members and
corresponding get and set methods for each member. Here is an example:
public class Example {
private String iFoo;
private int iBar;
public void setFoo(String rFoo) {
iFoo = rFoo;
}
public String getFoo() {
return iFoo;
}
public void setBar(int rBar) {
iBar = rBar;
}
public int getBar() {
return iBar;
}
}
We use a specific coding convention so that the template code is easier to write: instance variables are
prefixed with i and parameters are prefixed with r. You don't have to follow this
convention. The template code can be modified to use a different convention (or none are all!).
Looking at this source code we can see that it is described by the
following meta data: a class name plus a list of fields.
Each field is described by a field name and a field type. This
structure can easily be expressed in a simple text file format. Since
this type of meta data is so common, Jostraca provides a set of
utility classes for dealing with it:
org.jostraca.resource.SimpleDataObject and org.jostraca.resource.SimpleDataObjectReader.
With these classes you easily handle this type of meta data.
Take a look at the template to see them in action.
The API in very simple. For the above example, the text file describing the
meta data has the form:
Example
Foo String
Bar int
;
More than one data container may be described in the same schema file
by separating each one with ;.
A separate source file will be generated for each data container.
Note that there's nothing to stop you from representing this meta
data in XML if you like, and then parsing it yourself.
Usage:
We assume that your code requires some simple data containers. Choose a location in your
package hierarchy which is suitable and copy the DataContainer_java.jtm file
into this location. You may wish to rename the file, especially if you intend to make
many alterations to it.
The next step is to create a schema file for your program. Identify the data container objects that
you might meed. These will probably correspond to the business (or conceptual) entities you are using.
For example: Product, Customer, Supplier. Then for each entity, identify the data fields needed by that entity:
Products have names, prices and expiry dates. Customers have first names, second names and email addresses.
Suppliers have company names, email addresses and web site addresses. Choose appropriate types for these fields.
Thus:
Product
Name String
Price int
Expiry Date
;
Customer
FirstName String
SecondName String
Email String
;
Supplier
Name String
Email String
WebSite String
;
Once the schema is defined, DataContainer_java.jtm should
be updated to reflect the details of your project. Add a package
statement and any imports needed (For example: import
java.util.Date;). Since the template text is simply copied
directly to the resulting source code, you enter these details exactly
as you would for any normal Java program. The template contains
comments showing you where to put these statements.
Now run Jostraca on the template to generate the individual data
container classes. Use jostraca DataContainer_java.jtm
schemafile.txt at the command or shell prompt. The schema file
to use is specified after the name of the template.
Running Jostraca will generate the data container source code for
you. Once the data container source files have been created, review
them. If they are satisfactory, compile them and use as normal in your
application. Well done!
There are a couple of things to notice here. First, now that you have
part of your application encoded as meta data, can you identify other
areas that can be treated in the same way? What about the SQL
CREATE TABLE statements that create the tables that these
data containers represent? Second, you don't need to recompile the
template each time the meta data changes. For eaxmple, if you add an
extra field, just use the -C option to Jostraca. This
prevents the template from being compiled and just executes the
existing compiled template, which saves time. If you are using a
language such as Perl for the template code then this is not so
important.
Formats:
The schema file describes the data container objects to build. This file has
a very simple format:
ObjectName1
FieldName11 FieldType11
FieldName12 FieldType12
;
ObjectName2
FieldName21 FieldType21
FieldName22 FieldType22
;
...
Whitespace is ignored.
Summary of Use:
- Copy
DataContainer_java.jtm into your source tree.
- Create a schema text file in the same format as
sampleschema.txt.
- Edit
DataContainer_java.jtm: add package details, etc. as appropriate.
- Run
jostraca DataContainer_java.jtm schemafile.txt. This creates the individual data container objects.
- Compile the data container objects.
Download:
basic-datacontainer.tar.gz (2Kb)
Source Files:
DataContainer_java.jtm (text file)
sampleschema.txt
Person.java (text file)
Company.java (text file)
|