Quantcast

enum, GString and cast

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

enum, GString and cast

Luca Farsetti
hello all. I have some enums in my app and i wanna use it' for filter some criteria query.

I try to generalize filter so i try to do this into my criteria:

if( filter.getType().equals("enum")){

  def enumValue = filter.getValue() as "${filter.getEnumClassName()}"

  eq(filter.getFieldName(), enumValue)
}


but it doesn't work of couse. there is some way for accomplish this?

Thanks for suggestion

best regards

Luca
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: enum, GString and cast

sbglasius
Hi,

To get an Enum from a string you could do this:

def enumValue = SomeEnum.valueOf(filter.getValue())

Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
P: (+45)87880170, M: (+45)40449188, S: sbglasius
--- Press ESC once to quit - twice to save the changes.


2011/9/29 Luca Farsetti <[hidden email]>
hello all. I have some enums in my app and i wanna use it' for filter some criteria query.

I try to generalize filter so i try to do this into my criteria:

if( filter.getType().equals("enum")){

  def enumValue = filter.getValue() as "${filter.getEnumClassName()}"

  eq(filter.getFieldName(), enumValue)
}


but it doesn't work of couse. there is some way for accomplish this?

Thanks for suggestion

best regards

Luca

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: enum, GString and cast

sbglasius
Spotted a problem in my solution. You have the Enum class as a string value.

I assume, you have all your enums in the same package, so I cooked up this little example:

public enum EnumA {
    X, Y
}


public enum EnumB {
    X, Y
}


def enumType = 'EnumA'
def enumValue = 'X'

def clazz = Enum.forName('EnumA') // Here you have to have the full package and class name
println clazz.valueOf(enumValue)


Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
P: (+45)87880170, M: (+45)40449188, S: sbglasius
--- Press ESC once to quit - twice to save the changes.


2011/9/29 Søren Berg Glasius <[hidden email]>
Hi,

To get an Enum from a string you could do this:

def enumValue = SomeEnum.valueOf(filter.getValue())

Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
P: <a href="tel:%28%2B45%2987880170" value="+4587880170" target="_blank">(+45)87880170, M: <a href="tel:%28%2B45%2940449188" value="+4540449188" target="_blank">(+45)40449188, S: sbglasius
--- Press ESC once to quit - twice to save the changes.



2011/9/29 Luca Farsetti <[hidden email]>
hello all. I have some enums in my app and i wanna use it' for filter some criteria query.

I try to generalize filter so i try to do this into my criteria:

if( filter.getType().equals("enum")){

  def enumValue = filter.getValue() as "${filter.getEnumClassName()}"

  eq(filter.getFieldName(), enumValue)
}


but it doesn't work of couse. there is some way for accomplish this?

Thanks for suggestion

best regards

Luca


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: enum, GString and cast

Luca Farsetti
Thank you Soren, it help me so much. But i have another problem :)

My enum are like this:

public enum EnumA {
  
  VALUEA("Value label 1"), VALUEB("Value label 2");

  private String description;

  private EnumA(String description){
    this.description = description;
  }

  public String toString(){
    return description;
  }
}

and i haven't consider the possibility to lookup from description to key. 

i think i must modify my enums to make it working well

2011/9/29 Søren Berg Glasius <[hidden email]>
Spotted a problem in my solution. You have the Enum class as a string value.

I assume, you have all your enums in the same package, so I cooked up this little example:

public enum EnumA {
    X, Y
}


public enum EnumB {
    X, Y
}


def enumType = 'EnumA'
def enumValue = 'X'

def clazz = Enum.forName('EnumA') // Here you have to have the full package and class name
println clazz.valueOf(enumValue)


Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
P: <a href="tel:%28%2B45%2987880170" value="+4587880170" target="_blank">(+45)87880170, M: <a href="tel:%28%2B45%2940449188" value="+4540449188" target="_blank">(+45)40449188, S: sbglasius
--- Press ESC once to quit - twice to save the changes.


2011/9/29 Søren Berg Glasius <[hidden email]>
Hi,

To get an Enum from a string you could do this:

def enumValue = SomeEnum.valueOf(filter.getValue())

Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
P: <a href="tel:%28%2B45%2987880170" value="+4587880170" target="_blank">(+45)87880170, M: <a href="tel:%28%2B45%2940449188" value="+4540449188" target="_blank">(+45)40449188, S: sbglasius
--- Press ESC once to quit - twice to save the changes.



2011/9/29 Luca Farsetti <[hidden email]>
hello all. I have some enums in my app and i wanna use it' for filter some criteria query.

I try to generalize filter so i try to do this into my criteria:

if( filter.getType().equals("enum")){

  def enumValue = filter.getValue() as "${filter.getEnumClassName()}"

  eq(filter.getFieldName(), enumValue)
}


but it doesn't work of couse. there is some way for accomplish this?

Thanks for suggestion

best regards

Luca



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: enum, GString and cast

Alberto Vilches
In reply to this post by sbglasius
You can allways SomeEnum."${filter.getValue()}"

On Thu, Sep 29, 2011 at 17:02, Søren Berg Glasius <[hidden email]> wrote:
Hi,

To get an Enum from a string you could do this:

def enumValue = SomeEnum.valueOf(filter.getValue())

Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
P: <a href="tel:%28%2B45%2987880170" value="+4587880170" target="_blank">(+45)87880170, M: <a href="tel:%28%2B45%2940449188" value="+4540449188" target="_blank">(+45)40449188, S: sbglasius
--- Press ESC once to quit - twice to save the changes.



2011/9/29 Luca Farsetti <[hidden email]>
hello all. I have some enums in my app and i wanna use it' for filter some criteria query.

I try to generalize filter so i try to do this into my criteria:

if( filter.getType().equals("enum")){

  def enumValue = filter.getValue() as "${filter.getEnumClassName()}"

  eq(filter.getFieldName(), enumValue)
}


but it doesn't work of couse. there is some way for accomplish this?

Thanks for suggestion

best regards

Luca




--
Un saludo.
Alberto Vilches
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: enum, GString and cast

sbglasius
In reply to this post by Luca Farsetti
Hi Luca,

Let your enum implement org.springframework.context.MessageSourceResolvable (see this blog entry: http://adhockery.blogspot.com/2009/03/internationalizing-domain-classes-and.html), this way you don't have to handle the description, Grails resource bundles will do it for you.

When using this, you can make your select in your GSP look like this:

<g:select name="something" value="${bean.something}" from="${SomeEnum.values()}"/> 

and Grails will let the key be your enum name and let the bundle be the display value. Works like a charm, and keeps your Enum's DRY and without translations. 

Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
P: (+45)87880170, M: (+45)40449188, S: sbglasius
--- Press ESC once to quit - twice to save the changes.


2011/9/29 Luca Farsetti <[hidden email]>
Thank you Soren, it help me so much. But i have another problem :)

My enum are like this:

public enum EnumA {
  
  VALUEA("Value label 1"), VALUEB("Value label 2");

  private String description;

  private EnumA(String description){
    this.description = description;
  }

  public String toString(){
    return description;
  }
}

and i haven't consider the possibility to lookup from description to key. 

i think i must modify my enums to make it working well

2011/9/29 Søren Berg Glasius <[hidden email]>
Spotted a problem in my solution. You have the Enum class as a string value.

I assume, you have all your enums in the same package, so I cooked up this little example:

public enum EnumA {
    X, Y
}


public enum EnumB {
    X, Y
}


def enumType = 'EnumA'
def enumValue = 'X'

def clazz = Enum.forName('EnumA') // Here you have to have the full package and class name
println clazz.valueOf(enumValue)


Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
P: <a href="tel:%28%2B45%2987880170" value="+4587880170" target="_blank">(+45)87880170, M: <a href="tel:%28%2B45%2940449188" value="+4540449188" target="_blank">(+45)40449188, S: sbglasius
--- Press ESC once to quit - twice to save the changes.


2011/9/29 Søren Berg Glasius <[hidden email]>
Hi,

To get an Enum from a string you could do this:

def enumValue = SomeEnum.valueOf(filter.getValue())

Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
P: <a href="tel:%28%2B45%2987880170" value="+4587880170" target="_blank">(+45)87880170, M: <a href="tel:%28%2B45%2940449188" value="+4540449188" target="_blank">(+45)40449188, S: sbglasius
--- Press ESC once to quit - twice to save the changes.



2011/9/29 Luca Farsetti <[hidden email]>
hello all. I have some enums in my app and i wanna use it' for filter some criteria query.

I try to generalize filter so i try to do this into my criteria:

if( filter.getType().equals("enum")){

  def enumValue = filter.getValue() as "${filter.getEnumClassName()}"

  eq(filter.getFieldName(), enumValue)
}


but it doesn't work of couse. there is some way for accomplish this?

Thanks for suggestion

best regards

Luca




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: enum, GString and cast

Luca Farsetti
Thank you for the suggest Soren, but i can't use this in my application because i have a gwt/ext-gwt web interface :(.



2011/9/29 Søren Berg Glasius <[hidden email]>
Hi Luca,

Let your enum implement org.springframework.context.MessageSourceResolvable (see this blog entry: http://adhockery.blogspot.com/2009/03/internationalizing-domain-classes-and.html), this way you don't have to handle the description, Grails resource bundles will do it for you.

When using this, you can make your select in your GSP look like this:

<g:select name="something" value="${bean.something}" from="${SomeEnum.values()}"/> 

and Grails will let the key be your enum name and let the bundle be the display value. Works like a charm, and keeps your Enum's DRY and without translations. 

Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
P: <a href="tel:%28%2B45%2987880170" value="+4587880170" target="_blank">(+45)87880170, M: <a href="tel:%28%2B45%2940449188" value="+4540449188" target="_blank">(+45)40449188, S: sbglasius
--- Press ESC once to quit - twice to save the changes.


2011/9/29 Luca Farsetti <[hidden email]>
Thank you Soren, it help me so much. But i have another problem :)

My enum are like this:

public enum EnumA {
  
  VALUEA("Value label 1"), VALUEB("Value label 2");

  private String description;

  private EnumA(String description){
    this.description = description;
  }

  public String toString(){
    return description;
  }
}

and i haven't consider the possibility to lookup from description to key. 

i think i must modify my enums to make it working well

2011/9/29 Søren Berg Glasius <[hidden email]>
Spotted a problem in my solution. You have the Enum class as a string value.

I assume, you have all your enums in the same package, so I cooked up this little example:

public enum EnumA {
    X, Y
}


public enum EnumB {
    X, Y
}


def enumType = 'EnumA'
def enumValue = 'X'

def clazz = Enum.forName('EnumA') // Here you have to have the full package and class name
println clazz.valueOf(enumValue)


Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
P: <a href="tel:%28%2B45%2987880170" value="+4587880170" target="_blank">(+45)87880170, M: <a href="tel:%28%2B45%2940449188" value="+4540449188" target="_blank">(+45)40449188, S: sbglasius
--- Press ESC once to quit - twice to save the changes.


2011/9/29 Søren Berg Glasius <[hidden email]>
Hi,

To get an Enum from a string you could do this:

def enumValue = SomeEnum.valueOf(filter.getValue())

Med venlig hilsen / Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, DK-8680 Ry, Denmark
P: <a href="tel:%28%2B45%2987880170" value="+4587880170" target="_blank">(+45)87880170, M: <a href="tel:%28%2B45%2940449188" value="+4540449188" target="_blank">(+45)40449188, S: sbglasius
--- Press ESC once to quit - twice to save the changes.



2011/9/29 Luca Farsetti <[hidden email]>
hello all. I have some enums in my app and i wanna use it' for filter some criteria query.

I try to generalize filter so i try to do this into my criteria:

if( filter.getType().equals("enum")){

  def enumValue = filter.getValue() as "${filter.getEnumClassName()}"

  eq(filter.getFieldName(), enumValue)
}


but it doesn't work of couse. there is some way for accomplish this?

Thanks for suggestion

best regards

Luca





Loading...