Does anyone know of a way to avoid repeating a closure when emitting the same object more than once using the latest version of the Grails JSONBuilder?
I have a Group domain object that contains sets of Members and Leaders. I would like to find a way to emit a person without having to cut and paste the closure.
def builder = new JSONBuilder()
def result = builder.build {
array {
Group.list().each { Group group ->
g = {
id = group.id
name = group.name
members = array {
group.members?.person?.sort().each { Person person ->
m = { // Person closure copy #1
id = person.id
firstName = person.firstName
lastName = person.lastName
}
}
}
leaders = array {
group.leaders?.person?.sort().each { Person person ->
l = { // Person closure copy #2
id = person.id
firstName = person.firstName
lastName = person.lastName
}
}
}
}
}
}
}
I have tried defining the closure separately but that leads to errors such as: exception: No such property: id for class:.
Some notes:
1) The domain objects in the example are greatly simplified. I'm using JSONBuilder instead of render Group.list() as JSON or render Group.list().encodeAsJSON because I need to control what parts of my objects are encoded.
2) I'll accept authoritative answers that explain why this can't be done.
After repeated failures using closures, I have a solution. It doesn't use a closure directly but instead uses a closure that returns a map.
class Person {
...
def toMap = {
def map = [:]
map["id"] = this.id
map["firstName"] = this.firstName
map["lastName"] = this.lastName
return map
}
}
def builder = new JSONBuilder()
def result = builder.build {
array {
Group.list().each { Group group ->
g = {
id = group.id
name = group.name
members = array {
group.members?.person?.sort().each { Person person ->
m(person.toMap())
}
}
leaders = array {
group.leaders?.person?.sort().each { Person person ->
l(person.toMap())
}
}
}
}
}
}
The m(person.toMap()) syntax isn't intuitive but it works and allows me to avoid repeating myself. This blog entry provides the details and explains the origin of the current Grails JSONBuilder.
You can execute another closure in the same "context" by setting the "delegate" for the closure.
Closures aren't multi-thread safe (only one delegate at a time) so you will have to clone the closure each time if the closures are shared in a singleton class or static variable.
This is just an idea of refactoring the code, it might not work (I didn't test it).
You can replace assignment (=) with "setProperty" and DSL method calls with "invokeMethod" if you have to dynamicly decide the property name or method name in the DSL.
(reference http://groovy.codehaus.org/api/groovy/lang/Closure.html)
def personClosure = { Person person, varname ->
setProperty(varname, {
id = person.id
firstName = person.firstName
lastName = person.lastName
})
}
def groupMembersClosure = { memberList, memberListVarName, memberVarName ->
personClosure.delegate = delegate
setProperty(memberListVarName, array {
memberList?.person?.sort().each personClosure, memberVarName
})
}
def builder = new JSONBuilder()
def result = builder.build {
array {
Group.list().each { Group group ->
g = {
id = group.id
name = group.name
groupMembersClosure.delegate = delegate
groupMembersClosure(group.members, 'members', 'm')
groupMembersClosure(group.leaders, 'leaders', 'l')
}
}
}
}
Related
I have such entities, stored in database using GORM (I omitted irrelevant fields):
class Payment {
static hasMany = [paymentEntries: PaymentEntry]
}
class PaymentEntry {
static hasOne = [category: PaymentCategory]
static belongsTo = [payment: Payment]
static constraints = {
category(nullable: true)
}
}
class PaymentCategory {
static hasMany = [payments: PaymentEntry, rules: PaymentRule]
String name
}
So we have Payments on the top, which can have many PaymentEntryies, and each PaymentEntry can belong to one PaymentCategory.
I need to select Payments, which meet some conditions, but only the ones that also have PaymentEntries belonging to specific categories. Currently I do it in 3 queries:
Select categories which fit to part of category name, or just omit category query (in such case we want all the payments regardless of their entries category):
private static List<PaymentCategory> getCategories(String category) {
def result = null
if (category != null && category.length() > 0) {
def c = PaymentCategory.createCriteria()
result = c {
ilike("name", "%${category}%")
}
}
result
}
Select PaymentEntries ids, based on PaymentCategory:
private static List<Long> paymentEntryIds(String category) {
def categories = getCategories(category)
def c = PaymentEntry.createCriteria()
def result = new ArrayList()
// If category is selected, but there is no matching category, return empty list
if (!categorySelectedButNoMatch(category, categories)) {
result = c {
if (category == null) {
isNull("category")
} else if (categories != null && categories.size() > 0) {
inList("category", categories)
}
projections {
property("id")
}
}
}
result
}
Finally select Payments, limited to the ones that contain specific PaymentEntries:
def paymentEntriesIds = paymentEntryIds(selectedCategory)
def c = Payment.createCriteria()
def result = new ArrayList()
// If there are no payment entries matching category criteria, we return empty list anyway, so there
// is no need querying payments.
if (paymentEntriesIds.size() > 0) {
result = c {
paymentEntries {
inList("id", paymentEntriesIds)
}
if (importEntryId != null) {
eq("importEntry.id", importEntryId)
}
if (query != null) {
ilike("description", query)
}
// Omitted ordering and paging
}
}
result
This works, but it runs 3 queries to the database. I'm pretty sure this code could be cleaner, and it could be done in less queries. All the ideas on how to improve it are welcome.
You can use at least 3 different methods: detached queries, subqueries of where queries and subqueries of HQL.
Example of DetachedQuery with criteria looks like:
def subquery = new DetachedCriteria(PaymentCategory).build {
projections {
groupProperty 'id'
}
ilike("name", "%${category}%")
}
Then you can use this subquery in another query:
return PaymentEntry.createCriteria().list {
category {
inList "id", subquery
}
}
I didn't test it on your domain exactly, this should just show the idea. Refer to DetachedCriteria part of GORM documentation: https://grails.github.io/grails-doc/latest/guide/single.html#detachedCriteria
We use it for some very complicated queries. Sometimes createAlias will be required to correctly work with associations.
This has to be something really easy, but am pretty new to Grails & Groovy. I am writing a Grails application in version 2.4.3.
There's a class called UserInfo
class UserInfo extends UserDetails {
String userID = ""
static constraints = {
userID blank:false
}
}
Now the UserDetails class:
class UserDetails {
String userName = ""
String userType = "" // 'Admin' or 'Guest' or 'IT' or 'Supervisor'
...................
//Lot more Informations
....................
static constraints = {
userName blank:false
userType blank:false
}
}
And now in another controller class i want to get the User Type information.
def getUserData = {
def type = []
int count = 0
UserInfo.each {
type [i] = it.userType; // Error: No such property: operator for class: UserInfo
i++;
}
}
The goal here is to get list of UserTypes ('Admin' or 'Guest' or 'IT' or 'Supervisor') and the total numbers of each user type.
For e.g., Admin = 230, Guest = 120, .........
The code above doesn't do that and in fact gives an error. I want to use the data above and plot a X - Y Graph.
Ideally i should have the data in a def in an array model as shown below.
def type = []
type << [totalNum: new Integer("230"), userType: 'Admin']
type << [totalNum: new Integer("120"), userType: 'Guest']
............................................................
............................................................
Once i have populated all the data into a def, then i could use Google Visualization to draw the graph easily.
As of now i still don't know how to extract the data from each instance of UserInfo class and then get it to a def.
Appreciate your help!
You are missing the list() method here:
UserInfo.list().each {...}
I don't think it's possible with the syntax you're using. UserInfo.each refers to a colosure over static members of the UserInfo class. However if you want to enumerate things, this can be done with the following syntax.
class UserInfo {
enum UserType {
Admin, Guest, IT
}
}
Enumerating over these then becomes
def getUserData = {
def types = []
def count = 0
UserInfo.UserType.each {
types << it
count++
}
}
However, in grails, with your original class, you're just missing a findAll() or list() call for a domain object. This however will produce duplicates if you have more than one of each user type.
def getUserData = {
def types = []
def count = 0
UserInfo.findAll().each{ UserInfo it ->
types << it.userType
count++
}
}
I have a method which returns Iqueryable result, but the result is based on an if else condition, where if condition satisfies then I will use "AssetDetails" class object ,otherwise "UserandClientdetails" object.
Here is the code:
private IQueryable<?> GetAssetDetails(ShareViewModel item)
{
...
if (type == "Video")
{
if (type == "Video")
{
return from meta in my.Assets().OfType<Model.Video>()
join content in my.Contents() on meta.ContentId equals content.ID
join channel in my.Channels() on content.ChannelId equals channel.ID
where meta.ID == item.ID
select new AssetDetails
{
ContentTitle = content.Title,
ChannelName = channel.ChannelName,
...
};
}
else
{ return from meta in my.Assets().OfType<Model.Client>()
join country in db.Countries on meta.ResellerCountry equals country.ID
where meta.ID == item.ID
select new UserAndClientDetails
{
Name = meta.ResellerName,
UserName = meta.ResellerEmail,
..
};}
So how to decide type of Iqueyable here at runtime??
So, I was able to verify that this works, so I'll go ahead and post it as an answer.
You can return IQueryable instead of the generic IQueryable<>. That will accept any IQueryable<T>. However, IQueryable, since it has no direct inner type, is very limited. So, you'll still likely need to cast to IQueryable<> at some other point in your code to get anything done:
// Piece of code where you know you are working with `IQueryable<AssetDetails>`
IQueryable<AssetDetails> assetDetails = GetAssetDetails(someItem);
That's a little dangerous, though, as you're assuming that your code is working perfectly and the right type of thing is being returned. Better would be:
try
{
var assetDetails = (IQueryable<AssetDetails>)GetAssetDetails(someItem);
// do something with `assetDetails`
}
catch (InvalidCastException)
{
// recover gracefully
}
What about using a base class ?
public abstract class BaseDetails
{
// ...
}
public class AssetDetails : BaseDetails
{
// ...
}
public class UserAndClientDetails: BaseDetails
{
// ...
}
Then you method would be like :
private IQueryable<BaseDetails> GetAssetDetails(ShareViewModel item)
{
// return either IQueryable<AssetDetails> or IQueryable<UserAndClientDetails>
}
class Client {
String name
static hasMany = [courses:Course]
}
class Course {
String name
static belongsTo = [client:Client]
}
I have this and I want to get all Clients that has a Course with name = "blabla"
I was trying to do : Clients.findWhere(Course.any { course -> course.name = "math" })
You can do this with criteria:
Client.withCriteria {
courses {
eq('name', 'math')
}
}
I believe that the following where query is equivalent to the above criteria:
Client.where { courses.name == 'math' }
or you may find you need another closure:
Client.where {
courses {
name == 'math'
}
}
but I rarely use where queries myself so I'm not 100% sure of that.
There are probably a lot of different syntactical expressions to achieve the same thing. I can say definitively that this works in my project though.
def ls = Client.list {
courses {
eq('name','math')
}
}
I want to make a copy of a domain object. What is the simplest way to accomplish this?
I realize I could create a new record, and then iterate over each of the fields copying the data field-by-field - but I figured there must be an easier way to do this...
In Rails there is a simple way to do this:
#rails < 3.1
new_record = old_record.clone
#rails >= 3.1
new_record = old_record.dup
Is there any equivalent in Grails?
I've adapted a piece of code that make the deep clone of domain classes. I've been using in my system and it works very well (in most of cases). The code bellow is an adaptation found in http://grails.1312388.n4.nabble.com/Fwd-How-to-copy-properties-of-a-domain-class-td3436759.html
In my application the user has the option to saveAs some type of objects and I use the deepClone to do that.
You can specify "not cloneable" properties. For that you need to specify a static map (in your class) with the properties that you don't want to clone, for example:
static notCloneable = ['quoteFlows','services']
static hasMany = [quotePacks: QuotePack, services: Service, clients: Client, quoteFlows: QuoteFlow]
static Object deepClone(domainInstanceToClone) {
//TODO: PRECISA ENTENDER ISSO! MB-249 no youtrack
//Algumas classes chegam aqui com nome da classe + _$$_javassist_XX
if (domainInstanceToClone.getClass().name.contains("_javassist"))
return null
//Our target instance for the instance we want to clone
// recursion
def newDomainInstance = domainInstanceToClone.getClass().newInstance()
//Returns a DefaultGrailsDomainClass (as interface GrailsDomainClass) for inspecting properties
GrailsClass domainClass = domainInstanceToClone.domainClass.grailsApplication.getDomainClass(newDomainInstance.getClass().name)
def notCloneable = domainClass.getPropertyValue("notCloneable")
for(DefaultGrailsDomainClassProperty prop in domainClass?.getPersistentProperties()) {
if (notCloneable && prop.name in notCloneable)
continue
if (prop.association) {
if (prop.owningSide) {
//we have to deep clone owned associations
if (prop.oneToOne) {
def newAssociationInstance = deepClone(domainInstanceToClone?."${prop.name}")
newDomainInstance."${prop.name}" = newAssociationInstance
} else {
domainInstanceToClone."${prop.name}".each { associationInstance ->
def newAssociationInstance = deepClone(associationInstance)
if (newAssociationInstance)
newDomainInstance."addTo${prop.name.capitalize()}"(newAssociationInstance)
}
}
} else {
if (!prop.bidirectional) {
//If the association isn't owned or the owner, then we can just do a shallow copy of the reference.
newDomainInstance."${prop.name}" = domainInstanceToClone."${prop.name}"
}
// ##JR
// Yes bidirectional and not owning. E.g. clone Report, belongsTo Organisation which hasMany
// manyToOne. Just add to the owning objects collection.
else {
//println "${prop.owningSide} - ${prop.name} - ${prop.oneToMany}"
//return
if (prop.manyToOne) {
newDomainInstance."${prop.name}" = domainInstanceToClone."${prop.name}"
def owningInstance = domainInstanceToClone."${prop.name}"
// Need to find the collection.
String otherSide = prop.otherSide.name.capitalize()
//println otherSide
//owningInstance."addTo${otherSide}"(newDomainInstance)
}
else if (prop.manyToMany) {
//newDomainInstance."${prop.name}" = [] as Set
domainInstanceToClone."${prop.name}".each {
//newDomainInstance."${prop.name}".add(it)
}
}
else if (prop.oneToMany) {
domainInstanceToClone."${prop.name}".each { associationInstance ->
def newAssociationInstance = deepClone(associationInstance)
newDomainInstance."addTo${prop.name.capitalize()}"(newAssociationInstance)
}
}
}
}
} else {
//If the property isn't an association then simply copy the value
newDomainInstance."${prop.name}" = domainInstanceToClone."${prop.name}"
if (prop.name == "dateCreated" || prop.name == "lastUpdated") {
newDomainInstance."${prop.name}" = null
}
}
}
return newDomainInstance
}
There is not. It has been requested http://jira.grails.org/browse/GRAILS-3532. Someone has added some code to that issue that might be helpful to you though.