วันพุธที่ 9 มิถุนายน พ.ศ. 2553

การ สร้าง service เพื่อ edit ข้อมูลในฐานข้อมูลโดยใช้ simple method

ในบทความนี้จะแสดงการสร้าง service ที่ใช้แก้ไขข้อมูลใน entity โดยใช้ simple method ในไฟล์ *Services.xml มีวิธีทำดังนี้

1. สร้างฟอร์มชื่อ EditBlog ที่ *Forms.xml
<form name="EditBlog" type="single" target="editBlog" title="" 
            default-map-name="resultMap" default-entity-name="NewBlog">
        <auto-fields-entity entity-name="NewBlog" default-field-type="edit"/>
        <field name="statusId">
            <drop-down allow-empty="false">
                <entity-options description="${description}" entity-name="StatusItem" key-field-name="statusId">
                <entity-constraint name="statusTypeId" value="CONTENT_STATUS"/>
                </entity-options>
            </drop-down>
        </field>
        <field name="editButton" title="Save" ><submit button-type="button"/></field>
</form>

2. include form ชื่อ EditBlog เข้าไปในไฟล์ *Screens.xml
<screen name="EditBlog">
        <section>
            <actions>
                <set field="newBlogId" from-field="parameters.newBlogId" global="true"/>
                <entity-one value-field="resultMap" entity-name="NewBlog" auto-field-map="true"></entity-one>
            </actions>
            <widgets>
                <decorator-screen name="CommonDecorator" location="component://myblog/widget/CommonScreens.xml">
                    <decorator-section name="body">
                        <container id="content">
                             <container style="post">
                                   <container style="entry">
                                        <include-form location="component://myblog/widget/MyBlogForms.xml" name="EditBlog"/>
                                   </container>
                             </container>
                        </container>
                    </decorator-section>
                </decorator-screen>
            </widgets>
        </section>
    </screen>

3. กลับไปดูที่ไฟล์ *Forms.xml ฟอร์มชื่อ EditBlog มี target="editBlog"
<form name="EditBlog" type="single" target="editBlog" title="" 
            default-map-name="resultMap" default-entity-name="NewBlog">
        <auto-fields-entity entity-name="NewBlog" default-field-type="edit"/>
        <field name="statusId">
            <drop-down allow-empty="false">
                <entity-options description="${description}" entity-name="StatusItem" key-field-name="statusId">
                <entity-constraint name="statusTypeId" value="CONTENT_STATUS"/>
                </entity-options>
            </drop-down>
        </field>
        <field name="editButton" title="Save" ><submit button-type="button"/></field>
</form

4. ใน controller.xml ทำการสร้าง request-map และสั่งให้มีการการ invoke simple method ชื่อ editBlog ที่ services.xml
<request-map uri="editBlog">
        <security https="true" auth="false"/>
        <event type="service" invoke="editBlog"/>
        <response name="success" type="request-redirect-noparam" value="home"></response>
</request-map>

5. ใน services.xml มีการ invoke editBlog ในไฟล์ *Services.xml
<service name="editBlog" engine="simple" auth="false"
            location="component://myblog/script/com/org/toon/myblog/MyBlogServices.xml" invoke="editBlog">
            <attribute name="newBlogId" mode="INOUT" type="String" optional="false"></attribute>
            <attribute name="newBlogTitle" mode="IN" type="String" optional="false"></attribute>
            <attribute name="newBlogBody" mode="IN" type="String" optional="false"></attribute>
            <attribute name="statusId" mode="IN" type="String" optional="true"></attribute>
</service>

6. สร้าง service ชื่อ editBlog ในไฟล์ MyBlogServices.xml
<simple-method method-name="editBlog" short-description="edit a Blog">
        <entity-one value-field="newData" entity-name="NewBlog" auto-field-map="true"></entity-one>
        <set-nonpk-fields value-field="newData" map="parameters"/>
        <store-value value-field="newData"/>
</simple-metho

7. จบส่วนในการสร้างและเรียกใช้ Service ต่อไปจะทำการ controller ให้เรียก view ที่มีหน้า form ของการ edit new blog มาแสดงผล

8.  เพิ่ม controller ชื่อ EditBlog ใน controller.xml ให้เรียก view ชื่อ CreateBlog มาแสดงผล เมื่อมีการเรียกเข้ามาที่ controller นี้
<request-map uri="EditBlog"><security https="true" auth="false"/>
        <response name="success" type="view" value="EditBlog"/>
    </request-map>

9. สร้าง view ชื่อ EditBlog เรียกฟอร์มการสร้างมาแสดงผล
<view-map name="EditBlog" type="screen" page="component://myblog/widget/MyBlogScreens.xml#EditBlog"/>

10. ทำการ ./startofbiz.sh ใหม่อีกครั้ง แล้วทดลองเแก้ไขข้อมูลลงใน entity

ไม่มีความคิดเห็น:

แสดงความคิดเห็น