您的位置:首页 > 其它

实列3_1

2015-07-23 10:32 281 查看


目录结构 增加加service 文件 进行数据更新



增加有控制逻辑文件



增加有更新请求



增加方法



增加更新逻辑



最终显示结果



参考:

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:

Create service for the creation of a party with party type "PERSON".
Create a party first in the service which will be creating person.

Writing Services

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.

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>


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.

Create a file by name "services.xml" in servicedef directory.
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.

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.

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>


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:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: