You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Sa...@kyoceramita.com on 2005/06/20 22:44:27 UTC
New Module BeanGenerator
Hi All,
I have been working on reusable component called BeanGenerator, which as
following functionality. Please advise if it is eligible to be posted as
component in Commons.
Availability of DynaBean from BeanUtils has simplified the programmer
lives to quite a bit extent but it lacks in certain functionality which is
very much required to all programmers to name a few like CompileTime
method semantics validation, generating the equals, hashCode for beans,
type conversion from all Strings types to business specific type etc.
So I had been wondering if there is a tool, which takes the beans
information in the xml file and generates the corresponding beans
automatically. Some of the visible advantages of this are,
1. From the same xml bean data, we can automatically generate two bean
classes.
For ex., if you specify bean information for com.test.User, tool can
generate com/test/User.java file and also com/test/UserForm.java file,
with corresponding access method. so by calling new User().getFormBean(),
would return com.test.UserForm, which can be automatically used in the
jsps as action forms. Also by calling new UserForm().getBusinessBean() may
automatically return a new instance of com.test.User, with all fields
automatically type converted and populated.
2. By just marking a set of attributes as primary key fields, tool can
generate the equals, hashCode in a consistent manner
3. If user wish, tool can generate the containers for specific beans. For
ex., if you have com.test.User and if you want container for this, tool
can generate com.test.Users, with all access fields which returns the User
instead of object, which avoids ugly typecast things all across
4. Also tool can add necessary comparator implementation for marked
attributes, so that beans in a container can be automatically sorted. so
by calling users.sort (User.FIELD_FIRSTNAME) or users.sort (User.LASTNAME)
etc.
6. Generate appropriate reset, toString, validate methods, which are
really a nightmare to maintain in enterprise applications (if you add a
attribute or modify one)
5. Tool can automatically generate java files, compile them and create
corresponding jar files and put it in a specified folder. So modifying the
bean specification is as simple as just adding a task and executing it.
As you clearly visible, by having the Bean information in xml once, we can
programmatically can do many things, which would be very error prone or
laborious otherwise. Also it is clear that, this is not to do with
BeanUtils module what we have as of now.
Please advise if this is of any use for other java developers or
designers. and also if this can hosted as new module in commons may be
like Commons BeanGenerator.
Thanking you all,
Santhosh.
Re: New Module BeanGenerator
Posted by Mattias Jiderhamn <mj...@expertsystem.se>.
I have some interrest in what you are suggesting, because it is loosely
related to something we do in our application which I have considered
extracting and releasing as Open Source. Let me tell you a little about our
problem and solution, and then maybe, maybe we can find some common ground.
We are currently using a homegrown O/R mapper and a homegrown MVC framework
rather than, say, Hibernate and Struts. The original reason for this is
becaus I didn't know about these framworks as we started out. We have
planned to move to Hibernate, but with almost a quarter of a million LOC,
it's in the long term plans.
When it comes to Struts, it seems now we will never get there. The main
reason as of now, is that Struts cannot (or I cannot figure out how to make
Struts or any other MVC framework I know of) update hierarchial data. The
majority of forms in our application have a head section and then lines and
often sublines in up to four levels, and also "parallell" hierarchies. A
concrete example is an invoice which has a header, 3 addresses (buyer,
supplier, delivery), one or more address has one or more contact, the
invoice also has 5 lines and some lines have one or more text lines. This
should all be update in a single form (i.e. posted to the server with one
click).
To solve these issues, we have a utility application that generates source
code and other helpful stuff. The input to the generator is a trivial XML
file, basically mapping database tables to fully qualified Java classes
(package + classname) and also naming the "child" tables/collections and
their type (List/Map). Together with this XML file, the application also
queries the database for metadata.
An example for the above scenario might look something like this
<table name="invoice">
<package>com.foo.invoices</package>
<childRecords>
<childRecord>
<name>invoiceAddresses</name>
<className>InvoiceAddressObject</className>
<collectionType>Map</collectionType>
</childRecord>
<childRecord>
<name>invoiceLines</name>
<className>InvoiceLineArticleObject</className>
<collectionType>List</collectionType>
<indexField>invoiceLineId</indexField>
</childRecord>
</childRecords>
</table>
<!-- ... Definitions for addresses and lines... -->
Firstly this app generates Java sources for Beans and O/R mapping. I have
also added support for what I call XML forms. The app generates an XML file
describing the object hierarchy and how to map form data to it. With about
4 lines of code, pointing out the request, the XML file and the object to
update, the hierarchy is automatically updated including creation and
deletion of children (and grandchildren, and grand-grand...). After the XML
file is generated, it is manually edited to add information about required
fields, field validation, key fields for children etc.
Such an XML file for the above scenario may look like this
<?xml version="1.0"?>
<form>
<object class="com.foo.invoices.Invoice">
...
<property id="vatNo" />
<property id="currencyId" required="true"
validator="com.foo.bar.CurrencyValidator" />
...
<child name="invoiceAddresses" class="com.foo.invoices.InvoiceAddress">
<key id="addressTypeId" />
...
<property id="streetAddress" />
<property id="zipCode" />
<property id="city" />
<delete id="DELETE_ADDRESS" />
<child name="invoiceContacts" class="com.foo.invoices.InvoiceContact">
<key id="positionTypeId" />
...
<property id="name" />
<property id="phone" />
...
<delete id="DELETE_CONTACT" />
</child>
</child>
<child name="invoiceLines" class="com.foo.invoices.InvoiceLineArticle">
<key id="invoiceLineId" />
<property id="articleId" required="true" validator="..." />
...
<delete id="DELETE_LINE" />
<child name="invoiceLineTexts" class="com.foo.invoices.InvoiceLineText">
<key id="textTypeId" />
<property id="invoiceLineText" />
<delete id="DELETE_LINE_TEXT" />
</child>
</child>
...
</object>
</form>
Whether a new BeanGenerator module will have any support for what we
need/use or not, I would like to make the following suggestions:
- Make the metadata source pluggable; XML, database, whatever
- Avoid tight coupling with Struts
At 2005-06-20 22:44, you wrote:
>Hi All,
>
>I have been working on reusable component called BeanGenerator, which as
>following functionality. Please advise if it is eligible to be posted as
>component in Commons.
>
>Availability of DynaBean from BeanUtils has simplified the programmer
>lives to quite a bit extent but it lacks in certain functionality which is
>very much required to all programmers to name a few like CompileTime
>method semantics validation, generating the equals, hashCode for beans,
>type conversion from all Strings types to business specific type etc.
>
>So I had been wondering if there is a tool, which takes the beans
>information in the xml file and generates the corresponding beans
>automatically. Some of the visible advantages of this are,
>
>1. From the same xml bean data, we can automatically generate two bean
>classes.
>For ex., if you specify bean information for com.test.User, tool can
>generate com/test/User.java file and also com/test/UserForm.java file,
>with corresponding access method. so by calling new User().getFormBean(),
>would return com.test.UserForm, which can be automatically used in the
>jsps as action forms. Also by calling new UserForm().getBusinessBean() may
>automatically return a new instance of com.test.User, with all fields
>automatically type converted and populated.
>
>2. By just marking a set of attributes as primary key fields, tool can
>generate the equals, hashCode in a consistent manner
>
>3. If user wish, tool can generate the containers for specific beans. For
>ex., if you have com.test.User and if you want container for this, tool
>can generate com.test.Users, with all access fields which returns the User
>instead of object, which avoids ugly typecast things all across
>
>4. Also tool can add necessary comparator implementation for marked
>attributes, so that beans in a container can be automatically sorted. so
>by calling users.sort (User.FIELD_FIRSTNAME) or users.sort (User.LASTNAME)
>etc.
>
>6. Generate appropriate reset, toString, validate methods, which are
>really a nightmare to maintain in enterprise applications (if you add a
>attribute or modify one)
>
>5. Tool can automatically generate java files, compile them and create
>corresponding jar files and put it in a specified folder. So modifying the
>bean specification is as simple as just adding a task and executing it.
>
>As you clearly visible, by having the Bean information in xml once, we can
>programmatically can do many things, which would be very error prone or
>laborious otherwise. Also it is clear that, this is not to do with
>BeanUtils module what we have as of now.
>
>Please advise if this is of any use for other java developers or
>designers. and also if this can hosted as new module in commons may be
>like Commons BeanGenerator.
>
>Thanking you all,
>Santhosh.
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org