Grails : Overriding getter method in domain class - grails

I can't override grails getter method and becoming crazy.
What I want is use a double value and a string value to get a formatted data, but when I'm typing my override method into String, the double value is null and when into double, it obviously get me an error because a String is returned !
I get a domain class like that :
class Project {
...
String currency
Double initialTargetAmount
...
}
First Override method (initialTargetAmount is null in this case) :
//#Override Comment or uncomment it does not make any change to the behaviour
public String getInitialTargetAmount() {
println "${initialTargetAmount} ${currency}" // display "null EUR"
"${initialTargetAmount} ${currency}" // initialTargetAmount is null
}
Second method :
//#Override Comment or uncomment it does not make any change to the behaviour
public Double getInitialTargetAmount() {
println "${initialTargetAmount} ${currency}" // display "1000.00 EUR"
"${initialTargetAmount} ${currency}" // Error : String given, Double expected
}
Any help is welcome.
Snite

Groovy has dynamic getters and setters.
So, initalTargetAmount field "creates" automatically a Double getInitialTargetAmount method. Which is why it works when you have the Double return Type. But when you set String, getInitialTargetAmount automatically refers to a field String initalTargetAmount which doesn't exist
Try changing the name of the method, for example getInitialAmountWithCurrency() and it will work. Maybe your best bet will be to override toString() method ^^

Your getter should be always the same type of your field, and it's noot a good approach to change the getter like this, because Grails (Hibernate internally) will understand that your object instance changed and will try to update it ( it will check the old and new values).
You're trying in fact is to have a String representation of your amount, so you have a couple of options to this:
1 - A new method
Creating a new method that returns String will not interfere in the hibernate flow and you can use it anywere.
class Project {
...
String currency
Double initialTargetAmount
...
String displayTargetAmount() {
"${initialTargetAmount} ${currency}"
}
}
2 - TagLib
Depending on your needs, you could create a TagLib to make this custom representations of your class. This can include html formatting.
class ProjectTagLib {
static namespace = "proj"
def displayAmount = { attrs ->
if(!attrs.project) {
throwTagErrro("Attribute project must be defined.")
}
Project project = attrs.remove('project')
//just an example of html
out << "<p>${project.initialTargetAmount} , ${project.currency}</p>"
}
}

Related

String constant reference as value of Annotation attribute causes compile error

I am using my properties file to get the values for #Scheduled annotation attributes.I am able to get the values from properties file but when I tries to pass String constant reference to Annotation attribute then compile time exception is raised.
#Slf4j
#CompileStatic
class TestJobService {
static lazyInit = false
public static String jobInterval = getSomePropertiesFileValues?.fixedRateInMS instanceof String? getSomePropertiesFileValues.fixedRateInMS:'10000'
#Scheduled(fixedDelayString = TestJobService.jobInterval)
void executeEveryTenSeconds() {
def date = new Date()
println date.format('yyyy/MM/dd HH:mm', TimeZone.getTimeZone('IST'))
}
}
Attribute 'fixedDelayString' should have type 'java.lang.String'; but
found type 'java.lang.Object' in
#org.springframework.scheduling.annotation.Scheduled
Then I tried to use String to pass like:
#Slf4j
#CompileStatic
class TestJobService {
static lazyInit = false
#Scheduled(fixedDelayString = '${getSomePropertiesFileValues.fixedRateInMS}')
void executeEveryTenSeconds() {
def date = new Date()
println date.format('yyyy/MM/dd HH:mm', TimeZone.getTimeZone('IST'))
}
}
OR
public static final String jobInterval = getSomePropertiesFileValues?.fixedRateInMS instanceof String? getSomePropertiesFileValues.fixedRateInMS:'10000'
prevents the variable from being treated as an inline constant and compiler complains of not being inline constant.
I understand that using single quote '${getSomePropertiesFileValues.fixedRateInMS}'we can get compiler to know that I want GString behaviour. But I don't know is this a bug in Groovy or its a functionality which I need to implement in some other way to pass the string values as annotation attributes. Any lead or any help is highly appreciable.

Not able to pass value to another class

I want to append a certain "id" to an api. This is the initial portion of how I make the API call in my viewController..
func myAPICall() {
APIHelper(API: WebServices.getAllOrganizations as NSString, json: bodyStr as NSString,
methodType: Constants.Get_Method as NSString....)
}
Now, WebServices.getAllOrganizations is defined elsewhere, in a swift file like so...
public class WebServices {
static let getAllOrganizations: String = "organization/getAllOrganizationDetails
}
MY ATTEMPT TO PASS THE ID TO THE SWIFT FILE :
To pass the value, I assigned it to a global variable like so...
ArrayData.shared.plantIDForOrganization = Int("\(dic["id"]!)")!
And further, I changed my swift file to this...
public class WebServices {
static let getAllOrganizations: String = "organization/getAllOrganizationDetails/\(ArrayData.shared.plantIDForOrganization)"
}
But by doing this, the value is not properly passed to the API. A 0 is passed to the API instead of the actual id number.
What could be a more efficient way of passing value from my viewcontroller to the swift file..?
When is ArrayData.shared.plantIDForOrganization initialized, and when does WebServices.getAllOganizations get its value? Once the latter is set, it won't "react" to changes in plantIDForOrganization.
I suggest you change this to a computed property, like so:
public class WebServices {
static var getAllOrganizations: String {
return "organization/getAllOrganizationDetails/\(ArrayData.shared.plantIDForOrganization)"
}
}
Also, try to eliminate thos force-unwrapping from your code.
If you have to pass a parameter use a static method instead:
public class WebServices {
static func getAllOrganizations(id: String) -> String { "organization/getAllOrganizationDetails/\(id)" }
}

Change display name of Grails Domain Class

I have a one-to-one relationship, and have a combobox for selecting the other one. How do I change the display name of the combo box?
Here's a screenshot of the combo box, to make myself clear:
I'm new to grails, and are attempting to create a simple data-driven app for in house use.
Edit: My toString():
def toString = { "${naam} [${gemeente}]" }
Override the toString method of said domain class.
String toString() {
return this.name;
}
The sig needs to be String toString() { ... } (you are returning a def and it is a closure, not a method)
In grails, the select tag also has an optional attribute named "optionValue" that will allow you to define a bean field/property to display.
In Grails 3, the following works:
String toString() {
return "${name}"
}
in your example, this would be:
String toString() {
return "${naam} [${gemeente}]"
}
cheers!

Problem with GRAILS select - trying to insert a number for a field that appears as text in dropdown list

Here is the domain class I have defined:
package mypackage
public enum UFModeType {
I(0),
O(1),
R(3)
Integer mode
public UserFileModeType(Integer mode) {
this.mode = mode;
}
static list() {
[I, O, R]
}
}
This is a property of another domain Parent where it is as follows:
package mypackage
class Parent {
String name
... ... ...
UFModeType uFMode
static mapping = {
table 'parent_table_with_ufMode_col_as_number'
version false
tablePerHierarchy false
id generator:'sequence', params:[sequence:'myseq']
columns {
id column:'parentid'
uFMode column: 'UFMODE'
}
}
static constraints = {
userFileMode(nullable: true)
}
}
The gsp call for this looks like this:
g:select name="uFMode" from="${mypackage.UFModeType?.list()}" value="${parentInstance?.uFMode?.name()}" /
I have tried a lot of variants of the above in the gsp call but I am getting error that the db insert fails saying the entry of ufmode is invalid number, thus this is not being passed as a number. I printed the params in the controllers save and it shows this:
Params in save=[uFMode:I ...
I am sure I may be missing some minor thing in syntax, but I have tried a lot of things without much success, so any inputs will be greatly appreciated.
Thanks!
Try changing
value="${parentInstance?.uFMode?.name()}
to
value="${parentInstance?.uFMode?.mode()}
From the definition of UFModeType you give you do not have a name attribute.

Adding a method to a domain class

I have a domain class containing a couple of fields. I can access them from my .gsps. I want to add a method to the domain class, which I can call from the .gsps (this method is a kind of virtual field; it's data is not coming directly from the database).
How do I add the method and how can I then call it from the .gsps?
To add a method, just write it out like you would any other regular method. It will be available on the object when you display it in your GSP.
def someMethod() {
return "Hello."
}
Then in your GSP.
${myObject.someMethod()}
If you want your method to appear to be more like a property, then make your method a getter method. A method called getFullName(), can be accessed like a property as ${person.fullName}. Note the lack of parentheses.
Consider class like below
class Job {
String jobTitle
String jobType
String jobLocation
String state
static constraints = {
jobTitle nullable : false,size: 0..200
jobType nullable : false,size: 0..200
jobLocation nullable : false,size: 0..200
state nullable : false
}
def jsonMap () {
[
'jobTitle':"some job title",
'jobType':"some jobType",
'jobLocation':"some location",
'state':"some state"
]
}
}
You can use that jsonMap wherever you want. In gsp too like ${jobObject.jsonMap()}

Resources