Skip to content

Commit ba373f4

Browse files
Merge pull request #10050 from mendix/kv-code-images
Replace images with code snippets
2 parents ea5e76e + f4ac69b commit ba373f4

8 files changed

Lines changed: 222 additions & 56 deletions

File tree

content/en/docs/howto/extensibility/howto-connector-kit.md

Lines changed: 111 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ weight: 80
88

99
## Introduction
1010

11-
Mendix provides the tools to enable Java developers to easily add powerful and robust new microflow actions to their Mendix toolbox. These microflow actions can be shared in the Mendix Marketplace, so anyone can benefit from them without having to know Java. This is particularly useful when building connectors to services.
11+
Mendix offers tools that enable Java developers to seamlessly add powerful, and robust new microflow actions to their Mendix toolbox. You can share these microflow actions in the Mendix Marketplace allowing anyone to benefit from them without needing Java knowledge. This is especially useful when building connectors to services.
1212

13-
The diagram below illustrates the power of Mendix's integration with Java. It shows a Mendix Slack bot that enables users to determine things and people in pictures taken with a mobile Slack app:
13+
The diagram below highlights the power of Mendix's integration with Java. It shows a Mendix Slack bot that enables users to identify objects and people in images taken with the mobile Slack app.
1414

1515
{{< figure src="/attachments/howto/extensibility/howto-connector-kit/slack-rekogition-bot-architecture.png" alt="Slack Rekognition Bot design" class="no-border" >}}
1616

17-
The Mendix application consists of a small number of microflows that use Mendix microflow actions to offer a conversational user interface using [Slack](https://slack.com/) and different Amazon services: [S3](https://aws.amazon.com/s3/), [Rekognition](https://aws.amazon.com/rekognition/), and [Lex](https://aws.amazon.com/lex/).
17+
The Mendix application consists of a few microflows that use Mendix microflow actions to provide a conversational user interface through [Slack](https://slack.com/) and various Amazon services: [S3](https://aws.amazon.com/s3/), [Rekognition](https://aws.amazon.com/rekognition/), and [Lex](https://aws.amazon.com/lex/).
1818

19-
This image shows what the microflow toolbox looks like after including all the modules that provide connectors to the services used:
19+
This image below illustrates the microflow toolbox after including all the modules that provide connectors to the services used:
2020

2121
{{< figure src="/attachments/howto/extensibility/howto-connector-kit/slack-rekogition-bot-toolkit.png" alt="Slack Rekognition bot toolbox" class="no-border" >}}
2222

2323
For the basics of building toolbox actions, see the blog post [Introducing the Mendix Connector Kit](https://www.mendix.com/blog/introducing-mendix-connector-kit/).
2424

25-
This how-to teaches you how to do the following:
25+
This document shows you how to do the following:
2626

2727
* Use advanced features when creating your own microflow actions
2828
* Create the **Create object list** action
@@ -31,7 +31,7 @@ This how-to teaches you how to do the following:
3131

3232
## Creating Generic Actions Using Type Parameters
3333

34-
Let's start with type parameters. In the **Type parameters** tab in the Java action definition dialog box, you can use a type parameter if you want to ensure that certain parameters of your action share the same entity but you do not know the name of this entity when defining the actions.
34+
In the **Type parameters** tab of the Java Action definition dialog box, you can use a type parameter to ensure that certain parameters of your action share the same entity, even if you do not yet know the name of that entity when defining the action.
3535

3636
For example, suppose you want to create an action that takes two objects of the same entity and returns a list containing both objects. You can use a type parameter to guarantee that both the input parameters for specifying the objects and the resulting list all use the same entity.
3737

@@ -59,11 +59,42 @@ To create a generic action using type parameters, follow these steps:
5959

6060
The Java implementation still uses strings to specify the name of an entity, which means that you can upgrade your existing Java actions to use these new parameter types without having to refactor your existing code.
6161

62-
Finally, here's the actual Java implementation of the action defined:
62+
Finally, here is the actual Java implementation of the action defined:
6363

64-
{{< figure src="/attachments/howto/extensibility/howto-connector-kit/join_objects_javacode.png" alt="Java implementation join object" class="no-border" >}}
64+
```java
65+
public class JoinObjectsInList extends UserAction<java.util.List<IMendixObject>>
66+
{
67+
private final java.lang.String Entity;
68+
private final IMendixObject Object1;
69+
private final IMendixObject Object2;
70+
71+
public JoinObjectsInList(
72+
IContext context,
73+
java.lang.String _entity,
74+
IMendixObject _object1,
75+
IMendixObject _object2
76+
)
77+
{
78+
super(context);
79+
this.Entity = _entity;
80+
this.Object1 = _object1;
81+
this.Object2 = _object2;
82+
}
83+
84+
@Override
85+
public java.util.List<IMendixObject> executeAction() throws Exception
86+
{
87+
// BEGIN USER CODE
88+
List<IMendixObject> resultList = new ArrayList<>();
89+
resultList.add(Object1);
90+
resultList.add(Object2);
91+
return resultList;
92+
// END USER CODE
93+
}
94+
}
95+
```
6596

66-
You now have a reusable action in your toolbox that will join two objects into a list as illustrated by this example:
97+
You now have a reusable action in your toolbox that will join two objects into a list as shown by the example below:
6798

6899
{{< figure src="/attachments/howto/extensibility/howto-connector-kit/join_objects_use.png" alt="Join objects use" class="no-border" >}}
69100

@@ -75,7 +106,7 @@ The following example illustrates how you can use microflow parameters. The micr
75106

76107
{{< figure src="/attachments/howto/extensibility/howto-connector-kit/init-loop.png" alt="Init loop" class="no-border" >}}
77108

78-
Here's an alternative to the microflow above that uses a custom Java action to replace the loop, instantiation, and initialization of the objects with a Java action:
109+
Here is an alternative to the above microflow that uses a custom Java action to replace the loop, instantiation, and initialization of the objects with a Java action:
79110

80111
{{< figure src="/attachments/howto/extensibility/howto-connector-kit/init-list-use.png" alt="Init list loop with action" class="no-border" >}}
81112

@@ -92,24 +123,44 @@ As you can see below, this action uses a new parameter type (**Microflow**) to i
92123

93124
{{< figure src="/attachments/howto/extensibility/howto-connector-kit/initialize_list_mf_pars.png" alt="Initialize list using microflow action parameters" class="no-border" >}}
94125

95-
In the Java implementation for this action, you'll see the following details for the parameters:
126+
In the Java implementation for this action, see the following details for the parameters:
96127

97128
* **ResultEntity** – a string with the entity name used for the default object and the result list
98-
* **DefaultObject** – an IMendixObject instance containing the default object
129+
* **DefaultObject** – an `IMendixObject` instance containing the default object
99130
* **InitializationMicroflow** – a string containing the name of the initializing microflow
100131
* **ListSize** – a long variable containing the number of objects desired in the list
101132

102-
{{< figure src="/attachments/howto/extensibility/howto-connector-kit/initilialize_list_java_1.png" alt="Initialize list java implementation 1" class="no-border" >}}
133+
```java
134+
private final java.lang.String ResultEntity;
135+
private final IMendixObject DefaultObject;
136+
private final java.lang.String InitializationMicroflow;
137+
private final java.lang.Long ListSize;
138+
139+
public CreateObjectList(
140+
IContext context,
141+
java.lang.String _resultEntity,
142+
IMendixObject _defaultObject,
143+
java.lang.String _initializationMicroflow,
144+
java.lang.Long _listSize
145+
)
146+
{
147+
super(context);
148+
this.ResultEntity = _resultEntity;
149+
this.DefaultObject = _defaultObject;
150+
this.InitializationMicroflow = _initializationMicroflow;
151+
this.ListSize = _listSize;
152+
}
153+
```
103154

104-
The `executeAction` method is where all the magic happens:
155+
The `executeAction` method handles the main functionality:
105156

106-
1. It initializes an ArrayList for the result.
107-
2. It has a for-loop to create the desired number of objects.
108-
3. The objects are created using `Core.instantiate()`. The entity name specified in the action is used as the input to specify what entity to instantiate.
109-
4. The system determines if a default object was specified. If so, it copies all the attribute values to the new object.
110-
5. The system executes the initialization microflow using `Core.microflowCall()`.
157+
1. It initializes an `ArrayList` to store the results.
158+
2. A `for` loop runs to create the desired number of objects.
159+
3. Each object is created using `Core.instantiate()` with the entity name specified in the action used as the input to specify which entity to instantiate.
160+
4. If a default object is defined, its attribute values are copied to the new object.
161+
5. The system then executes the initialization microflow using `Core.microflowCall()`.
111162
6. The newly instantiated and initialized object is added to the result list.
112-
7. The list of new objects is returned.
163+
7. Finally, the method returns the list of newly created objects.
113164

114165
```java
115166
@Override
@@ -161,13 +212,13 @@ Microflow parameters are especially useful for handling events. For example, the
161212

162213
## Using Import and Export Mappings
163214

164-
Now we will discuss an example of how you can use mappings in your Java actions. In this example, you'll create an action to import a string using an import mapping. This is not particularly useful, seeing there is a default action in your toolbox already that provides this functionality called **Import with mapping**. However, as an example, it illustrates how to use mappings.
215+
In this section, you can see how to use mappings in your Java actions. In this example, you will create an action to import a string using an import mapping. This is not particularly useful, seeing there is a default action in your toolbox already that provides this functionality called **Import with mapping**. However, as an example, it illustrates how to use mappings.
165216

166-
This is an image of what we are building: an action to import JSON strings:
217+
This is an image of what we are building: an action to import JSON strings.
167218

168219
{{< figure src="/attachments/howto/extensibility/howto-connector-kit/example_import_string_use.png" alt="Example import string use" class="no-border" >}}
169220

170-
The action requires the user to provide a string with the JSON to import, select an import mapping, and define the entity of the result. Finally, a name needs to be provided for the result of the import mapping.
221+
The action requires the user to provide a string with the JSON to import, select an import mapping, and define the entity of the result. Finally, provide a name for the result of the import mapping.
171222

172223
The action is defined as follows:
173224

@@ -180,22 +231,54 @@ The action is defined as follows:
180231

181232
Implement the action in Java as follows:
182233

183-
1. Create an InputStream from the JSON input so it can be read by the import mapping.
234+
1. Create an `InputStream` from the JSON input so it can be read by the import mapping.
184235
2. Use `Core.integration().importStream()` to import the JSON with the specified mapping.
185236
3. Return the first object imported.
186237

187-
{{< figure src="/attachments/howto/extensibility/howto-connector-kit/import_string_java.png" alt="Import String Java action" class="no-border" >}}
238+
```java
239+
public ImportString(
240+
IContext context,
241+
java.lang.String InputString,
242+
java.lang.String ImportMapping,
243+
java.lang.String ResultEntity
244+
)
245+
{
246+
super(context);
247+
this.InputString = InputString;
248+
this.ImportMapping = ImportMapping;
249+
this.ResultEntity = ResultEntity;
250+
}
251+
252+
@Override
253+
public IMendixObject executeAction() throws Exception
254+
{
255+
// BEGIN USER CODE
256+
try (InputStream is = new ByteArrayInputStream(this.InputString.getBytes(StandardCharsets.UTF_8))) {
257+
258+
// Import the string by executing the mapping
259+
List<IMendixObject> objects = Core.integration().importStream(getContext(), is, this.ImportMapping, null, false);
260+
261+
// Return first object created in mapping
262+
return objects.get(0);
263+
264+
} catch (Exception e) {
265+
logger.error(e);
266+
throw new MendixRuntimeException(String.format("Failed to import JSON string: ", e.getMessage()));
267+
}
268+
// END USER CODE
269+
}
270+
```
188271

189-
## Some Development Tips
272+
## Development Tips
190273

191274
### Unit Testing
192275

193-
When developing connector modules, you can use the unit test module to test the actions you are implementing.
276+
When developing connector modules, you can use the [Unit Testing](https://marketplace.mendix.com/link/component/390) module to test the actions you are implementing.
194277

195278
If you want to publish your module with custom microflow actions to the Mendix Marketplace for easy reuse, it's best to have a module containing only the reusable parts. Add another module to your app with all the test microflows and anything else you need
196279
while developing your application.
197280

198-
In the screenshot below, observe two important points. First, the **ConnectorKitDemo** module only contains the actions you want to publish to the Marketplace. To do this, right-click the module and select **Export module package...**. Second, the **ConnectorKitDemoTests** module contains all the functionality you need while developing the reusable module: a small domain model with some sample data and some test pages. It also contains the unit test microflow **Test_InitProduct**, which will be called by the unit test module.
281+
In the screenshot below, observe two important points. First, the **ConnectorKitDemo** module only contains the actions you want to publish to the Marketplace. To do this, right-click the module and select **Export module package...**. Second, the **ConnectorKitDemoTests** module contains all the functionality you need while developing the reusable module: a small domain model with some sample data and some test pages. It also contains the unit test microflow **Test_InitProduct**, which will be called by the [Unit Testing](/appstore/modules/unit-testing/) module.
199282

200283
### Managing Libraries
201284

0 commit comments

Comments
 (0)