0
votes

I'm currently have the task to move our java-application code to a web-application. I would decide to use grails for this task. (I developed already a few simple applications in grails and they worked nicely.)

But with one behaviour, I have my serious problems.

Giving a domain model with:
a customer has many addresses
an address belongs to customer and has many contacts
a contact belongs to address

edit: Because I use the "belongsTo" setting, the cascading save would not be a problem (I think).

What I would need: While creating a customer I need the possibility to attach a few addresses and there I should have the possibility to add a few contacts. But all should be committed to the database after pressing the save button in the customer view. (The same with editing a customer...)

Current behaviour: With the default controllers and views, I first need to commit the customer and could than add the addresses the same with the contacts (first commit the address and than add the contacts).

edit:
(as I understand correctly) the data binding is used by default in the "generate-controller" and "generate-view" resulting classes.
The thing where I have no idea is, how to handle the web-page-flow, that I could add an address and there a few contacts, without saving the customer domain before. And after switching several times between the views "customer.create", "address.create" and "contact.create" having the whole structure still present.
(I tried to save the customer-domain in the session-object and in the create.address view save-method, I fetched the customer from the session and added the addressInstance to the address-list of the customer, saved the customer again in the session and switched back to a new editSession-action for the customerInstance which fetches the instance to be edited from the session. But it seemed to me, that only the customerInstance is contained in the session and not the associated objects... [omg, hope I could make me understand...])

Would my needed workflow somehow be possible? (Hopefully you could point me to some documentation or examples, please)

Thanks in advance, Susanne.

1
Since you already have worked with Grails I guess "bind and save" is a too simple answer... :) Can you be a bit more specific? - Martin Hauner
Best place to start is by beginning to understand how GORM works. Start from here. Association in GORM is what need to be understood in order to achieve what you seek for. - dmahapatro
Thanks a lot for your answers! I edited my post to - hopefully - make it better understandable, where I think I have my problems to find a solution. I think, my problem is not the cascading save, but keeping the object-structure available between several view changes. - susi
You can use bindData. This will help to save to only limited fields i.e. the customer information only. grails.org/doc/latest/ref/Controllers/bindData.html - Shashank Agrawal

1 Answers

0
votes

There are many possibilities, here are a few suggestions that may help you find a solution:

The user friendliest design is to create all three objects on the same page (Why would a user want to switch between three screens to setup a new Customer?). This takes a bit more work on the page but you can create/edit and save the customer in one go.

If you have separate screens to add Customer, Address & Contact let the user start with creating the customer. save it. When the user adds an Address pass along the Customer id. save it linked to the Customer (you have the id). Same for Contacts.

If you don't want the user to explicitly save the Customer before he can add an Address you can save the Customer automatically when the user clicks "add address" on the Customer page. Same for Contacts.

If you don't want to show the unfinished customer to someone else you can add an in creation flag on the customer to filter unfinished Customers (or you use another table to link to unfinished users, if you dislike the 'state' information on the customer). When the user finally decides the Customer is ready (pressing save on the Customer page) you clear the unfinished state (or remove it from the unfinished table) to make the new Customer visible to all users.

Doing it like this you could even recover from session timeouts and offer the user to continue creation of the customer.