Business is booming at the bakery, and we need to move beyond the WebTools Entity Engine interface and start implementing some user-level applications to read from and write to the database. For the remainder of this chapter, we shall assume that we have in place one or more OFBiz Events and/or Services written in Java that will handle the logic of our application's interface to the OFBiz Entity Engine. What remains to be described are specific instances using Entity Engine tools and utilities.
Note: while using the OFBiz Mini-Language and widget tool kit often make reading and writing to an OFBiz data source easier, they are but one option when choosing to use the Entity Engine programmatically. Widgets are discussed elsewhere in this book while the OFBiz Mini-Language is documented on the OFBiz Wiki:
To retrieve a single database table row given a table's primary key, follow this example. See the comments within the code for more information. (Note: code is provided for demonstration purposes only. You will need to fill in the gaps in many places to make these code snippets function):
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntity;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
// From within a Service, get the GenericDelegator object directly
// from the context
// The object named ctx is passed from the Service Engine as an
// instantiation of the current context
GenericDelegator delegator = ctx.getDelegator();
GenericValue recipe = null;
// All GenericDelegator calls throw GenericEntityException on failure
// So place inside a try/catch block
String recipeId = "r001"; // For demonstration purposes only.
try {
recipe = delegator.findOne("Recipe",
UtilMisc.toMap("recipeId", recipeId), false);
}
catch (GenericEntityException e) {
Debug.logWarning(e.getMessage(), module);
}
// recipe is a GenericValue object, so we can access field
// values directly as shown
if(recipe != null) { // Note: a null GenericValue object is valid
String recipeName = (String) recipe.get("recipeName");
}
To iterate through a list of GenericValue
objects as returned by a call to the entity engine, use code similar to the following:
// Within an event, get the GenericDelegator from the request
// attribute. As shown here
GenericDelegator delegator =
(GenericDelegator) request.getAttribute("delegator");
List<GenericValue> recipes = null;
try {
// The delegator.findList() method returns a Java List or null
// if no values are found
recipes = delegator.findList("recipes",
EntityCondition.makeCondition("recipeId",
EntityOperator.EQUALS, "r001"), null, null, null, false);
}
catch (GenericEntityException e) {
return ServiceUtil.returnError(e.getMessage());
}
// recipes contains a list of GenericValue objects. Each object
// represents a single returned record from the data source.
// Loop through this list and extract values as appropriate
if(UtilValidate.isNotEmpty(recipes)) {
Iterator itr = invoiceItems.iterator();
while(itr.hasNext()) {
// Pick off each GenericValue on the list
GenericValue thisRecipe = (GenericValue) itr.next();
// Access the thisRecipe GenericValue object just like
// you would any other
String recipeId = thisRecipe.getString("recipeId");
// Do more stuff…
}
}
The following Java code snippets are examples of commonly used OFBiz query constructs.
To return a single database record, that is, a GenericValue
object, given a primary key value, from the specified table, use the following:
SQL equivalent |
|
OFBiz code snippet |
try { GenericValue aValue = delegator.findOne("SomeTable", UtilMisc.toMap("primaryKeyId","SomeValue"), false); } catch (GenericEntityException e) { // Process any errors here }
|
Return all records from the given table. Take all Entity Engine defaults or set required values to null
as shown here:
SQL equivalent |
|
OFBiz solution |
Use the Entity Engine |
OFBiz code snippet |
try { List allRecords = delegator.findList("SomeTable",null,null, null,null,false); } catch(GenericEntityException ge) { // Process Errors Here }
|
To select only unique records from a table where a table's column value is equal to some given value, use the following as an example:
SQL equivalent |
|
OFBiz solution |
Use the |
OFBiz code snippet |
// Create an EntityFindOptions object EntityFindOptions findOptions = new EntityFindOptions(); // Set the findOptions to distinct is true findOptions.setDistinct(true); EntityCondition findCondition = UtilMisc.toList(EntityCondition.makeCondition( 'column1',EntityOperator.EQUALS, 'value')); try { List allRecords = delegator.findList('SomeTable', findCondition, EntityOperator.AND), null,null,findOptions,false); } catch(GenericValueException ge) { // Process Errors Here }
|
To read data, the following GenericDelegator
methods are typically used:
GenericDelegator method |
Returned result set data type |
Usage |
---|---|---|
|
|
Used for large data sets. |
|
|
Returns a list of one or more |
|
|
Returns a single |
Note: while there are a fair number of legacy GenericDelegator
methods to choose from when building queries to read data (available and in use within the OFBiz code), the earlier mentioned methods are the current best practice advice for new code.
To summarize, read requests to the Entity Engine return a result set containing either:
Rows are automatically mapped into GenericValue
objects based on an entity's model, by the Entity Engine. Because OFBiz does all the low-level data mapping and data type conversions, applications have a much cleaner, consistent view of the data. A single request for data may be run on many different underlying databases without the application developer changing any code.
The OFBiz delegator API contains a rich set of methods to use when accessing the Entity Engine through the Java interface. For more information, please see the JavaDocs online at:
3.149.249.174