Base Entity Classes
Introduction ===
To support the object-oriented Domain Driven Architecture, there is a set of Java entity classes in the org.opentaps.domain.base.entities package for all the entities defined with the ofbiz entity engine, both from the original ofbiz applications and the opentaps applications. The entity classes contain all the fields of the entity, accessor (get/set) methods for each field, and fromMap and toMap methods to convert the Java class to a Map. The only exception is that all floating-point values are automatically returned as BigDecimal, instead of Double. This is done in the base Entity.java
Contents
Generating Base Entities
The entity classes are automatically generated using a freemarker template hot-deploy/opentaps-common/templates/BaseEntity.ftl, based on the entitymodel.xml definitions for all the entities, including view-entities and fields defined by the extend-entity tags, and the Java types defined in the fieldtype XML files for the entity engine. To generate base entities, from the opentaps directory,
$ ant make-base-entities
It will clear out all the files in the base entities package, start opentaps and load the delegator, and then regenerate the Java classes based on the current entity definitions.
Using Base Entities
The base entity Java classes could be used as a replacement for the ofbiz GenericEntity/GenericValue objects. To go from a GenericValue to a Java class, use the Repository.loadFromGeneric methods, such as:
Repository repository = new Repository(delegator); List enumerationEntities = repository.loadFromGeneric(Enumeration.class, enumerations);
When you are working from a repository, you should use the loadFromGeneric method which also sets the repository for a new object:
GenericValue value = getDelegator().findByPrimaryKey("Invoice", UtilMisc.toMap("invoiceId", invoiceId)); Invoice invoice = (Invoice) this.loadFromGeneric(Invoice.class, value, this);
These methods uses reflection to access the fromMap method of entity classes. They can create a one object from one GenericValue or a List of objects from a List of GenericValues.
To go from a Java class, you can simply use the toMap method to create a GenericValue, such as:
Enumeration enumeration = new Enumeration(); // set its values GenericValue value = new GenericValue(enumeration.toMap());
For convenience, we have also implemented all the get_(String fieldName) and the set (String fieldName, Object value) methods of the ofbiz GenericEntity/GenericValue, so you can use these classes with the "." notation in freemarker pages. For example, for an object of the Invoice class, you can use
Invoice.getInvoiceId()
or
Invoice.invoiceId
as before.
DO NOT MODIFY THESE BASE ENTITY CLASSES. They should be automatically generated every time your data model changes, so all your changes will be overwritten. If you have more complex classes, extend these base entity classes and implement the additional methods there.
Localization
Backward-compatible localization is supported with two special get methods for all entities. You can specify the field name and a locale with:
get(fieldName, locale);
Or, you can specify the UI labels resource, such as FinancialsUiLabels, to use with:
get(fieldName, resourceName, locale);
Entity Relations
The auto generated based entities also provide you with methods to traverse the entity relationships defined in the entitymodel XML files. You can either use the basic getRelated_ methods, which allow you to specify the class name and the relationship name, such as:
getRelated(ReturnItem.class, "ReturnItem");
Or, you can use the methods which are also automatically generated from the entity model XML definitions for the relationships. For example, if your entity had a one to one relationship defined as:
Then your auto generated based entity would have a method defined as:
ReturnHeader getReturnHeader();
Similarly, if you define a one to many relationship, there will be a method which returns a list rather than an object. For example,
will cause the following method to be created:
Note that the method names are automatically pluralized if the relationship is one to many.
Finally, if your relationship has a title, it will also be added to the method, so:
will create a method:
Interacting with the Database
The ofbiz specific implementation of the Repository provides you with the following methods for interacting with the database which abstracts the underlying data access layer:
- findOne: finds an Entity by its primary key
- findOneNotNull: like findOne but throws EntityNotFoundException instead of returning null
- findList: finds a list of Entities using the arguments. Similar to findByAnd and findByCondition of the delegator.
- getFirst: gets the first Entity from a list, or null if there is no first value in the list
Convenience Methods
- getDistinctFieldValues: a static method on Entity which gets you a Set of the distinct values for a field name.