groovy oracle result capital column name issue - sql

In Groovy to set a bean you just need to give the GroovyRowResult while creating object.
Consider below People.groovy bean:
class People {
String name
int age
}
My sql query:
select * from People -- returns name and age of people
the GroovyRowResult is returned with column names (keys) in capitals like it:[NAME:"Alex", AGE: 21].
So when I try to set the bean like below:
le.rows(sqlQuery).each {
People p = new People(it)
}
I receive the Exception:
groovy.lang.MissingPropertyException: No such property: NAME for class: People. Possible solutions: name
I guess I can modify sql query to include double quotes on the alias, but have you guys handled it any different?

The rows() method returns a List<GroovyRowResult> where GroovyRowResult implements Map and then you can apply collectEntries to transform it, so that the keys are lowercase and you can use the resulting map:
sql.rows('select * from people').each {
People p = new People(it.collectEntries { k,v -> [k.toLowerCase(), v] })
println p.name
println p.age
}

Related

How to sort list of objects by two properties and collator

I have to sort list of object by lastName and firstName (if case there is the same lastName for multiple objects). I must also apply Collator to those.
Suppose I can do that for one property:
val collator = Collator.getInstance(context.getResources().getConfiguration().locale)
myList.sortedWith(compareBy(collator, { it.lastName.toLowerCase() }))
But is it possible to apply also to that another restriction to also sort by firstName?
You can add another sort criteria with thenBy:
val comparator =
compareBy(collator) { p: Person -> p.lastName.toLowerCase() }
.thenBy(collator) { p: Person -> p.firstName.toLowerCase() }
val result = myList.sortedWith(comparator)
Simplest would just be to concatenate the two properties in the selector lambda:
myList.sortedWith(
compareBy(collator) { "${it.lastName} ${it.firstName}".toLowerCase() }
)

Add record in listGrid with variable

I am adding record in grid using startEditingNew method as below.
var COLUMN_NAME = {
name : "user_name",
lastname : "user_surname",
age : "user_age"
};
addDataToGrid : function (name, lastname, age){
MyGrid_Grid.startEditingNew({
COLUMN_NAME.name: name,
COLUMN_NAME.lastname: lastname,
COLUMN_NAME.age: age
});
}
But, my above function raise error and does not add record to grid.
If I use "user_name" string instead of "COLUMN_NAME.name" , It works fine.
How can I use variable as column name??
Thanks in advance
This is a javascript question, not a SmartClient one.
Anyway, if you need to use that approach, you may write something like:
addDataToGrid : function (name, lastname, age){
var record = {};
record[COLUMN_NAME.name] = name;
record[COLUMN_NAME.lastname] = lastname;
record[COLUMN_NAME.age] = age;
MyGrid_Grid.startEditingNew(record);
}
see also Rules for unquoted JavaScript Object Literal Keys?

grails: show field of object in gsp

In my grails project I create instances of an object called Patient, that has got a field city.
class Patient {
String name;
String surname;
String cf;
String address;
String cap;
String city;
String province;
String country;
String phone_1;
String phone_2;
String email;
String vat;
}
When I create a Patient, I store city by its id, but I want that, in show() and list() methods, I see the name of the related city.
The Cities domain class is linked to a table in my db as follows
class Cities {
String cityName;
String capOfCity;
String provinceOfCity;
static constraints = {
}
static mapping = {
table 's_cap'
id column: 'idcap'
cityName column: 'comune'
capOfCity column: 'cap'
provinceOfCity column: 'prov'
version false
}
}
I suppose that I must perform a query to db to get its name by id, but how can I do it in gsp?
With your current approach you can do
def show(){
// look up your patient object
def patient = Patient.get(123)
def patientCityObject = Cities.findByCityName(patient.city)
[patientCityObject: patientCityObject ]
}
GSP:
<p>${patientCityObject.cityName}</p>
However,
If you define the association between your domains then Grails will load your city when you are accessing it. To access you city object associated with Patient, you can define it as follow:
class Patient {
...
Cities city;
...
}
Then when you have the patient object, you can easily access its property city.
def patient = Patient.get(123)
patient.city.cityName
This will give you the city object associated with your patient. Then you can pass that into your GSP and use it.
To learn more about GORM and object relation you can read Object Relational Mapping

JasperReports for grails: Using HashMap as Model?

Imagine I have two classes in Groovy that look like that:
class Person {
int id;
String name;
}
class Item {
int id;
int price;
}
Now it would be simple to create a JasperReport listing all persons' names using the following SQL:
SELECT name FROM Person
Also, it would be easy to pass a Model from which the list should be created:
def p = Person.withCriteria {
eq('name','SomeGuy')
}
chain(controller:'jasper',action:'index',model:[data:p],params:params)
But what I want to do is to use the following query:
SELECT name, (SELECT sum(i.price) FROM Item f WHERE f.id=p.id) as priceSum FROM Person p
And now this is the part where I don't know how to go on: How can I pass any Model to the jasperReport? I can't just use
def p = Person.withCriteria {
eq('name','SomeGuy')
}
because then, the priceSum attribute would be missing.
What I would like to do is something like this:
def l = ['name':'SomeGuy','priceSum':'5000']
chain(controller:'jasper',action:'index',model:[data:l],params:params)
But this doesn't work either, it gives me:
Message: Cannot get property 'name' on null object
Caused by: java.lang.NullPointerException: Cannot get property 'name' on null object
Is there anything simliar to this that would work?

Criteria API - Using UPPER in a ElementCollection

I have a class
#Entity
public class Person{
...
#ElementCollection
private Set<String> tags;
...
}
I want to use the JPA 2.0 Criteria API to search over these tags - but in a case insensitive way. Thus I want to both set the search parameter to UPPER and the column to UPPER (Pseudo-SQL: select p.* from Person p join Tags t on p.id=t.pId where upper(t.name)=upper('searchParameter'); )
Here is my code without the UPPER on the tags:
CriteriaBuilder builder = this.em.getCriteriaBuilder();
CriteriaQuery<Person> query = builder.createQuery(Person.class);
Root<Person> root = query.from(Person.class);
return this.em.createQuery(query.select(root).where(
builder.isMember(builder.upper(builder.literal(searchTag)),
root.get(Person_.tags)))).getResultList();
where searchTag is the input parameter.
How can I assign the UPPER to the Person_.tags "Column"?
In other words I want to write this Query with Criteria API:
SELECT p FROM Person p JOIN p.tags t WHERE UPPER(t) = UPPER('searchString')
Thank you
Ok, I finally have the solution:
cQuery.where(
builder.equal(
builder.upper(cQuery.from(Relation.class).join(Relation_.aliase)
.as(String.class)),
builder.upper(builder.literal(alias))
)
);
One has to use the ".as(..)" method.
suburbCriteria = criteriaBuilder.equal(
criteriaBuilder.upper(root.get(Property_.suburb)),
criteriaBuilder.upper(criteriaBuilder.literal(searchBean.getSuburb())));
You need to call upper and literal property name on joined Tags from Person.
CriteriaBuilder builder = this.em.getCriteriaBuilder();
CriteriaQuery<Person> query = builder.createQuery(Person.class);
Root<Person> root = query.from(Person.class);
Join<Person, Tag> tags = root.join(Person_.tags);
query.where(builder.isMember(
builder.upper(builder.literal(searchTag)),
builder.upper(builder.literal(tags.get(Tag_.name)))
));
return this.em.createQuery(query).getResultList();
I omitted query.select(root), not sure if it is required.
Tou can also omit builder.literal() on Tag_.name since is String.
If I am wrong somewhere, please edit my answer for further users.
Hope this helps.