User Javacode and embed it in Mendix.

I have a Java program which give me the geo-code of a location. I created an object CurrentGeo which holds the latitude and longitude of a location. In my Mendix modeller i created this microflow which passes the CurrentGeo as an object. In the modeller I want to receive this Geoloaction object, which holds two parameters in my program. How do I do that? I changed the java code package whereiam.actions between the // BEGIN USER CODE and // END USER CODE tag: import com.mendix.core.Core; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.webui.CustomJavaAction; import com.mendix.systemwideinterfaces.core.IMendixObject; /** */ public class WhereIAm extends CustomJavaAction<imendixobject> { private Boolean acquireLocation; public Where_I_Am(IContext context, Boolean acquireLocation) { super(context); this.acquireLocation = acquireLocation; } @Override public IMendixObject executeAction() throws Exception { **// BEGIN USER CODE** Position pos = null; float latitude1,longitude2; try { pos = new IPAddressBasedGeoAcquirer().acquireLocation(true); latitude1 = (float)(pos.getCoords().getLatitude()); longitude2 = (float)(pos.getCoords().getLongitude()); } catch (InterruptedException e) { e.printStackTrace(); } *this part must be different i THINK* CurrentGeo.setLatitude(context, latitude1); CurrentGeo.setLongitude(context, Longitude2); Core.commit(context, CurrentGeo.getMendixObject()); Core.execute(context, "Where_I_Am", params);* **// END USER CODE** } /** * Returns a string representation of this action */ @Override public String toString() { return "Where_I_Am"; } // BEGIN EXTRA CODE // END EXTRA CODE }
3 answers

You have 2 options (here I am assuming your domain model has an entity called CurrentGeo with a latitude and longitude attribute):

  • Pass in a new/empty CurrentGeo entity into the microflow, then operate upon it
  • Return a brand new CurrentGeo entity as the return value of the microflow

Option 1: Mendix objects are passed into java actions by reference (just like objects in java). So, you need to create a new CurrentGeo entity in your microflow, then pass it in as a parameter to the java action. In the java action, you can use the setter methods to set the latitude and longitude values. When you return to the microflow, the CurrentGeo object will have the updated info.

Option 2: Create the new CurrentGeo object.

CurrentGeo newG = new CurrentGeo(getContext()

Then operate upon it by calling the setter methods. Finally, return newG by calling:

return newG.getMendixObject()

You should configure the Java action in the modeler to either pass the GeoLocation object as a parameter and fill it there so you can use it in the microflow later, or return it as a result of the action.

Note that you may have more serious issues running this, since you're trying to get the location by IP but without passing any parameters. All I can possibly see this library do is try to get the IP address of the server itself, which would be kinda pointless. You'd have to find a way to obtain and pass the IP of the client.

Or you might be better off using a client widget for this altogether.


That is correct Bas but not my question.

How can I retrieve these parameters to my Mendix object?

The part


    CurrentGeo.setLatitude((double) latitude1);

    CurrentGeo.setLongitude((double) longitude2);

// CurrentGeo.setLatitude(this.getContext(Latitude), latitude1); // CurrentGeo.setLongitude(context, Longitude2); // Core.commit(context, CurrentGeo.getMendixObject()); // Core.execute(context, "WhereIAm", params); // END USER CODE

seems not to work!