Re: create new model object of a Form from one of its FormComp values??
if account doesnt have these setters then how do you expect the form to mutate the object? -igor On Jan 25, 2008 3:03 PM, infodoc <[EMAIL PROTECTED]> wrote: > > [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] > > - 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??
[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??
On Jan 21, 2008 10:54 PM, Eelco Hillenius <[EMAIL PROTECTED]> wrote: What Igor said: > > > bean.getdate() if you are using a bean, or just date if you are > binding to a property on the form subclass vs > (Date)textbox.getModelObject(); > Okay, I got that, thanks (: -- sp
Re: create new model object of a Form from one of its FormComp values??
> I do understand advantages of using bean with CompoundPropertyModel, Or regular property models even. > my question was related to the situation when you are forced to create your > target object after the form is submitted. For instance, when you create > different kinds of objects based on what exactly was submitted, or when > constructor of the target object takes certain values and there're no > setters for those values. Or, like in my situation, when both of those > restrictions apply. What Igor said: > bean.getdate() if you are using a bean, or just date if you are binding to a property on the form subclass vs (Date)textbox.getModelObject(); which requires you to keep a reference to the textbox component anyways. 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 - 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??
its simply easier to say: bean.getdate() if you are using a bean, or just date if you are binding to a property on the form subclass vs (Date)textbox.getModelObject(); which requires you to keep a reference to the textbox component anyways. -igor On Jan 21, 2008 11:42 AM, Sergey Podatelev <[EMAIL PROTECTED]> wrote: > On Jan 21, 2008 9:25 PM, Eelco Hillenius <[EMAIL PROTECTED]> wrote: > > Instead of a massive copy action e.g. in your onSubmit, you can > > use a bean that is already populated with the values. > > > I do understand advantages of using bean with CompoundPropertyModel, my > question was related to the situation when you are forced to create your > target object after the form is submitted. For instance, when you create > different kinds of objects based on what exactly was submitted, or when > constructor of the target object takes certain values and there're no > setters for those values. Or, like in my situation, when both of those > restrictions apply. > > Of course, maybe I'm missing something, and there's also a way to use this > approach even in cases described above. > > -- > sp > - 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??
On Jan 21, 2008 9:25 PM, Eelco Hillenius <[EMAIL PROTECTED]> wrote: Instead of a massive copy action e.g. in your onSubmit, you can > use a bean that is already populated with the values. I do understand advantages of using bean with CompoundPropertyModel, my question was related to the situation when you are forced to create your target object after the form is submitted. For instance, when you create different kinds of objects based on what exactly was submitted, or when constructor of the target object takes certain values and there're no setters for those values. Or, like in my situation, when both of those restrictions apply. Of course, maybe I'm missing something, and there's also a way to use this approach even in cases described above. -- sp
Re: create new model object of a Form from one of its FormComp values??
yep, that will work. -igor On Jan 21, 2008 11:17 AM, infodoc <[EMAIL PROTECTED]> wrote: > > 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] > > - 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]
Re: create new model object of a Form from one of its FormComp values??
> personally i would map the form to a bean, and then in onsubmit() > > transfer those properties to an instance of your domain object. > > > > > Igor, could you please tell in short how is this better than just get data > form the models of each component in onsubmit()? > I'm doing same thing within my app and was wondering if I should create a > bean just for form data extraction purposes. The good thing about using beans is that you can eliminate plumbing code. Instead of a massive copy action e.g. in your onSubmit, you can use a bean that is already populated with the values. I often use my domain objects directly - and I already have those objects anyway - so I save quite a bit of dumb copying code. Not everyone agrees with using domain objects directly though; ymmv. Eelco - 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??
it gives you the advantage of being able to construct your domain objects easily: new Person(personBean); you also dont have to access the form components directly to poll their models, to me this is a plus. -igor On Jan 21, 2008 9:00 AM, Sergey Podatelev <[EMAIL PROTECTED]> wrote: > On Jan 20, 2008 8:14 PM, Igor Vaynberg <[EMAIL PROTECTED]> wrote: > > personally i would map the form to a bean, and then in onsubmit() > > transfer those properties to an instance of your domain object. > > > > > Igor, could you please tell in short how is this better than just get data > form the models of each component in onsubmit()? > I'm doing same thing within my app and was wondering if I should create a > bean just for form data extraction purposes. > > -- > sp > - 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??
Having a bean makes things explicit. Getting stuff from a text component directly is very read unfriendly. See it as a small investment in maintenance. You will thank yourself later for taking the 1 minute to generate the bean :-) But it is all a matter of taste and one can argue about that for ages. Whatever gets the job done and is maintainable in 2 years from now is the winner imo. What that means for you, that is for you to decide :) Martijn On 1/21/08, Sergey Podatelev <[EMAIL PROTECTED]> wrote: > > On Jan 20, 2008 8:14 PM, Igor Vaynberg <[EMAIL PROTECTED]> wrote: > > personally i would map the form to a bean, and then in onsubmit() > > transfer those properties to an instance of your domain object. > > > > > Igor, could you please tell in short how is this better than just get data > form the models of each component in onsubmit()? > I'm doing same thing within my app and was wondering if I should create a > bean just for form data extraction purposes. > > -- > sp > -- Buy Wicket in Action: http://manning.com/dashorst Apache Wicket 1.3.0 is released Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3.0
Re: create new model object of a Form from one of its FormComp values??
On Jan 20, 2008 8:14 PM, Igor Vaynberg <[EMAIL PROTECTED]> wrote: personally i would map the form to a bean, and then in onsubmit() > transfer those properties to an instance of your domain object. > > Igor, could you please tell in short how is this better than just get data form the models of each component in onsubmit()? I'm doing same thing within my app and was wondering if I should create a bean just for form data extraction purposes. -- sp
Re: create new model object of a Form from one of its FormComp values??
personally i would map the form to a bean, and then in onsubmit() transfer those properties to an instance of your domain object. -igor On Jan 20, 2008 7:54 AM, infodoc <[EMAIL PROTECTED]> wrote: > > 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] > > - 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]