How to convert string to enum. CommComms EnumerationFromString?
I receive a string and need to convert it to an enumeration and found EnumerationFromString in CommunityCommons, but need help getting it to function: How to convert a string to an enumeration using CommComm's EnumerationFromString? What parameters to fill. It seems straightforward, but I seem to not succeed in pleasing the widget. What I have done so far is copying the widget and changed the output to the enumeration I want to receive. I passed my parameter $MyStringParameter’ into ‘To Convert’ and thought that it would do the trick: Can do it old-school via looping for each possible value and returning the related enum, but this should not be necessary. Webservices do this trick already in Mendix, so one could expect this to be facilitated somehow. There is a widget in commcomm called EnumerationFromString. How to convert a string to an enumeration using CommComm's EnumerationFromString. **Editted after Bart’s answer:
Tim van Steenbergen
Not sure how the CommunityCommons action works, but you can create your own very simple Java action to convert a string to enum.
Just create a new Java action with a string input parameter and the desired enum as output parameter. Next, deploy to eclipse, and open the java action in Eclipse (or any other editor), and simply return you input string:
I think you used it almost right, except
Did you noticed the following the Community commons class
// Replace LogLevel.class by the proxy class for your Enumeration
Optional<LogLevel> result = Misc.enumFromString(LogLevel.class, toConvert);
My take on this is
If we had to change so many things and make custom implementation for our own Enum’s then it is far better to write our own shortest implementation as Bart suggested
Otherwise, we must think about a generic implementation
I have a generic implementation with me which returns the translation value given the language. But if we want to return ENUM, then specific implementation is still needed.
Im still looking at how the above action has to be used with example, will keep you posted when I got to know it.
Just goto the Java action and select the enum you want to use. Then just commit this Java action. If you want to have it for multiple enums just copy the Java action and select the right enum for each copy. Then you can use this Java action: