Parse a string and return a list of values

0
I want to create a custom java action that parses a string on some delimiter and returns a List of the parsed values. I have an Invoice entity with an InvoiceID attribute.  My goal is to take a string of invoices like "44 55 66" and return the members of the SearchStringSplitResult entity with InvoiceID = 44, 55 or 66. Then I think I'll be able to create an association between SearchStringSplitRsult and Invoice that will allow me to return an Invoice list. This is the code for my java action: [Edited] // 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; import java.util.Arrays; import java.util.ArrayList; import java.util.List; public class SplitString extends CustomJavaAction<java.util.List<IMendixObject>> { private java.lang.String SearchString; private IMendixObject __SearchStringSplitResultObject; private myfirstmodule.proxies.SearchStringSplitResult SearchStringSplitResultObject; public SplitString(IContext context, java.lang.String SearchString, IMendixObject SearchStringSplitResultObject) { super(context); this.SearchString = SearchString; this.__SearchStringSplitResultObject = SearchStringSplitResultObject; } @Override public java.util.List<IMendixObject> executeAction() throws Exception { this.SearchStringSplitResultObject = __SearchStringSplitResultObject == null ? null : myfirstmodule.proxies.SearchStringSplitResult.initialize(getContext(), __SearchStringSplitResultObject); // BEGIN USER CODE String[] splitArray = SearchString.split(" "); ArrayList<IMendixObject> resultList = new ArrayList<IMendixObject>(); for (String splitArrayElement : splitArray) { myfirstmodule.proxies.SearchStringSplitResult SearchStringSplitResult = new myfirstmodule.proxies.SearchStringSplitResult(getContext()); IMendixObject myEntity = Core.instantiate(this.getContext(), SearchStringSplitResult.entityName.toString()); myEntity.setValue(this.getContext(), "SearchValue", splitArrayElement); resultList.add(myEntity); } return resultList; // END USER CODE } /** * Returns a string representation of this action */ @Override public java.lang.String toString() { return "SplitString"; } // BEGIN EXTRA CODE // END EXTRA CODE }    
asked
2 answers
2

Steven,

Split the string with:

str = "44 66 55";
String[] splitArray = str.split("\\s+");

Then loop over the splitArray and create the entities. The code assumes a space to be the delimiter, but this can be any character just change the regex in the split function.

Optionally add the entities you create in the loop to an ArrayList<IMendixObject> and return this list.

[Edit]

To get the list of objects use the code below adapt this to match your entity names:

package myfirstmodule.actions;

import java.util.ArrayList;
import myfirstmodule.proxies.testsplitresult;
import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;

public class Java_action extends CustomJavaAction<java.util.List<IMendixObject>>
{
	private String StringToSplit;

	public Java_action(IContext context, String StringToSplit)
	{
		super(context);
		this.StringToSplit = StringToSplit;
	}

	@Override
	public java.util.List<IMendixObject> executeAction() throws Exception
	{
		// BEGIN USER CODE
		String[] splitArray = StringToSplit.split(" ");
		
		ArrayList<IMendixObject> resultList = new ArrayList<IMendixObject>();
		
		for (String ValueStr : splitArray) {
			IMendixObject myEntity = Core.instantiate(getContext(), testsplitresult.entityName.toString());
			myEntity.setValue(getContext(),  testsplitresult.MemberNames.SearchAttribute.toString(), ValueStr);
			resultList.add( myEntity);
		}
		return resultList;
		// END USER CODE
	}

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

	// BEGIN EXTRA CODE
	// END EXTRA CODE
}

The action in the modeler is a little bit different than you posted:

Hope this will solve your issue.

answered
1

To add on to Erwin's answer: I'm guessing you're trying to retrieve objects of the invoice entity that already exist.

 

If that's the case, here's some code that should get you close:

str = "44 66 55";
String[] splitArray = str.split("\\s+");
String xpathQuery = "//MyModule.Invoice[";

for(int i =0; i < splitArray.length; i++) {
     xpathQuery += "InvoiceID = " + splitArray[i];
     if (i < splitArray.length) {
          xpathQuery += " or ";
     }
}

xpathQuery += "]";

return core.retrieveXPathQuery(getContext(), xpathQuery);

Note I wrote this without syntax checking and I haven't tested it. Hopefully it at least demostrates the concept. Use this reference for the call to retrieveXPathQuery.

answered