Skip to content

Commit 7f31be9

Browse files
committed
Replace Images with code blocks for datastorage API hwo-to (see Code examples provided as images are wrong mendix#8518)
1 parent 2fdca9a commit 7f31be9

6 files changed

Lines changed: 110 additions & 4 deletions

File tree

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

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,37 @@ The microflow to execute the Java action is similar to the previous example, but
182182

183183
Below is the Java code to get the Dataset OQL, execute the OQL, and retrieve the Objects. You use the [Core.createOQLTextGetRequestFromDataSet](https://apidocs.rnd.mendix.com/10/runtime/com/mendix/core/Core.html#createOQLTextGetRequestFromDataSet(java.lang.String)) method to get the OQL query of the Dataset specified.
184184

185-
{{< figure src="/attachments/howto/extensibility/howto-datastorage-api/image043.png" class="no-border" >}}
185+
```java
186+
187+
@Override
188+
public java.util.List<IMendixObject> executeAction() throws Exception
189+
{
190+
// BEGIN USER CODE
191+
ILogNode logger = Core.getLogger( s:"RetrieveDatasetOql");
192+
List<IMendixObject> resultList = new ArrayList<IMendixObject>();
193+
IOQLTextGetRequest oqlGetRequest = Core.createOQLTextGetRequestFromDataSet(this.DataSetName);
194+
String oqlQuery = oqlGetRequest.getQuery();
195+
logger.info(o:"OQL: " + oqlQuery);
196+
IDataTable resultDT Core.retrieveOQLDataTable(getContext(), oqlQuery);
197+
int colCount = resultDT.getSchema().getColumnCount();
198+
resultDT.forEach(row → {
199+
logger.info(o:"Row: " + row.getValue(getContext(), i:0));
200+
IMendixObject obj = Core.instantiate(getContext(), this.ResultEntity);
201+
for (int i = 0; i < colCount; i++) {
202+
String colName = resultDT.getSchema().getColumnSchema(i).getName();
203+
Object colValue = row.getValue(getContext(), i);
204+
if (obj.hasMember(colName)) {
205+
obj.setValue(getContext(), colName, colValue);
206+
} else {
207+
logger.info(String.format("Target entity %s does not have attribute named %s", this.ResultEntity, colName));
208+
}
209+
resultList.add(obj);
210+
}
211+
});
212+
return resultList;
213+
//END USER CODE
214+
}
215+
```
186216

187217
## Retrieving Objects Using SQL
188218

@@ -270,7 +300,26 @@ In Postgres you can query a list of the dates of all Mondays between these dates
270300
271301
For example:
272302
273-
{{< figure src="/attachments/howto/extensibility/howto-datastorage-api/image032.png" class="no-border" >}}
303+
```sql
304+
with first_day_of_month as (
305+
SELECT *
306+
FROM generate_series
307+
( date_trunc('month', '2017-01-24 00:00'::timestamp)
308+
, '2017-11-05 12:00', '1 months'
309+
) as firstday
310+
),
311+
firstmonday as (
312+
select fdom.firstday::date +
313+
((8 - extract(dow from fdom.firstday))::integer % 7)
314+
as first_monday_date
315+
from first_day_of_month as fdom
316+
)
317+
select fm.first_monday_date
318+
from firstmonday as fm
319+
where fm.first_monday_date >= '2017-01-24 00:00'::timestamp
320+
and fm.first_monday_date <= '2017-11-05 12:00'::timestamp
321+
;
322+
```
274323
275324
### Creating the Java Action
276325
@@ -282,15 +331,72 @@ You create a Java action with parameters for the start date and the end date. Yo
282331
283332
1. Specify the required SQL statement in the Java method. JDBC queries expect the parameters to be specified by question marks (?) in the SQL statement.
284333
285-
{{< figure src="/attachments/howto/extensibility/howto-datastorage-api/image034.png" class="no-border" >}}
334+
```java
335+
@Override
336+
public java.util.List<IMendixObject> executeAction() throws Exception
337+
{
338+
// BEGIN USER CODE
339+
String sql=
340+
"with first_day_of_month as ( \n" +
341+
" SELECT * \n" +
342+
" FROM generate_series \n" +
343+
" ( date_trunc('month', ?::timestamp) \n" +
344+
" , ?, '1 months' \n" +
345+
" ) as firstday \n" +
346+
"), \n" +
347+
"firstmonday as ( \n" +
348+
" select fdom.firstday::date + \n" +
349+
" ((8 - extract(dow from fdom.firstday))::integer % 7) \n" +
350+
" as first_monday_date \n" +
351+
" from first_day_of_month as fdom \n" +
352+
") \n" +
353+
"select fm.first_monday_date \n" +
354+
"from firstmonday as fm \n" +
355+
"where fm.first_monday_date >= ?::timestamp \n" +
356+
"and fm.first_monday_date <= ?::timestamp \n" +
357+
";"
358+
;
359+
logger.info("executeAction: " + sql);
360+
```
286361
287362
2. Next, use the Mendix API to execute some statements using the JDBC connection. Here you create a prepared statement, define the JDBC parameter values, and execute the SQL query.
288363
289-
{{< figure src="/attachments/howto/extensibility/howto-datastorage-api/image035.png" class="no-border" >}}
364+
```java
365+
List<IMendixObject> resultList = null;
366+
resultList = Core.dataStorage().executeWithConnection(connection -> [
367+
List<IMendixObject> result = new ArrayList<IMendixObject>();
368+
try {
369+
PreparedStatement stmt = connection.prepareStatement (sql);
370+
// bind start and end date variables
371+
stmt.setDate(1, new java.sql.Date(this.StartDate.getTime()));
372+
stmt.setDate(2, new java.sql.Date(this.EndDate.getTime()));
373+
stmt.setDate(3, new java.sql.Date(this.StartDate.getTime()));
374+
stmt.setDate(4, new java.sql.Date(this.EndDate.getTime()));
375+
ResultSet rset = stmt.executeQuery();
376+
ResultSetMetaData rmd = rset.getMetaData();
377+
```
290378
291379
3. Using the `FirstMondayDate` Java proxy, instantiate a new Mendix object and set the date attribute.
292380
4. Finally, return the created list of dates.
293381
382+
```java
383+
while (rset.next()) {
384+
// create FirstMondayData Mendix entity and add to list
385+
FirstMondayDate dateObj = new hr.proxies.FirstMondayDate(getContext());
386+
result.add(dateObj.getMendixObject());
387+
dateObj.setDate(rset.getDate(1));
388+
logger.debug(String.format("Created object %s", dateObj));
389+
}
390+
} catch (SQLException e) {
391+
logger.error("Failed to execute sql statement: " + e.getMessage());
392+
throw new MendixRuntimeException (e);
393+
}
394+
return result;
395+
});
396+
return resultList;
397+
// END USER CODE
398+
```
399+
294400
{{< figure src="/attachments/howto/extensibility/howto-datastorage-api/image036.png" class="no-border" >}}
295401
296402
When you use this in a microflow, you just need to specify the start and end dates, and the name of the resulting list. This example iterates through all the data objects in the list and prints the date of that object.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)