Ant's
Ant's

Reputation: 13811

How Do I Decide My Domain Model In This Case?

I need to develop a application for a user's management in a IT Project. This is done in order to learn Grails. I have some problem to start with :

In my sample app, a user has many tasks, belongs to a project, have many holiday status, belongs to the Resource planning and thats it. (kind of requirements!)

Now..... When it comes to domain modeling, how I actually model this? I came up the solution of modeling something like this :

class User { 
        //relationships. . . . 
        static belongsTo = [ company : Company, role : Role, resource : Resource] 
        static hasMany = [ holidays : Holiday, tasks : Tasks ] 
        Profile profile 
        Project project 
        String login 
        String password 
        static constraints = { 
                login(unique:true,size:6..15) 
                profile(nullable:true) 
        } 
        String toString() { 
                this.login 
        } 
} 

Now taking advantage of Grails scaffolding. I generated the view, hmmm well thats where I got struck!

With this model in place, clearly when creating a new User., I need to give away project details, resource details. Even though I can change the view as I need, for example I need only two fields say login and password for the new User to register my site. But as per data modeling, how I can handle other fields like Profile, Project and other relationships. This sounds complicated! I know i'm a newbie to both web development and I want your suggestions and how do I proceed with this?

Thanks in advance.

Upvotes: 0

Views: 156

Answers (1)

socha23
socha23

Reputation: 10239

You need to override the save action in your controller and fill those additional fields there.

Try adding the following code in UserController:

def save = {
    def userInstance = User.get(params.id)
    if (!userInstance) {
        userInstance = new User()

        // here you can fill in additional fields:
        userInstance.profile = myMethodToGetTheProfile()
        userInstance.project = myMethodToGetTheProject()
        ...
    }
    userInstance.properties = params
    if (userInstance.save(flush: true)) {
        flash.message = message(code: 'default.created.message', args: [message(code: 'User.propertyName.label', default: 'User'), userInstance.id])}
        redirect(action: session.lastActionName ?: "list", controller: session.lastControllerName ?: controllerName)
    } else {
        render(view: "form", model: [userInstance: userInstance, mode: 'edit'])
    }
}

The implementation of methods to get the default project depends on your app's logic.

Upvotes: 3

Related Questions