getting domain class property value by its String name - grails

May be a basic question..
I am doing
MyDomain.dirtyPropertyNames.each {
aInstance.oldVal = newDomainObject.getPersistentValue(it)
aInstance.newVal = ? // how to get the property value here from the list obviously MyDomain.it doesnt work..
aInstance.save()
}
The dirtyPropertyNames is a list of Property name Strings, how do I get the Domain.property of each property in the list?
Thanks

Does
myDomain."$it"
Do what you want?

I prefer the following form for property access through a name.
myDomain[it]
For instance, if your property name is "lockedFlag" the following are true.
assert myDomain.lockedFlag == myDomain["lockedFlag"]
assert myDomain.lockedFlag == myDomain."lockedFlag"
I hope this helps.

Related

Create nullable object based on nullable value

i have to create a domain class object which has a nullable attribute. This Creating is in an extansion of a primitive class.
For example: Domainclass
Meeting({required String id, required Datetime date, Example? example})
behind Example is an class like so: Example(String value)
MeetingPrimitive.dart:
extension MeetingPrimitiveX on Meeting {
Meeting toDomain() {
return Meeting(
id: id,
date: date,
example: example == null ? null : Example(example)
}
}
My question is, how can i simplify this:
example == null ? null : Example(example)
'''
thanks in advance
You generally can't.
You can change it in various ways, but nothing is more direct than what you have here.
One variant would be:
extension DoWith<T> on T {
R doWith<R>(R Function(T) action) => action(this);
}
With that, you can write:
example?.doWith(Example.new)
to conditionally call the Example.new constructor (tear-off of the unnamed constructor) if example is non-null.
Slightly shorter, requires a helper function, and ... isn't actually more readable IMO.

How to pass variable holding a value to groovy eq query?

How can I pass variable holding a value instead of passing a value directly to groovy search criteria?
for (def payee in payees)
{
def results = resp.cases.find("eq 'hrid','7547') // hard code values work
def results = resp.cases.find("eq 'hrid',??????) // how can I pass payee
}
I'm new to this. Please help. thanks
Basing on examples, check it:
def results = resp.cases.find { case -> case.hrid == payee }
GORM criteria queries are quite different than your example. Let's say you have a Payee domain class with a property named hrid:
grails-app/domain/com/something/Payee.groovy
class Payee {
String hrid
}
To get a list of all the Payee instances with an hrid of 7547, you'd run a criteria query like this:
String hridValue = '7547'
List payees = Payee.withCriteria {
eq 'hrid', hridValue
}
I explain Grails GORM queries in depth in a series of articles starting right here.

grails scaffolding configuration by domainclass

at the moment I try to deal with scaffolding in grails. There I have a little problem with the generation-templates where i don't find a solution. I want to configure the generating-algorithm in the domainmodel.
My thought was to define static variables in the model which aren't created in the database and only important for the generation-process.
For example I want to display only some special fields in the show.gsp but i want to display every field in the _form.gsp Or i want to do some gsp-imports but only in seperate gsp.
So i thought that i could define some static variables where the value contains some configuration-parameter i can interpretate in the generation template.
I hope everybody understand what I mean ?
Here is an example:
class Awesome {
Long awesome_level
Long person_name
Boolean itsMe
static String showFields
static transients = ['showFields']
static constraints = {
einrichtungs_type()
type_of_conzept()
anzahl_gruppen()
anzahl_kinder_pro_Gruppe()
offnungszeiten()
showFields(["person_name", "itsMe"])
}
In the Show-View i only want to display the fields in the array "showFields"
...
for (p in props) {
if (p.embedded && p.name in domainClass.showFields) {
def embeddedPropNames = p.component.persistentProperties*.name
def embeddedProps = p.component.properties.findAll { embeddedPropNames.contains(it.name) && !excludedProps.contains(it.name) }
Collections.sort(embeddedProps, comparator.constructors[0].newInstance([p.component] as Object[]))
%><fieldset class="embedded"><legend><g:message code="${domainClass.propertyName}.${p.name}.label" default="${p.naturalName}" /></legend><%
for (ep in p.component.properties) {
renderFieldForProperty(ep, p.component, "${p.name}.")
}
%></fieldset><%
} else {
renderFieldForProperty(p, domainClass)
}
...
I know that the if clause don't work. My problem is, that i am not able to get the value of the field "showFields".
Know my questions:
Is it able to receive the values of a field of a domainclass?
Is it able to execute a methode of a domainclass?
Is there an other way do define configuration-parameters which i can access in the generation-templates?
I hope i was able to display my problem and thank you for some help!
Greetz
V
I found a solution for that problem. First of all I thought it could be possible with the creation of a custom-Constraint. I am still thinking that this is also possible but i found a better /easyer way to add "configurations".
I use the tag attributes, which already exsits. If i understand it right, the attributes parameter i used to add attributes in select-Html-tags. Now i use it for adding some configuration-parameters. Here my solution:
You have to change the "renderEditor.template" that not all the configuration-parameter will add in a HTMl-tag as a attribute. cp.attributes.each { k, v ->
sb << "${k}=\"${v}\" "
} change to cp.attributes?.realAttributes.each { k, v ->
sb << "${k}=\"${v}\" "
}
When you realy want to this attributes-function add into the attributes-parameter the value "realAttributes" (or how you would like to call it)
Change the constraints of the model: (in my example:)
static constraints = { einrichtungs_type(attributes:
[showField:true])
}
Finaly change the generation-template
if(cp.attributes?.showField){
...
I hope these 4 steps help you, if you have nearly the same problems.
Greetz
V

How to get value of a domain constraint in Grails?

I have a text field whose length I would like to limit at the maxSize constraint of one of my domain classes.
So if I have a class foo:
class Foo {
String bar
static constraints = {
bar(maxSize: 100)
}
}
I would like to get that value of 100 for the property bar. Is this possible?
You should be able to do:
def maxBarSize = Foo.constraints.bar.getAppliedConstraint( 'maxSize' ).maxSize
I was having this issue in grails 3.1.8 and it has change a bit. at least in the gsp views I had to put this:
Foo.constrainedProperties ['bar']['maxSize']
Hope this help! Cheers!
Check the following code:
def foo = new Foo(bar: "stuff")
println foo.constraints.bar.maxSize

Unable to cast object of type WhereListIterator<system.web.Mvc.ListItem> to type system.web.mvc.listitem

I have a SelectList that I first check for a selected value != null and then want to use this selectedvalue in a where clause for a filter. Like so:
if(searchBag.Qualities.SelectedValue != null){
ListItem selected = (ListItem)searchBag.Qualities.SelectedValue;
}
I made the cast in a seperate useless line to pinpoint the problem.
This gives me a
Unable to cast object of type 'WhereListIterator`1[System.Web.Mvc.ListItem]' to type 'System.Web.Mvc.ListItem'.
Weuh?
--EDIT--
It was indeed because multiple selections were made.
This was because on creation I set the selected value to theItems.Where(i=>i.someCriterea) and I forgot to put .FirstOrDefault() at the end. Ending up in the possibility of multiple answers.
Since it was an IEnumerable, it was a lazy list and hence the WhereListIterator I guess.
I solved it by simple putting FirstOrDefault at the end.
Its already been explained, but here's another suggestion as to how to get what you're looking for:
if(searchBag.Qualities.SelectedValue != null){
ListItem selected = (ListItem)searchBag.Qualities.SelectedValue.FirstOrDefault();
}
I assume the SelectList lets you select more than 1 item?
So SelectedValue is probably a list? Not 1 listitem.
Cast it to:
WhereListIterator<ListItem> selected = (WhereListIterator<ListItem>)searchBag.Qualities.SelectedValue;
instead
and see what properties you have there.
SelectedValue is not a ListItem, it's the value of the selected list item. So given this:
var selectList = new SelectList(companiesList, "Id", "Name", companiesList[3]);
selectList.SelectedValue will equal companiesList[3].Id. If you want the actual list item you can do something like this:
ListItem selected = selectList.GetListItems().Where(x => x.Value == selectList.SelectedValue.ToString())
Just curious, why do you want the selected list item?

Resources