Grails removeFrom() issue - grails

Inside my Applicant domain class I have the following:
static hasMany = [recommendationFiles:ApplicantFile]
static mapping = {recommendationFiles joinTable: [name:"LETTER_FILES", key: "APPLICANT_ID", column: "LETTER_ID"]}
When I do the following:
def applicant = Applicant.findByENumber(session.user.eNumber)
def applicantFiles = applicant.recommendationFiles
println applicantFiles
applicantFiles.each {
applicant.removeFromRecommendationFiles(it)
}
applicant.save(flush:true)
I get this as an error which makes no sense to me:
| Error 2015-04-08 10:41:59,570 [http-bio-8080-exec-10] ERROR errors.GrailsExceptionResolver - ConcurrentModificationException occurred when processi
ng request: [POST] /scholarshipsystem/specialized/index - parameters:
_action_reUpload: Re-Upload
Stacktrace follows:
Message: null
Line | Method
->> 793 | nextEntry in java.util.HashMap$HashIterator
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 828 | next in java.util.HashMap$KeyIterator
| 106 | reUpload in scholarshipSystem.SpecializedController$$EP9HMKbd
| 195 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 918 | run . . . in ''
^ 662 | run in java.lang.Thread

There's a few ways to make this work. One way is to convert the list to an array and iterate over it.
def list = applicantFiles.toArray()
list.each {
applicant.removeFromRecommendationFiles(it)
}
Another way, if you're just removing the entire collection would be to...
applicant.recommendationFiles*.delete()
applicant.recommendationFiles.clear()

Related

How can I run GEB from a Grails service?

I'm trying to run GEB from a web-service on Grails since I need to extract data from a page and use it on my aplication.
The server is suposed to scrap the info and then return it into a view.
If I run it from the grails console it works fine but when I run it from the service it doesn't work
here's the code
package com.bamboozled
import geb.Browser
import grails.transaction.Transactional
#Transactional
class ScraperService {
Data scrapIt(String nombreCiclo, String numeroSolicitud) {
//Variable con los datos a retornar
def d = new Data()
//Inicia el bot
Browser.drive {
go "http://some.page/page.php"
$("form").ciclo = nombreCiclo
$("form").nosol = numeroSolicitud
$("form").find("input", name: "BOTONACEPTAR").click()
def a = getAvailableWindows()
def data = [" "," "," "," "," "]
withWindow(a[1]) {
data[0] = $("td")[5].text()
data[1] = $("td")[6].text()
data[2] = $("td")[7].text()
data[3] = $("td")[8].text()
data[4] = $("td")[9].text()
}
d.numeroSolicitud = data[0]
d.avance = data[1]
d.seEncuentraEn = data[2]
d.numeroCheque = data[3]
d.cheque = data[4]
}
return d
}
}
Here is the controller where I call it
package com.bamboozled
class DataController {
static scaffold = true
def ScraperService
def index() { }
def form() {
}
def consulta(){
def p = Ciclo.findAll()
[solicitudes : p]
}
def resultados() {
def d = ScraperService.scrapIt(params.nombreCiclo, params.numeroSolicitud)
[res : d]
}
}
When I run it it gives this error
URI
/bamboozled/data/resultados
Class
java.lang.NoClassDefFoundError
Message
geb/Browser
Around line 200 of PageFragmentCachingFilter.java
197: if (CollectionUtils.isEmpty(cacheOperations)) {
198: log.debug("No cacheable annotation found for {}:{} {}",
199: new Object[] { request.getMethod(), request.getRequestURI(), getContext() });
200: chain.doFilter(request, response);
201: return;
202: }
203:
Around line 63 of AbstractFilter.java
60: try {
61: // NO_FILTER set for RequestDispatcher forwards to avoid double gzipping
62: if (filterNotDisabled(request)) {
63: doFilter(request, response, chain);
64: }
65: else {
66: chain.doFilter(req, res);
Around line 17 of grails-app/services/com/bamboozled/ScraperService.groovy
14: def d = new Data()
15:
16: //Inicia el bot
17: Browser.drive {
18:
19: go "http://http://some.page/page.php"
20:
Around line 20 of grails-app/controllers/com/bamboozled/DataController.groovy
17: }
18:
19: def resultados() {
20: def d = ScraperService.scrapIt(params.nombreCiclo, params.numeroSolicitud)
21: [res : d]
22: }
23:
Trace
Line | Method
->> 200 | doFilter in PageFragmentCachingFilter.java
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 63 | doFilter in AbstractFilter.java
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 748 | run . . . in java.lang.Thread
Caused by ControllerExecutionException: Runtime error executing action
->> 200 | doFilter in PageFragmentCachingFilter.java
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 63 | doFilter in AbstractFilter.java
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 748 | run . . . in java.lang.Thread
Caused by InvocationTargetException: null
->> 200 | doFilter in PageFragmentCachingFilter.java
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 63 | doFilter in AbstractFilter.java
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 748 | run . . . in java.lang.Thread
Caused by NoClassDefFoundError: geb/Browser
->> 17 | $tt__scrapIt in ScraperService.groovy
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 20 | resultados in DataController.groovy
| 200 | doFilter in PageFragmentCachingFilter.java
| 63 | doFilter in AbstractFilter.java
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 748 | run . . . in java.lang.Thread
Any idea of how can I make it run?

Grails 2.4.4 file upload error when the file exists

I have an action that receives a file correctly and saves it to a destination folder without problems.
When the destination folder has a file with the same name, the method transferTo, first deletes the existing file, then copies the new one (http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/commons/CommonsMultipartFile.html#transferTo-java.io.File-).
But if the destination file exists, Grails is throwing this error, and the existing file is deleted but the uploaded one is not copied.
I'm working in WinXP, so I don't think it is a permissions issues (the file is deleted so I guess has nothing to do with permissions).
| Error 2015-05-24 23:47:58,199 [http-bio-8090-exec-3] ERROR errors.GrailsExceptionResolver - FileNotFoundException occurred when processing request: [POST] /ehr/test/upload - parameters:
doit: upload
overwrite: true
SYNCHRONIZER_TOKEN: 8deaf46b-b6ff-4362-ac70-7223f37ae806
SYNCHRONIZER_URI: /ehr/test/upload
opts\Signos.opt (Access is denied). Stacktrace follows:
Message: opts\Signos.opt (Access is denied)
Line | Method
->> 221 | <init> in java.io.FileOutputStream
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 171 | <init> in ''
| 417 | write . . in org.apache.commons.fileupload.disk.DiskFileItem
| 85 | upload in test.TestController
| 198 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 744 | run . . . in java.lang.Thread
The upload action looks like:
def upload(boolean overwrite)
{
if (params.doit)
{
def errors = []
def f = request.getFile('opt')
def xml = new String( f.getBytes() )
def destination = config.opt_repo + f.getOriginalFilename()
File fileDest = new File( destination )
if (!overwrite && fileDest.exists())
{
errors << "The OPT already exists, do you want to overwrite?"
return [errors: errors, ask_overwrite: true]
}
// Some validation logic here ...
if (errors.size() == 0)
{
// http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/commons/CommonsMultipartFile.html#transferTo-java.io.File-
// If the file exists, it will be deleted first
f.transferTo(fileDest)
}
}
}

NullPointer exception when scaffolding first Grails app

Starting out with Grails I have installed Grails version 2.4.0 and using JDK 1.7.0_55 on GNU/Linux I do the sequence of actions below but keep getting a NullPointerException. Can someone provide a pointer to what I am missing or doing wrong ?
grails create-app firstgrails
cd firstgrails
grails
grails> run-app
I go to localhost:8080/firstgrails and see the generated page.
Next I do
grails> create-domain-class tag
and go to the firstgrails/grails-app/domain/firstgrails folder and edit Tag.groovy so it contains:
// Tag.groovy
package firstgrails
class Tag {
String name
String description
}
Next
grails> create-controller tag
and edit firstgrails/grailsapp/controllers/firstgrails/TagController.groovy into
// TagController.groovy
package firstgrails
class TagController {
static scaffold = Tag
}
I do a reload in the browser and firstgrails.TagController is added to the list of available
controllers under the welcome message. Selecting it, I create a tag with name and description and the tag shows up in the list of tags as expected. But now whenever I try to add a second tag I get an 500 error: Internal Server Error with
URI: /firstgrails/tag/create
Class: java.lang.NullPointerException
Message: null
The trace in the console is:
Error 2014-06-03 18:28:31,714 [http-bio-8080-exec-2] ERROR errors.GrailsExceptionResolver - NullPointerException occurred when processing request: [GET] /firstgrails/tag/create
Stacktrace follows:
Message: Error processing GroovyPageView: Error executing tag <g:form>: Error executing tag <g:render>: null
Line | Method
->> 527 | doFilter in /tag/create
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Caused by GrailsTagException: Error executing tag <g:form>: Error executing tag <g:render>: null
->> 35 | doCall in /tag/create
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Caused by GrailsTagException: Error executing tag <g:render>: null
->> 30 | doCall in /tag/create
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Caused by NullPointerException: null
->> 333 | hash in java.util.concurrent.ConcurrentHashMap
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 988 | get in ''
| 141 | getValue in grails.util.CacheEntry
| 81 | getValue in ''
| 73 | doCall . in tag_create$_run_closure2_closure24
| 78 | doCall in tag_create$_run_closure2
| 81 | run . . . in tag_create
| 189 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run in java.lang.Thread
| Error 2014-06-03 18:30:33,050 [http-bio-8080-exec-9] ERROR errors.GrailsExceptionResolver - NullPointerException occurred when processing request: [GET] /firstgrails/tag/create
Stacktrace follows:
Message: Error processing GroovyPageView: Error executing tag <g:form>: Error executing tag <g:render>: null
Line | Method
->> 527 | doFilter in /tag/create
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Caused by GrailsTagException: Error executing tag <g:form>: Error executing tag <g:render>: null
->> 35 | doCall in /tag/create
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Caused by GrailsTagException: Error executing tag <g:render>: null
->> 30 | doCall in /tag/create
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Caused by NullPointerException: null
->> 333 | hash in java.util.concurrent.ConcurrentHashMap
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 988 | get in ''
| 141 | getValue in grails.util.CacheEntry
| 81 | getValue in ''
| 73 | doCall . in tag_create$_run_closure2_closure24
| 78 | doCall in tag_create$_run_closure2
| 81 | run . . . in tag_create
| 189 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run in java.lang.Thread
| Error 2014-06-03 18:32:21,796 [http-bio-8080-exec-5] ERROR errors.GrailsExceptionResolver - NullPointerException occurred when processing request: [GET] /firstgrails/tag/create
Stacktrace follows:
Message: Error processing GroovyPageView: Error executing tag <g:form>: Error executing tag <g:render>: null
Line | Method
->> 527 | doFilter in /tag/create
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Caused by GrailsTagException: Error executing tag <g:form>: Error executing tag <g:render>: null
->> 35 | doCall in /tag/create
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Caused by GrailsTagException: Error executing tag <g:render>: null
->> 30 | doCall in /tag/create
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Caused by NullPointerException: null
->> 333 | hash in java.util.concurrent.ConcurrentHashMap
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 988 | get in ''
| 141 | getValue in grails.util.CacheEntry
| 81 | getValue in ''
| 73 | doCall . in tag_create$_run_closure2_closure24
| 78 | doCall in tag_create$_run_closure2
| 81 | run . . . in tag_create
| 189 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run in java.lang.Thread
I tried this and able to reproduce it. If we generate view then it is resolved but without views it is giving error. There is an issue in grails jira, see this issue they have provided a patch for it.
Hope this helps.

Grails "don't flush the Session after an exception occurs" error message

I see that this error message has been posted several times already in the context of hibernate.
I am getting this error while using grails service and a domain class, any help will be really appreciated
Domain class
class Coupon {
Date dateCreated
Date lastUpdated
String code
String email
String address
String state
String city
String zip
def couponCodeGeneratorService
def beforeValidate() {
println code+"---------8-"
code = couponCodeGeneratorService.generate()
println code+"----------"
}
static constraints = {
email blank:false,email:true
address blank:false
state blank:false
city blank:false
zip blank:false
}
}
Service
class CouponCodeGeneratorService {
Random randomGenerator = new Random()
def serviceMethod() {
}
def generate(){
def group1 = randomGenerator.nextInt(9999)+"";
def group2 = randomGenerator.nextInt(9999)+"";
def group3 = randomGenerator.nextInt(9999)+"";
def group4 = randomGenerator.nextInt(9999)+"";
return group1.padLeft(4,"0") +group2.padLeft(4,"0")+group3.padLeft(4,"0")+group4.padLeft(4,"0")
}
}
The error I am getting is
---------8-
4844634041715590----------
4844634041715590---------8-
| Error 2012-09-10 11:32:54,938 [http-bio-8080-exec-7] ERROR hibernate.AssertionFailure - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
Message: null id in com.easytha.Coupon entry (don't flush the Session after an exception occurs)
Line | Method
->> 19 | beforeValidate in com.easytha.Coupon
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 46 | onApplicationEvent in org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener
| 24 | save . . . . . . . in com.easytha.CouponController
| 186 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter . . . . . in grails.plugin.cache.web.filter.AbstractFilter
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run . . . . . . . in ''
^ 662 | run in java.lang.Thread
| Error 2012-09-10 11:32:54,944 [http-bio-8080-exec-7] ERROR errors.GrailsExceptionResolver - AssertionFailure occurred when processing request: [POST] /EasyTha/coupon/save - parameters:
zip: asdf
address: asd
email: s.s#s.xom
state: asd
code:
create: Create
city: asdf
null id in com.easytha.Coupon entry (don't flush the Session after an exception occurs). Stacktrace follows:
Message: null id in com.easytha.Coupon entry (don't flush the Session after an exception occurs)
Line | Method
->> 19 | beforeValidate in com.easytha.Coupon
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 46 | onApplicationEvent in org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener
| 24 | save . . . . . . . in com.easytha.CouponController
| 186 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter . . . . . in grails.plugin.cache.web.filter.AbstractFilter
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run . . . . . . . in ''
^ 662 | run in java.lang.Thread
I am not very familiar with Hibernate, also is this a correct way to create a coupon code that looks like a credit card number?
I suspect the problem may be that the CouponCodeGeneratorService is transactional. Therefore, when you call the service method from inside your beforeValidate you're opening and closing a transaction (even though you don't touch the database inside the method), which among other things will cause another flush of the session.
Try making the service non-transactional:
static transactional = false

Grails/GORM belongsTo Back Reference Naming

I created the domains in Grails to focus on the business logic behind our process. In several cases, this translates to a back reference that GORM generates that exceeds the Oracle limits and generates a "ORA-00972: identifier is too long" error. I was able to use the static mapping block to remap the long table name, but I can't figure out how to do the same for the generated back reference.
Without exposing any company confidential information, the following example illustrates the problem.
class UnfortunatelyLongClassName {
static mapping = {
table "long_class" // This works great!
}
List<Part> parts
static hasMany = [parts:Part]
}
class Part {
String name
// This generates UNFORTUNATELY_LONG_CLASS_NAME_ID and causes the error
static belongsTo = [UnfortunatelyLongClassName]
}
Rough DDL of the tables generated...
LONG_CLASS (
ID number(19, 0) not null,
VERSION number(19, 0) not null,
primary key (id),
);
PART (
ID number(19, 0) not null,
VERSION number(19, 0) not null,
NAME varchar2(255),
PARTS_IDX number(10, 0),
UNFORTUNATELY_LONG_CLASS_NAME_ID number(19, 0) not null,
primary key (id),
foreign key FK589895C372DB95A (UNFORTUNATELY_LONG_CLASS_NAME_ID) references UNFORTUNATELY_LONG_CLASS_NAME(ID)
);
Are there any static mapping commands or other Grails/GORM tricks to get this to create a shorter identifier?
If I use the following...
static belongsTo = [unfortunatelyLongClassName:UnfortunatelyLongClassName]
static mapping = {
unfortunatelyLongClassName column:"ulcn_id"
}
I get the following errors...
| Error 2012-07-24 17:53:49,060 [pool-7-thread-1] ERROR context.ContextLoader - Context initialization failed
Message: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.InvalidPropertyException: No property found for name [unfortunatelyLongClassName] for class [class mycompany.myproject.mypackage.Part]
Line | Method
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 138 | run in java.util.concurrent.FutureTask
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run in ''
^ 662 | run . . in java.lang.Thread
Caused by InvalidPropertyException: No property found for name [unfortunatelyLongClassName] for class [class mycompany.myproject.mypackage.Part]
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 138 | run in java.util.concurrent.FutureTask
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run in ''
^ 662 | run . . in java.lang.Thread
| Error 2012-07-24 17:53:49,094 [pool-7-thread-1] ERROR context.GrailsContextLoader - Error executing bootstraps: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.InvalidPropertyException: No property found for name [unfortunatelyLongClassName] for class [class mycompany.myproject.mypackage.Part]
Message: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.InvalidPropertyException: No property found for name [unfortunatelyLongClassName] for class [class mycompany.myproject.mypackage.Part]
Line | Method
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 138 | run in java.util.concurrent.FutureTask
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run in ''
^ 662 | run . . in java.lang.Thread
Caused by InvalidPropertyException: No property found for name [unfortunatelyLongClassName] for class [class mycompany.myproject.mypackage.Part]
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 138 | run in java.util.concurrent.FutureTask
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run in ''
^ 662 | run . . in java.lang.Thread
Use the Map syntax in belongsTo to generate the backlink and rename it via a mapping:
static belongsTo = [unfortunatelyLongClassName:UnfortunatelyLongClassName]
static mapping = {
unfortunatelyLongClassName column:"ulcn_id"
}
belongsTo should also work with fields in your Domain class, so you could do:
UnfortunatelyLongClassName unfortunatelyLongClassName
static belongsTo = UnfortunatelyLongClassName
static mapping = {
unfortunatelyLongClassName column:"ulcn_id"
}
Since the previous versions throw missing property exceptions, you could try creating the property with a short name and skipping the mapping block:
UnfortunatelyLongClassName ulcn
static belongsTo = UnfortunatelyLongClassName
What if you try to rename the id of the long class? Like:
static mapping = {
id name: 'simple_id_name'
}

Resources