If I just copy/paste my java code in a new Java action, I get quite a few errors (indicated by red lines): see the image below. If I hover over these errors, Eclipse suggests actions, namely to import additional classes.
Afterwards, you imports should look like this:
import java.util.Collection;
import java.util.HashMap;
import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.ISession;
import com.mendix.systemwideinterfaces.core.IUser;
import com.mendix.webui.CustomJavaAction;
These imports fix your compile errors. The other points in my comments above still stand.
The Java method you need is:
Core.getActiveSessions();
You can find the Mendix Server API here.
You could create a microflow which takes a String as input parameter and creates and ActiveSession object for a user. Given such a microflow, you could write Java code like this (NB: on top of my head, without an IDE so will need a bit of fixing):
HashMap<String, Object> params = new HashMap<String, Object>();
Collection<? extends ISession> activeSessions = Core.getActiveSessions();
for (ISession session : activeSessions) {
IUser user = session.getUser(this.getContext());
params.put("UserName", user.getName());
Core.execute(this.getContext(), "YourModule.YourMicroflow", params);
}
You have now created a record for each active session, containing the name of the user of the session.
EDIT:
I have corrected the Java code. Note however that from Mendix 7 Core.getActiveSessions() is deprecated and the suggestion is to query the Sessions table. This means that you should be able to do all of this in a microflow, without any custom Java code: simply retrieve a list of all System.Session objects, iterate over them and create a record for each of these sessions.
Hello Rom,
Thank you for your provided information. it is really usefull. But i still do not get it working.
First i have created the MF in Mendix.
And my code:
// This file was generated by Mendix Business Modeler.
//
// 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 administratie.actions;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;
/**
* Deze java actie haalt de actieve sessies op
*/
public class GetCurrentSessions extends CustomJavaAction<String>
{
private String GetCurrentSessions;
public GetCurrentSessions(IContext context, String GetCurrentSessions)
{
super(context);
this.GetCurrentSessions = GetCurrentSessions;
}
@Override
public String executeAction() throws Exception
{
// BEGIN USER CODE
HashMap<String, Object> params = new HashMap<String, Object>();
Collection<? extends ISession> activeSessions = Core.getActiveSessions();
for (ISession session : activeSessions) {
IUser user = session.getUser(this.getContext());
params.put("UserName", user.getName());
Core.execute(this.getContext(), Administratie.ShowCurrentLoggedInUsers, params);
}
throw new com.mendix.systemwideinterfaces.MendixRuntimeException("Java action was not implemented");
// END USER CODE
}
/**
* Returns a string representation of this action
*/
@Override
public String toString()
{
return "GetCurrentSessions";
}
// BEGIN EXTRA CODE
// END EXTRA CODE
}
The errors i get:
compile:
[javac] Compiling 4 source files to D:\Users\XXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\deployment\run\bin
[javac] D:\Users\XXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:33: error: cannot find symbol
[javac] HashMap<String, Object> params = new HashMap<String, Object>();
[javac] ^
[javac] symbol: class HashMap
[javac] location: class GetCurrentSessions
[javac] D:\Users\XXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:33: error: cannot find symbol
[javac] HashMap<String, Object> params = new HashMap<String, Object>();
[javac] ^
[javac] symbol: class HashMap
[javac] location: class GetCurrentSessions
[javac] D:\Users\XXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:34: error: cannot find symbol
[javac] Collection<? extends ISession> activeSessions = Core.getActiveSessions();
[javac] ^
[javac] symbol: class Collection
[javac] location: class GetCurrentSessions
[javac] D:\Users\XXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:34: error: cannot find symbol
[javac] Collection<? extends ISession> activeSessions = Core.getActiveSessions();
[javac] ^
[javac] symbol: class ISession
[javac] location: class GetCurrentSessions
[javac] D:\Users\XXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:34: error: cannot find symbol
[javac] Collection<? extends ISession> activeSessions = Core.getActiveSessions();
[javac] ^
[javac] symbol: variable Core
[javac] location: class GetCurrentSessions
[javac] D:\Users\XXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:35: error: cannot find symbol
[javac] for (ISession session : activeSessions) {
[javac] ^
[javac] symbol: class ISession
[javac] location: class GetCurrentSessions
[javac] D:\Users\XXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:36: error: cannot find symbol
[javac] IUser user = session.getUser(this.getContext());
[javac] ^
[javac] symbol: class IUser
[javac] location: class GetCurrentSessions
[javac] D:\Users\XXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:38: error: cannot find symbol
[javac] Core.execute(this.getContext(), Administratie.ShowCurrentLoggedInUsers, params);
[javac] ^
[javac] symbol: variable Administratie
[javac] location: class GetCurrentSessions
[javac] D:\Users\XXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:38: error: cannot find symbol
[javac] Core.execute(this.getContext(), Administratie.ShowCurrentLoggedInUsers, params);
[javac] ^
[javac] symbol: variable Core
[javac] location: class GetCurrentSessions
[javac] Note: D:\Users\XXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\com\mendix\core\Core.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 9 errors
What must i do from here on?
Thank you!
I have downloaded IntelliJ IDEA and Ecplise also Brackets. Unfortunately i do not get these errors you mentioned which will help me to import the correct modules in the java code. Can you maybe share these errors you mentioned? and what i need to refactor to this code in order to get this working. Maybe sharing your code what you see on your eclipse in total?
Thank you
I have included these imports in my code. Currently it is still not working. But it started with 9 erros.Right know i got 2 errors:
compile:
[javac] Compiling 4 source files to D:\Users\XXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\deployment\run\bin
[javac] D:\Users\XXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:38: error: method getUser in interface ISession cannot be applied to given types;
[javac] IUser user = session.getUser(this.getContext());
[javac] ^
[javac] required: no arguments
[javac] found: IContext
[javac] reason: actual and formal argument lists differ in length
[javac] D:\Users\XXXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:40: error: cannot find symbol
[javac] Core.execute(this.getContext(), Administratie.GetCurrentSessionsWithJava, params);
[javac] ^
[javac] symbol: variable Administratie
[javac] location: class GetCurrentSessions
[javac] Note: D:\Users\XXXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\com\mendix\core\Core.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 2 errors
BUILD FAILED
D:\Users\XXXXXX\Documents\WIJZ - New-branch - ProductieIncidenten v6\deployment\build_core.xml:27: Compile failed; see the compiler error output for details.
Total time: 11 seconds
my code
package administratie.actions;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.ISession;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import java.util.Collection;
import java.util.HashMap;
import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IUser;
/**
* Deze java actie haalt de actieve sessies op
*/
public class GetCurrentSessions extends CustomJavaAction<String>
{
public GetCurrentSessions(IContext context)
{
super(context);
}
@Override
public String executeAction() throws Exception
{
// BEGIN USER CODE
HashMap<String, Object> params = new HashMap<String, Object>();
Collection<? extends ISession> activeSessions = Core.getActiveSessions();
for (ISession session : activeSessions) {
IUser user = session.getUser(this.getContext());
params.put("UserName", user.getName());
Core.execute(this.getContext(), Administratie.GetCurrentSessionsWithJava, params);
}
// END USER CODE
}
/**
* Returns a string representation of this action
*/
@Override
public String toString()
{
return "GetCurrentSessions";
}
// BEGIN EXTRA CODE
// END EXTRA CODE
}
i am still figuring out what to do here. If you have any tips, it would be wonderfull!
i have managed to get the two errors to one error:
compile:
[javac] Compiling 4 source files to D:\Users\Skucukde\Documents\WIJZ - New-branch - ProductieIncidenten v6\deployment\run\bin
[javac] D:\Users\Skucukde\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:40: error: cannot find symbol
[javac] Core.execute(this.getContext(), Administratie.GetCurrentSessionsWithJava, params);
[javac] ^
[javac] symbol: variable Administratie
[javac] location: class GetCurrentSessions
[javac] Note: D:\Users\Skucukde\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\com\mendix\core\Core.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 1 error
i have managed to get the two errors to one error
compile:
[javac] Compiling 4 source files to D:\Users\Skucukde\Documents\WIJZ - New-branch - ProductieIncidenten v6\deployment\run\bin
[javac] D:\Users\Skucukde\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\administratie\actions\GetCurrentSessions.java:40: error: cannot find symbol
[javac] Core.execute(this.getContext(), Administratie.GetCurrentSessionsWithJava, params);
[javac] ^
[javac] symbol: variable Administratie
[javac] location: class GetCurrentSessions
[javac] Note: D:\Users\Skucukde\Documents\WIJZ - New-branch - ProductieIncidenten v6\javasource\com\mendix\core\Core.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 1 error
Hi Rom,
I have managed to get zero Errors:
i have changed a bit the code. Can you review this?
// This file was generated by Mendix Business Modeler.
//
// 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 administratie.actions;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.ISession;
import com.mendix.systemwideinterfaces.core.IUser;
import java.util.Collection;
import java.util.HashMap;
/**
* Deze java actie haalt de actieve sessies op
*/
public class GetCurrentSessions extends CustomJavaAction<String>
{
public GetCurrentSessions(IContext context)
{
super(context);
}
@Override
public String executeAction() throws Exception
{
// BEGIN USER CODE
HashMap<String, Object> params = new HashMap<String, Object>();
Collection<? extends ISession> activeSessions = Core.getActiveSessions();
for (ISession session : activeSessions) {
IUser user = session.getUser();
params.put("UserName", user.getName());
Core.execute(this.getContext(), "Administratie.ShowCurrentLoggedInUsers", params);
}
throw new com.mendix.systemwideinterfaces.MendixRuntimeException("Java action was not implemented");
// END USER CODE
}
/**
* Returns a string representation of this action
*/
@Override
public String toString()
{
return "GetCurrentSessions";
}
// BEGIN EXTRA CODE
// END EXTRA CODE
}
thank you
Hi Rom,
Would it also be possible to return a list instead of an String?
Kind Regards,
Sefa Kucukdemirci
Hi Rom,
Would it also be possible to return a list instead of an String?
Kind Regards,
Sefa Kucukdemirci