Versions Compared

Key

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

Table of Contents

 

 

In this tutorial, we will follow the ในบทช่วยสอนนี้ เราจะทำตาม guideline for developing a plugin to develop our  เพื่อพัฒนาปลั๊กอิน Mood Rating Form Field plugin. Please also refer to the very first tutorial ของเรา โปรดอ้างอิงถึงบทช่วยสอนแรกสุด วิธีการพัฒนา Bean Shell Hash Variable for more details steps. เพื่อลายระเอียดสำหรับขั้นตอนต่อไป

1.

...

ปัญหาคืออะไร?

เราต้องการให้มีฟิลด์การจัดเรตพร้อมอีโมจิซึ่งสามารถนำไปใช้กับรูปแบบอื่นได้

2. วิธีแก้ปัญหา?

เราจะพัฒนา Form Field Element Plugin เพื่อแสดงฟิลด์อีโมจิ

3. อินพุตที่จำเป็นสำหรับปลั๊กอินของคุณคืออะไร?

ในการพัฒนาปลั๊กอิน Mood Rating Form Field เราจะต้องให้อินพุตมาตรฐานบางอย่างสำหรับ

We would like to have a rating field with some smiley images which can be reuse for other form.

2. How to solve the problem?

We will develop a Form Field Element Plugin to render our mood rating field. 

3. What is the input needed for your plugin?

To develop a Mood Rating Form Field plugin, we will need to provide some standard inputs for a Form Field element.

  1. Field Id (รหัสฟิลด์)
  2. Field Label (ป้ายฟิลด์)
  3. Validator (เครื่องมือตรวจสอบ)
  4. Readonly (อ่านอย่างเดียว)
  5. Workflow Variable (ตัวแปรเวิร์กโฟลว์)

4.

...

ผลลัพธ์และผลลัพธ์ที่คาดหวังจากปลั๊กอินของคุณคืออะไร?

ฟิลด์ฟอร์มแสดงการเลือกอีโมจิและปุ่มตัวเลือกA form field shown selection of smiley images and its radio button.

5.

...

มีทรัพยากร / API

...

ที่สามารถนำกลับมาใช้ใหม่ได้หรือไม่?

To develop the เพื่อการพัฒนาปลั๊กอิน Mood Rating Form Field plugin, we can extends the Radio field in core product then replace its template and plugin properties options.Field lugin,เราสามารถขยายฟิลด์ Radio หลักจากนั้นแทนที่ตัวเลือกคุณสมบัติแม่แบบและปลั๊กอิน

6.

...

เตรียม environment

...

การพัฒนาของคุณ

เราจำเป็นต้องให้ซอร์สโค้ด Joget Workflow ของเราพร้อมและสร้างโดยทำตาม We need to always have our Joget Workflow Source Code ready and builded by following this guideline

The following tutorial is prepared with a Macbook Pro and บทช่วยสอนต่อไปนี้จัดทำขึ้นด้วย Macbook Pro และ the Joget Source Code is version 5.0.1. Please refer to the แนวทางสำหรับการพัฒนาปลั๊กอิน article for other platform commands.โปรดอ้างอิงถึง แนวทางสำหรับการพัฒนาปลั๊กอิน สำหรับคำสั่งแพลตฟอร์มอื่น ๆ

สมมติว่าไดเรกทอรีโฟลเดอร์ของเรามีดังนี้Let's say our folder directory is as follows. 

Code Block
- Home
  - joget
    - plugins
    - jw-community
      -5.0.1

The ไดเรกทอรี "plugins" directory is the folder we will create and store all our plugins and the ปลั๊กอิน" คือโฟลเดอร์ที่เราจะสร้างและจัดเก็บปลั๊กอินของเราทั้งหมดและไดเรกทอรี "jw-community" directory is where the เป็นที่เก็บซอร์สโค้ด Joget Workflow Source code is stored.

เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างโครงการ maven ในไดเรกทอรี "ปลั๊กอิน"Run the following command to create a maven project in "plugins" directory.

Code Block
languagebash
cd joget/plugins/
~/joget/jw-community/5.0.1/wflow-plugin-archetype/create-plugin.sh org.joget mood_rating 5.0.1

Then, the shell script will ask us to key in a version number for the plugin and ask us for a confirmation before it generates the maven project.

จากนั้นเชลล์สคริปต์จะขอให้เราใส่หมายเลขเวอร์ชันสำหรับปลั๊กอินและขอให้เรายืนยันก่อนที่จะสร้างโครงการ Maven

Code Block
languagebash
Define value 
Code Block
languagebash
Define value for property 'version':  1.0-SNAPSHOT: : 5.0.0
[INFO] Using property: package = org.joget
Confirm properties configuration:
groupId: org.joget
artifactId: mood_rating
version: 5.0.0
package: org.joget
Y: : y

We should get a เราควรได้รับข้อความ "BUILD SUCCESS" message shown in our terminal and a ที่ปรากฏในเครื่องของเราและโฟลเดอร์ "mood_rating" folder created in the "plugins" folder.ที่สร้างขึ้นในโฟลเดอร์ "ปลั๊กอิน"

เปิดโครงการ maven ด้วย IDE ที่คุณชื่นชอบ เราแนะนำให้ใช้ Open the maven project with your favourite IDE. I will be using NetBeans.  

7.

...

เริ่มโค้ด!

a. Extending the abstract class of a plugin type

Create a สร้างคลาส "MoodRatingField" class under ภายใต้ "org.joget" package. Then, extend the class with จากนั้นขยายคลาสด้วย org.joget.apps.form.lib.Radio class. The คลาส org.joget.apps.form.lib.Radio class is an implementation of  เป็นการดำเนินการของคลาส org.joget.apps.form.model.Element abstract class. Please refer to โปรดอ้างอิงถึง Form Field Element Plugin.

b. Implement all the abstract methods

As usual, we have to implement all the abstract methods. We will using เช่นเคยเราจะต้องใช้ abstract methods ทั้งหมด เราจะใช้ AppPluginUtil.getMessage method to support i18n and using constant variable เพื่อสนับสนุน i18n และใช้ตัวแปรคงที่ MESSAGE_PATH for สำหรับ message resource bundle directory.

Code Block
languagejava
titleImplementation of all basic abstract methods
collapsetrue
package org.joget;
 
import java.util.Map;
import org.joget.apps.app.service.AppPluginUtil;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.lib.Radio;
import org.joget.apps.form.model.FormBuilderPalette;
 
public class MoodRatingField extends Radio {
    
    private final static String MESSAGE_PATH = "message/form/MoodRatingField";
    
    @Override
    public String getName() {
        return "Mood Rating";
    }

    @Override
    public String getVersion() {
        return "5.0.0";
    }
    
    @Override
    public String getClassName() {
        return getClass().getName();
    }
    
    @Override
    public String getFormBuilderCategory() {
        return FormBuilderPalette.CATEGORY_CUSTOM;
    }
    
    @Override
    public String getLabel() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.MoodRatingField.pluginLabel", getClassName(), MESSAGE_PATH);
    }
 
    @Override
    public String getDescription() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.MoodRatingField.pluginDesc", getClassName(), MESSAGE_PATH);
    }
    
    @Override
    public String getPropertyOptions() {
        return AppUtil.readPluginResource(getClass().getName(), "/properties/form/moodRatingField.json", null, true, MESSAGE_PATH);
    }
    
    @Override
    public String getFormBuilderTemplate() {
        return "<label class='label'>"+getLabel()+"</label>";
    }
}

Now, we have to create a UI for admin user to provide inputs for our plugin. In getPropertyOptions method, we already specify our ตัวเลือกคุณสมบัติปลั๊กอิน definition file is located at ตอนนี้เราต้องสร้าง UI สำหรับผู้ใช้ผู้ดูแลระบบเพื่อให้อินพุตสำหรับปลั๊กอินของเรา ใน getPropertyOptions method, เราได้ระบุไฟล์ ตัวเลือกคุณสมบัติปลั๊กอิน ของเราไว้ที่ "/properties/form/moodRatingField.json". Let us create a ให้เราสร้าง directory "resources/properties/form" under ภายใต้ "mood_rating/src/main" directory. After creating the หลังจากสร้าง directory, create a file named ให้เราสร้างไฟล์ชื่อ "moodRatingField.json" in the ในโฟลเดอร์ "properties"

ในไฟล์ตัวเลือกคุณสมบัติ เราจะต้องตั่งค่าตัวเลือกดังต่อไปนี้ โปรดทราบว่าเราสามารถใช้ " folder.In the properties definition options file, we will need to provide options as below. Please note that we can use "@@message.@@message.key@@" syntax to support i18n in our properties options.เพื่อสนับสนุน i18n ในตัวเลือกคุณวมบัติของเรา

Code Block
languagejs
[{
    title : '@@form.moodRating.config@@',
    properties : [{
        name : 'id',
        label : '@@form.radio.id@@',
        type : 'textfield',
        required : 'True',
        regex_validation : '^[a-zA-Z0-9_]+$',
        validation_message : '@@form.radio.invalidId@@'
    },
    {
        name : 'label',
        label : '@@form.radio.label@@',
        type : 'textfield',
        value : '@@org.joget.MoodRatingField.pluginLabel@@'
    }]
},
{
    title : '@@form.radio.advancedOptions@@',
    properties : [{
        label : '@@form.radio.data@@',
        type : 'header'
    },
    {
        name : 'validator',
        label : '@@form.radio.validator@@',
        type : 'elementselect',
        options_ajax : '[CONTEXT_PATH]/web/property/json/getElements?classname=org.joget.apps.form.model.FormValidator',
        url : '[CONTEXT_PATH]/web/property/json[APP_PATH]/getPropertyOptions'
    },
    {
        label : '@@form.radio.ui@@',
        type : 'header'
    },
    {
        name : 'readonly',
        label : '@@form.radio.readonly@@',
        type : 'checkbox',
        value : 'False',
        options : [{
            value : 'true',
            label : ''
        }]
    },
    {
        label : '@@form.radio.workflow@@',
        type : 'header'
    },
    {
        name : 'workflowVariable',
        label : '@@form.radio.workflowVariable@@',
        type : 'textfield'
    }]
}]

After completing the properties option to collect input, we can work on the main methods of the plugin which are renderTemplate and formatData method. Since we extends Radio class, we do not need to implement formatData method.หลังจากเสร็จสิ้นตัวเลือกคุณสมบัติเพื่อรวบรวมอินพุต  เราสามารถทำงานกับวิธีการหลักของปลั๊กอินซึ่งเป็นวิธี renderTemplate และ formatData เนื่องจากเราขยายคลาส Radio เราไม่จำเป็นต้องใช้เมธอด formatData

Code Block
languagejava
    @Override
    public String renderTemplate(FormData formData, Map dataModel) {
        String template = "moodRatingField.ftl";
        
        // set value
        String value = FormUtil.getElementPropertyValue(this, formData);
        dataModel.put("value", value);
        String html = FormUtil.generateElementHtml(this, formData, template, dataModel);
        return html;
    }

In the renderTemplate, we specify the template file to ใน renderTemplate เราระบุไฟล์เทมเพลตเป็น "moodRatingField.ftl" . Let create this file under ให้สร้างไฟล์นี้ภายใต้ไดเรกทอรี "mood_rating / src / main / resources / templates" directory. Then, using FreeMaker syntax to construct our template as belowจากนั้นใช้ไวยากรณ์ FreeMaker เพื่อสร้างเทมเพลตของเราดังต่อไปนี้:

Code Block
languagexml
<div class="form-cell mood_rating" ${elementMetaData!}>
    <label class="label">${element.properties.label} <span class="form-cell-validator">${decoration}</span><#if error??> <span class="form-error-message">${error}</span></#if></label>
    <div class="form-cell-value" id="${elementParamName!}${element.properties.elementUniqueKey!}">
    <#if !(request.getAttribute("org.joget.MoodRatingField")??) >
        <style>
            .mood_rating .tdstyle {text-align:center;width:20%;border:0px none transparent !important;}
        </style>
    </#if>    
    <table style="width:150px">
        <tbody>
            <tr>
                <td class="tdstyle"><img height="25" width="25" src="${request.contextPath}/plugin/org.joget.MoodRatingField/images/smiley5.png"></td>
                <td class="tdstyle"><img height="25" width="25" src="${request.contextPath}/plugin/org.joget.MoodRatingField/images/smiley4.png"></td>
                <td class="tdstyle"><img height="25" width="25" src="${request.contextPath}/plugin/org.joget.MoodRatingField/images/smiley3.png"></td>
                <td class="tdstyle"><img height="25" width="25" src="${request.contextPath}/plugin/org.joget.MoodRatingField/images/smiley2.png"></td>
                <td class="tdstyle"><img height="25" width="25" src="${request.contextPath}/plugin/org.joget.MoodRatingField/images/smiley1.png"></td>
            </tr>
            <tr>
                <#list ['5', '4', '3', '2', '1'] as i>
                    <td class="tdstyle">
                        <input grouping="${elementParamName!}" id="${elementParamName!}" name="${elementParamName!}" type="radio" value="${i}" <#if error??>class="form-error-cell"</#if> <#if element.properties.readonly! == 'true'> disabled</#if> <#if value?? && value == i>checked</#if> />
                    </td>
                </#list>
            </tr>
        </tbody>
    </table>
    </div>
    <div style="clear:both;"></div>
</div>

There are some smiley image files will be used by the template, let put those image files under "mood_rating/src/main/มีเทมเพลตไฟล์อีโมจิบางไฟล์ที่จะใช้โดยให้วางไฟล์รูปภาพเหล่านั้นไว้ในไดเรกทอรี "mood_rating / src / main / resources / resources / image" directory.

c.

...

จัดการ dependency libraries

...

ของปลั๊กอินของคุณ

ไม่จำเป็นต้องมี library เพิ่มเติมThere are no additional library needed.

d.

...

เตรียมปลั๊กอิน internationalization (i18n)

...

ของคุณให้พร้อม

We are using เรากำลังใช้ i18n message key in ใน getLabel and และ getDescription method. We will use เราจะใช้ i18n message key in our properties options definition as well. Then, we will need to create a message resource bundle properties file for our plugin.ในไฟล์ตัวเลือกคุณสมบัติของเราเช่นกัน จากนั้นเราจะต้องสร้าง message resource bundle ในไฟล์คุณสมบัติสำหรับปลั๊กอินของเรา

สร้าง Create a directory, "resources/message/form", under ภายใต้ "mood_rating/src/main" directory. Then, create a จากนั้นสร้างไฟล์ "MoodRatingField.properties" file in the folder. In the properties file, add all the message keys and its label as below.ในโฟลเดอร์. ในไฟล์คุณสมบัติให้เพิ่ม message keys และ label ทั้งหมดดังต่อไปนี้

Code Block
org.joget.MoodRatingField.pluginLabel=Mood Rating
org.joget.MoodRatingField.pluginDesc=Form Field for rating mood
form.moodRating.config=Edit Mood Rating

e.

...

ลงทะเบียนปลั๊กอินของคุณที่ Felix Framework

Next, we will have to register our plugin class in the Activator class (Auto generated in the same class package) to tell the Felix Framework that this is a plugin.ต่อไปเราจะต้องลงทะเบียนคลาสปลั๊กอินของเราในคลาส Activator (สร้างอัตโนมัติในแพ็คเกจคลาสเดียวกัน) เพื่อบอก Felix Framework ว่านี่เป็นปลั๊กอิน

Code Block
languagejava
    public void start(BundleContext context) {
        registrationList = new ArrayList<ServiceRegistration>();
        //Register plugin here
        registrationList.add(context.registerService(MoodRatingField.class.getName(), new MoodRatingField(), null));
    }

f.

...

สร้าง และ ทดสอบ

มาสร้างปลั๊กอินของเรากัน เมื่อกระบวนการสร้างเสร็จสิ้นเราจะพบไฟล์ Let's build our plugin. Once the building process is done, we will find a "mood_rating-5.0.0.jar" file created under "ที่สร้างขึ้นภายใต้ไดเรกทอรี "mood_rating / target" directory.

จากนั้นเราให้อัพโหลดปลั๊กอิน Then, let's upload the plugin jar to Manage Plugins. After uploading the jar file, double check that the plugin is uploaded and activated correctly.

Image Removed

 หลังจากอัปโหลดไฟล์ jar ตรวจสอบอีกครั้งว่ามีการอัปโหลดและเปิดใช้งานปลั๊กอินอย่างถูกต้อง

Image Added

จากนั้นให้ตรวจสอบฟิลด์แสดงอารมณ์ข้อมูลใน Then, check the Mood Rating field is shown in the ตัวสร้างฟอร์ม.

Drag it to the Form Builder Canvas and set its properties.ลากไปที่ตัวสร้างฟอร์มและตั้งค่าคุณสมบัติ

 Image Removed

Save the properties and check the field is render in canvas as following.

Image Removed

Image Added

บันทึกคุณสมบัติและตรวจสอบว่าฟิลด์แสดงผลเป็น canvas ดังต่อไปนี้

Image Added

ตรวจสอบและทดสอบในรูปแบบCheck and test the field in form.

8.

...

ขั้นต่อไป แชร์หรือขาย

คุณสามารถดาวน์โหลด source code จาก You can download the source code from mood_rating_src.zip.

To download the ready-to-use plugin jar, please find it in หากต้องการดาวน์โหลด jar ปลั๊กอินที่พร้อมใช้งานโปรดค้นหาที่ http://marketplace.joget.org/. (Coming Soonเร็ว ๆ นี้)