Show form from Java

1
Hi all, I've been working on implementing a requesthandler that, eventually, will show a new form. I have tried this by calling a microflow from Java which would show the page, but for some reason the page isn't shown. After having done some digging around, I ran into a function called addOpenFormFeedback. The only problem is that I am not able to call this function. I have tried importing the relevant libraries, but neither one of these enables me to call the addOpenFormFeedback function. Can anyone give me a nudge in the right direction and help me by explaining how to open a form from Java? My current code (any help/tips in general are appreciated as well!): public void processRequest(IMxRuntimeRequest mxrequest, IMxRuntimeResponse response, String arg2) throws Exception { String[] pathParts = mxrequest.getResourcePath().split("/"); String queryName = pathParts[pathParts.length-1]; System.out.println(queryName); IContext systemContext = Core.createSystemContext(); String xpathQuery = "//MyFirstModule.procesInfo[procesId='" + queryName + "']"; List<IMendixObject> entityList =Core.retrieveXPathQuery(systemContext, xpathQuery); if(entityList.get(0)!= null){ IMendixObject procesInfoObj =entityList.get(0); List<IMendixObject> accList = Core.retrieveByPath(systemContext, procesInfoObj, "MyFirstModule.procesInfo_Account"); if(accList.get(0) != null){ IMendixObject foundAccount = accList.get(0); Boolean succesfulLogin = LoginHelper.createSession(systemContext, response, foundAccount); if(succesfulLogin) { Map<String,Object> parameters = new HashMap<String,Object>(); parameters.put("procesInfo", procesInfoObj); //response.setStatus(HttpServletResponse.SC_SEE_OTHER); // response.addHeader("location", ".."); //Core.execute(, "MyFirstModule.ExecuteByJava"); System.out.println("Account found!"); } else { System.out.println("Could not login user"); } } else { System.out.println("Nothing found"); } } else { System.out.println("No entities found"); } }
asked
5 answers
2

After logging in redirect to a second requesthandler that opens the form. If you do like above in one java action the browser is not updated with correct session cookies to allow opening a form. In other words: Mendix will not accept you as being logged in.

Edit 1: don't add a requesthandler in this piece of java, it will be added every time this code is touched an will probably result in errors. You can use the deeplink module or add the /new request handler in an after startup mf.

As Pieter says: don't use the systemcontext for initializeSession

See kerberos module for the required cookies:

            response.addCookie(XAS_SESSION_ID, session.getId().toString(), "/", "", -1);
            response.addCookie(XAS_ID, "0."+String.valueOf(Core.getXASId()),"/", "", -1);
            response.addCookie(OriginURI, OriginURIValue, "/","",SECONDS_PER_YEAR);

if you want to pass data to the requesthandler use parameters and set location to /new/?parameter=value

answered
0

Just thought I'd post my current Java code, hoping that someone will read this and be able to help me to eventually show the page.

Basically, what I've tried to do is implement Chris his comment, though I can't quite work it out. My first requesthandler is set up at www.whatever.com/1111/ and handles the checks and login. The second requesthandler is set up at www.whatever.com/1111new/ which does nothing more than call a microflow (in my opinion quite similar to the way Deeplink works) which would show a page.

Any help/input is very much appreciated!

Requesthandler #1:

public class procesIDHandler extends RequestHandler {

@Override
public void processRequest(IMxRuntimeRequest mxrequest,
        IMxRuntimeResponse response, String arg2) throws Exception {

    String[] pathParts = mxrequest.getResourcePath().split("/");
    String queryName = pathParts[pathParts.length-1]; 
    System.out.println(queryName); //Just added this to check queryName to match procesId

    IContext systemContext = Core.createSystemContext();
    String xpathQuery = "//MyFirstModule.procesInfo[procesId='" + queryName + "']";
    List<IMendixObject> entityList =Core.retrieveXPathQuery(systemContext, xpathQuery);
    if(entityList.get(0)!= null){
        IMendixObject procesInfoObj =entityList.get(0);
        List<IMendixObject> accList = Core.retrieveByPath(systemContext, procesInfoObj, "MyFirstModule.procesInfo_Account"); 
        if(accList.get(0) != null){
            IMendixObject foundAccount = accList.get(0);

            Map<String,Object> parameters = new HashMap<String,Object>();
            parameters.put("Account", foundAccount);
            //Execute a microflow to get the user object
            IMendixObject userObject = (Core.execute(systemContext, "MyFirstModule.GetUser", parameters));
            String userName = userObject.getMember(systemContext, "Name").getValue(systemContext).toString(); 
            IUser user = Core.getUser(systemContext, userName);

                //Create new session for the current user
                ISession newSession = Core.initializeSession(systemContext, user, null,"nl_NL");
                response.addCookie(XAS_SESSION_ID, newSession.getId().toString(),"/" ,"" ,-1 );
                response.addCookie("XAS_ID", "0." + Core.getXASId(),"/" ,"" ,-1);

                //Set up second requesthandler to redirect user via a microflow.
                String newHandlerlocation = queryName + "new/";
                Core.addRequestHandler(newHandlerlocation, new secondHandler(systemContext, procesInfoObj));
                response.setStatus(HttpServletResponse.SC_SEE_OTHER);
                //Link to new requesthandler
                response.addHeader("location", "../" + queryName + "new/");

        }
        else {
            System.out.println("Nothing found");
        }
    }
    else {
    System.out.println("No entities found");
    }

}

}

Requesthandler #2:

public class secondHandler extends RequestHandler {

public IContext context;
public IMendixObject procesInfoObj;

public secondHandler(IContext context, IMendixObject procesInfoObj) 
{
    this.context = context;
    this.procesInfoObj = procesInfoObj;
}

@Override
public void processRequest(IMxRuntimeRequest mxrequest,
        IMxRuntimeResponse response, String arg2) throws Exception {

            Map<String, Object> args = new HashMap<String,Object>();
            args.put("procesInfo", procesInfoObj);     
            Core.execute(context, "MyFirstModule.ExecuteByJava", args);
}

}

answered
0

I am trying this as well. A session can be created by using the code below. I made a PendingLink entry so I can call the DeepLink module, and specifically the DeepLink.DeepLinkHome microflow.

When breakpointing, I can see that the microflow that shows the page from the DeepLinkHome is triggered. However, nothing changes on the screen, it stays blank.

Anyone knows what is missing here?

ISession session = Core.initializeSession(context, user, null, "");

response.addCookie("XASSESSIONID", session.getId().toString(), "/", "", -1);

response.addCookie("XASID", "0."+String.valueOf(Core.getXASId()),"/", "", -1);

Core.execute(context, "DeepLink.DeepLinkHome");

answered
0

The deeplink module will create a microflow record that is executed when the user is navigating to their home page.

So when accessing the deeplink module the request handler first creates a record based on the path parameters of the used link and associates this to the user.

Then the user logs in (or is already logged in) and the requesthandler redirects the user to the app.

Now the homepage MF is run and the pending link is retrieved and executed.

The execution takes place after the user has been redirected to the app (index.html).

In your code the user is never redirected to the app. So the microflow will run but will never show the form.

In order for the request handlers you are writing you need to do something similar to the deeplink handler.

Create a record (in Willems case also create the needed parameters in a record).

Then make sure the requesthandler logs in the user and redirects to the index.html (the app) and in the homepage microflow, retrieve the record containing the microflow name and parameters and then execute this microflow from a java action

answered
0

As in the original post, OP asked for general tips as well:

  • Don't use System.out. Use Core.getLogger instead
  • Use the XPath class from communityCommons when doing XPath retrieves
  • Your code seems to accept any input from the user (queryName) and insert it into an XPath query. Since you then do a retrieve using System context you are probably enabling users to query for things they should not have access to.
answered