miércoles, 24 de febrero de 2010

Modifying and creating new data entities (OpenTaps)

So now we will create some new data entities for our OpenTaps installation.

Our main products are seeds, and, as you already know, they are living beings that need some special care and control while handling them. Not all seeds grow and some may die as time passes, so we need to control the germination rate of the seeds we handle.

Each variety of seed has different tolerances for the germination rate, so, some kind of seeds may be sold with a 99% of germination rate, while others would be ok with just an 80%. So we need a new field for our product entity to handle the minimum germination rate accepted for each of our products.
So we will create two new files in our citsaProduct module:



  • entitymodel.xml -> Here we will describe our new entities, and their data types and fields
  • entitygroup.xml -> Here we will configure entity groups, it usually works if we want to use different databases for our entities, but for now we will use only one database.
So first of all, lets add a new field to the Product entity, but the problem is that Product is defined in the file applications/product/entitydef/entitymodel.xml but we don't want to modify that file, it would cause trouble in future upgrades, so fortunately the framework lets us extend an existing entity with new fields without touching the original configuration file, so lets modify our own entitymodel.xml and add the following text:


<?xml version="1.0" encoding="UTF-8"?>
<entitymodel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/entitymodel.xsd">

<extend-entity entity-name="Product">
<field name="germinationRate" type="fixed-point"/>
</extend-entity>

</entitymodel>

As simple as that, we use the tag extend-entity to add new fields and keys to an existing entity, in this case, we are adding a new field named germinationRate with a data type of fixed-point

Look at how the data types don't look like SQL data types, the framework has some common or generic definitions that will be translated to true SQL data types at runtime, you can look the real data types by looking at the xmls located in the folder /opentaps-1.4/framework/entity/fieldtype

Now we want to make a new entity named "Package", just to make an example on how to create new entities, so lets modify again our entitymodel.xml file and add the following text:


<?xml version="1.0" encoding="UTF-8"?>
<entitymodel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/entitymodel.xsd">

<entity entity-name="Package" package-name="org.ofbiz.citsa">
<field name="packageTypeId" type="id-ne"/>
<field name="name" type="name"/>
<field name="shortName" type="name"/>
<prim-key field="packageTypeId"/>
</entity>

<extend-entity entity-name="Product">
<field name="germinationRate" type="fixed-point"/>
</extend-entity>

</entitymodel>
Finally, we have to modify the entitygroup.xml file to tell it to recognize our new Package entity:



<?xml version="1.0" encoding="UTF-8"?>
<entitygroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/entitygroup.xsd">
<entity-group entity="Package" group="org.ofbiz"/>
</entitygroup>

As a last step, we will need to modify the file ofbiz-component.xml of our module, so we can tell the Ofbiz Framework to load our new data entities:


<?xml version="1.0" encoding="UTF-8" ?>
<ofbiz-component name="citsaProduct"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/ofbiz-component.xsd">

<resource-loader name="main" type="component"/>

<classpath location="build/classes/*" type="dir"/>
<classpath location="config" type="dir"/>

<entity-resource loader="main" location="entitydef/entitymodel.xml" reader-name="main" type="model"/>
<entity-resource loader="main" location="entitydef/entitygroup.xml" reader-name="main" type="group"/>

</ofbiz-component>


After all this steps we should run the following script:

ant make-base-entities

This will generate Hibernate entity files in the folder hot-deploy/opentaps-common/src/entities that we will later use in our code

Finally, we just need to restart our server, and it should alter the Product table and create a new table named "Package"

0 comentarios:

Publicar un comentario