From 81cb2880919108e2bcb35d766f9250665d60e95f Mon Sep 17 00:00:00 2001 From: Kurt Nordstrom Date: Thu, 1 Aug 2019 08:28:13 -0400 Subject: [PATCH] Added postUpdate hook to delete FileUpload with null owner (#95) --- .../domain/org/olf/general/FileUpload.groovy | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/service/grails-app/domain/org/olf/general/FileUpload.groovy b/service/grails-app/domain/org/olf/general/FileUpload.groovy index 24c8261..744019b 100644 --- a/service/grails-app/domain/org/olf/general/FileUpload.groovy +++ b/service/grails-app/domain/org/olf/general/FileUpload.groovy @@ -1,5 +1,6 @@ package org.olf.general import grails.gorm.MultiTenant +import grails.gorm.multitenancy.Tenants class FileUpload implements MultiTenant { @@ -13,9 +14,9 @@ class FileUpload implements MultiTenant { static constraints = { fileContentBytes nullable: true - fileContentType nullable: true, blank: false + fileContentType nullable: true lastModified nullable: true - 'owner' nullable: true + owner nullable: true } static mapping = { @@ -23,7 +24,31 @@ class FileUpload implements MultiTenant { fileContentBytes column: 'fu_bytes', sqlType: 'longblob' fileName column: 'fu_filename' fileSize column: 'fu_filesize' - lastModified column: 'fu_last_mod' - owner column: 'fu_owner', cascade: 'save-update' + lastModified column: 'fu_last_mod' + owner column: 'fu_owner', type: 'string', length: 36 } + + def afterUpdate() { + log.info("afterUpdate() for FileUpload") + final Serializable currentTenantId = Tenants.currentId() + Tenants.withId(currentTenantId) { + try { + def deleteList = [] + def fuList = FileUpload.findAllWhere(owner: null) + fuList.each { + FileUpload upload = it + if(upload.owner == null) { + deleteList.add(upload) + } + } + deleteList.each { + log.info("Deleting fileUpload with id of ${it.id}") + it.delete() + } + } catch(Exception e) { + log.error("Error trying to delete ownerless fileUpload objects: ${e.getMessage()}") + } + } + } + }