I am receiving NumberFormatException error in my Grails code which is to sort movies into a database. The error suggests it is from the cron plugin. I have done my research and I have been trying to catch the error by using NumberFormatException but to no avail. I think the problem lies in the IndexService. Any help would be much appreciated.
The exact error is:
2014-07-25 10:09:07,779 [quartzScheduler_Worker-1] ERROR listeners.ExceptionPrinterJobListener - Exception occurred in job: Grails Job
Message: java.lang.NumberFormatException: For input string: "N/A"
I am using:
Grails version: 2.4.2
Groovy version: 2.3.3
JVM version: 1.7.0_51
quartz - 1.0.2
My code:
IndexService
package movie
import groovy.io.FileType
import groovy.json.JsonSlurper
class IndexService {
static transactional = false
def grailsApplication
private cleanText(String title)
{
//split string into array via .,+,[,] etc...
def splitTitle=(title.toLowerCase()).tokenize('+.[]!£$%^+=~#:; \t \n \r \f / \\ { } # & - ( )')
//Get rid of extention
splitTitle.remove(splitTitle.size()-1)
//Get rid of Dvdrip and author
//unwanted phrases
def unwanted=["dvdrip","eng","www","torentz","3xforum","bugz","ro","maxspeed","xvid","720p","bluray","yify","1080p","hd","x264","RARBG","mp3","mp4","flv","brrip","rip"]
//Get rid of dates
//get 2000 to current date to put into the unwanted list
for(i in 2000..3000){unwanted.add(i.toString())}
//def empty string to put all our cleaned up text into
def cleanedText = ""
//cleaning up word mess...
splitTitle.each {word->
if(!unwanted.contains(word))
{
cleanedText=cleanedText+word+" "
}
}
//returning with +'s
return (cleanedText[0..-2]).replaceAll("\\s","+")
}
def getInfo(String title)
{
//returned nice with +
def cleanTitle=cleanText(title)
//define my reader
def scan = new JsonSlurper()
def scanner =scan.parseText(("http://www.omdbapi.com/?i=&plot=full&t="+cleanTitle).toURL().text)
if(scanner.Response=="True")
{
/*
organisied like this:
String artwork
String title
Date dateReleased
String userRated
String lengthOfFilm
String genre
String plot
float rating
*/ //returns our info we have scraped
return [
true,
scanner.Poster,
scanner.Title,
new Date(scanner.Released),
scanner.Rated,
scanner.Runtime,
scanner.Genre,
scanner.Plot,
scanner.imdbRating.toFloat()/10
]
}
else
{
return [null,cleanTitle]
}
}
def indexFiles()
{
//returns fileLocation
def fileLocation=grailsApplication.config.grails.movie.location
//Setup as file object
def dir = new File(fileLocation)
//recurse all files
dir.eachFileRecurse (FileType.FILES) { file ->
println(file)
//only create a record if no file test if record exists
if(!Record.findByPathToFile(file.getCanonicalPath())) {
//get record propreties set
def record = new Record()
record.pathToFile = file.getCanonicalPath()
//call to get data of a film returns as a list
def info = getInfo(file.getName())
//check if everthing is alright
info.each{print(it)}
if (info[0] == null) {
try {
//set fallback propeties
record.artwork = null
record.title = info[1].replaceAll("\\+"," ")
record.genre = null
record.dateReleased = new Date(file.lastModified())
record.lengthOfFilm = null
record.plot = null
record.rating = 0
record.userRated = null
record.save(failOnError: true)
}
catch (NumberFormatException e) {
//catch any errors
log.error("Error caught :${e} \n")
}
}
else
{
try
{
//set good propeties
record.artwork = info[1]
record.title = info[2]
record.genre = info[6]
record.dateReleased = info[3]
record.lengthOfFilm = info[5]
record.plot = info[7]
print(info[8])
record.rating = info[8]
record.userRated = info[4]
record.save(failOnError: true)
}
catch (NumberFormatException e) {
//catch any errors
info.each
{
print(it)
}
log.error("Error caught :${e} \n")
}
}
}
}
}
}
Record domain
package movie
class Record {
//static searchable = true
//data will be recieved via http://www.omdbapi.com/
String artwork
String title
Date dateReleased
String pathToFile
String userRated
String lengthOfFilm
String genre
String plot
float rating
static constraints = {
artwork nullable: true
title nullable: false, unique: true
dateReleased nullable: false
pathToFile nullable: false
userRated nullable: true
lengthOfFilm nullable: true
genre nullable: true
plot nullable: true, maxSize: 2000
rating nullable: true
}
}
And finally IndexJob
package movie
class IndexJob {
static triggers = {
/*
cronExpression: "s m h D M W Y"
| | | | | | `- Year [optional]
| | | | | `- Day of Week, 1-7 or SUN-SAT, ?
| | | | `- Month, 1-12 or JAN-DEC
| | | `- Day of Month, 1-31, ?
| | `- Hour, 0-23
| `- Minute, 0-59
`- Second, 0-59
*/
cron name: "indexTrigger", cronExpression: "0 0/1 * * * ? *"
}
def indexService
def execute() {
indexService.indexFiles()
}
}
Try this:
package movie
import groovy.io.FileType
import groovy.json.JsonSlurper
class IndexService {
static transactional = false
def grailsApplication
private cleanText(String title)
{
//split string into array via .,+,[,] etc...
def splitTitle=(title.toLowerCase()).tokenize('+.[]!£$%^+=~#:; \t \n \r \f / \\ { } # & - ( )')
//Get rid of extention
splitTitle.remove(splitTitle.size()-1)
//Get rid of Dvdrip and author
//unwanted phrases
def unwanted=["dvdrip","eng","www","r5","unique","torentz","3xforum","bugz","ro","maxspeed","xvid","720p","bluray","yify","1080p","hd","x264","RARBG","mp3","mp4","flv","brrip","rip"]
//Get rid of dates
//get 2000 to current date to put into the unwanted list
for(i in 2000..3000){unwanted.add(i.toString())}
//def empty string to put all our cleaned up text into
def cleanedText = ""
//cleaning up word mess...
splitTitle.each {word->
if(!unwanted.contains(word))
{
cleanedText=cleanedText+word+" "
}
}
//returning with +'s
return (cleanedText[0..-2]).replaceAll("\\s","+")
}
def getInfo(String title)
{
//returned nice with +
def cleanTitle=cleanText(title)
//define my reader
def scan = new JsonSlurper()
def scanner =scan.parseText(("http://www.omdbapi.com/?i=&plot=full&t="+cleanTitle).toURL().text)
if(scanner.Response=="True")
{
/*
organisied like this:
String artwork
String title
Date dateReleased
String userRated
String lengthOfFilm
String genre
String plot
float rating
*/ //returns our info we have scraped
def imdb = scanner.imdbRating
if (imdb.equalsIgnoreCase("n/a")) {
imdb = "0"
}
return [
true,
scanner.Poster,
scanner.Title,
new Date(scanner.Released),
scanner.Rated,
scanner.Runtime,
scanner.Genre,
scanner.Plot,
imdb.toFloat()/10
]
}
else
{
return [null,cleanTitle]
}
}
def indexFiles()
{
//returns fileLocation
def fileLocation=grailsApplication.config.grails.movie.location
//Setup as file object
def dir = new File(fileLocation)
//recurse all files
dir.eachFileRecurse (FileType.FILES) { file ->
//only create a record if no file test if record exists
if(Record.findByPathToFile(file.getCanonicalPath())==null) {
//get record propreties set
def record = new Record()
record.pathToFile = file.getCanonicalPath()
//call to get data of a film returns as a list
def info = getInfo(file.getName())
//check if everthing is alright
if (info[0] == null) {
try {
//set fallback propeties
record.artwork = null
record.title = info[1].replaceAll("\\+"," ")
record.genre = null
record.dateReleased = new Date(file.lastModified())
record.lengthOfFilm = null
record.plot = null
record.rating = 0
record.userRated = null
record.save(failOnError: true)
}
catch (Exception e) {
//catch any errors
//log.error("Error caught :${e} \n")
}
}
else
{
try
{
//set good propeties
record.artwork = info[1]
record.title = info[2]
record.genre = info[6]
record.dateReleased = info[3]
record.lengthOfFilm = info[5]
record.plot = info[7]
record.rating = info[8]
record.userRated = info[4]
record.save(failOnError: true)
}
catch (Exception e) {
catch any errors
log.error("Error caught :${e} \n")
}
}
}
}
}
}
It took me an hour to work that out!
Related
I know that this question has many questions around in Stackoverflow but even though it has many answers about it, thoses answers couldn't help me at all and either figured it out yet.
In my WebAPP it works normal, but when I transform it to an API it fails(topic title error)... I have tried following:
save first Test(flush:true) // error the C must be NOT NULL
save testInstance.addToAs(a).save() //b and c
Why I can't save the whole object?
Domain Water
static hasMany = [personWaters : Person,
equipmentWaters: Equipment,
sampleWaters : WaterSample]
Domain WaterSample
static belongsTo = [water: Water]
Controller refactored some parts
def save() {
def waterParams = request.JSON
def sampleList = [] as Set
def equipmentList = [] as Set
def personList = [] as Set
waterAPIService.personPopulate(waterParams.personWaters, personList)
waterAPIService.equipmentPopulate(waterParams.equipmentWaters, equipmentList)
waterAPIService.samplePopulate(waterParams.sampleWaters, sampleList)
Date validity = Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", waterParams.validity)
Date sample = Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", waterParams.sampleDate)
waterParams.remove('personWaters')
waterParams.remove('equipmentWaters')
waterParams.remove('sampleWaters')
waterParams.remove('validity')
waterParams.remove('sampleDate')
waterParams.put('personWaters', personList)
waterParams.put('equipmentWaters', equipmentList)
waterParams.put('sampleWaters', sampleList)
waterParams.put('validity', validity)
waterParams.put('sampleDate', sample)
def waterInstance = new Water(waterParams)
//def test = waterAPIService.sampleValidated(sampleList ? sampleList.size() : 0, sampleList)
def test = 99
if (test == 99) {
if (saveTest(waterInstance)) {
render([message: ['ok']] as JSON)
} else {
def errors = []
errors.add('Erro ao salvar')
errors = waterInstance.errors.allErrors.collect { g.message([error: it]) }
render([message: errors] as JSON)
}
} else {
render render([message: ["Critério de estabilização não atendido para o parâmetro " + waterAPIService.messageSampleValidated(test)]] as JSON)
}
}
def saveTest(Water waterInstance) {
try {
println waterInstance.validate()
if (!waterInstance.hasErrors() && waterInstance.save() && waterInstance.validate()) {
return true
} else {
log.info("error", waterInstance.errors)
return false
}
} catch (e) {
log.info("exception error", e)
}
}
Service
def personPopulate(personWaters, personList) {
personWaters.each {
personList << Person.get(it.id)
}
return personList
}
def equipmentPopulate(equipmentWaters, equipmentList) {
equipmentWaters.each {
equipmentList << Equipment.get(it.id)
}
return equipmentList
}
def samplePopulate(sampleWaters, sampleList) {
sampleWaters.each {
def sample = new WaterSample()
sample.dateTime = Date.parse("yyyy-MM-dd'T'HH:mm:ss", it.dateTime)
sample.levelWater = it.levelWater
sample.conductivity = it.conductivity
sample.dissolvedOxygen = it.dissolvedOxygen
sample.redoxPotential = it.redoxPotential
sample.ph = it.ph
sample.temperature = it.temperature
sampleList << sample
}
return sampleList
}
waterParam JSON
{
"climateCondition":"SUNNY",
"equipmentWaters":[
{
"id":2
},
{
"id":4
}
],
"personWaters":[
{
"id":86
},
{
"id":143
}
],
"sampleWaters":[
{
"ReportId":2,
"conductivity":0,
"IDWeb":0,
"dissolvedOxygen":0,
"dateTime":"2015-12-07T17:08:00Z",
"levelWater":0,
"ID":4,
"UpdateDate":"0001-01-01T00:00:00Z",
"ph":0,
"redoxPotential":0,
"temperature":0
},
{
"ReportId":2,
"conductivity":0,
"IDWeb":0,
"dissolvedOxygen":0,
"dateTime":"2015-12-07T17:09:00Z",
"levelWater":0,
"ID":5,
"UpdateDate":"0001-01-01T00:00:00Z",
"ph":0,
"redoxPotential":0,
"temperature":0
},
{
"ReportId":2,
"conductivity":0,
"IDWeb":0,
"dissolvedOxygen":0,
"dateTime":"2015-12-07T18:13:00Z",
"levelWater":0,
"ID":6,
"UpdateDate":"0001-01-01T00:00:00Z",
"ph":0,
"redoxPotential":0,
"temperature":0
}
]
}
Ok, this was a headache to find one of the solution:
If you trying to save an object, debug your it and inspect any of nested objects, for example: I'm sending approval.id: null, the object will give me below:
"approval":{
"class":"User",
"id":null
},
Which is trying to add a user with id null, whick wont find and wont save. So remove it from your json request.
Domain Class:
package com.myapp.gorm.log
import com.myapp.gorm.security.User
import com.myapp.gorm.system.TransactionJournal
class LogJournal {
static constraints = {
message nullable: false
category nullable: false
user1 nullable: false
}
static mapping = {
dateCreated column: "ts"
lastUpdated column: "ts_update"
}
LogMessage message
LogCategory category
User user1
User user2
String value
TransactionJournal transaction
Date dateCreated
Date lastUpdated
boolean equals(o) {
if (this.is(o)) return true
if (getClass() != o.class) return false
LogJournal that = (LogJournal) o
if (category != that.category) return false
if (dateCreated != that.dateCreated) return false
if (id != that.id) return false
if (lastUpdated != that.lastUpdated) return false
if (message != that.message) return false
if (transaction != that.transaction) return false
if (user1 != that.user1) return false
if (user2 != that.user2) return false
if (value != that.value) return false
return true
}
int hashCode() {
int result
result = message.hashCode()
result = 31 * result + category.hashCode()
result = 31 * result + user1.hashCode()
result = 31 * result + (user2 != null ? user2.hashCode() : 0)
result = 31 * result + (value != null ? value.hashCode() : 0)
result = 31 * result + (transaction != null ? transaction.hashCode() : 0)
result = 31 * result + dateCreated.hashCode()
result = 31 * result + (lastUpdated != null ? lastUpdated.hashCode() : 0)
result = 31 * result + id.hashCode()
return result
}
}
Error:
groovy.lang.MissingMethodException: No signature of method: c com.domainboost.gorm.log.LogJournal.save() is applicable for argument types: () values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), last(), any()
Or other domain classes work normally, only this one has a problem. I see grails cannot perform any method on this class instance.
There are no any validation errors.
Where I call this domain class (it's logback: appender):
class SystemAppender extends AppenderBase<ILoggingEvent> {
static appInitialized = false
#Override
public void append(ILoggingEvent event) {
if (appInitialized) {
LogMessage logMessage = LogMessage.findByMessage(event.getMessage())
if (!logMessage) {
logMessage = new LogMessage()
logMessage.message = event.getMessage()
logMessage.save(flash: true)
}
String categoryStr = event.argumentArray[0]
LogCategory logCategory = LogCategory.findByCategory(categoryStr)
if (!logCategory) {
logCategory = new LogCategory()
logCategory.category = categoryStr
logCategory.save(flash: true)
}
User user1 = null
if (event.argumentArray.contains(1)) {
user1 = event.argumentArray[1]
}
User user2 = null
if (event.argumentArray.contains(2)) {
user1 = event.argumentArray[2]
}
TransactionJournal tj = null
if (event.argumentArray.contains(3)) {
tj = event.argumentArray[3]
}
LogJournal logJournal = new LogJournal()
logJournal.category = logCategory
logJournal.message = logMessage
logJournal.user1 = user1
logJournal.user2 = user2
logJournal.transaction = tj
LogJournal.save()
}
}
}
And how I test this in grails console
import org.slf4j.Logger
import org.slf4j.LoggerFactory
def Logger logger = LoggerFactory.getLogger("sysLog")
logger.info("message", "category")
Error is on
LogJournal.save()
The code which ended with an error is in GormStaticApi.groovy
#CompileStatic(TypeCheckingMode.SKIP)
def methodMissing(String methodName, Object args) {
FinderMethod method = gormDynamicFinders.find { FinderMethod f -> f.isMethodMatch(methodName) }
if (!method) {
throw new MissingMethodException(methodName, persistentClass, args)
}
So it seems that method "save" is not found..
WTF ?
This
LogJournal.save()
Is looking for a static method on the class. You want a lower case initial letter to call save on the instance variable:
logJournal.save()
I am making my first migration script that involves the use of the Groovy/Grails DSL. It looks like this:
import my.package.MyObject
import my.package.MyObjectUtil
import javax.xml.bind.DatatypeConverter
databaseChangeLog = {
changeSet(author: 'wheresjim', id: '1387238199-1') {
comment {'Sets the timestamp in each MyObject where null using the message text'}
grailsChange {
change {
MyObjectUtil myObjectUtil = new MyObjectUtil()
def criteria = MyObject.where {
isNull("timestamp")
}
def PAGESIZE = 10
int numRows = criteria.count()
int pages = Math.ceil(numRows / PAGESIZE)
(pages..0).each { page ->
int offset = PAGESIZE * page + PAGESIZE
def data = criteria.list(offset: offset, max: PAGESIZE, sort: 'id', order: 'asc')
data.each { MyObject myObject ->
Date timestamp = new Date(0L)
try {
def thisMessage = myObjectUtil.createMyObjectFromMessage(myObject.messageText)
String dateStr = thisMessage.messageIdentification?.timestamp
timestamp = dateStr ? DatatypeConverter.parseDateTime(dateStr).getTime() : timestamp
} catch (Exception e) {
// Do nothing, this will be logged in the finally which catches another error condition
} finally {
if (timestamp == new Date(0L)) {
log.warn "Error attempting to set timestamp in MyObject ${myObject.id}, setting to $eventDateTime"
}
}
myObject.timestamp = timestamp
myObject.save(flush: true)
}
log.warn "Updated ${myObject.id}"
}
}
}
}
}
The MyObject.messageText is a clob in the database, and to my knowledge, there has been no effort to lazily load it.
I should note that this exact script works (it can find the clob text) using the grails console plugin on the app.
In MyObject make sure your have the following lines:
static mapping = {
messageText type: 'text'
}
I have a page with dynamic list boxes(selecting value from the first list populates the values in the second list box).
The validation errors for the list boxes are working fine, but while displaying the error messages the page is getting refreshed and the selected values are been set to initial status(need to select the values again in the list boxes)
The page is designed to add any number of list boxes using ajax calls, so adding and selecting the values again is going to be a rework.
Could you help me in displaying the validation errors and keeping the selected values as they are(previously I faced a similar situation which was resolved by replacing local variables of preprocess and postprocess with a global variable, this time no luck with that approach)
Any hints/help would be great
static constraints = {
deviceMapping(
validator: {val, obj ->
Properties dm = (Properties) val;
def deviceCheck = [:];
if (obj.customErrorMessage == null) {
for (def device : dm) {
if (device.key == null || "null".equalsIgnoreCase(device.key)) {
return ["notSelected"];
}
deviceCheck.put(device.key, "");
}
if (deviceCheck.size() != obj.properties["numberOfDevices"]) {
return ["multipleDevicesError"];
}
}
}
)
customErrorMessage (
validator: {
if ("sameDeviceMultipleTimes".equals(it)) {
return ['sameDeviceMultipleTimes']
}
}
)
}
public LinkedHashMap<String, Object> preProcess(sessionObject, params, request) {
Submission submission = (Submission) sessionObject;
def selectedFileName = sessionObject.fileName;
logger.debug("submission.deviceMapping :"+submission.deviceMapping)
try {
Customer customer = Customer.get(submission.customerId);
OperatingSystem operatingSystem = OperatingSystem.get(submission.operatingSystemId)
def ftpClientService = new FtpClientService();
def files = ftpClientService.listFilesInZip(customer.ftpUser, customer.ftpPassword, customer.ftpHost, customer.ftpToPackageDirectory, selectedFileName, operatingSystem, customer.ftpCustomerTempDirectory);
def terminalService = new TerminalService();
OperatingSystem os = OperatingSystem.get(submission.getOperatingSystemId());
def manufacturers = terminalService.getAllDeviceManufacturersForType(os.getType());
logger.debug("manufacturers after os type :"+manufacturers)
logger.debug("files in preprocess :"+files)
def devicesForFiles = [:]
files.each { file ->
def devicesForThisFile = [];
submission.deviceMapping.each { device ->
if (device.value == file.fileName) {
String manufacturer = terminalService.getManufacturerFromDevice("${device.key}");
def devicesForManufacturer = terminalService.getDevicesForManufacturerAndType(manufacturer, os.getType());
devicesForThisFile.push([device:device.key, manufacturer: manufacturer, devicesForManufacturer: devicesForManufacturer]);
}
}
devicesForFiles.put(file.fileName,devicesForThisFile);
}
logger.debug("devicesForFiles :"+devicesForFiles)
return [command: this, devicesForFiles: devicesForFiles, files: files, manufacturers: manufacturers];
} catch (Exception e) {
logger.warn("FTP threw exception");
logger.error("Exception", e);
this.errors.reject("mapGameToDeviceCommand.ftp.connectionTimeOut","A temporary FTP error occurred");
return [command: this];
}
}
public LinkedHashMap<String, Object> postProcess(sessionObject, params, request) {
Submission submission = (Submission) sessionObject;
Properties devices = params.devices;
Properties files = params.files;
mapping = devices.inject( [:] ) { map, dev ->
// Get the first part of the version (up to the first dot)
def v = dev.key.split( /\./ )[ 0 ]
map << [ (dev.value): files[ v ] ]
}
deviceMapping = new Properties();
params.files.eachWithIndex { file, i ->
def device = devices["${file.key}"];
if (deviceMapping.containsKey("${device}")) {
this.errors.reject("You cannot use the same device more than once");
return [];
//customErrorMessage = "sameDeviceMultipleTimes";
}
deviceMapping.put("${device}", "${file.value}");
}
if (params.devices != null) {
this.numberOfDevices = params.devices.size(); //Used for the custom validator later on
} else {
this.numberOfDevices = 0;
}
//logger.debug("device mapping :"+deviceMapping);
submission.deviceMapping = mapping;
return [command: this, deviceMapping: mapping, devicesForFiles: devicesForFiles ];
}
}
The problem is in your gsp page. Be sure that all field are initialised with a value
<g:text value="${objectInstance.fieldname}" ... />
Also the way it is selecting values is through id, so be sure to set it as well:
<g:text value="${objectInstance.fieldname}" id=${device.manufacturer.id} ... />
I have what I think is a simple problem but have been unable to solve...
For some reason I have a controller that uses removeFrom*.save() which throws no errors but does not do anything.
Running
Grails 1.2
Linux/Ubuntu
The following application is stripped down to reproduce the problem...
I have two domain objects via create-domain-class
- Job (which has many notes)
- Note (which belongs to Job)
I have 3 controllers via create-controller
- JobController (running scaffold)
- NoteController (running scaffold)
- JSONNoteController
JSONNoteController has one primary method deleteItem which aims to remove/delete a note.
It does the following
some request validation
removes the note from the job - jobInstance.removeFromNotes(noteInstance).save()
deletes the note - noteInstance.delete()
return a status and remaining data set as a json response.
When I run this request - I get no errors but it appears that jobInstance.removeFromNotes(noteInstance).save() does nothing and does not throw any exception etc.
How can I track down why??
I've attached a sample application that adds some data via BootStrap.groovy.
Just run it - you can view the data via the default scaffold views.
If you run linux, from a command line you can run the following
GET "http://localhost:8080/gespm/JSONNote/deleteItem?job.id=1¬e.id=2"
You can run it over and over again and nothing different happens. You could also paste the URL into your webbrowser if you're running windows.
Please help - I'm stuck!!!
Code is here link text
Note Domain
package beachit
class Note
{
Date dateCreated
Date lastUpdated
String note
static belongsTo = Job
static constraints =
{
}
String toString()
{
return note
}
}
Job Domain
package beachit
class Job
{
Date dateCreated
Date lastUpdated
Date createDate
Date startDate
Date completionDate
List notes
static hasMany = [notes : Note]
static constraints =
{
}
String toString()
{
return createDate.toString() + " " + startDate.toString();
}
}
JSONNoteController
package beachit
import grails.converters.*
import java.text.*
class JSONNoteController
{
def test = { render "foobar test" }
def index = { redirect(action:listAll,params:params) }
// the delete, save and update actions only accept POST requests
//static allowedMethods = [delete:'POST', save:'POST', update:'POST']
def getListService =
{
def message
def status
def all = Note.list()
return all
}
def getListByJobService(jobId)
{
def message
def status
def jobInstance = Job.get(jobId)
def all
if(jobInstance)
{
all = jobInstance.notes
}
else
{
log.debug("getListByJobService job not found for jobId " + jobId)
}
return all
}
def listAll =
{
def message
def status
def listView
listView = getListService()
message = "Done"
status = 0
def response = ['message': message, 'status':status, 'list': listView]
render response as JSON
}
def deleteItem =
{
def jobInstance
def noteInstance
def message
def status
def jobId = 0
def noteId = 0
def instance
def listView
def response
try
{
jobId = Integer.parseInt(params.job?.id)
}
catch (NumberFormatException ex)
{
log.debug("deleteItem error in jobId " + params.job?.id)
log.debug(ex.getMessage())
}
if (jobId && jobId > 0 )
{
jobInstance = Job.get(jobId)
if(jobInstance)
{
if (jobInstance.notes)
{
try
{
noteId = Integer.parseInt(params.note?.id)
}
catch (NumberFormatException ex)
{
log.debug("deleteItem error in noteId " + params.note?.id)
log.debug(ex.getMessage())
}
log.debug("note id =" + params.note.id)
if (noteId && noteId > 0 )
{
noteInstance = Note.get(noteId)
if (noteInstance)
{
try
{
jobInstance.removeFromNotes(noteInstance).save()
noteInstance.delete()
message = "note ${noteId} deleted"
status = 0
}
catch(org.springframework.dao.DataIntegrityViolationException e)
{
message = "Note ${noteId} could not be deleted - references to it exist"
status = 1
}
/*
catch(Exception e)
{
message = "Some New Error!!!"
status = 10
}
*/
}
else
{
message = "Note not found with id ${noteId}"
status = 2
}
}
else
{
message = "Couldn't recognise Note id : ${params.note?.id}"
status = 3
}
}
else
{
message = "No Notes found for Job : ${jobId}"
status = 4
}
}
else
{
message = "Job not found with id ${jobId}"
status = 5
}
listView = getListByJobService(jobId)
} // if (jobId)
else
{
message = "Couldn't recognise Job id : ${params.job?.id}"
status = 6
}
response = ['message': message, 'status':status, 'list' : listView]
render response as JSON
} // deleteNote
}
I got it working... though I cannot explain why.
I replaced the following line in deleteItem
noteInstance = Note.get(noteId)
with the following
noteInstance = jobInstance.notes.find { it.id == noteId }
For some reason the jobInstance.removeFromNotes works with the object returned by that method instead of .get
What makes it stranger is that all other gorm functions (not sure about the dynamic ones actually) work against the noteInstance.get(noteId) method.
At least it's working though!!
See this thread: http://grails.1312388.n4.nabble.com/GORM-doesn-t-inject-hashCode-and-equals-td1370512.html
I would recommend using a base class for your domain objects like this:
abstract class BaseDomain {
#Override
boolean equals(o) {
if(this.is(o)) return true
if(o == null) return false
// hibernate creates dynamic subclasses, so
// checking o.class == class would fail most of the time
if(!o.getClass().isAssignableFrom(getClass()) &&
!getClass().isAssignableFrom(o.getClass())) return false
if(ident() != null) {
ident() == o.ident()
} else {
false
}
}
#Override
int hashCode() {
ident()?.hashCode() ?: 0
}
}
That way, any two objects with the same non-null database id will be considered equal.
I just had this same issue come up. The removeFrom function succeeded, the save succeeded but the physical record in the database wasn't deleted. Here's what worked for me:
class BasicProfile {
static hasMany = [
post:Post
]
}
class Post {
static belongsTo = [basicProfile:BasicProfile]
}
class BasicProfileController {
...
def someFunction
...
BasicProfile profile = BasicProfile.findByUser(user)
Post post = profile.post?.find{it.postType == command.postType && it.postStatus == command.postStatus}
if (post) {
profile.removeFromPost(post)
post.delete()
}
profile.save()
}
So it was the combination of the removeFrom, followed by a delete on the associated domain, and then a save on the domain object.