Joget DX 8 Stable Released
The stable release for Joget DX 8 is now available, with a focus on UX and Governance.
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 How to develop a Bean Shell Hash Variable for more details steps.
/jw/web/client/app/{appId}/{appVersion}/form/download/{formId}/{recordId}/{fileName}.?attachment=trueSample:
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.
Form Id : the form that contains the File Upload field.
Download as Attachment: Whether or not to add "?attachedment=true" in link.
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 Guideline for developing a plugin for other platform command.
Let said our folder directory as following.
- 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.
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.
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_link_datalist_formatter" folder created in "plugins" folder.
Open the maven project with your favour IDE. I will be using NetBeans.
Then, we have to do a UI for admin user to provide inputs for our plugin. In getPropertyOptions method, we already specify our Plugin Properties Options definition file is locate at "/properties/fileLinkDatalistFormatter.json". Let us create a directory "resources/properties" under "file_link_datalist_formatter/src/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.
[{ 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 : '' }] }] }]
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; }
<!-- 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 -->
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?
public void start(BundleContext context) { registrationList = new ArrayList<ServiceRegistration>(); //Register plugin here registrationList.add(context.registerService(FileLinkDatalistFormatter.class.getName(), new FileLinkDatalistFormatter(), null)); }
Then, let upload the plugin jar to Manage Plugins. After upload the jar file, double check the plugin is uploaded and activated correctly.
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.
To download the ready-to-use plugin jar, please find it in http://marketplace.joget.org/.