Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Use {code} macro for the code blocks.

...

  • Using the command line, build and start OFBiz (with demo data), use command:

 

Code Block
$ ./ant load-demo start (For linux/mac it will work, for Windows $OFBIZ_HOME/ant.bat come bundled with OFBiz for Windows)

 

Above command will load demo data(Sample Data to run apps) comes with OFBiz in Derby Database, it comes configured with OFBiz.

...

It's very easy to setup a new custom component in OFBiz in hot-deploy directory. Using command line you just need run the following command and answer questions which are being asked for naming your custom component, component resource, webapp and base permission as shown below:

 

Code Block
$ ./ant create-component

Buildfile: /home/ofbiz_dev/Apache_OFBiz/ofbiz-15.12/build.xml

create-component:
    

    [input] Component name: (e.g. mycomponent) [Mandatory]
ofbizdemo
    

ofbizdemo
    [input] Component resource name: (e.g. MyComponent) [Mandatory]
OfbizDemo
    

OfbizDemo
    [input] Webapp name: (e.g. mycomponent) [Mandatory]
ofbizdemo
    

ofbizdemo
    [input] Base permission: (e.g. MYCOMPONENT) [Mandatory]
OFBIZDEMO
     

OFBIZDEMO
     [echo] The following hot-deploy component will be created:
     [echo]               Name: ofbizdemo
     [echo]               Resource Name: OfbizDemo
     [echo]               Webapp Name: ofbizdemo
     [echo]               Base permission: OFBIZDEMO
     [echo]               Folder:

     [echo]               Name: ofbizdemo
     [echo]               Resource Name: OfbizDemo
     [echo]               Webapp Name: ofbizdemo
     [echo]               Base permission: OFBIZDEMO
     [echo]               Folder: /home/ofbiz_dev/Apache_OFBiz/ofbiz-15.12/hot-deploy/ofbizdemo
     

     [echo]
             
         
    [input] Confirm:
 
  (Y, [N], y, n)

y

 

Component Directory Structure In IDE 

Running your first application

...

  1. Simply open $OFBIZ_HOME/hot-deploy/ofbizdemo/widget/OfbizDemoScreens.xml file from ofbizdemo component(you just created)

     

    Code Block
    languagexml
    <?
    xmlversion
    xmlversion="1.0"encoding="UTF-8"?>
    <screensxmlns
    
    <screensxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            
    
            xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/widget-screen.xsd">
        <screenname
    
        <screenname="main">
            <section>
                <actions>
                    <setfield
    
            <section>
                <actions>
                    <setfield="headerItem"value="main"/
    ><
    ><!-- this highlights the selected menu-item with name "main" -->
                </actions>
                <widgets>
                    <decorator-screenname
    
                </actions>
                <widgets>
                    <decorator-screenname="OfbizDemoCommonDecorator"location="${parameters.mainDecoratorLocation}">
                        <decorator-sectionname="body">
                            <labeltext="Hello World!! :)"/>
                        </decorator-section>
                    </decorator-screen>
                </widgets>
            </section>
        </screen>
    </screens>
     
    
                        <decorator-sectionname="body">
                            <labeltext="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($ ./ant load-demo start). It's required as you have created a new component with some security data for you component (Setup by default in your component data directory as OfbizDemoSecurityGroupDemoData.xml) and as you will restart it, ofbizdemo component will also be loaded.
  3. As OFBiz restarted direct your browser to your application here http://localhost:8080/ofbizdemo
  4. You will be asked to login. Login with user: admin password: ofbiz.
  5. As you login you will see ofbizdemo application up with the hello world message you have put in screen as shown in below given image.
    That's it, congratulations your first component is setup and running.

...

To create custom Entities/Tables in database, you simply need to provide entity definition in  $OFBIZ_HOME/hot-deploy/ofbizdemo/entitydef/entitymodel.xml file of your ofbizdemo application. This file structure is already setup when you used ant target to setup 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 ofbizdemo application.

 

Code Block
languagexml
<?xml version="1.0" encoding="UTF-8"?>
 
<entitymodel

 
<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

 
    <title>Entity of an Open For Business Project Component</title>
    <description>None</description>
    <version>1.0</version>
 
    <entity entity-name="OfbizDemoType" package-name="org.ofbiz.ofbizdemo" title="OfbizDemo Type Entity">
        <field

        <field name="ofbizDemoTypeId" type="id-ne"
><description>primary
><description>primary sequenced ID<
/description></field>        <field
/description></field>
        <field name="description" type="description"></
field>        <prim
field>
        <prim-key field="ofbizDemoTypeId"/>
    </entity>
 
    <entity entity

    </entity>
 
    <entity entity-name="OfbizDemo" package-name="org.ofbiz.ofbizdemo" title="OfbizDemo Entity">
        <field

        <field name="ofbizDemoId" type="id-ne"
><description>primary
><description>primary sequenced ID</
description></field>        <field
description></field>
        <field name="ofbizDemoTypeId" type="id-ne"></
field>        <field
field>
        <field name="firstName" type="name"></
field>        <field
field>
        <field name="lastName" type="name"></
field>        <field
field>
        <field name="comments" type="comment"></
field>        <prim
field>
        <prim-key field="ofbizDemoId"/>
        <relation

        <relation type="one" fk-name="ODEM_OD_TYPE_ID" rel-entity-name="OfbizDemoType">
            <key

            <key-map field-name="ofbizDemoTypeId"/>
        </relation>
    </entity>
 
</entitymodel>

        </relation>
    </entity>
 
</entitymodel>

 

Now have a look at $OFBIZ_HOME/hot-deploy/ofbizdemo/ofbiz-component.xml file. You already have resource entry made in it for loading these entities from their definitions to database when component loads. As shown below:

 

Code Block
languagexml
<entity-resource type="model" reader-name="main" loader="main" location="entitydef/entitymodel.xml"/>

 

 

To check simply re-start OFBiz (Ctrl+C followed by "./ant start") and direct your browser to Entity Data Maintenance Tool here: https://localhost:8443/webtools/control/entitymaint and search for entities OfbizDemoType and OfbizDemo. You will see it as shown in below given image.

...

Preparing data for entity

As you have setup your custom entities, now is the time to prepare some sample xmldata for it. You can do it in data xmlfiles already setup under data directory of your component as  $OFBIZ_HOME/hot-deploy/ofbizdemo/data/OfbizDemoTypeData.xml and  $OFBIZ_HOME/hot-deploy/ofbizdemo/data/OfbizDemoDemoData.xml. Set it up as shown below:

OfbizDemoTypeData.xml

 

<

Code Block
languagexml
titleOfbizDemoTypeData.xml
 <?xml version="1.0" encoding="UTF-8"?>
<entity

<entity-engine-
xml>    <OfbizDemoType
xml>
    <OfbizDemoType ofbizDemoTypeId="INTERNAL" description="Internal Demo - Office"/>
    <OfbizDemoType

    <OfbizDemoType ofbizDemoTypeId="EXTERNAL" description="External Demo - On Site"/>

</entity-engine-xml>


OfbizDemoDemoData.xml

>

 


Code Block
languagexml
titleOfbizDemoDemoData.xml

 

 <
<?xml version="1.0" encoding="UTF-8"?
><entity
><entity-engine-
xml>    <OfbizDemo
xml>
    <OfbizDemo ofbizDemoId="SAMPLE_DEMO_1" ofbizDemoTypeId="INTERNAL" firstName="Sample First 1" lastName="Sample Last 1" comments="This is test comment for first record."/>
    <OfbizDemo

    <OfbizDemo ofbizDemoId="SAMPLE_DEMO_2" ofbizDemoTypeId="INTERNAL" firstName="Sample First 2" lastName="Sample last 2" comments="This is test comment for second record."/>
    <OfbizDemo

    <OfbizDemo ofbizDemoId="SAMPLE_DEMO_3" ofbizDemoTypeId="EXTERNAL" firstName="Sample First 3" lastName="Sample last 3" comments="This is test comment for third record."/>
    <OfbizDemo

    <OfbizDemo ofbizDemoId="SAMPLE_DEMO_4" ofbizDemoTypeId="EXTERNAL" firstName="Sample First 4" lastName="Sample last 4" comments="This is test comment for fourth record."/>

</entity-engine-
xml>
xml>


 

Now again have a look at $OFBIZ_HOME/hot-deploy/ofbizdemo/ofbiz-component.xml file. You already have resource entry made in it for loading data prepared in these files as:

Entry to be done in ofbiz-component.xml

 

...

Code Block
languagexml
titleEntry to be done in ofbiz-component.xml
<entity-resource type="data" reader-name="seed" loader="main" location="data/OfbizDemoTypeData.xml"/>

...


<entity-resource type="data" reader-name="demo" loader="main" location="data/OfbizDemoDemoData.xml"/>

Loading data in entity

At this moment to load this sample data into entities/tables defined you can either run "ant load-demo" on console or can directly go here in webtools to load entity xml https://localhost:8443/webtools/control/EntityImport.

Simply put your xml data in " Complete XML document (root tag: entity-engine-xml):" text area and hit "Import Text", as shown in below given image

XML Data Import from WebtoolsImage Modified

As you will hit Import Text, it will load data and will show the result as shown below:

XML Data Load ResultImage Modified

After completing the data load process again visit Entity Data Maintenance(https://localhost:8443/webtools/control/entitymaint) and check your entities, you will find this data here that you just loaded.


That's it, you have successfully imported the data in the database tables, super easy, right!

 

Form and Services

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

Create a Service

Before preparing form, let's write a service to create records in database for OfbizDemo entity in service definition xml file ($OFBIZ_HOME/hot-deploy/ofbizdemo/servicedef/services.xml)

services.xml

 

<?xml version="1.0" encoding="UTF-8"?>
        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 $OFBIZ_HOME/hot-deploy/ofbizdemo/ofbiz-component.xml file. You already have resource entry made in it for loading services defined in this file as:

 

<!-- service resources: model(s), eca(s) and group definitions -->
<service-resource type="model" loader="main" location="servicedef/services.xml"/>

 

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

 

Running service via Web Tools: This a smart utility provided by framework to run your service.
On submission of the form above, you will presented a form to enter IN parameters of the service.

 

Create the add Form

Let's create our first form for this service and for that let's edit the existing file at location $OFBIZ_HOME/hot-deploy/ofbizdemo/widget/OfbizDemoForms.xml and add Create Form for OfbizDemo as shown below:

OfbizDemoForms.xml

 

<?xml version="1.0" encoding="UTF-8"?>
        xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/widget-form.xsd">
 
    <form name="AddOfbizDemo" type="single" target="createOfbizDemo">
        <!--We have this utility in OFBiz to render form based on service definition. Service attributes will automatically lookedup and will be shown on form -->
        <auto-fields-service service-name="createOfbizDemo"/>
        <field name="ofbizDemoTypeId" title="${uiLabelMap.CommonType}">
            <drop-down allow-empty="false" current-description="">
                <!---We have made this drop down options dynamic(Values from db) using this -->
                <entity-options description="${description}" key-field-name="ofbizDemoTypeId" entity-name="OfbizDemoType">
                    <entity-order-by field-name="description"/>
                </entity-options>
            </drop-down>
        </field>
        <field name="submitButton" title="${uiLabelMap.CommonAdd}"><submit button-type="button"/></field>
    </form>
</forms>

 

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 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

Adding Form Location to the Main Screen

 

<?xml version="1.0" encoding="UTF-8"?>
        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="main-decorator" location="${parameters.mainDecoratorLocation}">
                    <decorator-section name="body">
                        <screenlet title="Add Ofbiz Demo">
                            <include-form name="AddOfbizDemo" location="component://ofbizdemo/widget/OfbizDemoForms.xml"/>
                        </screenlet>
                    </decorator-section>
                </decorator-screen>
            </widgets>
        </section>
    </screen>
</screens>

 

Controller Entry for Form

Before you go to the form and start creating OfbizDemo records from add form, you will need to make an entry in $OFBIZ_HOME/hot-deploy/ofbizdemo/webapp/ofbizdemo/WEB-INF/controller.xml file for the target service which will called when form is submitted. You can do it as shown below under Request Mappings in your ofbizdemo apps controller file:

 

<request-map uri="createOfbizDemo">
    <security https="true" auth="true"/>
    <event type="service" invoke="createOfbizDemo"/>
    <response name="success" type="view" value="main"/>
</request-map>

 


Everything set, let's have a look into to our recently create form http://localhost:8080/ofbizdemo

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


Create a Find Form

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

1.) Add the forms (FindOfbizDemo and ListOfbizDemo) in OfbizDemoForms.xml

OfbizDemoForms.xml

 

<form name="FindOfbizDemo" type="single" target="FindOfbizDemo" default-entity-name="OfbizDemo">
    <field name="noConditionFind"><hidden value="Y"/><!-- if this isn't there then with all fields empty no query will be done --></field>
    <field name="ofbizDemoId" title="${uiLabelMap.OfbizDemoId}"><text-find/></field>
    <field name="firstName" title="${uiLabelMap.OfbizDemoFirstName}"><text-find/></field>
    <field name="lastName" title="${uiLabelMap.OfbizDemoLastName}"><text-find/></field>
    <field name="ofbizDemoTypeId" title="${uiLabelMap.OfbizDemoType}">
        <drop-down allow-empty="true" current-description="">
            <entity-options description="${description}" key-field-name="ofbizDemoTypeId" entity-name="OfbizDemoType">
                <entity-order-by field-name="description"/>
            </entity-options>
        </drop-down>
    </field>
    <field name="searchButton" title="${uiLabelMap.CommonFind}" widget-style="smallSubmit"><submit button-type="button" image-location="/images/icons/magnifier.png"/></field>
</form>
 
<form name="ListOfbizDemo" type="list" list-name="listIt" paginate-target="FindOfbizDemo" default-entity-name="OfbizDemo" separate-columns="true"
    odd-row-style="alternate-row" header-row-style="header-row-2" default-table-style="basic-table hover-bar">
    <actions>
       <!--Preparing search results for user query by using OFBiz stock service to perform find operations on a single entity or view entity -->
       <service service-name="performFind" result-map="result" result-map-list="listIt">
           <field-map field-name="inputFields" from-field="ofbizDemoCtx"/>
           <field-map field-name="entityName" value="OfbizDemo"/>
           <field-map field-name="orderBy" from-field="parameters.sortField"/>
           <field-map field-name="viewIndex" from-field="viewIndex"/>
           <field-map field-name="viewSize" from-field="viewSize"/>
        </service>
    </actions>
    <field name="ofbizDemoId" title="${uiLabelMap.OfbizDemoId}"><display/></field>
    <field name="ofbizDemoTypeId" title="${uiLabelMap.OfbizDemoType}"><display-entity entity-name="OfbizDemoType"/></field>
    <field name="firstName" title="${uiLabelMap.OfbizDemoFirstName}" sort-field="true"><display/></field>
    <field name="lastName" title="${uiLabelMap.OfbizDemoLastName}" sort-field="true"><display/></field>
    <field name="comments" title="${uiLabelMap.OfbizDemoComment}"><display/></field>
</form>

 

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

 

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 search on one entity or one view entity.

 

 

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

 

<!-- Find and list all ofbizdemos in a tabular format -->
<screen name="FindOfbizDemo">
    <section>
        <actions>
            <set field="headerItem" value="findOfbizDemo"/>
            <set field="titleProperty" value="PageTitleFindOfbizDemo"/>
            <set field="ofbizDemoCtx" from-field="parameters"/>
        </actions>
        <widgets>
            <decorator-screen name="main-decorator" location="${parameters.mainDecoratorLocation}">
                <decorator-section name="body">
                    <section>
                        <condition>
                            <if-has-permission permission="OFBIZDEMO" action="_VIEW"/>
                        </condition>
                        <widgets>
                            <decorator-screen name="FindScreenDecorator" location="component://common/widget/CommonScreens.xml">
                                <decorator-section name="search-options">
                                    <include-form name="FindOfbizDemo" location="component://ofbizdemo/widget/OfbizDemoForms.xml"/>
                                </decorator-section>
                                <decorator-section name="search-results">
                                    <include-form name="ListOfbizDemo" location="component://ofbizdemo/widget/OfbizDemoForms.xml"/>
                                </decorator-section>
                            </decorator-screen>
                        </widgets>
                        <fail-widgets>
                            <label style="h3">${uiLabelMap.OfbizDemoViewPermissionError}</label>
                        </fail-widgets>
                    </section>
                </decorator-section>
            </decorator-screen>
        </widgets>
    </section>
</screen>

 

3.) Add request mapping for accessing this new Find Ofbiz Demo page in controller.xml

 

<!-- Request Mapping -->
<request-map uri="FindOfbizDemo"><security https="true" auth="true"/><response name="success" type="view" value="FindOfbizDemo"/></request-map>
  
<!-- View Mapping -->
<view-map name="FindOfbizDemo" type="screen" page="component://ofbizdemo/widget/OfbizDemoScreens.xml#FindOfbizDemo"/>

 

4.) Now, let's add a new menu for showing find option.
Creating a menu is really simple in OFBiz, all the menus are defined is *menus.xml.
When we create a component from ant target, we get a file named OfbizDemoMenus.xml

Make the following entry in the OfbizDemoMenus.xml file.

OfbizDemoMenus.xml

 

<?xml version="1.0" encoding="UTF-8"?>
    <menu name="MainAppBar" title="${uiLabelMap.OfbizDemoApplication}" extends="CommonAppBarMenu" extends-resource="component://common/widget/CommonMenus.xml">
        <menu-item name="main" title="${uiLabelMap.CommonMain}"><link target="main"/></menu-item>
        <menu-item name="findOfbizDemo" title="${uiLabelMap.OfbizDemoFind}"><link target="FindOfbizDemo"/></menu-item>
    </menu>
</menus>

Use of UI Labels

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

Here is the example of UI Labels (while creating component <component-name>UiLabels.xml is created by default, in our case it is OfbizDemoUiLabels.xml)

OfbizDemoUiLabels.xml

 

<property key="OfbizDemoFind">
    <value xml:lang="en">Find</value>
</property>
<property key="OfbizDemoFirstName">
    <value xml:lang="en">First Name</value>
</property>
<property key="OfbizDemoId">
    <value xml:lang="en">OFBiz Demo Id</value>
</property>
<property key="OfbizDemoLastName">
   <value xml:lang="en">Last Name</value>
</property>

 

Now simply restart the server, under ofbizdemo application (https://localhost:8443/ofbizdemo/control/main) you will see the Find menu option.

...