Versions Compared

Key

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

...

From this place if you want to run these services then you can run them by webtools--> Run Service . By this place you can test your services.

 NoteNote:* At this place you must read http://www.nabble.com/The-fancy-new-entity-auto-service-execution-engine-td18674040.html. This feature has been recently added against the traditional approach of writing CRUD operations for an entity.

           This new feature enables you to just define the services by mentioning the operation you want to perform.Basically just set the engine attribute to "entity-auto" and the   invoke attribute to "create", "update", or "delete".
like you can take a look in the following code from services.xml of example component:  

Code Block
<service name="createExample" default-entity-name="Example" engine="entity-auto" invoke="create" auth="true">
    <description>Create a Example</description>
    <permission-service service-name="exampleGenericPermission" main-action="CREATE"/>
    <auto-attributes include="pk" mode="OUT" optional="false"/>
    <auto-attributes include="nonpk" mode="IN" optional="true"/>
    <override name="exampleTypeId" optional="false"/>
    <override name="statusId" optional="false"/>
    <override name="exampleName" optional="false"/>
</service>

...

Here the path is the path of the file where the event is written. it will be practice/script/org/hotwax/practice.

 and and for java event controller entry will be like:

Code Block

<request-map uri="createPracticePersonJavaEvent">

...


    <security https="true" auth="true"/>

...


    <event type="java" path="org.hotwax.practice.PracticeEvents" invoke="createPracticePersonJavaEvent"/>

...


    <response name="success" type="view" value="{color}{color:#0033ff}CreatePracPersonJavaEvent{color}{color:#0033ff}"/>

...


    <response name="error" type="view" value="{color}{color:#0033ff}CreatePracPersonJavaEvent{color}{color:#0033ff}"/>

...


</request-map>

Here the path is the classpath in which this event is defined. 

...

7.a : Process fields coming from the form like:     

Code Block

<call-map-processor in-map-name="parameters" out-map-name="createPersonContext">

...


    <simple-map-processor name="createPersonMap">

...


        <process field="firstName">

...


            <copy/>

...


            <not-empty>
                <fail-property property="PracticeFirstNameMissingError" resource="PracticeUiLabels

...

"/>
            </not-empty>&nbsp;&nbsp;&nbsp;
        </process>
    </simple-map-processor>

...


</call-map-processor>

...


<check-errors/>

7.b : Create some Ui labels for showing them in fail-property like PracticeFirstNameMissingError.

7.c : Now call service createPracticePerson service by passing out map which is obtained after processing fields as a in map to the service.

Java Event: 

Here the java event which you will be writing will be fairly simple. For reference you can check any of the *Events.java file.

Step - 1 : The contents can will be :

Code Block
public static String createPracticePersonJavaEvent(HttpServletRequest request, HttpServletResponse response){
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
}

Step - 2 : Now you have to process the fields comming from the form like :            

Code Block
String salutation = (String) request.getParameter("salutation");
String firstName = (String) request.getParameter("firstName");

Step - 3 : Now prepare a map for the values which you have to pass to the service which you will call "createPracticePerson" . Like :            

Code Block
Map createPersonCtx = UtilMisc.toMap("salutation", salutation, "firstName", firstName);

Step - 4 : Then at the end just call the service "createPracticePerson" like :             

Code Block
try{
    Map person = dispatcher.runSync("createPracticePerson", createPersonCtx);
 }catch (GenericServiceException e){
     Debug.logError(e.toString(), module);          return "error";
 }
return "success";

After writting event in Java don't forget to compile it by running "ant" command. At this moment you will need to add build.xml file to your component directory i.e. at hot-deploy/practice/ For the content of build.xml file you can refer "example" component.
Here in build.xml file ensure one thing you are having follwing entry:

Code Block
<target name="classpath">
    <path id="local.class.path">
        <fileset dir="../../framework/base/lib/j2eespecs" includes="*.jar"/>
    </path>
</target>

This will needed for the classes like

Code Block
import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;

So create a file by name build.xml and then compile it. It will create a build directory in your component directory which will be containing all *.jar and class files after compilation. For the content of build.xml file you can refere example component. 
For running the simple event don't forget to make an entry for <classpath type="dir" location="script"/> in ofbiz-component.xml file.

For running the java event make an entry <classpath type="jar" location="build/lib/*"/> in ofbiz-component.xml file.

ECA(Event Condition Action): 

...

Step - 2 :  Now you have to create a file by name "secas.xml" in "servicedef" directory. Seca definition will come here. (Take reference from secas.xml of "party" component). This will be

Code Block

<eca service="createPracticePerson" event="commit">

...


    <condition field-name="partyId" operator="is-not-empty"/>

...


    <action service="createPartyRoleVisitor" mode="sync"/>

...


</eca>

Step - 3 : Do an entry in ofbiz-component.xml file for this seca definition to to be loaded. It will be :

...

Step - 2 :  Now you have to create a file by name "eecas.xml" in "entitydef" directory, which will be in your component directory "practice". Eeca definition will come here. (Take reference from eecas.xml of "accounting" component). This will be :   

Code Block

<\!-\- To create party role whenever a party is created \-->

...


<eca entity="Party" operation="create" event="return">

...


    <condition field-name="partyId" operator="is-not-empty"/>

...


    <action service="createPartyRoleCustomer" mode="sync"/>

...


</eca>

Step - 3 : Do an entry in ofbiz-component.xml file for this seca definition to to be loaded. It will be :

...

 Create a group service by name "partyGroup" like :

Code Block

<\!-\- Group service \-->

...


<service name="partyGroup" engine="group" auth="true">

...


    <description>Creates a party, person and party role Client</description>

...


    <group>
        <invoke name="createPracticePerson" result-to-context="true"/>

...


        <invoke name="createPartyRoleClient"/>

...


    </group>

...


</service>

Don't forget to restart the server before testing it. 

Interface:

The interface service engine has been implemented to help with defining services which share a number of the same parameters. An interface service cannot be invoked, but rather is a defined service which other services inherit from. Each interface service will be defined using the interface engine.

...

Step - 2 : Create new screen, form and service for creating a person. Here service will implement the interface. (For creating interface take reference from services_fixedasset.xml of accounting component) it will be like :

Code Block
<!-- Peson Interface -->
<service name="createPersonInterface" engine="interface" location="" invoke="">
    <attribute name="firstName" mode="IN" type="String" optional="false"/>
    <attribute name="middleName" mode="IN" type="String" optional="true"/>
    <attribute name="lastName" mode="IN" type="String" optional="false"/>
    <attribute name="suffix" mode="IN" type="String" optional="true"/>
</service>

<service name="createPracticePersonInterfaceService" engine="simple"
        location="org/hotwax/practice/PracticeServices.xml" invoke="createPracticePersonInterfaceService" auth="false">
    <description>Creates a new Person</description>
    <implements service="createPersonInterface"/>
    <attribute name="partyId" mode="OUT" type="String" optional="false"/>
    <override name="suffix" optional="false"/>
</service>

Here we are implementing an interface and overriding the behaviour of the attribute "suffix", which will have effect when this service will be in action.
Implementation of service createPracticePersonInterfaceService will be the same as createPracticePerson.

Don't forget to restart the server after this implementation.

...

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

           That implies that when ever you do a change you need to restart the server to have those changes in effect.
At this place an important reading is at http://docs.ofbiz.org/display/OFBTECH/General+Entity+Overview.

You will rarely find this way to define new entity because you are already having entities there in OFBiz already defined which will be useful for the conduction of your business process. Though you may feel at some place to add more fields to an existing entity so how can you do that? The next  step will show you the way how you can extend an entity for your customized needs.

...

Step - 1 :  For extending an entity use

Code Block
<extend-entity entity-name="">
    <field name="" type=""/>
</extend-entity>

This is the simplest form it can be more complex. This will add up one more field to the entity you already have. Now it depends which field you want for your custom needs. Here you can also defined relation of this field with other entities you want. But before doing this you should search extesively may be you will be adding a field for a purpose and there is already a field which will serve the purpose, so be concisous about this. Also go for a extensive study of data model then do this.
 
For entity engine configuration dont forget to read : Entity Engine Configuration Guide
 

Conclusion:

If you have followed all the steps and developed practice application from this application then this will really help you in understanding other implementations in OFBiz. These things are basic foundation of working in OFBiz. Now you know that how you can start the development in OFBiz. Don't leave the extra links provided in this tutorial as they will really help you a lot in understanding the things which are there. Parallaly with the development of this application you can watch OFBiz videos which are available at Framework Introduction Videos.

...