Skip to content

Instantly share code, notes, and snippets.

@stephanbruijnis
Last active November 8, 2022 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephanbruijnis/237df04a88abab2a921f04b6e43d9656 to your computer and use it in GitHub Desktop.
Save stephanbruijnis/237df04a88abab2a921f04b6e43d9656 to your computer and use it in GitHub Desktop.
Retrieves the objects of the type of ReturnEntity based on the identifiers retrieved in the SQL query
// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.
package myfirstmodule.actions;
import com.mendix.core.Core;
import com.mendix.core.CoreException;
import com.mendix.logging.ILogNode;
import com.mendix.systemwideinterfaces.MendixRuntimeException;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixIdentifier;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.systemwideinterfaces.core.meta.IMetaObject;
import com.mendix.systemwideinterfaces.core.meta.IMetaPrimitive;
import com.mendix.webui.CustomJavaAction;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Executes the SQL query
*
* Retrieves the objects of the type of ReturnEntity based on the identifiers retrieved in the SQL query.
*
* The query should only return the identifier (id column) of the entity you wish to retrieve. For example the planning$day.id and the JavaAction will retrieveById these objects and return all Day objects matching the SQL query
*
*/
public class RetrieveObjectsByIdAdvancedSql extends CustomJavaAction<java.util.List<IMendixObject>>
{
private java.lang.String Sql;
private java.lang.String ReturnEntity;
public RetrieveObjectsByIdAdvancedSql(IContext context, java.lang.String Sql, java.lang.String ReturnEntity)
{
super(context);
this.Sql = Sql;
this.ReturnEntity = ReturnEntity;
}
@java.lang.Override
public java.util.List<IMendixObject> executeAction() throws Exception
{
// BEGIN USER CODE
logger.debug("executeAction: " + this.Sql);
List<IMendixObject> resultList = null;
resultList = Core.dataStorage().executeWithConnection(connection ->
{
List<IMendixObject> objects = new ArrayList<IMendixObject>();
List<IMendixIdentifier> resultIDs = new ArrayList<IMendixIdentifier>();
try {
PreparedStatement stmt = connection.prepareStatement(this.Sql);
ResultSet rset = stmt.executeQuery();
while(rset.next()) {
resultIDs.add(Core.createMendixIdentifier(rset.getLong(1)));
}
}
catch (SQLException e) {
logger.error("Failed to execute sql statement: " + e.getMessage());
throw new MendixRuntimeException(e);
}
try {
objects = Core.retrieveIdList(getContext(), resultIDs);
}
catch (CoreException e) {
logger.error("Failed to retrieve objects by id: " + e.getMessage());
}
return objects;
});
return resultList;
// END USER CODE
}
/**
* Returns a string representation of this action
*/
@java.lang.Override
public java.lang.String toString()
{
return "RetrieveObjectsByIdAdvancedSql";
}
// BEGIN EXTRA CODE
private final ILogNode logger = Core.getLogger(this.getClass().getName());
// END EXTRA CODE
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment