How to convert string to enum. CommComms EnumerationFromString?

1
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:
asked
6 answers
11

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:

Regards,

Ronald

 

 

answered
8

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:

 

That's it!

answered
4

https://medium.com/mendix/customizing-java-actions-mendix-25ec5cc14f24 will give you a good explanation on how to customize this EnumerationFromString JA the right way.

Make sure that when you replace communitycommons.proxies.LogLevel with modulefolder.proxies.enumname, that the modulefolder name is lowercase.

 

Regards,

Roel

answered
3

Hi Tim,

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

  1. 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
  2. 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.

answered
1

Hi, I’m trying to use the CC EnumerationToString java action.  I duplicated the action and renamed it and placed it in my custom module.  When I use the Java Action in my microflow, I’m using the correct string and enums as inputs but I’m getting the following error when trying to compile the Java to run the app locally:

 

…...\actions\EnumerationFromString_Countries.java:39: error: cannot find symbol
    [javac]         Optional<LogLevel> result = Misc.enumFromString(ENUM_Countries_PostCodeLookup, toConvert);
    [javac]                                                         ^
    [javac]   symbol:   variable ENUM_Countries_PostCodeLookup
    [javac]   location: class EnumerationFromString_Countries
    [javac] Note: Some input files use or override a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] 1 error

 

I suspect that the issue is I need to replace the LogLevel.class in the Java Action, but I’m not sure what to replace it with.  I tried using my enum’s name but still got errors.  My enum is ENUM_Countries_PostCodeLookup. 

 

Your help will be much appreciated.

answered
0

Excellent answers here! Thank you!

 

answered
0

Hi, I’m trying to use the CC EnumerationToString java action.  I duplicated the action and renamed it and placed it in my custom module.  When I use the Java Action in my microflow, I’m using the correct string and enums as inputs but I’m getting the following error when trying to compile the Java to run the app locally:

 

…...\actions\EnumerationFromString_Countries.java:39: error: cannot find symbol
    [javac]         Optional<LogLevel> result = Misc.enumFromString(ENUM_Countries_PostCodeLookup, toConvert);
    [javac]                                                         ^
    [javac]   symbol:   variable ENUM_Countries_PostCodeLookup
    [javac]   location: class EnumerationFromString_Countries
    [javac] Note: Some input files use or override a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] 1 error

 

I suspect that the issue is I need to replace the LogLevel.class in the Java Action, but I’m not sure what to replace it with.  I tried using my enum’s name but still got errors.  My enum is ENUM_Countries_PostCodeLookup. 

 

Your help will be much appreciated.

answered