Quantcast

Dynamic inList constraints

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Dynamic inList constraints

Tim Yates
Hi all!

I'm writing an archival tool for one of our systems, and have a property in one of my domain objects which should be a folder local to the server.

I first tried to use a service:

def fileService
static transients = [ 'fileService' ]
static constraints = {
  internalName( unique: true, inList:fileService.listOriginFiles() )
}

But that failed with MissingPropertyExceptions on the fileService (oddly, suggesting I tried fileService):

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is groovy.lang.MissingPropertyException: No such property: fileService for class: org.picr.workflow.Project
Possible solutions: fileService

So then, I got rid of the service, and tried this:

static constraints = {
  internalName( unique: true, inList:{ new File( CH.config.folder.origin ).listFiles().findAll { it.directory }*.name }() )
}

This works, but fetches the foldernames at startup, and then does not update when the folder structure updates

Is there any way to get this constraint to update?

Cheers,

Tim

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dynamic inList constraints

Erik  Pragt
Hi Tim,

You are using the 'inList'. Wouldn't it be better to use custom validation in this case? Maybe something like:

static constraints = {
  internalName( validator: { val, obj ->
      (new File( CH.config.folder.origin ).listFiles().findAll { it.directory }*.name }()).contains(val)
  )
}

Haven't tested it though, so it might not work

Erik Pragt



On 6 jul 2010, at 11:17, Tim Yates wrote:

Hi all!

I'm writing an archival tool for one of our systems, and have a property in one of my domain objects which should be a folder local to the server.

I first tried to use a service:

def fileService
static transients = [ 'fileService' ]
static constraints = {
  internalName( unique: true, inList:fileService.listOriginFiles() )
}

But that failed with MissingPropertyExceptions on the fileService (oddly, suggesting I tried fileService):

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is groovy.lang.MissingPropertyException: No such property: fileService for class: org.picr.workflow.Project
Possible solutions: fileService

So then, I got rid of the service, and tried this:

static constraints = {
  internalName( unique: true, inList:{ new File( CH.config.folder.origin ).listFiles().findAll { it.directory }*.name }() )
}

This works, but fetches the foldernames at startup, and then does not update when the folder structure updates

Is there any way to get this constraint to update?

Cheers,

Tim


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dynamic inList constraints

Tim Yates
Hi Erik!

Yeah, that works great! Thanks!! :-)

 I was trying to use inList, as the same bit of code could fill my select box in the view as well (hence my first thought of using a service)

But I'll stick with this now as it works :-)

Shame we can't use services in custom validators though, as it would eliminate the code duplication

Thanks again,

Tim

On Tue, Jul 6, 2010 at 11:13 AM, Erik Pragt <[hidden email]> wrote:
Hi Tim,

You are using the 'inList'. Wouldn't it be better to use custom validation in this case? Maybe something like:

static constraints = {
  internalName( validator: { val, obj ->
      (new File( CH.config.folder.origin ).listFiles().findAll { it.directory }*.name }()).contains(val)
  )
}

Haven't tested it though, so it might not work

Erik Pragt



On 6 jul 2010, at 11:17, Tim Yates wrote:

Hi all!

I'm writing an archival tool for one of our systems, and have a property in one of my domain objects which should be a folder local to the server.

I first tried to use a service:

def fileService
static transients = [ 'fileService' ]
static constraints = {
  internalName( unique: true, inList:fileService.listOriginFiles() )
}

But that failed with MissingPropertyExceptions on the fileService (oddly, suggesting I tried fileService):

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is groovy.lang.MissingPropertyException: No such property: fileService for class: org.picr.workflow.Project
Possible solutions: fileService

So then, I got rid of the service, and tried this:

static constraints = {
  internalName( unique: true, inList:{ new File( CH.config.folder.origin ).listFiles().findAll { it.directory }*.name }() )
}

This works, but fetches the foldernames at startup, and then does not update when the folder structure updates

Is there any way to get this constraint to update?

Cheers,

Tim



Loading...