iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 27
0
Software Development

Mendix從入門到了解,工程師和業務都要知道低代碼平台是什麼系列 第 27

Microflows的Java升級版

在寫Java以前想一下

在決定用Java解決問題前,其實很多功能在內建的Microflows中都能夠解決了,再加上Java有可能會使原本的結構變得更混亂,甚至讓之後接手工作的工程師工作上困難,因此,跟自創widget一樣,官方不太建議自己用Java寫Mendix上的Microflows。

當然,特殊情況下,該寫的Java還是要捲起袖子來寫,這時要注意, 用Java寫出來的activity要符合
1.單一職責原則
2.符合Unix哲學
3.讓不同的microflows都能夠重複使用

使用Java Action

點選Project explorer > User Manager > Add other... > Java Action,在點選Java Action 後會跳出下列命名的視窗

https://ithelp.ithome.com.tw/upload/images/20201011/201295307icfMOLXji.png

https://ithelp.ithome.com.tw/upload/images/20201011/20129530Y1ebKAn5zZ.png

按OK後會自動開啟新的檔案,這裡可以讓我們設定輸出的parameters 和設定return type。

https://ithelp.ithome.com.tw/upload/images/20201011/20129530iXlM0jBJrF.png

在設定parameter時,可以設定type和相關的category

有效的type選項有
• Object
• List
• Enumeration
• Boolean
• Date and time
• Decimal
• Integer/Long
• String
• Nothing

轉往Java IDE

1.選擇 Project > Deploy for Eclipse,之後系統會自動將source code檔案存在project資料夾中

2.開啟Eclipse後,可以在package explorer 中的 javasource > usermanager.actions 找到檔案

開始編寫Java

Mendix 會自動生成檔案,格式如下:

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à , etc. are supported in comments.

package usermanager.actions;

import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

public class Java_action extends CustomJavaAction<java.util.List<IMendixObject>>
{
	public Planner(IContext context)
	{
		super(context);
	}

	@java.lang.Override
	public java.util.List<IMendixObject> executeAction() throws Exception
	{
		// BEGIN USER CODE
		throw new com.mendix.systemwideinterfaces.MendixRuntimeException("Java action was not implemented");
		// END USER CODE
	}

	/**
	 * Returns a string representation of this action
	 */
	@java.lang.Override
	public java.lang.String toString()
	{
		return "Java_action";
	}

	// BEGIN EXTRA CODE
	// BEGIN EXTRA CODE

}

要注意,編寫的部分要在 // BEGIN USER CODE// END USER CODE// BEGIN EXTRA CODE// BEGIN EXTRA CODE 之間!

Mendix Java API

  1. 前往 com.mendix.core.Core 資料夾,在這個資料夾中有會使用到的函數(function)

3.要連結Domain model中的實體(entity)時,需使用Mendix設定好的proxies,這可以在javasource > usermanager.proxies 找到,另外,每個實體在IMendixObject中都有自己的constructors、 getters和 setters 可以使用

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à , etc. are supported in comments.

package usermanager.actions;

import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;

public class Java_action extends CustomJavaAction<java.util.List<IMendixObject>>
{
	private IMendixObject __Parameter;
	private usermanager.proxies.Employee Parameter;
    private IMendixObject __Parameter_2;

	public Planner(IContext context, IMendixObject Parameter, IMendixObject Parameter_2)
	{
		super(context);
		this.__Parameter = Parameter;
		this.__Parameter_2 = Parameter_2;
	}

	@java.lang.Override
	public java.util.List<IMendixObject> executeAction() throws Exception
	{
		this.Parameter = __Parameter == null ? null : usermanager.proxies.Employee.initialize(getContext(), __Parameter);

		// BEGIN USER CODE
		throw new com.mendix.systemwideinterfaces.MendixRuntimeException("Java action was not implemented");
		// END USER CODE
	}

	/**
	 * Returns a string representation of this action
	 */
	@java.lang.Override
	public java.lang.String toString()
	{
		return "Java_action";
	}

	// BEGIN EXTRA CODE
	// END EXTRA CODE
}

這篇文章只有簡單介紹Mendix上的Java使用方式,但其實Mendix上的Java有很多不同的使用方法,關於更深入的Java使用方式可以參考官方文件


上一篇
Mendix上要不要做自己的Widget呢?
下一篇
誰說低代碼平台上就不能寫自己的CSS
系列文
Mendix從入門到了解,工程師和業務都要知道低代碼平台是什麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言