Book Image

Apache OfBiz Cookbook

Book Image

Apache OfBiz Cookbook

Overview of this book

Apache Open For Business (OFBiz) is an enterprise resource planning (ERP) system that provides a common data model and an extensive set of business processes. But without proper guidance on developing performance-critical applications, it is easy to make the wrong design and technology decisions. The power and promise of Apache OFBiz is comprehensively revealed in a collection of self-contained, quick, practical recipes in this Cookbook. This book covers a range of topics from initial system setup to web application and HTML page creation, Java development, and data maintenance tasks. Focusing on a series of the most commonly performed OFBiz tasks, it provides clear, cogent, and easy-to-follow instructions designed to make the most of your OFBiz experience. Let this book be your guide to enhancing your OFBiz productivity by saving you valuable time. Written specifically to give clear and straightforward answers to the most commonly asked OFBiz questions, this compendium of OFBiz recipes will show you everything you need to know to get things done in OFBiz. Whether you are new to OFBiz or an old pro, you are sure to find many useful hints and handy tips here. Topics range from getting started to configuration and system setup, security and database management through the final stages of developing and testing new OFBiz applications.
Table of Contents (15 chapters)
Apache OFBiz Cookbook
Credits
About the Author
About the Reviewers
Preface

Reading data from an OFBiz data source


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

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:

http://cwiki.apache.org/confluence/x/CwFk

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

SELECT FROM 'SomeTable' WHERE primaryKeyId='SomeValue';

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

SELECT * FROM 'SomeTable';

OFBiz solution

Use the Entity Engine GenericDelegator object's findList method to return a list of values. Set all findList parameters to null.

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

SELECT DISTINCT FROM SomeTable WHERE column1='value';

OFBiz solution

Use the EntityfindOptions object to tell OFBiz to find and return only distinct values.

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

find

EntityListIterator

Used for large data sets.

findList

List<GenericValue>

Returns a list of one or more GenericValue objects, one representing each row returned from the database query.

findOne

GenericValue

Returns a single GenericValue object matching the query conditions.

Note

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:

  • A single row from the database

  • A list of one or more rows

  • A null value

  • An error

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:

http://ci.apache.org/projects/ofbiz/site/javadocs/