The purpose of this document is to assist people interested in the Open For Business (OFBiz) project to get started as quickly as possible. I have been using the OFBiz framework for a few months now and have found it to be very helpful in building Java based web applications. There are a lot of components that can significantly reduce the cost of your project. Unfortunately, like many open source products the documentation is fairly light. This document does not cover everything in detail but does provide some helpful tips to help you started. This Quick Start document will cover the following topics:
Comparing Ofbiz with Other
Open Source Projects
If you found OFBiz chances are you have seen several other open source projects. What makes OFBiz different than most of these projects is that OFBiz is an entire framework of components and tools to help develop web applications in Java. A good web application is broken down into different logical layers. There is a web or presentation layer, a business logic layer, and a database or persistent layer. Frameworks such as Struts, Cocoon, and Velocity focus primarily on the web tier. Struts for example, follows a model-view-controller (MVC) architecture for combining JSP pages with Java Beans or Action classes as they are called. Struts is a very good framework for building web applications but it does not provide components for accessing databases or controlling workflow. These components you are expected to build yourself. This is where OFBiz differs from many other open source Java projects. If you’re already working with another Java framework such as Struts you could easily include some of the OFBiz components into your project.
The easiest way to get started with OFBiz is to download the ofbiz-XX-XX-complete.zip version of the source. This is significantly bigger than the ofbiz-XX-XX-src.zip version because it includes the entire download of Catalina (Tomcat 4.X), but if you can afford the download time it will be worth it. The complete version comes with several scripts (e.g. ofbiz.bat ) that will start Catalina with the OFBiz components installed and running without any compiling. And if you have Jakarata Ant you will be able to build the source without modifying anything. This will save you a lot of time at first. I wouldn’t recommend getting the source from CVS or the even downloading the "source only" version until you are more familiar with OFBiz and its components. Otherwise you will spend a considerable amount of time setting up classpaths and configuration files.
Requirements:
Recommendations:
Separating the Entity Engine from Other Components
Getting the sample applications (e.g. E-commerce application) up and running is fairly straightforward, but working with the components separately can be difficult but not impossible. The difficult part is learning how to configure the components independently from the other components. This section demonstrates how the OFBiz entity engine can be configured and tested separately from the other components. Working with individual OFBiz components can help you learn more quickly the OFBiz framework.
OFBiz's entity engine is one of the most mature components and I think one of its greatest assets. Writing persistent layer code in JDBC may not be that difficult but it can take a lot of time and is error prone. OFBiz's entity engine can greatly simplify writing database components and lets you focus on the business logic of the application.
A good strategy for working with the OFBiz entity engine is to create your own JUnit tests that verify that your entity definitions are setup and working correctly. I like the Extreme Programming (XP) philosophy of code a little, test a little, etc. This is also a good method for maintaining and testing your code base. This is not a tutorial on JUnit for more information JUnit see the references section below. I have found this to be a good way to work with the OFBiz entity engine components.
Key Files:
Configuration Steps
Here are some steps you can use to help you understand how to configure the OFBiz entity engine. I use JBuilder 5.0 Professional for my development. Some of my examples will be specific to JBuilder, but you should be able extract the necessary information for you development environment.
<entity-model-reader name="main"> <resource loader="mainfile" location="entitymodel.xml"/> <resource loader="mainfile" location="entitymodel_bookstore.xml"/> </entity-model-reader>
<delegator name="default" entity-model-reader="main" entity-group-reader="main"> <group-map group-name="org.ofbiz.commonapp" datasource-name="localhsql"/> <group-map group-name="com.mybookstore.bookstore" datasource-name="other_datasource"/> </delegator> <datasource name="localhsql" helper-class="org.ofbiz.core.entity.GenericHelperDAO" field-type-name="hsql" check-on-start="true" add-missing-on-start="true"; use-foreign-keys="true"; use-foreign-keys-indices="true"; use-fk-initially-deferred="false"; join-style="theta-oracle"> <sql-load-path path="commonapp/db" prepend-env="ofbiz.home"/> <sql-load-path path="ecommerce/etc" prepend-env="ofbiz.home"/> <inline-jdbc jdbc-driver="org.hsqldb.jdbcDriver" jdbc-uri="jdbc:hsqldb:../../data/ofbiz" jdbc-username="sa" jdbc-password="" isolation-level="ReadUncommitted"/> <!-- <jndi-jdbc jndi-server-name="localjndi" jndi-name="java:/HsqlDataSource"/> --> </datasource>
This delegator example uses the OFBiz 'default' delegator. The example demonstrates how you can use 'group-maps' to group components into different data sources. In this example all of the org.ofbiz.commonapp components use the localhsql (hypersonic database) and the com.mybookstore.bookstore components use the 'other_datasource' data source. This feature let you use different databases from a single delegator.
<!-- ========================================================= --> <! - Bookstore Entities --> <!-- ========================================================= --> <entity-group group="com.mybookstore.bookstore" entity="Store" /> <entity-group group="com.mybookstore.bookstore" entity="Books" /> <entity-group group="com.mybookstore.bookstore " entity="Authors" />
In the above example, I have defined three entities that are included in the entitymodel_bookstore.xml. Note also that these entities belong to the group com.mybookstore.bookstore which we defined in the entityengine.xml in step 3. This means the entity tables for Store, Books, and Authors will be found in the 'other_datasource' configuration.
JBuilder Setup
Steps 5 through 8 describe how to setup an OFBiz entity project in JBuilder. Your development environment may be different but hopefully you can extract the necessary information to get an OFBiz entity project setup in your Java IDE. I like to use a combination of an IDE for source level debugging and the OFBiz Ant scripts to build, setup, and deploy the entire web application. For this reason I keep the configuration files in the same location for both development environments.
You can add other source directories to your project as you need them.
For the etc directory I create the jar file as follows: jar cvf ofbiz_etc.jar *.xml And for the DTD files I include all of them with the command: jar cvf ofbiz_dtd.jar *
public void testFindByPrimaryKey() { try { Debug.logInfo("Entered testFindByPrimaryKey"); //Instantiate the delegator. GenericDelegator delegator = GenericDelegator.getGenericDelegator("default"); assertNotNull("Error creating delegator object", delegator); //Create an Book Entity String bookId = delegator.getNextSeqId("Book").toString(); GenericValue bookValue = delegator.makeValue("Book", UtilMisc.toMap("bookId", bookId, "author", "Joe Smith', "name", "My Favorite Book", "publisher", "Some Publisher")); GenericValue book = delegator.create(bookValue); assertNotNull("Couldn't create Book entity", book); // Find book by primary key GenericValue foundBook = delegator.findByPrimaryKey("Book", UtilMisc.toMap("bookId", bookId)); assertNotNull("Couldn't find Book: " + bookId, foundBook); }
Creating Your First Web Application
Again the easiest way to learn OFBiz is to start using it and the easiest way to learn the OFBiz web controller is to use one existing web application that is already setup. For example in the <OFBIZ_HOME> directory there is a ecommerce, ordermgr, workeffort, and webtools application. Each of the projects includes a webapps directory where all the JSP and control files exists. You need to become familiar with the following files when dealing with the web controller. .
Key Files:
Here are some steps you can take to quickly get a web application working in OFBiz:
<Context path="/your_webapp" docBase="../../ofbiz/your_webapp/webapp" debug="0" reloadable="true"> <Logger className="org.apache.catalina.logger.FileLogger" prefix="your_webapp_log." suffix=".txt" timestamp="true"/> </Context>
<request-map uri="listbooks"><security https="true" auth="false"/> <event type="java" path="com.mybookstore.books.BookEvents" invoke="listBooks"/> <response name="success" type="view" value="listbooks"/> <response name="error" type="view" value="error"/> </request-map> <view-map name="listbooks" type="region"/>
Assuming the default context for my application is 'bookstore', you would access the above URI by putting in http://localhost:8080\bookstore\control\listbooks in your browser if you were running Catalina locally. The OFBiz web controller would then execute the method 'listBooks' in the BookEvents class. If the listBooks method returned the string 'success' the controller would pass the response to a 'view' called 'listbooks.' If an error occurred the controller would pass the response to the 'error' view. Views in OFBiz are mapped to a region. Regions are specified in the regions.xml file explained in step 4. The example shows a Java event but there are other event types that you can use. See the OFBiz documents for more details on the other event services that are available.
<define id='listbooks' region='LEFT_ONLY_REGION'> <put section='title'>List of Books</put> <put section='content' content='/books/listbooks.jsp'/> </define> <define id='LEFT_ONLY_REGION' region='MAIN_REGION'> <put section='leftbar' content='LEFTBAR_REGION'/> </define> <define id='LEFTBAR_REGION' template='/templates/leftbar_template.jsp'> <put section='first' content='/menus/adminmenu.jsp'/> <put section='second' content='/menus/customermenu.jsp'/> </define>
This document provides some tips for starting with OFBiz. There are many other topics that are not covered in this document. If you find errors in this document please forward them to me and I will make the corrections. Also, if there are other tips or helps you would like included in this document please let me know. The OFBiz project has many components that you can include in your project. Some of these components include a rules engine, workflow engine, and simple languages. Future 'Quick Tips' documents on these topics will be forth coming.