วันพุธที่ 3 พฤศจิกายน พ.ศ. 2553

สร้างเงื่อนไขในการแสดง field ใน form

ในการใช้  form ของ OFBiz หากมีเงื่อนไขในการแสดงผล field ต่างกัน ก็สามารถใช้ form เดียวกันได้ เพียงแต่ต้องกำหนดเงื่อนไขในการแสดงผลของ field นั้นให้ถูกต้อง
ในกรณีที่ต้องการแสดงผล field โดยใช้เงื่อนไขนี้ OFBiz จะเตรียมคำสั่ง use-when ไว้ให้ใช้ ดังนี้
<field name="USER_TITLE" use-when="roleTypeId==SUPPLIER||roleTypeId==CUSTOMER||roleTypeId==INTERNAL_ORGANIZATION" title="${uiLabelMap.CommonTitle}${roleTypeId}"><text size="10" maxlength="30"/></field>
<field name="USER_FIRST_NAME" use-when="roleTypeId!=SUPPLIER||roleTypeId!=CUSTOMER||roleTypeId!=INTERNAL_ORGANIZATION" title="${uiLabelMap.PartyFirstName}" tooltip="${uiLabelMap.CommonRequired}" widget-style="required"><text size="30" maxlength="60"/></field>
<field name="USER_MIDDLE_NAME" use-when="roleTypeId==SUPPLIER||roleTypeId==CUSTOMER||roleTypeId==INTERNAL_ORGANIZATION" title="${uiLabelMap.PartyMiddleInitial}"><text size="4" maxlength="4"/></field>

จากโค้ดสังเกตว่า field ชื่อ USER_TITLE นั้น จะแสดงก็ต่อเมื่อมี roleTypeId = SUPPLIER หรือ มี roleTypeId = CUSTOMER หรือ roleTypeId = INTERNAL_ORGANIZATION หากมี roleTypeId เป็นอื่นๆ ที่ไม่ใช่ SUPPLIER หรือ CUSTOMER หรือ INTERNAL_ORGANIZATION ก็จะไม่แสดง

การใช้คำสั่ง ignore-if-empty ใน drop down list

ในการคิวรี่ข้อมูลออกมาจาก entity บางครั้งมีข้อมูลที่เป็น empty ติดมาด้วย
หากไม่ต้องการนำมาแสดงใน drop down list ให้ใช้คำสั่ง ignore-if-empty ต่อท้ายการ select ข้อมูลออกมา


จะเห็นว่าข้อมูลในส่วนของ group name เป็นค่าว่าง อาจเนื่องมาจากการทดสอบการเพิ่มข้อมูลและการสร้าง services ต่างๆ ในขั้นตอนการพัฒนาที่ไม่ได้ถูกลบออกจากฐานข้อมูล หากต้องการซ่อนค่าดังกล่าว ให้ทำการเพิ่มโค้ดในส่วนของ entity-constrain ดังนี้

<field name="partyIdFrom" title="${uiLabelMap.PartyGroupDepartmentName}" tooltip="${uiLabelMap.CommonRequired}">
            <drop-down allow-empty="true" >
               <entity-options key-field-name="partyId" description="${groupName}" entity-name="PartyRoleDetailAndPartyDetail">
                <entity-constraint name="roleTypeId" operator="equals" value="DEPARTMENT"/>
                <entity-constraint name="groupName" operator="like" value="%" ignore-if-empty="true"/>
                    <entity-order-by field-name="groupName"/>
                </entity-options>
            </drop-down>
</field>

ในส่วนของการ entity-constrain มีการ filter เอาฟิล์ด groupname ที่มีค่าเป็น empty ออก โดยใช้ property ignore-if-empty
ผลลัพธ์ที่ได้จะเป็นดังนี้


ซึ่งจะไม่แสดงค่าว่างตามเงื่อนไขที่กำหนดไว้

วันจันทร์ที่ 1 พฤศจิกายน พ.ศ. 2553

การ convert ชนิดของข้อมูลใน simple method ของ OFBiz

ในบางกรณีที่ชนิดของข้อมูลที่รับมาจาก field ในฟอร์มเป็นคนละชนิดกับที่อยู่ใน entity เราสามารถใช้ simple method แปลงข้อมูลที่รับมาจาก field เพื่อเอาไปทำงานต่อไปได้ ดังนี้

<simple-method method-name="addProductionRunRoutingTask" short-description="Check parameters and add a production run task.">
        <call-map-processor in-map-name="parameters" out-map-name="context">
            <simple-map-processor name="prepareAddRoutingTask">
                <process field="productionRunId">
                    <copy/>
                </process>
                <process field="routingTaskId">
                    <copy/>
                    <not-empty>
                        <fail-property resource="ManufacturingUiLabels" property="ManufacturingRoutingTaskIdMissing"/>
                    </not-empty>
                </process>
                <process field="priority">
                    <copy/>
                    <not-empty>
                        <fail-property resource="ManufacturingUiLabels" property="ManufacturingProductionRunPriorityMissing"/>
                    </not-empty>
                    <convert type="Long">
                        <fail-property resource="ManufacturingUiLabels" property="ManufacturingRoutingSeqIdFormatNotCorrect"/>
                    </convert>
                </process>
                <process field="estimatedStartDate">
                    <copy/>
                    <convert type="Timestamp">
                        <fail-property resource="ManufacturingUiLabels" property="ManufacturingProductionRunStartDateNotCorrect"/>
                    </convert>
                </process>
                <process field="estimatedCompletionDate">
                    <copy/>
                    <convert type="Timestamp">
                        <fail-property resource="ManufacturingUiLabels" property="ManufacturingProductionRunCompletionDateNotCorrect"/>
                    </convert>
                </process>
                <process field="estimatedSetupMillis">
                    <copy/>
                    <convert type="BigDecimal">
                        <fail-property resource="ManufacturingUiLabels" property="ManufacturingProductionRunQuantityNotCorrect"/>
                    </convert>
                </process>
                <process field="estimatedMilliSeconds">
                    <copy/>
                    <convert type="BigDecimal">
                        <fail-property resource="ManufacturingUiLabels" property="ManufacturingProductionRunQuantityNotCorrect"/>
                    </convert>
                </process>
                <process field="workEffortName">
                    <copy/>
                </process>
                <process field="description">
                    <copy/>
                </process>
            </simple-map-processor>
        </call-map-processor>
        <check-errors/>
        <call-service service-name="addProductionRunRoutingTask" in-map-name="context">
        </call-service>
        
        <set field="workEffortAssocMap.workEffortIdFrom" from-field="parameters.workEffortIdFrom"/>
        <set field="workEffortAssocMap.workEffortIdTo" from-field="parameters.routingTaskId"/>
        <set field="workEffortAssocMap.workEffortAssocTypeId" value="WORK_EFF_BREAKDOWN"/>
        <set field="workEffortAssocMap.fromDate" from-field="parameters.estimatedStartDate"/>
        <call-service service-name="createWorkEffortAssoc" in-map-name="workEffortAssocMap"/>
        
        <property-to-field resource="DefaultMessages" property="service.default.message" field="successMessage"/>
    </simple-method>

จะเห็นว่าในส่วนของการรับค่าจากฟอร์ม จะมีการแปลงข้อมูลที่รับจาก field ดังนี้

<process field="priority">
                    <copy/>
                    <not-empty>
                        <fail-property resource="ManufacturingUiLabels" property="ManufacturingProductionRunPriorityMissing"/>
                    </not-empty>
                    <convert type="Long">
                        <fail-property resource="ManufacturingUiLabels" property="ManufacturingRoutingSeqIdFormatNotCorrect"/>
                    </convert>
</process> 

ในส่วนของ convert-type จะเป็นการแปลงข้อมูลจาก field ชื่อ priority ที่ได้รับมาจากฟอร์ม ให้กลายเป็นชนิด Long เพื่อนำไปใช้ในเซอร์วิส addProductionRunRoutingTask ต่อไป