Skip to content

Commit 67633dd

Browse files
committed
Copy code from original blog and link to original
1 parent 29e339d commit 67633dd

1 file changed

Lines changed: 143 additions & 141 deletions

File tree

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

Lines changed: 143 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -129,37 +129,37 @@ The Java action illustrated below does the following:
129129
* Loops through all columns of a record and copies the column value to an attribute with the same name. If an attribute with a column name does not exist, a message is printed, and the loop continues
130130
* The Mendix object created is added to the list to be returned
131131

132-
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.
132+
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. (See [RetrieveAdvancedOql.java](https://github.com/ako/QueryApiBlogPost/blob/master/javasource/hr/actions/RetrieveAdvancedOql.java) in the *QueryApiBlogPost* GitHub repo for the full source code).
133133

134134
```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));
135+
@Override
136+
public java.util.List<IMendixObject> executeAction() throws Exception
137+
{
138+
// BEGIN USER CODE
139+
List<IMendixObject> resultList = new ArrayList<IMendixObject>();
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 ResultEnitity 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+
}
156157
}
157-
}
158-
resultList.add(obj);
159-
});
160-
return resultList;
161-
// END USER CODE
162-
}
158+
resultList.add(obj);
159+
});
160+
return resultList;
161+
// END USER CODE
162+
}
163163
```
164164

165165
The result is a generic OQL action that you can use in your microflows as follows:
@@ -180,38 +180,38 @@ The microflow to execute the Java action is similar to the previous example, but
180180

181181
{{< figure src="/attachments/howto/extensibility/howto-datastorage-api/image042.png" class="no-border" >}}
182182

183-
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.
183+
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. (See [RetrieveDatasetOql.java](https://github.com/ako/QueryApiBlogPost/blob/master/javasource/hr/actions/RetrieveDatasetOql.java) in the *QueryApiBlogPost* GitHub repo for the full source code).
184184

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

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-
}
215215
```
216216

217217
## Retrieving Objects Using SQL
@@ -227,46 +227,45 @@ The Java implementation below implements the following steps:
227227
* 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.
228228

229229
```java
230-
@Override
231-
public java.util.List<IMendixObject> executeAction() throws Exception
232-
{
233-
// BEGIN USER CODE
234-
logger.info("executeAction: " + this.Sql);
235-
List<IMendixObject> resultList = null;
236-
resultList = Core.dataStorage().executeWithConnection(connection -> {...});
237-
return resultList;
238-
// END USER CODE
239-
}
230+
@Override
231+
public java.util.List<IMendixObject> executeAction() throws Exception
232+
{
233+
// BEGIN USER CODE
234+
logger.info("executeAction: " + this.Sql);
235+
List<IMendixObject> resultList = null;
236+
resultList = Core.dataStorage().executeWithConnection(connection -> {...});
237+
return resultList;
238+
// END USER CODE
239+
}
240240
```
241241

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

245245
```java
246-
resultList = Core.dataStorage().executeWithConnection(connection -> {
247-
List <IMendixObject> result = new ArrayList<IMendixObject>();
248-
try {
249-
PreparedStatement stmt = connection.prepareStatement(this.Sql);
250-
ResultSet rset = stmt.executeQuery();
251-
ResultSetMetaData rmd = rset.getMetaData();
252-
int colCount = rmd.getColumnCount();
253-
```
246+
resultList = Core.dataStorage().executeWithConnection(connection ->
247+
{
248+
List<IMendixObject> result = new ArrayList<IMendixObject>();
249+
try {
250+
PreparedStatement stmt = connection.prepareStatement(this.Sql);
251+
ResultSet rset = stmt.executeQuery();
252+
ResultSetMetaData rmd = rset.getMetaData();
253+
int colCount = rmd.getColumnCount(); ```
254254

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

257257
```java
258-
while(rset.next()){
259-
IMendixObject obj = Core.instantiate(getContext(),this.ResultEntity);
260-
result.add(obj);
261-
for(int colIdx=1' colIdx <= colCount ; colIdx++){
262-
String colName = rmd.getColumnName (colIndx);
263-
onj.setValue(getContext(), colName,rset.getObject(colIdx));
264-
}
265-
logger.debug(String.format("Created object %s", obj));
266-
}
267-
```
268-
269-
You can find the complete Java source code on GitHub: [RetrieveAdvancedSQL](https://github.com/ako/QueryApiBlogPost/blob/master/javasource/hr/actions/RetrieveAdvancedSql.java).
258+
while(rset.next()){
259+
IMendixObject obj = Core.instantiate(getContext(),this.ResultEntity);
260+
result.add(obj);
261+
for(int colIdx=1; colIdx <= colCount ; colIdx++){
262+
String colName = rmd.getColumnName(colIdx);
263+
obj.setValue(getContext(),colName,rset.getObject(colIdx));
264+
}
265+
logger.debug(String.format("Created object %s", obj));
266+
} ```
267+
268+
You can find the complete Java source code in the *QueryApiBlogPost* GitHub repo on GitHub: [RetrieveAdvancedSQL](https://github.com/ako/QueryApiBlogPost/blob/master/javasource/hr/actions/RetrieveAdvancedSql.java).
270269

271270
You now have a generic SQL action that can be used in microflows to retrieve data from your application database. The query in this example returns the same data as the OQL earlier, so you can reuse the non-persistable entity DepartmentSummary as defined previously.
272271

@@ -329,76 +328,79 @@ You create a Java action with parameters for the start date and the end date. Yo
329328

330329
### Creating the Java Code
331330

331+
{{% alert color="info" %}}
332+
All the code is also available in [CreateDateRangeList.java](https://github.com/ako/QueryApiBlogPost/blob/master/javasource/hr/actions/CreateDateRangeList.java) in the *QueryApiBlogPost* GitHub repo.
333+
{{% /alert %}}
334+
332335
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.
333336

334337
```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);
338+
@Override
339+
public java.util.List<IMendixObject> executeAction() throws Exception
340+
{
341+
// BEGIN USER CODE
342+
String sql =
343+
"with first_day_of_month as (\n " +
344+
" SELECT * \n" +
345+
" FROM generate_series\n" +
346+
" ( date_trunc('month', ?::timestamp)\n" +
347+
" , ?, '1 months'\n" +
348+
" ) as firstday\n" +
349+
"),\n" +
350+
"firstmonday as (\n" +
351+
" select fdom.firstday::date + \n" +
352+
" ((8 - extract(dow from fdom.firstday))::integer % 7) \n" +
353+
" as first_monday_date\n" +
354+
" from first_day_of_month as fdom\n" +
355+
")\n" +
356+
"select fm.first_monday_date\n" +
357+
"from firstmonday as fm\n" +
358+
"where fm.first_monday_date >= ?::timestamp\n" +
359+
"and fm.first_monday_date <= ?::timestamp\n" +
360+
";"
361+
;
362+
logger.info("executeAction: " + sql);
360363
```
361364

362365
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.
363366

364367
```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();
368+
List<IMendixObject> resultList = null;
369+
resultList = Core.dataStorage().executeWithConnection(connection -> {
370+
List<IMendixObject> result = new ArrayList<IMendixObject>();
371+
try {
372+
PreparedStatement stmt = connection.prepareStatement(sql);
373+
// bind start and end date variables
374+
stmt.setDate(1,new java.sql.Date(this.StartDate.getTime()));
375+
stmt.setDate(2, new java.sql.Date(this.EndDate.getTime()));
376+
stmt.setDate(3,new java.sql.Date(this.StartDate.getTime()));
377+
stmt.setDate(4, new java.sql.Date(this.EndDate.getTime()));
378+
ResultSet rset = stmt.executeQuery();
379+
ResultSetMetaData rmd = rset.getMetaData();
377380
```
378381

379382
3. Using the `FirstMondayDate` Java proxy, instantiate a new Mendix object and set the date attribute.
380383
4. Finally, return the created list of dates.
381384

382385
```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
386+
// loop through first monday records
387+
while(rset.next()){
388+
// create FirstMondayDate mendix entity and add to list
389+
FirstMondayDate dateObj = new hr.proxies.FirstMondayDate(getContext());
390+
result.add(dateObj.getMendixObject());
391+
dateObj.setDate(rset.getDate(1));
392+
logger.debug(String.format("Created object %s", dateObj));
393+
}
394+
} catch (SQLException e) {
395+
logger.error("Failed to execute sql statement: " + e.getMessage());
396+
throw new MendixRuntimeException(e);
397+
}
398+
return result;
399+
});
400+
return resultList;
401+
// END USER CODE
398402
```
399403

400-
{{< figure src="/attachments/howto/extensibility/howto-datastorage-api/image036.png" class="no-border" >}}
401-
402404
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.
403405

404406
{{< figure src="/attachments/howto/extensibility/howto-datastorage-api/image037.png" class="no-border" >}}

0 commit comments

Comments
 (0)