Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • framework components: These are lower-level components that provide the technical layer and tools to the application components; the features provided by these components are typically the ones provided by any other development framework (data layer, business logic layer, transaction handling, data source pools, etc…)
  • application components: These are generic business components required for ERP applications that can be extended/customized (product, order, party, manufacturing, accounting, etc…); application components have access to the services and tools provided by the framework components and to the services published by other application components.
  • plugins components: These components are similar to applications components but meant for special purpose applications like eCommerce, google base integration, eBay integration, etc

...

  1. Simply open $OFBIZ_HOME/plugins/ofbizDemo/widget/OfbizDemoScreens.xml file from the ofbizDemo plugin (you just created)


    Code Block
    languagexml
    <?xmlversion="1.0"encoding="UTF-8"?>
    <screens xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/widget-screen.xsd">
        <screen name="main">
            <section>
                <actions>
                    <set field="headerItem" value="main"/><!-- this highlights the selected menu-item with name "main" -->
                </actions>
                <widgets>
                    <decorator-screen name="OfbizDemoCommonDecorator" location="${parameters.mainDecoratorLocation}">
                        <decorator-section name="body">
                            <label text="Hello World!! :)"/>
                        </decorator-section>
                    </decorator-screen>
                </widgets>
            </section>
        </screen>
    </screens>

    We have only added the <label text="Hello World!! :)" />

  2. Now you will need to restart OFBiz by reloading data($ ./gradlew loadAll ofbiz). It's required as you have created a new component with some security data for your component (Setup by default in your component data directory as OfbizDemoSecurityGroupDemoData.xml) and as you will restart it, the ofbizdemo component will also be loaded.
  3. As OFBiz restarted direct your browser to your application here https://localhost:8443/ofbizDemo
  4. You will be asked to log in. Login with user: admin password: ofbiz.
  5. As you log in you will see the ofbizdemo application up with the hello world message you have put on screen as shown below the given image.
    That's it, congratulations your first component is setup set up and running.

Creating First Database Entity (Table) 

...

To create custom Entities/Tables in the database, you simply need to provide entity definition in the $OFBIZ_HOME/plugins/ofbizDemo/entitydef/entitymodel.xml file of your ofbizdemo application. This file structure is already set up when you used the Gradle task to set up your component. You simply need to go in and provide entity definition as shown below. Here we are going to add two new entities for the ofbizdemo application.


Code Block
languagexml
<?xml version="1.0" encoding="UTF-8"?>
 
<entitymodel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/entitymodel.xsd">
 
    <title>Entity of an Open For Business Project Component</title>
    <description>None</description>
    <version>1.0</version>
 
    <entity entity-name="OfbizDemoType" package-name="org.apache.ofbiz.ofbizdemo" title="OfbizDemo Type Entity">
        <field name="ofbizDemoTypeId" type="id"><description>primary sequenced ID</description></field>
        <field name="description" type="description"></field>
        <prim-key field="ofbizDemoTypeId"/>
    </entity>
 
    <entity entity-name="OfbizDemo" package-name="org.apache.ofbiz.ofbizdemo" title="OfbizDemo Entity">
        <field name="ofbizDemoId" type="id"><description>primary sequenced ID</description></field>
        <field name="ofbizDemoTypeId" type="id"></field>
        <field name="firstName" type="name"></field>
        <field name="lastName" type="name"></field>
        <field name="comments" type="comment"></field>
        <prim-key field="ofbizDemoId"/>
        <relation type="one" fk-name="ODEM_OD_TYPE_ID" rel-entity-name="OfbizDemoType">
            <key-map field-name="ofbizDemoTypeId"/>
        </relation>
    </entity>
 
</entitymodel>

...

In our previous section, we have seen how to create the entities (tables), now it's time to create a form which that will allow you to make entries in that entity.

...

Code Block
languagexml
titleservices.xml
<?xml version="1.0" encoding="UTF-8"?>
<services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/services.xsd">
 
    <description>OfbizDemo Services</description>
    <vendor></vendor>
    <version>1.0</version>
 
    <service name="createOfbizDemo" default-entity-name="OfbizDemo" engine="entity-auto" invoke="create" auth="true">
        <description>Create an Ofbiz Demo record</description>
        <auto-attributes include="pk" mode="OUT" optional="false"/>
        <auto-attributes include="nonpk" mode="IN" optional="false"/>
        <override name="comments" optional="true"/>
    </service>
 
</services>

Now again have a look at $OFBIZat the $OFBIZ_HOME/plugins/ofbizDemo/ofbiz-component.xml file. You already have resource entry made in it for loading services defined in this file as:

...

For this service definition to load you will need to restart OFBiz. To test this service you directly go to webtools --> Run Service option here: https://localhost:8443/webtools/control/runService

Image RemovedImage Added

Info

Running service via Web Tools: This is a smart utility provided by the framework to run your service.

On submission of the form above, you will presented present a form to enter IN parameters of the service.

...

Internationalization of Apache OFBiz is really easy, we define the UI Labels in various languages, and on the basis of the user's locale, the respective label is shown.

...

Here you can notice we have used auto-fields-service to auto-generate the form based on service definition IN/OUT attributes. 

Go to Screens xml XML file(OfbizDemoScreens.xml) add this form location in decorator body to your screen that you used to show the Hello World... text. As shown below

...


Everything set, let's have a look into to our recently create form https://localhost:8443/ofbizDemo

Primary The primary key(ofbizDemoId) is not needed to be send sent in with the form, it will be auto sequenced by OFBiz in db database records.


Create a Find Form

Let's create a find form for the entity OfbizDemo , so that you search OfbizDemos being created.

...

Form or Screen's action tag is used for data preparation logics logic for your view.


Info

We have used OOTB OFBiz generic service performFind to do the search operations which is easy and efficient to use when you have to perform a search on one entity or one view entity.

...

2.) In next step, we will include these form forms in the screen, let's add these form in forms in the OfbizDemoScreens.xml file. For this include the FindOfbizDemo screen defined below in the OfbizDemoScreens.xml

...

As we have seen above Internationalization of Apache OFBiz is really easy, we define the UI Labels in various languages, and on the basis of the user's locale, the respective label is shown.

...

Whenever you have to build a business logic you should prefer to write services to leverage features from its built-in Service Engine.

The service "createOfbizDemo" that you created earlier was using engine="entity-auto" and hence you didn't need to provide its implementation and OFBiz took care of create operation.  When you need to work on complex operations in service involving multiple entities from database and custom logics to be built, you need to provide custom implementation to your service. In this section, we will focus on this.

Service in Java

You can implement a service in Java as directed here in the below-given steps:

1.) Define your service, here again, we will be operating on the same entity(OfbizDemo) of our custom Ofbiz Demo application. Open your service definition file $OFBIZ_HOME/plugins/ofbizDemo/servicedef/services.xml and add a new definition as:

...

 Example: src/main/java/com/companyname/ofbizdemo/services. Services for your application which that have to be implemented in Java can be placed in this java directory.

3.) Define new Java Class in file OfbizDemoServices.java here in services directory and implement a method, which is going to be invoked by your service definition, as shown below:

...

package com.companyname.ofbizdemo.services;
import java.util.Map;

import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.GenericEntityException;
import org.apache.ofbiz.entity.GenericValue;
import org.apache.ofbiz.service.DispatchContext;
import org.apache.ofbiz.service.ServiceUtil;

public class OfbizDemoServices {

    public static final String module = OfbizDemoServices.class.getName();

    public static Map<String, Object> createOfbizDemo(DispatchContext dctx, Map<String, ? extends Object> context) {
        Map<String, Object> result = ServiceUtil.returnSuccess();
        Delegator delegator = dctx.getDelegator();
        try {
            GenericValue ofbizDemo = delegator.makeValue("OfbizDemo");
            // Auto generating next sequence of ofbizDemoId primary key
            ofbizDemo.setNextSeqId();
            // Setting up all non primary key field values from context map
            ofbizDemo.setNonPKFields(context);
            // Creating record in database for OfbizDemo entity for prepared value
            ofbizDemo = delegator.create(ofbizDemo);
            result.put("ofbizDemoId", ofbizDemo.getString("ofbizDemoId"));
            Debug.log("==========This is my first Java Service implementation in Apache OFBiz. OfbizDemo record created successfully with ofbizDemoId: "+ofbizDemo.getString("ofbizDemoId"));
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError("Error in creating record in OfbizDemo entity ........" +module);
        }
        return result;
    }


4.) Stop the server and re-start using "./gradlew ofbiz", it will compile your class and will make it available when ofbiz restarts which updated jar file.

...

Service in Groovy

To utilize feature features of on-the-fly compilation and less line fewer lines of code you can implement services for building business logics logic in OFBiz using Groovy DSL.

...

4.) Stop the server and re-start using"./gradlew ofbiz", this time we just need to load the new service definition, no explicit compilation is required as its it's a service implementation in Groovy.

...

To make sure this new service implementation is being executed, you can check this line in the console log that you have put in your code using Debug.log(....). For logging in OFBiz, you must always use Debug class methods in Java classes.

...

2021-07-22 00:29:08,212 |jsse-nio-8443-exec-7 |GroovyBaseScript |I| ==========This is my first Groovy Service implementation in Apache OFBiz. OfbizDemo record created successfully with ofbizDemoId: .....


To get more details around about using Groovy DSL for service and events implementation in Apache OFBiz you can refer document created by Jacopo Cappellato in OFBiz Wiki here.

...

Events in Apache OFBiz are simply methods used to work with HttpServletRequest and HttpServletResponse objects. You don't need to provide definitions of these as you did with services. These are directly called from the controller. Events are also useful when you want to add custom server-side validations to input parameters. For performing db database operations you still call prebuilt services from events.

...

Difference between service and event

Here are some difference differences between services and events,

  • Events are used for validations and conversions using a map processor, while services are used for business logics logic like CRUD operations.
  • Service returns Map.
  • Event returns String.
  • Services are loaded with the server, any changes in definition (not implementation if in MiniLang) needs a reload.
  • We can call service inside the event. But we cannot call event events inside the service.
  • An event is specific local piece functionality normally used in one place for one purpose and called from its location.
  • A service is a piece of functionality which that can be located anywhere on the network, is are most of the time used in several different places, and is called by its 'name'.
  • In case of events, you have access to HttpServletRequest and HttpServletResponse objects and you can read/write whatever you want. In the case of services, you have access only to service parameters.

...

So to customize the UI part of your application first of all to make it easy we will be using Freemarker Templates instead of inbuilt Form Widgets. First of all, we will see how to use Freemarker and Groovy scripts with Apache OFBiz, and then we'll see how to put on custom styling on it by defining your own decorators. Initially, we will be using OFBiz default decorators.

...

Having your UI in Freemarker gives you the freedom to experiment with it, doing CSS tweaks, and make your application the way a user wants. In this section, we will see how we can do that.

We will be doing it by defining a custom decorator for your application view. A decorator in OFBiz is nothing but a screen that you define and reuse afterwards afterward by including in your other screens of the application. You are already doing it with the default decorator (main-decorator –> ApplicationDecorator) which comes with OFBiz. Just observe your screens you have prepared so far, you will find that, you were using this main decorator, please refer below line in OfbizDemoScreens.xml.

...

In the sample given below, we are going to use Bootstrap to style our sample Freemarker screen we developed in the last part of this tutorial. Follow the below-given steps to build your own decorator.

...

<init-param>
    <param-name>allowedPaths</param-name>
    <param-value>/error:/control:/select:/index.html:/index.jsp:/default.html:/default.jsp:/images:/includes/maincss.css:/css:/js</param-value>
</init-param>


6.) Add a new directory named "includes" at location $ OFBIZ_HOME/plugins/ofbizDemo/webapp/ofbizDemo/ and create two new files in this new directory you just added named PreBody.ftl and PostBody.ftl. We will be using(including) these two files in our decorator screen to build a complete HTML page.


PreBody.ftl

...

8.) Update screen named "OfbizDemoCommonDecorator"(which will serve as a custom decorator for your app) as shown below:

...

If you want to order styleSheets or javaScripts JavaScripts with empty square brackets you simply add the file at the end of the layoutSettings.styleSheets or layoutSettings.javaScripts JavaScripts list, with [+0] you add it at front of it.

...

9.) Use this decorator in your Freemarker screen that you created in the last part as:

OfbizDemoScreens.xml

...

10. Now restart OFBiz as you have made entries to allowedPaths in web.xml. As it reloads hit https://localhost:8443/ofbizDemo/control/AddOfbizDemoFtl you should see the page with custom styles that you have used instead of using the default OFBiz theme. It should look like:

...

Here you can now play with it as you want. Try changing the header or having a new one, adding a footer, putting in validations, etc. So this way you can customize the UI layer of OFBiz with Freemarker templates, CSS and JS.

You may want to add your own CSS or JS files, you can include those the same way we did for Bootstrap files.

...

What's next?

If you have followed all the steps and developed practice application applications from this tutorial then this will help you in understanding other implementation implementations in OFBiz. These things are the basic foundation of working in OFBiz. Now you know, how you can start development in OFBiz. Don't leave behind the extra links provided in this tutorial as they will help you a lot in understanding the things which are given there in detail.
Here is another good reading that can be of help is available at FAQ Tips Tricks Cookbook HowTo.
Now the next thing that comes in the way is the business processes which are really needed to be understood well for understanding the OOTB process flow in OFBiz and OOTB data model, so for this, books are available at : at OFBiz Related Books. Understanding well the OFBiz OOTB available data model and business processes will help in building better business solutions top of it.

...