fetch only some attributes with XPath in java

0
In client API mx.data.get there's: filter.attributes "if provided, only the given attributes will be fetched"   Example JS to retrieve only "Description" attribute: mx.data.get({ xpath: "//System.TimeZone", filter: { attributes: ["Description"] }, callback: arr => console.info(arr[0].jsonData.attributes) }) Resulting ConnectionBus_Retrieve trace: SQL@30e0c7ce(T55-C6ce92e92): SELECT "system$timezone"."id", "system$timezone"."description" FROM "system$timezone" I can't find a java counterpart. How do I set the explicit attributes to fetch in java?
asked
5 answers
1

In this case use OQL

 

SELECT Description From System.TimeZone

 

Use an NP entity and this module

answered
0

IRetrievalSchema.addAllMetaPrimitiveNames() was removed from public API in Mendix 7.

 

The internal implementation class in Mendix 10x still has this method and the client method mx.data.get with parameter filter.attributes uses it.

 

It's possible to assign a list of attributes to fetch using java reflection.

 

Mendix 6 javadoc

 

 

answered
-1

You can find the Java APIs here: https://apidocs.rnd.mendix.com/8/runtime/com/mendix/core/Core.html

especially see the retrievebyPath methods. Once you run them, then you can filter the resulting object the way you like.

 

I just made a small usecase for this. Assume my requirement is to a new object with contents from an existing object, assuming both entities have same params with same data types. 

this is a quick example, just get some ideas from it.  Need some polishing to do for production uses.

 

// 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 java.util.Collection;
import java.util.List;
import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.systemwideinterfaces.core.meta.IMetaObject;
import com.mendix.systemwideinterfaces.core.meta.IMetaPrimitive;
import com.mendix.systemwideinterfaces.core.meta.IMetaPrimitive.PrimitiveType;
import com.mendix.webui.CustomJavaAction;
import myfirstmodule.proxies.NP_Country;

public class JA_Clone extends CustomJavaAction<IMendixObject>
{
	/** @deprecated use source.getMendixObject() instead. */
	@java.lang.Deprecated(forRemoval = true)
	private final IMendixObject __source;
	private final myfirstmodule.proxies.Country source;

	public JA_Clone(
		IContext context,
		IMendixObject _source
	)
	{
		super(context);
		this.__source = _source;
		this.source = _source == null ? null : myfirstmodule.proxies.Country.initialize(getContext(), _source);
	}

	@java.lang.Override
	public IMendixObject executeAction() throws Exception
	{
		// BEGIN USER CODE
		IContext context = getContext();

		// Retrieve Country objects
		// TODO: perhaps search for options to get only few attributes from the query
		List<IMendixObject> countries = Core.retrieveXPathQuery(context, "//MyFirstModule.Country");

		// Get the meta object of the non-persistent entity
		IMetaObject npMetaObject = Core.getMetaObject(NP_Country.entityName);

			// Create a new Non-Persistent EmployeeData object
			IMendixObject countryData = Core.instantiate(context, NP_Country.entityName);
			
			// Get all members (attributes) of the country object
			// for this usecase i just need 1 object
			Collection<? extends IMetaPrimitive> members = countries.get(0).getMetaObject().getMetaPrimitives();

			for (IMetaPrimitive member : members)
			{
				String attributeName = member.getName();
				PrimitiveType type = member.getType();
				// Check if the non-persistent entity has the same attribute
				if (npMetaObject.getMetaPrimitive(attributeName) != null)
				{
					// Copy the value from the source object to the non-persistent object
					Object value = countries.get(0).getValue(context, attributeName);

					// Handle the value based on its type
					switch (type)
					{
						case Boolean:
							countryData.setValue(context, attributeName, (Boolean) value);
							break;
						case DateTime:
							countryData.setValue(context, attributeName, (java.util.Date) value);
							break;
						case Decimal:
							countryData.setValue(context, attributeName, (java.math.BigDecimal) value);
							break;
						case Integer:
							countryData.setValue(context, attributeName, (Integer) value);
							break;
						case Long:
							countryData.setValue(context, attributeName, (Long) value);
							break;
						case String:
							countryData.setValue(context, attributeName, (String) value);
							break;
						default:
							// Handle other types as needed
							break;
					}
				}
			}
//
//			// Add the EmployeeData object to the list
//			employeeDataList.add(employeeData);
		return countryData;
		// END USER CODE
	}

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

	// BEGIN EXTRA CODE
	// END EXTRA CODE
}

 

answered
-1

Hi Ilya,

You can access the official documentation using this link: IMendixObject

In your case, really depends on your final goal but I would say you can use one of these methods:

 

getMember() - Returns the member object with memberName.

or

getMembers() - Returns a HashMap with all members of this object.

image.png

 

getValue() - Returns the value of the member with memberName.

image.png

 

If this answer helped you, mark as accept :)

 

Best Regards,

Ricardo Pereira

answered
-1

Try asking the GPT chat about it, I just asked this one your question and he showed me a good code

answered