In this tutorial, we will follow the guideline for developing a plugin to develop our Hyperlink Options Filter plugin. Please also refer to the very first tutorial How to develop a Bean Shell Hash Variable for more details steps.

ในบทช่วยสอนนี้เราจะทำตามคำแนะนำในการพัฒนาปลั๊กอินเพื่อพัฒนาปลั๊กอินตัวกรองไฮเปอร์ลิงก์ของเรา โปรดอ้างอิงถึงบทช่วยสอนแรกวิธีการพัฒนา How to develop a Bean Shell Hash Variable สำหรับขั้นตอนรายละเอียดเพิ่มเติม

1. What is the problem?

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

We want to have a filter similar to the following.

เราต้องการมีตัวกรองคล้ายกับต่อไปนี้


2. How to solve the problem?

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

We will develop a Datalist Filter Type Plugin to render our Hyperlink Options Filter. 

เราจะพัฒนา Datalist Filter Type Plugin เพื่อแสดงตัวกรองตัวเลือกไฮเปอร์ลิงก์ของเรา

3. What is the input needed for your plugin?

สิ่งที่จำเป็นในการป้อนข้อมูลสำหรับปลั๊กอินของคุณ?

To develop a Hyperlink Options Filter plugin, we will need to provide some inputs as following.

ในการพัฒนาปลั๊กอินตัวกรองไฮเปอร์ลิงก์ตัวกรองเราจะต้องให้อินพุตบางอย่างดังต่อไปนี้

  1. The options to populate as links

    ตัวเลือกเพื่อเติมเป็นลิงก์

  2. Whether or not to show the data count of each options.

    แสดงจำนวนข้อมูลของแต่ละตัวเลือกหรือไม่

4. What is the output and expected outcome of your plugin?

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

A list of hyperlinks which will list all the options with its data count. When click the hyperlink will filter the datalist.

รายการไฮเปอร์ลิงก์ซึ่งจะแสดงรายการตัวเลือกทั้งหมดพร้อมนับข้อมูล เมื่อคลิกที่ไฮเปอร์ลิงก์จะทำการกรองข้อมูล


5. Are there any resources/API that can be reused?

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

Refer to Datalist Filter Type Plugin.

อ้างถึง Datalist Filter Type Plugin

6. Prepare your development environment

เตรียมสภาพแวดล้อมการพัฒนาของคุณ

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 the Joget Source Code is version 5.0.1. Please refer to the Guideline for Developing a Plugin article for other platform commands.

Let's say our folder directory is as follows. 

เราจำเป็นต้องให้ซอร์สโค้ด Joget Workflow ของเราพร้อมและสร้างโดยปฏิบัติตาม this guideline

บทช่วยสอนต่อไปนี้จัดทำขึ้นด้วย Macbook Pro และรหัสแหล่งที่มาของ Joget เป็นรุ่น 5.0.1 โปรดอ้างอิง Guideline for Developing a Plugin สำหรับคำสั่งแพลตฟอร์มอื่น ๆ

สมมติว่าไดเรกทอรีโฟลเดอร์ของเรามีดังนี้

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

Run the following command to create a maven project in "plugins" directory.

ไดเรกทอรี "ปลั๊กอิน" คือโฟลเดอร์ที่เราจะสร้างและจัดเก็บปลั๊กอินทั้งหมดของเราและไดเรกทอรี "jw-community" เป็นที่เก็บ Joget Workflow Source code เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างโครงการ maven ในไดเรกทอรี "ปลั๊กอิน"

cd joget/plugins/
~/joget/jw-community/5.0.1/wflow-plugin-archetype/create-plugin.sh org.joget hyperlink_options_filter 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

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

We should get a "BUILD SUCCESS" message shown in our terminal and a "hyperlink_options_filter" folder created in the "plugins" folder.

Open the maven project with your favourite IDE. I will be using NetBeans.  

เราควรได้รับข้อความ "BUILD SUCCESS" ที่ปรากฏในเครื่องของเราและโฟลเดอร์ "hyperlink_options_filter" ที่สร้างขึ้นในโฟลเดอร์ "ปลั๊กอิน" เปิดโครงการ maven ด้วย IDE ที่คุณชื่นชอบ ฉันจะใช้  NetBeans

7. Just code it!

เพียงแค่รหัส!

a. Extending the abstract class of a plugin type

การขยายคลาสนามธรรมของประเภทปลั๊กอิน

Create a "HyperlinkOptionsFilter" class under "org.joget" package. Then, extend the class with org.joget.apps.datalist.model.DataListFilterTypeDefault abstract class. Please refer to Datalist Filter Type Plugin.

สร้างคลาส "HyperlinkOptionsFilter" ภายใต้แพ็คเกจ "org.joget" จากนั้นขยายคลาสด้วย org.joget.apps.datalist.model.DataListFilterTypeDefault คลาสนามธรรม โปรดอ้างอิงถึง Datalist Filter Type Plugin

b. Implement all the abstract methods

ใช้วิธีนามธรรมทั้งหมด

As usual, we have to implement all the abstract methods. We will using AppPluginUtil.getMessage method to support i18n and using constant variable MESSAGE_PATH for message resource bundle directory.

ตามปกติเราต้องใช้วิธีนามธรรมทั้งหมด เราจะใช้วิธี AppPluginUtil.getMessage เพื่อสนับสนุน i18n และใช้ตัวแปร MESSAGE_PATH คงที่สำหรับไดเรกทอรีทรัพยากรข้อความมัด

package org.joget;

import org.joget.apps.app.service.AppPluginUtil;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.datalist.model.DataListFilterTypeDefault;

public class HyperlinkOptionsFilter extends DataListFilterTypeDefault {
    private final static String MESSAGE_PATH = "message/HyperlinkOptionsFilter";
    
    public String getName() {
        return "Hyperlink Options Filter Type";
    }
 
    public String getVersion() {
        return "5.0.0";
    }
 
    @Override
    public String getLabel() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.HyperlinkOptionsFilter.pluginLabel", getClassName(), MESSAGE_PATH);
    }
 
    @Override
    public String getDescription() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.HyperlinkOptionsFilter.pluginDesc", getClassName(), MESSAGE_PATH);
    }
 
    public String getClassName() {
        return this.getClass().getName();
    }
 
    public String getPropertyOptions() {
        return AppUtil.readPluginResource(getClass().getName(), "/properties/hyperlinkOptionsFilter.json", null, true, MESSAGE_PATH);
    }
}

Now, we have to create a UI for admin user to provide inputs for our plugin. In getPropertyOptions method, we already specify our Plugin Properties Options definition file is located at "/properties/hyperlinkOptionsFilter.json". Let us create a directory "resources/properties" under "hyperlink_options_filter/src/main" directory. After creating the directory, create a file named "hyperlinkOptionsFilter.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.key@@" syntax to support i18n in our properties options.

ตอนนี้เราต้องสร้าง UI สำหรับผู้ใช้ผู้ดูแลระบบเพื่อให้อินพุตสำหรับปลั๊กอินของเรา ในวิธีการ getPropertyOptions เราได้ระบุไฟล์ข้อกำหนด Plugin Properties Options ของเราแล้วที่ "/properties/hyperlinkOptionsFilter.json" ให้เราสร้างไดเรกทอรี "resources / properties" ภายใต้ไดเรกทอรี "hyperlink_options_filter / src / main" หลังจากสร้างไดเรกทอรีให้สร้างไฟล์ชื่อ "hyperlinkOptionsFilter.json" ในโฟลเดอร์ "properties"

ในไฟล์ตัวเลือกคำจำกัดความของคุณสมบัติเราจะต้องระบุตัวเลือกดังต่อไปนี้ โปรดทราบว่าเราสามารถใช้ไวยากรณ์ "@@ message.key @@" เพื่อรองรับ i18n ในตัวเลือกคุณสมบัติของเรา

[{
    title : '@@HyperlinkOptionsFilter.config@@',
    properties : [{
        name:'defaultValue',
        label:'@@HyperlinkOptionsFilter.defaultValue@@',
        type:'textfield'
    },
    {
        name : 'showLabel',
        label : '@@HyperlinkOptionsFilter.showLabel@@',
        type : 'checkbox',
        options : [{
            value : 'true',
            label : ''
        }]
    },
    {
        name : 'displayFull',
        label : '@@HyperlinkOptionsFilter.displayFull@@',
        type : 'checkbox',
        value : 'true',
        options : [{
            value : 'true',
            label : ''
        }]
    },
    {
        name : 'showCount',
        label : '@@HyperlinkOptionsFilter.showCount@@',
        type : 'checkbox',
        value : '',
        options : [{
            value : 'true',
            label : ''
        }]
    },
    {
        name : 'options',
        label : '@@HyperlinkOptionsFilter.options@@',
        type : 'grid',
        columns : [{
            key : 'value',
            label : '@@HyperlinkOptionsFilter.value@@'
        },
        {
            key : 'label',
            label : '@@HyperlinkOptionsFilter.label@@'
        }]
    }]
},
{
    title : '@@HyperlinkOptionsFilter.chooseOptionsBinder@@',
    properties : [{
        name : 'optionsBinder',
        label : '@@HyperlinkOptionsFilter.optionsBinder@@',
        type : 'elementselect',
        options_ajax : '[CONTEXT_PATH]/web/property/json/getElements?classname=org.joget.apps.form.model.FormLoadOptionsBinder',
        url : '[CONTEXT_PATH]/web/property/json[APP_PATH]/getPropertyOptions'
    }]
}]

After completing the properties option to collect input, we can work on the main methods of the plugin which are getTemplate and getQueryObject method. In getTemplate method, we will retrieve options and its count based on the configured plugin properties.

หลังจากเสร็จสิ้นตัวเลือกคุณสมบัติเพื่อรวบรวมอินพุตเราสามารถทำงานกับวิธีหลักของปลั๊กอินซึ่งเป็นวิธี getTemplate และ getQueryObject ในเมธอด getTemplate เราจะดึงข้อมูลตัวเลือกและจำนวนตามคุณสมบัติปลั๊กอินที่กำหนดค่าไว้

    public String getTemplate(DataList datalist, String name, String label) {
        PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");
        Map dataModel = new HashMap();
        
        dataModel.put("element", this);
        dataModel.put("name", datalist.getDataListEncodedParamName(DataList.PARAMETER_FILTER_PREFIX+name));
        dataModel.put("label", label);
        
        Map<String, String> options = getOptionMap();
        if ("true".equalsIgnoreCase(getPropertyString("showCount"))) {
            DataListBinder binder = datalist.getBinder();
            for (String key : options.keySet()) {
                DataListFilterQueryObject filter = getQueryObject(datalist, name, key);
                int count = 0;
                if (binder != null) {
                    if (filter != null) {
                        count = binder.getDataTotalRowCount(datalist, binder.getProperties(), new DataListFilterQueryObject[]{filter});
                    } else {
                        count = binder.getDataTotalRowCount(datalist, binder.getProperties(), new DataListFilterQueryObject[]{});
                    }
                }
                
                options.put(key, options.get(key) + " (" + count + ")");
            }
        }
        
        String value = getValue(datalist, name, getPropertyString("defaultValue"));
        dataModel.put("value", value);
        dataModel.put("options", options);
            
        return pluginManager.getPluginFreeMarkerTemplate(dataModel, getClassName(), "/templates/hyperlinkOptionsFilter.ftl", null);
    }
    
    protected Map<String, String> getOptionMap() {
        Map<String, String> optionMap = new ListOrderedMap();
        
        // load from "options" property
        Object[] options = (Object[]) getProperty(FormUtil.PROPERTY_OPTIONS);
        for (Object o : options) {
            Map option = (HashMap) o;
            Object value = option.get(FormUtil.PROPERTY_VALUE);
            Object label = option.get(FormUtil.PROPERTY_LABEL);
            if (value != null && label != null) {
                optionMap.put(value.toString(), label.toString());
            }
        }
        // load from binder if available
        Map optionsBinderProperties = (Map) getProperty("optionsBinder");
        if (optionsBinderProperties != null && optionsBinderProperties.get("className") != null && !optionsBinderProperties.get("className").toString().isEmpty()) {
            PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");
            FormBinder optionBinder = (FormBinder) pluginManager.getPlugin(optionsBinderProperties.get("className").toString());
            if (optionBinder != null) {
                optionBinder.setProperties((Map) optionsBinderProperties.get("properties"));
                FormRowSet rowSet = ((FormLoadBinder) optionBinder).load(null, null, null);
                if (rowSet != null) {
                    optionMap = new ListOrderedMap();
                    for (FormRow row : rowSet) {
                        Iterator<String> it = row.stringPropertyNames().iterator();
                        // get the key based on the "value" property
                        String value = row.getProperty(FormUtil.PROPERTY_VALUE);
                        if (value == null) {
                            // no "value" property, use first property instead
                            String key = it.next();
                            value = row.getProperty(key);
                        }
                        // get the label based on the "label" property
                        String label = row.getProperty(FormUtil.PROPERTY_LABEL);
                        if (label == null) {
                            // no "label" property, use next property instead
                            String key = it.next();
                            label = row.getProperty(key);
                        }
                        optionMap.put(value, label);
                    }
                }
            }
        }
        
        if (!optionMap.containsKey("")) {
            Map<String, String> tempOptionMap = new ListOrderedMap();
            tempOptionMap.put("", AppPluginUtil.getMessage("HyperlinkOptionsFilter.all", getClassName(), MESSAGE_PATH));
            tempOptionMap.putAll(optionMap);
            optionMap = tempOptionMap;
        }
        
        return optionMap;
    }
    
    protected DataListFilterQueryObject getQueryObject(DataList datalist, String name, String value) {
        DataListFilterQueryObject queryObject = new DataListFilterQueryObject();
        if (datalist != null && datalist.getBinder() != null && value != null && !value.isEmpty()) {
            String columnName = datalist.getBinder().getColumnName(name);
            List<String> valuesList = new ArrayList<String>();
            String query = "("+columnName+" = ? or "+columnName+" like ? or "+columnName+" like ? or "+columnName+" like ?)";
            valuesList.add(value);
            valuesList.add(value + ";%");
            valuesList.add("%;" + value + ";%");
            valuesList.add("%;" + value);
            queryObject.setOperator(DataListFilter.OPERATOR_AND);
            queryObject.setQuery(query);
            queryObject.setValues(valuesList.toArray(new String[0]));
            return queryObject;
        }
        return null;
    }
 
    public DataListFilterQueryObject getQueryObject(DataList datalist, String name) {
        String value = getValue(datalist, name, getPropertyString("defaultValue"));
        return getQueryObject(datalist, name, value);
    }

In the getTemplate, we specify the template file to "hyperlinkOptionsFilter.ftl". Let create this file under "hyperlink_options_filter/src/main/resources/templates" directory. Then, using FreeMaker syntax to construct our template as below:

ใน getTemplate เราระบุไฟล์เทมเพลตเป็น "hyperlinkOptionsFilter.ftl" ให้สร้างไฟล์นี้ภายใต้ไดเรกทอรี "hyperlink_options_filter / src / main / resources / templates" จากนั้นใช้ไวยากรณ์ FreeMaker เพื่อสร้างแม่แบบของเราดังต่อไปนี้:

<div id="${name!}_container" style="display:none;margin:5px 0;">
    <input id="${name!}" name="${name!}" type="hidden" value="${value!?html}" />
    <#if element.properties.showLabel! == "true" >
        <label><strong>${label!?html} :</strong></label>&nbsp;&nbsp;
    </#if>
    <#list options?keys as key>
        <a ref="${key?html}" href="${key?html}" class="<#if value! == key >active</#if>"><span><#if value! == key ><strong></#if>${options[key]!?html}<#if value! == key ></strong></#if></span></a>&nbsp;&nbsp;
    </#list>
    <script type="text/javascript">
        $(document).ready(function(){
            <#if element.properties.displayFull! == "true" >
                $('#${name!}_container').insertBefore($('#${name!}_container').closest(".filters"));
            </#if>
            $('#${name!}_container').show();
            $('#${name!}_container a').click(function(){
                var value = $(this).attr("ref");
                $(this).parent().find("input").val(value);
                $(this).closest("form").submit();
                return false;
            });
        });
    </script>
</div>

c. Manage the dependency libraries of your plugin

จัดการไลบรารีของปลั๊กอินของคุณ

We need to include "commons-collections" library in our POM file.

เราจำเป็นต้องรวมไลบรารี "คอมมอนคอลเลกชัน" ในไฟล์ POM ของเรา

        <!-- Change plugin specific dependencies here -->
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <!-- End change plugin specific dependencies here -->

d. Make your plugin internationalization (i18n) ready

เตรียมปลั๊กอินสากลให้พร้อม (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.

เรากำลังใช้คีย์ข้อความ i18n ในวิธี getLabel และ getDescription เราจะใช้คีย์ข้อความ i18n ในการกำหนดตัวเลือกคุณสมบัติของเราเช่นกัน จากนั้นเราจะต้องสร้างไฟล์คุณสมบัติมัดทรัพยากรสำหรับปลั๊กอินของเรา

Create a directory, "resources/message", under "hyperlink_options_filter/src/main" directory. Then, create a "HyperlinkOptionsFilter.properties" file in the folder. In the properties file, add all the message keys and its label as below.

สร้างไดเรกทอรี "resources / message" ภายใต้ไดเรกทอรี "hyperlink_options_filter / src / main" จากนั้นสร้างไฟล์ "HyperlinkOptionsFilter.properties" ในโฟลเดอร์ ในไฟล์คุณสมบัติให้เพิ่มคีย์ข้อความและป้ายกำกับทั้งหมดดังต่อไปนี้

org.joget.HyperlinkOptionsFilter.pluginLabel=Hyperlink Options
org.joget.HyperlinkOptionsFilter.pluginDesc=Show options as Hyperlink to perform filter.
HyperlinkOptionsFilter.all=All
HyperlinkOptionsFilter.config=Configure Hyperlink Options Filter
HyperlinkOptionsFilter.options=Options
HyperlinkOptionsFilter.value=Value
HyperlinkOptionsFilter.label=Label
HyperlinkOptionsFilter.chooseOptionsBinder=Choose Options Binder
HyperlinkOptionsFilter.optionsBinder=Options Binder
HyperlinkOptionsFilter.defaultValue=Default Value
HyperlinkOptionsFilter.showCount=Show Data Count?
HyperlinkOptionsFilter.displayFull=Display in full width (Above other filters)
HyperlinkOptionsFilter.showLabel=Show label?

e. Register your plugin to the Felix Framework

ลงทะเบียนปลั๊กอินของคุณไปที่ 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 ว่านี่เป็นปลั๊กอิน

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

f. Build it and test

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

Let's build our plugin. Once the building process is done, we will find a "hyperlink_options_filter-5.0.0.jar" file created under "hyperlink_options_filter/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.

มาสร้างปลั๊กอินของเรากัน เมื่อกระบวนการสร้างเสร็จสิ้นเราจะพบไฟล์ "hyperlink_options_filter-5.0.0.jar" ที่สร้างขึ้นภายใต้ไดเรกทอรี "hyperlink_options_filter / target" จากนั้นลองอัปโหลดปลั๊กอินไปที่ Manage Plugins หลังจากอัปโหลดไฟล์ jar ให้ตรวจสอบอีกครั้งว่ามีการอัปโหลดและเปิดใช้งานปลั๊กอินอย่างถูกต้อง


Then, check the Hyperlink Options Filter is shown as a selection of filter type in the Datalist Builder.

จากนั้นตรวจสอบว่าตัวเลือกไฮเปอร์ลิงค์นั้นแสดงเป็นตัวเลือกประเภทตัวกรองใน Datalist Builder


Configure its properties.

กำหนดค่าคุณสมบัติ


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

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


Check and test the filter in datalist.

ตรวจสอบและทดสอบตัวกรองใน datalist


8. Take a step further, share it or sell it

ก้าวไปอีกขั้นแบ่งปันหรือขายมัน

You can download the source code from hyperlink_options_filter_src.zip.

To download the ready-to-use plugin jar, please find it in http://marketplace.joget.org/. (Coming Soon)

คุณสามารถดาวน์โหลดซอร์สโค้ดได้จาก hyperlink_options_filter_src.zip

หากต้องการดาวน์โหลด jar ปลั๊กอินที่พร้อมใช้งานโปรดค้นหาได้ที่ http://marketplace.joget.org/ (เร็ว ๆ นี้)