Grails querying the database - grails

I am trying to query a database within grails using:
def per = User.get(springSecurityService.principal.id)
def query = Post.whereAny {
author { username == per.username }
}.order 'dateCreated', 'desc'
messages = query.list(max: 10)
My User is in a User domain and in the Post domain I have:
String message
User author
Date dateCreated
when I run this query its empty every time my method in the controller for populating the database is:
def updateStatus() {
def status = new Post(message: params.message)
System.out.println("test " + params.message)
status.author = User.get(springSecurityService.principal.id)
status.save()
}
I am very new at quering databases in grails so if anyone could recommend some reading this would be good too.
Thanks

New version of spring-security-core plugin add methods to you controller, thus you can replace:
def per = User.get(springSecurityService.principal.id)
def query = Post.whereAny {
author { username == per.username }
}.order 'dateCreated', 'desc'
messages = query.list(max: 10)
with
messages = Post.findAllByAuthor(principal, [sort: 'dateCreated', order 'desc', max: 10])
Without declaring springSecurityService
You can replace the updateStatus action with:
def updateStatus(){
def status = new Post(message: params.message, author: principal)
status.save()
}

Empty query may be resolved with:
def updateStatus() {
def status = new Post(message: params.message)
System.out.println("test " + params.message)
status.author = User.get(springSecurityService.principal.id)
status.save(flush: true)
}
This may help you find the data later as the save() just gives your hibernate context the instruction to persist the data, but does not force hibernate to do it at that moment. passing flush:true will force data to be persisted immediately. Also status.save(failOnError:true) will reveal if your domain object properties have validation issues preventing the domain instance from being saved at all to the database (by throwing an Exception). You can use both if desired:
def updateStatus() {
def status = new Post(message: params.message)
System.out.println("test " + params.message)
status.author = User.get(springSecurityService.principal.id)
status.save(flush: true, failOnError: true)
}
hope that helps.
You can set flush and failOnError: true globally as well in grails-app/conf/Config.groovy:
grails.gorm.failOnError=true
grails.gorm.autoFlush=true

Related

Defining OR Condition with grails criteria api

I have the following domain objects:
class User {
String name
Transaction transaction
static constraints = {
transaction nullable: true
}
}
class Transaction {
boolean successful
User user
static belongsTo = User
}
I want to select all users that have no successful transaction. This means I want the users without any transaction (transaction == null) and the users that have a transaction with the successful value false (transaction.successful == false). I want to do this with the Criteria API (because this can be combined with other conditions based on user input).
I tried this:
def c = User.createCriteria()
def results = c {
or {
isNull 'transaction'
transaction {
eq 'successful', false
}
}
}
However this gives me only the users that have a transaction (with the successful value false). But I do not get the users where transaction is null
The following code shows how I created some sample data:
def createUserAndTransaction(String name, Boolean successful = null) {
User u = new User(name: name)
if (successful != null) {
Transaction t = new Transaction(user: u, successful: successful)
u.setTransaction(t)
}
u.save()
}
def init = { servletContext ->
createUserAndTransaction 'john', true
createUserAndTransaction 'mike', false
createUserAndTransaction 'pablo'
}
My criteria query only returns mike in this case. But I want mike and pablo. What am I missing?
So the problem is that it defaults to an inner join. You have to create an alias for the table and define its join type:
createAlias("transaction", "t", CriteriaSpecification.LEFT_JOIN)
or {
isNull("t")
eq("t.successful", false)
}

Domain class hasMany fails to add entry

I'm a Grails noob so please excuse my noob question.
I've created a domain classes User and Device. User hasMany devices:Device, and Device belongsTo user:User.
It is important that only 1 device will never belong to two users so my UserController code looks like this:
class UserController {
static allowedMethods = [create: 'POST']
def index() { }
def create() {
def user = User.findByUsername(request.JSON?.username)
def device = Device.findById(request.JSON?.deviceId)
if (device) {
device.user.devices.remove(device)
}
// device can only be owned by 1 person
def new_device = new Device(id: request.JSON?.deviceId, type: request.JSON?.deviceType)
if ( !user ) {
user = new User(
username: request.JSON?.username
)
user.devices = new HashSet() // without this I get null on the add in next line
user.devices.add(new_device)
user.save()
if(user.hasErrors()){
println user.errors
}
render "user.create " + request.JSON?.username + " devices.size " + user.devices.size()
} else {
user.devices.add( new_device )
user.save()
if(user.hasErrors()){
println user.errors
}
render "user.create exists, new token: " + user.token + " devices.size " + user.devices.size()
}
}
}
But now I get a strange server error:
null id in Device entry (don't flush the Session after an exception occurs)
What am I missing here??
Thanks a lot!
First of all, there are special methods to add to and remove from. Do not operate straight on hasMany collections. Maybe this is problematic.

Filtering filtered data

I'm new to grails and MVC so please bear with me.
I have some links on my GSP that do some static filtering. For instance, the example below returns only
those Request domain class instances with status Open. But I also want to be able to do some dynamic filtering on the same model (results in the code bellow).
Use case would be something like this: User sees all Request domain class instances in the table. He clicks on the link Open requests and gets only those Request instances that have status property with value Open. Than he sets dateFrom and dateTo using date picker control and clicks on the Filter button which calls the method/action that further filters data from the table. So it should return only those request that are opened and that are created within the specified period.
def openedRequests = {
def contact = Contact?.findByUser(springSecurityService.currentUser)
def productlines = contact.productlines()
def requestCriteria = Request.createCriteria()
def results = requestCriteria.list {
eq("status", "Open")
and {
'in'("productline",productlines)
}
}
render(view:'supportList', model:[requestInstanceList:results, requestInstanceTotal: results.totalCount])
}
EDIT
On my GSP I have few links that call controller actions which perform some domain class instances filtering. For example I have OpenedRequests, ClosedRequests, NewRequests. But I also have some textboxes, comboboxes, datePicker controls for additional filtering. I call the filterRequests action with a button.
def filterRequests = {
def contact = Contact?.findByUser(springSecurityService.currentUser)
def productlines = contact.productlines()
def requestCriteria = Request.createCriteria()
def results = requestCriteria.list {
if(params.fDateFrom && params.fDateTo){
def dateFrom = new SimpleDateFormat("dd.MM.yyyy").parse(params.fDateFrom_value)
def dateTo = new SimpleDateFormat("dd.MM.yyyy").parse(params.fDateTo_value)
between("dateCreated",dateFrom,dateTo)
}
if(params?.fStatus){
eq("status",params.fStatus)
}
if(params?.fCompany){
eq("company", params.fCompany)
}
and {'in'("productline",productlines)
}
if(params.sort != null && params.order != null){
order(params.sort, params.order)
}
}
render(view:'supportList', model:[requestInstanceList:results, requestInstanceTotal: results.totalCount])
}
I want to be able to filter Request instances with some of mentioned links and than if I set up some additional filters, for example dateFrom i dateTo with datePicker. I want those filters to be aware of previous filtering with link if there were any. What is the right way to do this?
You can use DetachedCriterias which where introduced with Grails 2.0.
A DetachedCriteria is independed from any session and can be reused easily:
def openRequests = new DetachedCriteria(Request).build {
eq("status", "Open")
and {
'in'("productline",productlines)
}
}
Then upon your next sub-filter request you can reuse the DetachedCriteria and perform a sub-query on it, like:
def results = openRequests.findByStartDateBetweenAndEndDateBetween(dateFrom, dateTo, dateFrom, dateTo)
Of course you have to remember somehow what the original query was (session, request param), to use the correct criteria as a basis for the sub-query.
(Disclaimer: I haven't yet tried detached criterias myself)
David suggested that I use Detached Criteria but I am using Grails 1.3.7 for my app. So, at the moment this isn't an option. I also thought of using database views and stored procedures but I wasn't sure how that will work with Grails (but that is something that I will definitely have to explore) and I wanted some results fast so I did something not very DRY. When I filter table with one of the mentioned links I save the name of the link/action in session and in filterRequest action (that does additional filtering) I check the session to see if there has been any previous 'link filtering' and if it were I apply those filters on the table with criteria, and after that I apply the filters that were manualy entered. I don't like it but that's all I came up with with my limited understanding of Grails. Below is my filterRequest action:
def filterRequests = {
def contact = Contact?.findByUser(springSecurityService.currentUser)
def productlines = contact.productlines()
def requestCriteria = Request.createCriteria()
def results = requestCriteria.list {
if(session.filter == "newRequests"){
and{
isNull("acceptedBy")
ne("status", "Closed")
}
}
if(session.filter == "openRequests"){
and{
ne("status",'Closed')
}
}
if(session.filter == "closedRequests"){
and{
eq("status", "Closed")
}
}
if(session.filter == "myRequests"){
and{
eq("acceptedBy", contact.realname)
}
}
if(params.fDateFrom && params.fDateTo){
def dateFrom = new SimpleDateFormat("dd.MM.yyyy").parse(params.fDateFrom_value)
def dateTo = new SimpleDateFormat("dd.MM.yyyy").parse(params.fDateTo_value)
and{
between("dateCreated",dateFrom,dateTo)
}
}
if(params?.fAcceptedBy){
and{
eq("acceptedBy", params.fAcceptedBy)
}
}
if(params?.fStartedBy){
and{
eq("startedBy", params.fStartedBy)
}
}
if(params?.fCompany){
and{
ilike("company", "%" + params.fCompany +"%")
}
}
and {'in'("productline",productlines)
}
if(params.sort != null && params.order != null){
order(params.sort, params.order)
}
}
}

Problem with Grails GORM

I am developing an application which queries data from an XML file and creates multiple objects with that data.
class Search {String artist}
class Performance {static belongsTo = [events:Event, artists:Artist]}
class Location {static belongsTo = [events:Event]}
class Event {static hasMany = [performances:Performance]}
class Artist {static hasMany = [performances:Performance]}
This are the domain classes (for the sake of simplicity only relationships are shown).
Then I want to create instances of this objects when the user inserts a new artist in the SearchController. I tried to do that with the following code for the save closure in the SearchController but it seems that it's not working. The resultList is a Map with the values queried from the XML file.
def save = {
def searchInstance = new Search(params)
def resultsList = searchService.lastFmVenues(params.artist)
resultsList.each{
def performanceInstance = new Performance()
def locationInstance = new Location(venue:it.venue, street:it.street, city:it.city, postcode:it.postalcode, country:it.country, lat:it.lat, lng:it.lng)
def artistInstance = new Artist(name:params.artist).addToPerformances(performanceInstance)
def eventInstance = new Event(eventId:it.eventID, title:it.eventTitle, date:it.date, location:locationInstance)
if (searchInstance.save(flush:true) && eventInstance.save(flush: true) && artistInstance.save(flush: true) && locationInstance.save(flush: true) && performanceInstance.save(flush:true)) {
flash.message = "${message(code: 'default.created.message', args: [message(code: 'search.label', default: 'Search'), searchInstance.id])}"
}
else {
render(view: "create", model: [searchInstance: searchInstance])
}
}
redirect(action: "show", id: searchInstance.id)
}
Any ideas?
Thank you.
Try saving your objects with save(failOnError: true). This will cause grails to throw an exception if the objects don't validate. The default behavior is to simply return false from the save method.
You can make failOnError the default behavior by setting grails.gorm.failOnError=true in your Config.groovy, but I wouldn't recommend it for anything besides troubleshooting.

httpSession in Grails

I need to access the domain class User of the current session. The following code works:
class RatingController {
def rate = {
def rating = params.rating
def artist = Artist.get( params.id )
def user = User.get(1)
user.addToRatings(new Rating(artist:artist, rating:rating))
user.save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
}
But instead of explicitly get the user with ID equal 1 (User.get(1)) I need to access the user of the current session. I tried the following code, but it doesn't work:
class RatingController {
def rate = {
def rating = params.rating
def artist = Artist.get( params.id )
def user = user.session
user.addToRatings(new Rating(artist:artist, rating:rating))
user.save()
render(template: "/artist/rate", model: [artist: artist, rating: rating])
}
}
I'm still struggling to fully understand the httpSession concept, so a little help would be great.
Thank in advance.
UPDATE
In my UserController, my authentication looks like this:
def authenticate = {
def user = User.findByLoginAndPassword(params.login, params.password)
if(user){
session.user = user
flash.message = "Hello ${user.name}!"
redirect(controller:"event", action:"list")
}else{
flash.message = "Sorry, ${params.login}. Please try again."
redirect(action:"create")
}
}
the http session is nothing more than data that is maintained among a sequence of requests from a single source, like a browser.
To put something on the session, just do
session.setAttribute("key", value)
and to get data off the session just do
session.getAttribute("key")
Grails also adds some fanciness to session access as outlined here. Their example shows
def user = session["user"]
session["user"] = "John"
asset "John" == session.user
Note that if you are using a grails plugin for authentication and authorization, it will probably provide a way to get the user. For example Spring Security gives you the following
springSecurityService.getCurrentUser()
depending on the details of your plugin, that user might or might not be on the session.
In your code I noticed you wrote
def user = user.session
When I think you mean
def user = session.user
The other thing I do is make it session.userId ie session.userId = user.id and then
def user = User.get(session.userId)
Not sure if that's necessary.
You'll need to use .merge():
user.merge(flush: true)
This is because the instance is detached from the persistence context when you save it to HttpSession. Here is the doc from grails:
http://grails.org/doc/2.3.1/ref/Domain%20Classes/merge.html

Resources