XPath retrieve with attribute name stored in string variable.

2
Hello,  I’m using the Mx Model Reflection Module to monitor my entities and attributes.  The user can craft “custom searches” using that module to select the (1) entity he wants to search in, the (2) attribute he wants to look closer at, and a (3) threshold which will filter the output. I’m now facing trouble implementing that feature.  I’m stuck at the retrieve from my database. The Challenge is that I want to filter my retrieve according to the selected attribute and threshold. Here I’m having trouble defining the attribute to filter on in XPath. The Name of the Attribute is stored in a string variable in an model reflection entity. Hence I can’t retrieve based on that value since it’s not recognizable/recognized as an attribute. To make it more clear:  Normally, you can filter your retrieve like this: [Name = ‘Test’] or [CustomerNo < 120] What I want to do is the following: [«Name of attribute stored in String Variable»  =/</>  «User defined threshhold (e.g 120)»] How can I formulate the XPath constraint in a way, so the String resembling the attribute name is recognized as an actual attribute name.  I hope it came clear what I want to achieve. Is this possible just using Xpath or do I need to extend/customize using Java? Any hints on how to solve this are greatly appreciated!    Best Regards    Jan   
asked
3 answers
5

I think the only way to do this is by doing your retrieve in a java action. In a java action, your xpath is just a string. This string can be created based on whatever you want. In the modeler, it is always hard coded. You can turn statements on and off by using booleans and “and” operators, you can also compare to dynamic data, but you can not dynamically create the whole compare statement. In a java action you can do this.

answered
5

Since there are not much Java code snippets using the new XPath method in Jave here a code snippet from Java where you pass the entity name and attribute as string and an XPathParameter to look for.
 

        if (xpathParameterString == "empty") {
            xpathString = "//"+entityName+"["+attribuutString+"="+ xpathParameterString+"]";
        }

        List<IMendixObject> resultList = Core.createXPathQuery(xpathString)

                .setDepth(1)
                .execute(context());
        
        return resultList;

Regards,

Ronald

answered
0

Hi, 

thanks for your answers Andreas and Ronald. However, I’m having trouble implementing your code Ronald. 

I hard coded some stuff for testing,  but when trying to create a "List”-object, I get a compilation error (“symbol not found”), telling me that the class List can not be found (see below). I tried importing some more classes but I had no success. Otherwise, the code seems pretty clear to me and should work.. 
Which version of Mendix are you referring to? Maybe it is a version error. 

Thank you in advance

Jan 

My Code: I added the Core import and declared the xpathString variable:

// This file was generated by Mendix 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 myfirstmodule.actions;

import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;

public class Masterflow_Method_Simulation_JavaAction extends CustomJavaAction<java.util.List<IMendixObject>>
{
	private java.lang.String XPathParameterString;
	private java.lang.String AttributeName;
	private java.lang.String Threshold;

	public Masterflow_Method_Simulation_JavaAction(IContext context, java.lang.String XPathParameterString, java.lang.String AttributeName, java.lang.String Threshold)
	{
		super(context);
		this.XPathParameterString = XPathParameterString;
		this.AttributeName = AttributeName;
		this.Threshold = Threshold;
	}

	@java.lang.Override
	public java.util.List<IMendixObject> executeAction() throws Exception
	{
		// BEGIN USER CODE
		throw new com.mendix.systemwideinterfaces.MendixRuntimeException("Java action was not implemented");
		String xpathString = "";
		if (XPathParameterString == "empty") {
            xpathString = "//"+"MyFirstModule.CRM"+"["+"IndustryCode"+"="+"2"+"]";
        }

        List<IMendixObject> resultList = Core.createXPathQuery(xpathString)

                .setDepth(1)
                .execute(context());

        return resultList;

		// END USER CODE
	}

	/**
	 * Returns a string representation of this action
	 */
	@java.lang.Override
	public java.lang.String toString()
	{
		return "Masterflow_Method_Simulation_JavaAction";
	}

	// BEGIN EXTRA CODE
	// END EXTRA CODE
}

Error:

Buildfile: C:\Users\xxxxx\Documents\Mendix\xxxx\deployment\build_core.xml

compile:
    [javac] Compiling 2 source files to C:\Users\xxxxx\Documents\Mendix\xxxxx\deployment\run\bin
    [javac] C:\Users\xxxxx\Documents\Mendix\xxxxx\javasource\myfirstmodule\actions\Masterflow_Method_Simulation_JavaAction.java:41: error: cannot find symbol
    [javac]         List<IMendixObject> resultList = Core.createXPathQuery(xpathString)
    [javac]         ^
    [javac]   symbol:   class List
    [javac]   location: class Masterflow_Method_Simulation_JavaAction
    [javac] 1 error

BUILD FAILED
C:\Users\xxxxx\Documents\Mendix\xxxxx\deployment\build_core.xml:29: Compile failed; see the compiler error output for details.

Total time: 1 second


 

answered