实列3_1

目录结构 增加加service 文件 进行数据更新
增加有控制逻辑文件 
增加有更新请求

增加方法
增加更新逻辑
最终显示结果
参考:
    1. relationship.An really important reading at this moment is at : OFBiz Security

Part 3

Writing CRUD Operations

Create, Update and Delete operations for an entity will be done by services which we will be writing in minilang. At first approach we will write our own services for performing these operations for making a better understanding with it. Onwards we will be doing this by calling already implemented services. For doing so we will take the entities from Party model which are:
– Party
– Person
A person is a party so for the creation of a person record, first a party record needs to be created with partyTypeId="PERSON". So there can be two ways to do that:

  1. Create service for the creation of a party with party type "PERSON".
  2. Create a party first in the service which will be creating person.
Writing Services
  1. Create directory by name "servicedef" in component directory "practice". This directory will contain all the service definition files e.g. services.xml, secas.xml.
    Note : * If it is a service which is written in Java then it will be placed in "src" directory and if it is a service which is written in minilang then it will be placed in script directory. e.g. for java applications/party/src/org/ofbiz/party/party/PartyServices.java and for minilang applications/party/script/org/ofbiz/party/party/PartyInvitationServices.xml. Respective class path and file path will be mentioned in the service definition.
  2. In controller you have to create an entry for the request for the execution of a service and set the response like.

    ?
    <request-map uri= "createPracticePerson" >
         <security https= "true"  auth= "true" />
         <event type= "service"  invoke= "createPracticePerson" />
         <response name= "success"  type= "view"  value= "PersonForm" />
    </request-map>
  3. Now all the services which you have written needs to be loaded when server starts so you need to do an entry for service definition in ofbiz-component.xml file which will be like:

    ?
    <service-resource type= "model"  loader= "main"  location= "servicedef/services.xml" />

    So whenever you make any change in any service definition then you must restart the server to have changes in effect.

Writing CRUD Operations for Party Entity

First we will be writing services for Party then while writing services for creating Person we will be calling the service for party.

  1. Create a file by name "services.xml" in servicedef directory.
  2. Define services for CRUD operations for Party entity. Name of services will be createPracticeParty, updatePracticeParty, deletePracticeParty and specify the correct location to the file where these services will be implemented like /framework/example/script/org/ofbiz/example/example/ExampleServices.xml.
  3. Create directory structure and PracticeServices.xml file in your component directory for giving the implementation of these services. (For implementation take reference from services.xml and ExampleServices.xml files of Example component)

    Important

    Icon
    • Do not use the <override> tag as it is introduced later in the tutorial.
      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.
    • At this place you must read http://markmail.org/message/dj4wvtm4u2nxoz3r. This feature has been 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:  

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

    engine="entity-auto" invoke="create" play the role for creating the records for the default-entity "Example."
    Here for practice you may go by following further steps those steps will help you in understanding the concept then onwards you can practice the pattern given above  in your code as its the best practice for these kind of simple operations in OFBiz.

Writing CRUD Operations for Person Entity

- Here for the creation of record for person entity we will need to have the partyId for that so we will first call the service createPracticeParty then after getting the partyId we will create the record for person.
- Here we will be adding one add form in the bottom of the list form which we have for the person entity. This form will be calling the services for creating a record for person.

  1. Create the add form for the creation of person and add this in the same screen for person form.

    ?
    <form name= "CreatePerson"  type= "single"  target= "createPracticePerson" >
          <auto-fields-service service-name= "createPracticePerson" />
          <field name= "submitButton"  title= "Create"  widget-style= "smallSubmit" ><submit button-type= "button" /></field>
    </form>
  2. Write CRUD operations for person entity.this is a code for createPracticePerson in services.xml

    ?
    <service name= "createPracticePerson"  default -entity-name= "Person"  engine= "simple"
              location= "component://practice/script/org/hotwax/practice/PracticeServices.xml"  invoke= "createPracticePerson"  auth= "true" >
         <description>Create a Person</description>
         <auto-attributes include= "pk"  mode= "OUT"  optional= "false" />
         <attribute name= "salutation"  mode= "IN"  type= "String"  optional= "true" />
         <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>  


    similar for Update and Delete 
    # Now convert the List form with editable field (Ref. ListExampleItems from ExampleForms.xml) and add Update and delete option with it and also in the same screen there is add form also. As shown bellow:

    ?
    <form name= "ListPersons"  type= "list"  list-name= "persons"  list-entry-name= "person"  target= "updatePracticePerson"  paginate-target= "personForm" >
             <auto-fields-service service-name= "updatePracticePerson"  default -field-type= "edit"  map-name= "person" />
             <field name= "partyId" ><hidden/></field>
             <field name= "submitButton"  title= "Update"  widget-style= "smallSubmit" ><submit button-type= "button" /></field>
             <field name= "deletePracticePerson"  title= "Delete Person"  widget-style= "buttontext" >
             <hyperlink target= "deletePracticePerson?partyId=${person.partyId}"  description= "Delete" />
           </field>
    </form>


    # Create controller entries for these services which are going to be called by this form.
    Now run the application and see the output screen as bellow:
    Output Screen:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值