0

My Code to gather a string for the current user looks like this.

package timetracker2

import grails.timesecurity.*
import timetracker2.TimeController

class Time {

   def springSecurityService

   int id
   Project projectname
   Date dateCreated
   Date dateWorked
   Double timeSpent
   TimeClassification classification

   String currentUser = {
      String currentUser = springSecurityService.getCurrentUser()
      def user = springSecurityService.getCurrentUser()
      String username = user
      return username
   }

   static constraints = {
      timeSpent blank: false
      classification blank: true
   }

   static mapping = {
      table 'Time'
      cache true
      id column: 'Time_id'
      dateCreated column: 'Date_created'
      dateWorked column: 'Date_Worked'
      timeSpent column: 'Time_Spent'
      currentUser column: 'Username'
   }
}

this is the string that I get out.... timetracker2.Time$_closure1@108115

What is up with that???? Any help would be very much appreciated.

I have tried using get authorisation as well, and I have tried to add a toString method in my person class. Alas no success.

1
  • I restored the edit since the code didn't make any sense afterwards Feb 2, 2013 at 15:49

2 Answers 2

2

I suspect part of the problem here is that you're trying to use springSecurityService before it has been set. The initializer expression in the declaration

String currentUser = springSecurityService.getCurrentUser()

runs when the object is constructed, but the springSecurityService reference is null at this point as it only gets injected after the constructor has finished. You could put the code that refers to springSecurityService into a beforeInsert event instead, which would delay the call until the point where the object is first saved to the DB. And you probably want principal.username instead of getCurrentUser() (thanks to Burt Beckwith for the pointer)

class Time {
  def springSecurityService

  String currentUser
  // other fields...

  def beforeInsert() {
    currentUser = springSecurityService.principal.username
  }
7
  • Right, it has to go in beforeInsert and beforeUpdate if the user could change when editing. But additionally getCurrentUser() returns the whole domain class instance; it should be just springSecurityService.principal.username Feb 2, 2013 at 15:51
  • I am guessing I am doing something wrong here as I am now getting the following error. Cannot insert the value NULL into column 'Username', table 'TimeTracker.dbo.Time'; column does not allow nulls. INSERT fails. definitely closer, never used the beforeInsert before as below Feb 2, 2013 at 16:45
  • def beforeInsert = { String currentUser = springSecurityService.getCurrentUser() } def beforeUpdate = { String currentUser = springSecurityService.getCurrentUser() } Feb 2, 2013 at 16:45
  • #1, don't use getCurrentUser() - it's not a String. Use springSecurityService.principal.username. #2, use methods not closures, i.e. def beforeInsert() { ... } and def beforeUpdate() { ... } Feb 2, 2013 at 17:21
  • Same problem Cannot insert the value NULL into column 'Username', table 'TimeTracker.dbo.Time'; column does not allow nulls. INSERT fails def beforeInsert() { String currentUser = springSecurityService.principal.username } def beforeUpdate() { String currentUser = springSecurityService.principal.username } Feb 2, 2013 at 18:02
0

You're initializing the currentUser variable with the result of calling toString() on a closure which, when invoked, returns the current user.

What you want is

String currentUser = springSecurityService.getCurrentUser()

which initializes the currentUser variable with the value returned by springSecurityService.getCurrentUser().

5
  • Tried that previously, it always results in the error as follows could not instantiate test objecttimetracker2.Time Feb 2, 2013 at 14:22
  • Edit your question. Show the code of the Time class, the code which uses it and causes this exception, and the excat and complete exception stack trace.
    – JB Nizet
    Feb 2, 2013 at 14:24
  • As requested thanks for helping | Error 2013-02-02 14:20:44,396 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error executing bootstraps: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Feb 2, 2013 at 15:02
  • Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objecttimetracker2.Time Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is Feb 2, 2013 at 15:03
  • org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objecttimetracker2.Time Feb 2, 2013 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.