Versions Compared

Key

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

Table of Contents

 

In this tutorial, we will following the guideline of developing a plugin to develop our File Link Datalist Formatter. Please also refer to the very first tutorial 如何开发一个Bean Shell Hash Variable插件 for more details steps.

1. What is the problem?

We have a file uploaded using File Upload field in form. We want to show the file name as a link to download the file in our datalist.

2. What is your idea to solve the problem?

We can develop a Datalist Column Formatter插件 to convert the value to a link.

3. What is the input needed for your plugin?

The file download link format of Joget Workflow is

在本教程中,我们将遵循开发插件  来开发我们的File Link Datalist格式化器的  指导原则。 有关更多详细信息步骤,请参阅第一个教程  如何开发一个Bean Shell哈希变量插件

1.什么问题?

我们有一个文件上传使用文件上传领域的形式。我们想要显示文件名作为链接来下载我们的数据列表中的文件。

2.你有什么想法来解决这个问题?

我们可以开发一个  Datalist Column Formatter插件将这个值转换成一个链接。

3.你的插件需要什么输入?

Joget Workflow的文件下载链接格式是

/jw/web/client/app/{appId}/{appVersion}/form/download/{formId}/{recordId}/{fileName}.?attachment=true

Sample例如:

http://localhost:8080/jw/web/client/app/test/1/form/download/testFile/f6946830-c0a80070-48cad9b7-8b971682/CHANGES.txt.?attachment=true

From the link format, we need to get the following information from admin user.

  1. Form Id : the form that contains the File Upload field.
  2. Download as Attachment: Whether or not to add "?attachedment=true" in link.

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

The file name is displayed as a link in Datalist.

5. Is there any resources/API that can be reuse?

We can refer to the File Upload Field on how to generate the download link of the file.

6. Prepare your development environment

从链接格式,我们需要从管理员用户获得以下信息。

  1. 表单ID:包含“文件上传”字段的表单。
  2. 下载为附件:是否在链接中添加“ ?attachment = true”。

4.你的插件的输出和预期结果是什么?

文件名在Datalist中显示为链接。

5.有没有可重用的资源/ API?

我们可以参考文件上传字段来了解如何生成文件的下载链接。

6.准备你的开发环境

我们需要始终准备好Joget Workflow Source Code,并按照这个指导方针建立起来  。 

本教程的以下内容是使用Macbook Pro和Joget源代码5.0.0版编写的。 其他平台命令请参考  如何开发插件

让我们说我们的文件夹目录如下。 

 

Code Block
- Home

We need to always have our Joget Workflow Source Code ready and builded by following this guideline

The following of this tutorial is prepared with a Macbook Pro and Joget Source Code version 5.0.0. Please refer to 如何开发插件 for other platform command.

Let said our folder directory as following. 

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

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

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

“plugins”目录是我们要创建和存储我们所有插件的文件夹,“jw-community”目录是Joget Workflow源代码存储的地方。

运行以下命令在“plugins”目录下创建一个maven项目。

Code Block
languagebash
cd joget/plugins/
Code Block
languagebash
cd joget/plugins/
~/joget/jw-community/5.0.0/wflow-plugin-archetype/create-plugin.sh org.joget.tutorial file_link_datalist_formatter 5.0.0

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

然后,shell脚本将要求我们为您的插件输入一个版本,并在生成maven项目之前要求我们确认。

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

We should get "BUILD SUCCESS" message shown in our terminal and a "file我们应该在终端显示“BUILD SUCCESS”消息,在“plugins”文件夹中创建一个“file_link_datalist_formatter" folder created in "plugins" folder.formatter”文件夹。

用你喜欢的IDE打开maven项目。我将使用  NetBeans。  Open the maven project with your favour IDE. I will be using NetBeans.  

7. Just code it!

a.

...

继承插件类型抽象类

在“org.joget.tutorial”包下创建一个“FileLinkDatalistFormatter”类。然后,使用Create a "FileLinkDatalistFormatter" class under "org.joget.tutorial" package. Then, extend the class with org.joget.apps.datalist.model.DataListColumnFormatDefault abstract class. Please refer to   抽象类来扩展  该类。 请参阅  Datalist Column Formatter插件.

b.

...

实现所有的抽象方法

像往常一样,我们必须执行所有的抽象方法。我们将使用AppPluginUtil.getMessage方法来支持i18n,并使用常量变量MESSAGE_PATH作为消息资源包目录。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.

 

Code Block
languagejava
titleImplementation of all basic abstract methods
collapsetrue
package org.joget.tutorial;
import org.joget.apps.app.service.AppPluginUtil;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.datalist.model.DataList;
import org.joget.apps.datalist.model.DataListColumn;
import org.joget.apps.datalist.model.DataListColumnFormatDefault;

public class FileLinkDatalistFormatter extends DataListColumnFormatDefault {
    
    private final static String MESSAGE_PATH = "messages/FileLinkDatalistFormatter";
    
    public String getName() {
        return "File Link Datalist Formatter";
    }
 
    public String getVersion() {
        return "5.0.0";
    }
    
    public String getClassName() {
        return getClass().getName();
    }
 
    public String getLabel() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.tutorial.FileLinkDatalistFormatter.pluginLabel", getClassName(), MESSAGE_PATH);
    }
    
    public String getDescription() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.tutorial.FileLinkDatalistFormatter.pluginDesc", getClassName(), MESSAGE_PATH);
    }
 
    public String getPropertyOptions() {
        return AppUtil.readPluginResource(getClassName(), "/properties/fileLinkDatalistFormatter.json", null, true, MESSAGE_PATH);
    }
 
    public String format(DataList dataList, DataListColumn column, Object row, Object value) {
        throw new UnsupportedOperationException("Not supported yet."); 
    }
}

Then, we have to do a UI for admin user to provide inputs for our plugin. In getPropertyOptions method, we already specify our 插件属性选项与配置 definition file is locate at "/properties/fileLinkDatalistFormatter.json". Let us create a directory "resources/properties" under "file_然后,我们必须为管理员用户提供一个UI来为我们的插件提供输入。在getPropertyOptions方法中,我们已经指定了  插件属性选项和配置  定义文件位于“/properties/fileLinkDatalistFormatter.json”。让我们在“file_link_datalist_formatter / src / main”目录下创建一个目录“resources / main" directory. After create the directory, create a file named "fileLinkDatalistFormatter.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.

properties”。创建目录后,在“properties”文件夹中创建一个名为“fileLinkDatalistFormatter.json”的文件。

在属性定义选项文件中,我们需要提供如下的选项。请注意,我们可以在我们的属性选项中使用“@@ message.key @@”语法来支持i18n。

Code Block
languagejs
[{
    title : '@@datalist.fileLinkFormatter.config@@',
    properties : [{
        name : 'formDefId',
        label : '@@datalist.fileLinkFormatter.form@@',
        type : 'selectbox',
        options_ajax : '[CONTEXT_PATH]/web/json/console/app[APP_PATH]/forms/options',
        required : 'True'
    },
    {
        name : 'attachment',
        label : '@@datalist.fileLinkFormatter.attachment@@',
        type : 'checkbox',
        options : [{
            value : 'true',
            label : ''
        }]
    }]
}]
 
一旦我们完成了属性选项来收集输入,我们可以在插件的主要方法 ,格式化 方法编写代码。

 

Once we done the properties option to collect input, we can work on the main method of the plugin which is format method.

Code Block
language

 

Code Block
languagejava
    public String format(DataList dataList, DataListColumn column, Object row, Object value) {
        String result = (String) value;
 
        if (result != null && !result.isEmpty()) {
            try {
                String formDefId = getPropertyString("formDefId");
                AppDefinition appDef = AppUtil.getCurrentAppDefinition();
                result = "";
 
                String attachment = "";
                if ("true".equals(getPropertyString("attachment"))) {
                    attachment = "?attachment=true";
                }
 
                //get the id of this record
                String primaryKeyValue = (String) LookupUtil.getBeanProperty(row, dataList.getBinder().getPrimaryKeyColumnName());
                
                HttpServletRequest request = WorkflowUtil.getHttpServletRequest();
 
                //suport for multi values
                for (String v : value.toString().split(";")) {
                    if (!v.isEmpty()) {
                        // determine actual path for the file uploads
                        String fileName = v;
                        String encodedFileName = fileName;
 
                        try {
                            encodedFileName = URLEncoder.encode(fileName, "UTF8").replaceAll("\\+", "%20");
                        } catch (UnsupportedEncodingException ex) {
                            // ignore
                        }
 
                        String filePath = request.getContextPath() + "/web/client/app/" + appDef.getAppId() + "/" + appDef.getVersion().toString() + "/form/download/" + formDefId + "/" + primaryKeyValue + "/" + encodedFileName + "." + attachment;
 
                        if (!result.isEmpty()) {
                            result += ", ";
                        }
                        result += "<a href=\""+filePath+"\" target=\"_blank\">"+StringUtil.stripAllHtmlTag(fileName)+"</a>";
                    }
                }
            } catch (Exception e) {
                LogUtil.error(getClassName(), e, "");
            }
        }
        return result;
    }

 

c.

Manage the dependency libraries of your plugin

管理你的插件的依赖库

我们的插件使用orgOur plugin is using org.displaytag.util.LookupUtil and  class, it required displaytag and jsp-api library. So, we have to add it to our POM file.LookupUtil和类,它需要displaytag和jsp-api库。所以,我们必须将其添加到我们的POM文件。

Code Block
languagexml
Code Block
languagexml
<!-- Change plugin specific dependencies here -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>displaytag</groupId>
            <artifactId>displaytag</artifactId>
            <version>1.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jcl104-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!-- End change plugin specific dependencies here -->

d. Make your plugin internationalization (i18n) ready

We are using i18n message key in getLabel and getDescription method. We also used i18n message key in our properties options definition as well. So, we will need to create a message resource bundle properties file for our plugin.

plugin specific dependencies here -->

d.确保你的插件国际化 (i18n) 

我们在getLabel和getDescription方法中使用i18n消息密钥。我们还在我们的属性选项定义中使用了i18n消息密钥。所以,我们需要为我们的插件创建一个消息资源包属性文件。

在“file_link_datalist_formatter / src / main”目录下创建目录“resources / messages”。然后,在该文件夹中创建一个“FileLinkDatalistFormatter.properties”文件。在属性文件中,让我们添加所有的消息键和它的标签如下。Create directory "resources/messages" under "file_link_datalist_formatter/src/main" directory. Then, create a "FileLinkDatalistFormatter.properties" file in the folder. In the properties file, let add all the message keys and its label as below.

Code Block
org.joget.tutorial.FileLinkDatalistFormatter.pluginLabel=File Link Datalist Formatter
org.joget.tutorial.FileLinkDatalistFormatter.pluginDesc=To format the column value as attachment download link.
datalist.fileLinkFormatter.config=Configure File Link Formatter
datalist.fileLinkFormatter.form=Form
datalist.fileLinkFormatter.attachment=Download as Attachment?

e.

Register your plugin to Felix Framework

Felix框架中注册你的插件

We will have to register our plugin class in Activator class (Auto generated in the same class package) to tell Felix Framework that this is a plugin.

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

f.

Build it and testing

构建并测试

让我们建立我们的插件。一旦构建过程完成,我们将在“fileLet build our plugin. Once the building process is done, we will found a "file_link_datalist_formatter -5.0.0.jar" file is created under "file/ target”目录下创建一个“file_link_datalist_formatter/target" directory.

Then, let upload the plugin jar to Manage Plugins. After upload the jar file, double check the plugin is uploaded and activated correctly.

Image Removed

-5.0.0.jar”文件。

然后,让插件jar上传到  管理插件。上传jar文件后,仔细检查插件是否正确上传和激活。

Image Added

在userview中创建一个CRUD来测试我们的插件。在列表中,让我们配置文件上传字段列如下。Let create a CRUD in userview to test our plugin. In list, let configure the File Upload field column as following.

In the CRUD list, we can see our file name is converted to a link.在CRUD列表中,我们可以看到我们的文件名被转换为链接

8.

Take a step further, share it or sell it

再进一步,分享或出售

您可以从You can download the source code from file_link_datalist_formatter.zip.下载源代码  。

要下载现成的插件jar,请在To download the ready-to-use plugin jar, please find it in http://marketplace.joget.org/.找到它