Re: create new model object of a Form from one of its FormComp values??
[offline conversation returned to thread] Igor, I understand about using the AccountBean. That is not where the problem lies. Problem: how to update encapsulated field(s) of the Account object from the AccountBean??? See details below. Account does NOT have set methods for these fields, so I cannot simply copy properties from the bean to the newly-created object. That is the difficulty. Thanks, RDC i dont see why you have to give up encapsulation of anything... the bean you create does not extend account, in fact it extends nothing, just implements serializable. you only need this bean when you are creating a new instance of account. then in onsubmit() you do onsubmit() { AccountBean bean=getModelObject(); Account acc=new Account(bean.getEmail()); // copy other properties from bean to acc dao.save(acc); // ^ at this point you should have the id set by hibernate setModel(new HibernateEntityModel(acc)); // ^ notice we set a new model, from now on this form will operate on the instance of Account object } -igor On Jan 24, 2008 6:42 PM, RDC [EMAIL PROTECTED] wrote: Igor, Thanks again for your time. Here's the difficulty I am having with implementing the bean idea. Account is a subclass of another class, let's call it Persistable. Persistable has a private rowId field which represents the id of the object in a relational db. That class implements a public getRowId() method, but NOT a setRowId method. This is the design recommended by Hiberate, which I am using as my ORM. Hibernates sets the rowId field directly reflexively, using a value provided by the db. Let's say the Form has an AccountBean as its model object. That bean gets updated by the FormComponents without problem. It also gets saved to the db, and Hibernate sets its rowId field. So far, so good. The problem: how to set the rowId of the newly-created Account object from the AccountBean, while at the same time keeping respecting the encapsulation of rowId Only two choices I see are: 1. create a protected setRowID() method in Persistable (Persistable and Account are in different packages); or 2. set the rowId field reflexively. As in all good software engineering, both have pluses and minuses and involve tradeoffs. I am really hesitant to give up the safety of the strong encapsulation of the rowId field. Plus, there are other fields with similar limitation. This prompted me to try to find a different solution. Would love your thoughts on this. Like everyone, my knowledge of java is incomplete. Do you see any other options? RDC Although I believe your solution is the best because of its simplicity, due to other (non-wicket) constraints in my design (encapsulation of fields of an Account superclass), I was unable to use a bean or subclass the form. not really sure why this is an issue for you. give class Account { private String email,name; public Account(String email) { ... } ... } you can always do class AccountBean { private String email,name;} set instance of AccountBean as the model object of the form and then in onsubmit() { AccountBean bean=getModelObject(); Account account=new Account(bean.getEmail()); account.setName(bean.getName()); form.setModelObject(account); } you dont break any encapsulation... cheers, -igor and it is also more elegant to e.g. create a copy constructor or utility object for copying so that you don't have your submit method littered with code to copy properties. That's largely a matter of taste though. -- View this message in context: http://www.nabble.com/create-new-model-object-of-a-Form-from-one-of-its-FormComp-values---tp14983110p15098864.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: create new model object of a Form from one of its FormComp values??
Appreciate your help. I tried to create a bean and use a copy ctor as Eelco suggested. Expect that would work well. I ran into problems with that because of some read-only fields of superclasses of Account necessitated by my design. Therefore, a copy ctor was not possible without breaking encapsulation of those superclasses, which I didn't want to do. What worked for me - not necessarily a better or simpler solution - was to create a protected constructor of Account that didn't require the email address of the admin. I create this by Reflection and set it as model object of the Form, where it acts just like the bean suggested by Eelco. It gets updated normally by the FormComponents and everything seems to work well. In sum, instead of creating a new model object from a value of the Form upon Form submission, I instead created a new protected constructor that creates an (incomplete but temporary) object which acts like a bean. Perhaps not the most elegant solution, but works given other constraints in my overall design. RDC What Igor said: and it is also more elegant to e.g. create a copy constructor or utility object for copying so that you don't have your submit method littered with code to copy properties. That's largely a matter of taste though. Eelco -- View this message in context: http://www.nabble.com/create-new-model-object-of-a-Form-from-one-of-its-FormComp-values---tp14983110p15019899.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: create new model object of a Form from one of its FormComp values??
Thanks to everyone for their comments Igor, Assuming I use a bean, could you please give me a bit more detail on your suggestion? I want the model object of the editor Form to be the newly-created Account, so that other objects can simply call getModelObject() on the editor like normal and get the correct (new) value. Do I simply set the new Account as the model object of the editor Form? In onSubmit(), would I code something like the following? Account accountBean = (Account)getModelObject(); //bean updated with values from FormComponents Account newAccount = Account(accountBean ); //copy constructor using bean setModelObject(newAccount); RDC personally i would map the form to a bean, and then in onsubmit() transfer those properties to an instance of your domain object. -- View this message in context: http://www.nabble.com/create-new-model-object-of-a-Form-from-one-of-its-FormComp-values---tp14983110p15004311.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
create new model object of a Form from one of its FormComp values??
Hello All, Have tried to search for this in the forums, but am not having much success due to the multiple concepts involved. I apologize in advance for the long post. Question: is there a way to, after a Form has been submitted, create a new model object for that Form, based upon a value of a FormComponent within the Form, and then update that new Form model object with the values of the models of the other FormComponents in the Form?? For example, I have a Account editor, used to both create and edit Accounts, which consists of a Form containing a number of FormComponents, eg., account name, date created, etc. For the moment just consider the case of creating a new Account. The Account constructor requires the email address of the person who is to be the admin of the Account. Internally it converts that email address to a user object, saves that user to the db, and sets that user as admin of the Account. Therefore, one of the FormComponents in the Account editor is a TextField whose model is the email address of the admin to provide to the Account constructor. So far, so good. I have many editors like the one above for creating/editing other domain objects, and they work great. For them I initially set the editor Form model object to a new instance of the type of object to be edited, and then update that model object using the models of the FormComponents in that respective editor. I understand and am using a homegrown variant of ICompoundModel without problem. Again, so far, so good. The problem: since the Account constructor requires the admin email address, and that address cannot be determined in advance, I cannot pre-create an Account to set as a blank model object to initialize the Account editor. It is possible I could give a fake admin email address to the Account constructor, but that would create a fake user in the db, who would then have to be deleted - I would rather avoid that. What I had hoped to be able to do is: upon Form submission, get the admin email address from the TextField (in the Form) which handles that. Create a NEW Account using that address (and handling any errors that occur). Set the model object of the editor Form as that newly-created account. Update that Form model object (new account) using the models of the other FormComponents, like I do successfully with my editors for other objects. I have read the docs and reviewed the source code - it seems I need to insert code to do the above in the chain of calls that flow from Form submission, after successful validation of FormComponents. My question is, exactly where? My best guess is override the editor Form's beforeUpdateFormComponentModels method, or perhaps the Form's updateFormComponentModels method, maybe even the process method?. Inital attempts at this have been unsuccessful. Any and all suggestions/constructive criticism appreciated. Thank you in advance for your time. -- View this message in context: http://www.nabble.com/create-new-model-object-of-a-Form-from-one-of-its-FormComp-values---tp14983110p14983110.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]