Skip to content

Commit 2fdca9a

Browse files
committed
Replace code images with code blocks in datastorage API (pt 1)
1 parent 9e75fee commit 2fdca9a

5 files changed

Lines changed: 62 additions & 4 deletions

File tree

content/en/docs/howto/extensibility/howto-datastorage-api.md

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,36 @@ The Java action illustrated below does the following:
131131

132132
Note that in this case, as show in the domain model screenshot and the OQL screenshot above, the names of the attributes and columns match exactly.
133133

134-
{{< figure src="/attachments/howto/extensibility/howto-datastorage-api/image021.png" class="no-border" >}}
134+
```java
135+
ßOverride
136+
public java.util.List<IMendixObject> executeAction() throws Exception
137+
{
138+
// BEGIN USER CODE
139+
List<IMendixObject> resultList = new ArrayList<~>();
140+
IDataTable resultDT = Core.retrieveOQLDataTable(getContext(), this.OqlQuery);
141+
int colCount = resultDT.getSchema().getColumnCount();
142+
// Loop through records, add to mendix object list
143+
resultDT.forEach(row -> {
144+
// instantiate mendix object as specified by ResultEnititg parameter
145+
IMendixObject obj = Core.instantiate(getContext(), this.ResultEntity);
146+
for (int i = 0; i < colCount; i++) {
147+
// get column name
148+
String colName = resultDT.getSchema().getColumnSchema(i).getName();
149+
// get column value
150+
Object colValue = row.getValue(getContext(), i);
151+
if(obj.hasMember(colName)) {
152+
// set result object value
153+
obj.setValue(getContext(), colName, colValue);
154+
} else {
155+
logger.info(String,format("Target entity does not have attribute named %s",colName));
156+
}
157+
}
158+
resultList.add(obj);
159+
});
160+
return resultList;
161+
// END USER CODE
162+
}
163+
```
135164

136165
The result is a generic OQL action that you can use in your microflows as follows:
137166

@@ -167,16 +196,45 @@ The Java implementation below implements the following steps:
167196

168197
* Use *Core.dataStorage().executeWithConnection()* to execute some Java statements that receive a JDBC connection from the internal connection pool. This API is constructed to enable the Mendix Platform to guarantee that connections are returned to the pool after usage.
169198

170-
{{< figure src="/attachments/howto/extensibility/howto-datastorage-api/image026.png" class="no-border" >}}
199+
```java
200+
@Override
201+
public java.util.List<IMendixObject> executeAction() throws Exception
202+
{
203+
// BEGIN USER CODE
204+
logger.info("executeAction: " + this.Sql);
205+
List<IMendixObject> resultList = null;
206+
resultList = Core.dataStorage().executeWithConnection(connection -> {...});
207+
return resultList;
208+
// END USER CODE
209+
}
210+
```
171211

172212
* With the JDBC connection you can now implement your Java as you would with a regular JDBC connection.
173213
* A prepared statement is created, executed and the resulting records are made available through a `ResultSet`.
174214

175-
{{< figure src="/attachments/howto/extensibility/howto-datastorage-api/image027.png" class="no-border" >}}
215+
```java
216+
resultList = Core.dataStorage().executeWithConnection(connection -> {
217+
List <IMendixObject> result = new ArrayList<IMendixObject>();
218+
try {
219+
PreparedStatement stmt = connection.prepareStatement(this.Sql);
220+
ResultSet rset = stmt.executeQuery();
221+
ResultSetMetaData rmd = rset.getMetaData();
222+
int colCount = rmd.getColumnCount();
223+
```
176224

177225
* Next you loop through all the records in the `ResultSet` and create a Mendix object as specified by the user via ResultEntity.
178226

179-
{{< figure src="/attachments/howto/extensibility/howto-datastorage-api/image028.png" class="no-border" >}}
227+
```java
228+
while(rset.next()){
229+
IMendixObject obj = Core.instantiate(getContext(),this.ResultEntity);
230+
result.add(obj);
231+
for(int colIdx=1' colIdx <= colCount ; colIdx++){
232+
String colName = rmd.getColumnName (colIndx);
233+
onj.setValue(getContext(), colName,rset.getObject(colIdx));
234+
}
235+
logger.debug(String.format("Created object %s", obj));
236+
}
237+
```
180238
181239
You can find the complete Java source code on GitHub: [RetrieveAdvancedSQL](https://github.com/ako/QueryApiBlogPost/blob/master/javasource/hr/actions/RetrieveAdvancedSql.java).
182240
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)