String combine of one attribute in list of objects, by given separator

0
Hi,  Is there a function or app available to combine the values of one attribute in a list with objects, to a string with a given separator? I noticed there is an app for string splitt functionality (https://appstore.home.mendix.com/link/app/106771/), but can’t find one for the other way around. I understand I can use an iterator to achieve this, but I would expect it’s a standard functionality.  
asked
1 answers
2

Hi Joran,

I add the same requirement myself and i ended up writing a java action for this:

package utilmodule.actions;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;

public class ReduceListToString extends CustomJavaAction<java.lang.String>
{
	private java.util.List<IMendixObject> listToReduce;
	private java.lang.String entityMember;
	private java.lang.String separator;
	private java.lang.Boolean skipEmptyString;

	public ReduceListToString(IContext context, java.util.List<IMendixObject> listToReduce, java.lang.String entityMember, java.lang.String separator, java.lang.Boolean skipEmptyString)
	{
		super(context);
		this.listToReduce = listToReduce;
		this.entityMember = entityMember;
		this.separator = separator;
		this.skipEmptyString = skipEmptyString;
	}

	@Override
	public java.lang.String executeAction() throws Exception
	{
		// BEGIN USER CODE
		IContext context = getContext();
		if (this.listToReduce.isEmpty() 
				|| !this.listToReduce.get(0).hasMember(entityMember)) {
			return "";
		}
		else {
			Stream<String> stream = this.listToReduce.stream().
					map((e) -> e.getMember(context, this.entityMember).parseValueToString(context));
			if (skipEmptyString) {
				return stream.
						filter((s) -> s!=null && s.trim().length()>0).
						collect(Collectors.joining(this.separator));
			}
			else {
				return stream.collect(Collectors.joining(this.separator));
			}
		}
		// END USER CODE
	}

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

	// BEGIN EXTRA CODE
	// END EXTRA CODE
}

 

Hope this can help you.

Regards

answered