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

Working with large result sets (EntityListIterator)


Under normal conditions, the Entity Engine handles all the low-level database access tasks necessary to return a result set to memory for application use. Sometimes, a result set is too large to fit into memory. Under these conditions, a Java Out of Memory error will be thrown. To retrieve large result sets that exceed available memory limits, use the Entity Engine EntityListIterator object and retrieve partial result sets and process returned values in smaller chunks.

The following code snippet demonstrates the basic use of the EntityListIterator. Note: code has been left out to make the snippet readable. To use this code, you will need to add Java statements as appropriate:

// Don't forget to import EntityListIterator, EntityExpr,
// EntityCondition and other necessary Java packages
// Create any conditional query expressions
// We know we now have thousands of recipes, so to improve
// performance and conserve memory, we use the EntityListIterator
List<EntityExpr> exprs =
UtilMisc.toList(EntityCondition.makeCondition("notes",
EntityOperator.NOT_EQUAL, null),
EntityCondition.makeCondition("amount",
EntityOperator.GREATER_THAN, Long.valueOf(0)));
// Create a default EntityListIterator object
EntityListIterator eli = null;
try {
// Use the GenericDelegator method call to return a pointer
// to data set results. Note we are getting data from a
// view-entity
eli = delegator.find("RecipeTypeAndIngredientsView",
EntityCondition.makeCondition(exprs,EntityOperator.AND),
null, null, UtilMisc.toList("recipeId"), null);
List<String> processList = FastList.newInstance();
if (eli != null) {
GenericValue value = null;
// Use EntityListIterator to loop through the result set
// Each next method call retrieves a GenericValue
// OFBiz will get more values from the data source
// automatically
while (((recipeTypeAndIngredient =
(GenericValue) eli.next()) != null)) {
// Do some processing with this list
}
}
}
catch (GenericEntityException e) {
Debug.logError(e, module);
// When done, don't forget to close the EntityListIterator
}
finally {
if (eli != null) {
try {
eli.close();
}
catch (GenericEntityException e) {
Debug.logError(e, module);
}
}
}

To set scrolling and concurrent read options for a specific query, use the EntityFindOptions as shown in the code. Note: these options are valid for any Entity Engine data source query. However, changing scrolling behavior when processing a result set is only valid when using the EntityListIterator object as a pointer:

// Create a new EntityFindOptions object
EntityFindOptions efo = new EntityFindOptions();
// Set scrolling to INSENSITIVE. This allows forward and backward
// movement, but the result set is NOT sensitive to changes made by
// others. Data may be changed by other users while you are scrolling
// through the set.
// Scroll SENSITIVE instructs the Entity Engine to be aware and
// update the result set if any changes have occurred. Use with care
// as this setting introduces additional processing overhead.
efo.setResultSetType(EntityFindOptions.TYPE_SCROLL_INSENSITIVE);
// set concurrency to CONCUR_READ_ONLY so that a row may only be
// updated by the transaction that selected it.
efo.setResultSetConcurrency(EntityFindOptions.CONCUR_READ_ONLY);
efo.setSpecifyTypeAndConcur(true);
efo.setDistinct(false);

Pass the EntityFindOptions object as a parameter to the find method call.

The following Java code snippet shows direct manipulation of the result set cursor as provided by using EntityListIterator object methods:

GenericValue genericValue;
if ((genericValue = eli.next()) != null) {
// Get the last cursor location (end of the result set)
eli.last();
// Set the current cursor location as
int rowIndex = eli.currentIndex();
// See if we have processed at least 100 rows
if (rowIndex== 100) {
// If we have processed as least 100 rows
// Then get the value pointed to by the previous cursor
// position
genericValue = eli.previous();
// Do some processing here
}
}
// close
eli.close();
// Don't forget to add finally block here

To move within a result set, use the EnitityListIterator object's cursor movement methods as shown here:

EntityListIterator method

Usage

afterLast()

Sets the cursor position to just after the last result. This makes the previous() method call return the last result in the set.

beforeFirst()

Sets the cursor position to just before the first result. This makes a call to next() return the first result in the set.

currentIndex()

Returns the current cursor position.

first()

Sets the cursor position to the first result. If the result set is empty, this returns a boolean false.

getPartialList (int start, int number)

Returns a partial list of results starting at the indicated cursor position, containing at most number rows.

last()

Sets the cursor to the last result in the set.

first()

Sets the cursor to the first result in the set.