Customizing Java Actions | Mendix

Published in
6 min readMay 4, 2021

--

The ability to add Java on top of the existing functionalities in Mendix is like the cherry on top of the cake for Java Geeks. On the other hand, being able to customize existing code is truly a blessing for beginners or the ones like me who are not so comfortable with Java!

The Community Commons Function Library module provides a number of reusable Java methods for our apps, which can be called from microflows or other custom Java actions. Most of them are generic while a few are specific, but we have the possibility to adjust them all based on our needs. It’s all Java from here, but don’t you worry, we’ve got it!

Use Case

I will walk you through customizing java actions by using an example I faced.

The situation was to set the value of an attribute PrevStatus of type “Enumeration” based on a “String” type attribute returned by another object.

Case File Entity and Enum used for PrevStatus

Approach

  1. One of the approaches would be using “If-Else” via multiple decision activities to assign the Enumeration value based on which String it matches. However, there were lot of Statuses to be checked against and this solution wasn’t satisfying enough to proceed with doing all the hard work.
Left : Main microflow used to set Case to PrevStatus, Right : Submicroflow showing ‘If-Else’ approach to decide Prevstatus

2. Thus, came the second idea: work smarter, and look for a java action which we could reuse or adapt. In Community commons (downloaded from Marketplace) there is an action named “EnumerationFromString”. Although there was an issue with us using this action, because it is pre-configured to return/check for an Enum of LogLevel(an existing Enumeration in community commons module) by default. Using the action felt like the right approach though and we decided to adjust the values in the java code as per our desired Enum type.

https://bit.ly/MXW21

So, let’s begin to mess with the Java

Here’s a look at the Microflow created to set PrevStatus of the CaseFile object. The first activity returns the String value to be used for PrevStatus, next is the Java action named ‘EnumerationFromString’ in which the value returned from the first activity is passed as a parameter and last is the change object activity to set the CaseFile attributes wherein the value of PrevStatus is set as the Enumeration value returned from the Java action.

The Java action takes a string as Parameter and returns Enum of type LogLevel

Step 1 : Open “EnumerationFromString” Java action in Mendix and change return type to your desired Enumeration type.

Adjusting return value from LogLevel to CaseStatus Enumeration

Step 2: Navigate to the Java file of this action in your project directory.

Java action Path

Step 3: Opening the java file : Mostly java files are edited, compiled and debugged in Eclipse but if you are not comfortable with it as a beginner , you can open this file in any file editor you are comfortable with and in case of any errors, they will be displayed when running the application. (In this example the file is opened in Notepad++)

Here’s how the code behind the Java action looks like :

Java File of “EnumerationFromString” Java action

In java files only the code written between begin-end code blocks and the imports can be customized and therefore would be executed, everything else will be overwritten at run time.

Step 4: As mentioned in the comment under Begin Code in the image above, we can replace the default LogLevel with the Enumeration we want. That’s cool, Right?

To configure it for our Enumeration we just need to make a few adjustments:

a) In the import part at top, replace “communitycommons.proxies.LogLevel” with “main.proxies.Enum_CaseStatus” (modulefolder.proxies.enumname)

You can find your Enumeration by navigating to the module’s folder path in the javasource folder as shown below:

Path to Enumeration

b) The next and Final Step is to replace all occurrences of ‘LogLevel’ between Begin and End Code with our Enumeration name, the one we just added above

Customized Java File of EnumerationFromString action

And Voila! We’re Done

Save the file and Start/Run the app in Studio Pro to ensure code compilation and check for the desired results.

Insights into the Code

a) The import is added to make our Enum accessible to the Java action.

b) Result variable present between Begin and End code stores the result returned from function “enumFromString” that accepts 2 parameters:

  • Enumeration Class(since Enum is a class hence EnumName.class is passed)
  • and the String that needs to be converted.

The function is pre-defined in an existing community commons java file named ‘Misc’ (that’s why communitycommons.Misc is imported at top). Finally, the name of Enumeration value is returned as result.

Conclusion

Java can become our friend if we can try understanding it just a little bit better. Jokes apart, I hope this gave you some insight on how customizing Java actions in Mendix is achievable. Keeping a few things in mind would also help you to modify other java actions wherever feasible, or even to create an action of your own by taking some queues from existing ones.

Small steps will eventually lead to us climbing the mountain and who knows where the journey will take us if one befriends Java.

NOTE : This is an example of how I approached a particular scenario, there may be multiple other ways to achieve this but consuming and customizing the Java action was a learning for me.

From the Publisher -

If you enjoyed this article you can find more like it at our Medium page or at our own Community blog site.

For the makers looking to get started, you can sign up for a free account, and get instant access to learning with our Academy.

Interested in getting more involved with our community? You can join us in our Slack community channel or for those who want to be more involved, look into joining one of our Meet ups.

--

--