Extend Mendix with custom Java

I want to build functionality which can't be modeled with Microflows. Can I extend Mendix with Java code? Is it possible to use model elements within Java? For example: retrieve objects, call business rules, etc.
3 answers

Yes, you can! Beneath the "Resources" node within a module new Java actions can be created which can subsequently be used as microflow action calls. For each Java action a name, input parameters and a return value should be specified. This will correspond with the class name, constructor parameters and return value of the executeAction() method of the generated Java action (Java actions will be generated on deploy of the modeler project file).

Metamodel objects and enumerations can be used in the Java action by using proxy classes (these will also be generated automatically on deploy). These proxy classes will also provide easy access to attributes and references of the corresponding metaobject.

Retrieving, changing, committing, deleting of objects and executing java actions, microflows and business rules can be achieved by using the XAS Core API. Retrieving, changing, committing and deleting is performed by calling Core.retrieveXpathQuery()/Core.retrieveId, Core.change, Core.commit and Core.remove respectively. For executing java actions, microflows and business rules use Core.execute, provide the name of the module and the action/microflow/business rule name here.

A full description of the XAS API can be found in the javadoc which can be found in the folder "javadoc" in the root of your project.


This is all possible. You can use Java actions in Microflows. From within Java code, you can also retrieve objects, call business rules, etc.

Here's an example on how to call a microflow named DecryptPassword from Java. This will pass an encryptedPassword string to a microflow with a string named encryptedPassword as input parameter, and retrieves the decryptedPassword string that the microflow has as a return value.

private String decryptPassword(String encryptedPassword, IContext context) throws CoreException  
        Map<String,Object> parameters = new HashMap<String, Object>();
        parameters.put("encryptedPassword", encryptedPassword);
        String decryptedPassword = (String) Core.execute(context, "ExFunc.DecryptPassword", parameters);
        return decryptedPassword;

Example code for retrieving Mendix objects from Java using an Xpath query (split out for readability):

String calendarItemMeta = "ExFunc.CalendarItem";                        //string for xpath query
String uniqueIdForUrlMeta = "UniqueIdForUrl";                           //string for xpath query
String folderNameMetaString = "ExFunc.GenericObject_Folder/ExFunc.Folder/FolderId";     //string for xpath query

List<IMendixObject> calendarItemList;
calendarItemList = Core.retrieveXPathQueryEscaped(thisContext, "//%s[%s='%s' and %s='%s']", calendarItemMeta, uniqueIdForUrlMeta, eventDTO.getUniqueIdForUrl(), folderNameMetaString, exchangeFolder.getFolderId());

Two times a yes. Mendix Microflow DSL is extensible with 3rd generation programming languages, also with Java. Within micoflow there is an activity Java action (in the category 'Action Call'). For each Java Action the XAS generate a template file witch can be edited in some programming enviroment.

From Java code you can talk with the Mendix API. The Core API provides many methods witch make you live much easier. The core provides different methods for retrieving objects (data). You can retrieve objects by XPath, OQL, e.t.c.