You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by "Angelo C." <an...@gmail.com> on 2011/04/16 09:54:03 UTC

t5: create a record from another table, what is best practice?

Hi,
   I have a form with email and name fields, and when the page is activated,
it will retrieve an existing application, then initialize email and name,
when submit is clicked, 
   a Profile record is created in onSuccess and persisted, the following
code seems not working, what is a right practice in this use case?  
Thanks,
   

public class MakeProfile {

    @Inject
    private Session session;

    @Property
    private String email;
    @Property
    private String name;

	private Application app;
 
    void onActivate(String code_ref) {
        app = (Applicantion) session().createQuery("from Applicantion where
code = ?").setString(0, code_ref).uniqueResult();
        if (app != null) {
  			email = app.getEmail();
			name = app.getName();
      }
    }

    @CommitAfter
    Object onSuccess() {
    	Profile p = new Profile();
        p.setEmail(email);
		p.setName(name);
		session.save(p);
	}

}


--
View this message in context: http://tapestry.1045711.n5.nabble.com/t5-create-a-record-from-another-table-what-is-best-practice-tp4307107p4307107.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: t5: create a record from another table, what is best practice?

Posted by Taha Hafeez <ta...@gmail.com>.
public class MakeProfile {

   @Inject
   private Session session;

   @Property
   private String email;

   @Property
   private String name;

@Property
   @Persist(PersistenceConstants.FLASH)
        private Application app;

   void onActivate(String code_ref) {
      if(app == null){
         app = (Applicantion) session().createQuery("from Applicantion where
             code = ?").setString(0, code_ref).uniqueResult();
      }
   }

   void onPrepareForRender(){
      email = app.getEmail();
      name = app.getName();
   }

   @CommitAfter
   Object onSuccess() {
       Profile p = new Profile();
       p.setEmail(email);
               p.setName(name);
               session.save(p);
       }

}

regards
Taha


On Sat, Apr 16, 2011 at 2:31 PM, Angelo C. <an...@gmail.com> wrote:

> Hi,
>
> Thanks, but how to pass a string to prepareForRender?
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/t5-create-a-record-from-another-table-what-is-best-practice-tp4307107p4307173.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: t5: create a record from another table, what is best practice?

Posted by "Angelo C." <an...@gmail.com>.
Hi,

Thanks, but how to pass a string to prepareForRender?

--
View this message in context: http://tapestry.1045711.n5.nabble.com/t5-create-a-record-from-another-table-what-is-best-practice-tp4307107p4307173.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: t5: create a record from another table, what is best practice?

Posted by Taha Hafeez <ta...@gmail.com>.
Hi

onActivate() is called each time a page is loaded. So if you have a form on
a page, onActivate() will be called on both at the time of rendering and
submit/action phase

So if you have to initialize any variables before showing them in a form do
it in prepareForRender event of the form and if you want to do some thing
after submiting the form do it in prepareForSubmit event of the form. If you
want to do something common in both phases do it in prepare event of the
form.

Please refer to
http://tapestry.apache.org/tapestry5.2-dev/tapestry-core/ref/org/apache/tapestry5/corelib/components/Form.html

regards
taha


On Sat, Apr 16, 2011 at 1:24 PM, Angelo C. <an...@gmail.com> wrote:

> Hi,
>   I have a form with email and name fields, and when the page is activated,
> it will retrieve an existing application, then initialize email and name,
> when submit is clicked,
>   a Profile record is created in onSuccess and persisted, the following
> code seems not working, what is a right practice in this use case?
> Thanks,
>
>
> public class MakeProfile {
>
>    @Inject
>    private Session session;
>
>    @Property
>    private String email;
>    @Property
>    private String name;
>
>        private Application app;
>
>    void onActivate(String code_ref) {
>        app = (Applicantion) session().createQuery("from Applicantion where
> code = ?").setString(0, code_ref).uniqueResult();
>        if (app != null) {
>                        email = app.getEmail();
>                        name = app.getName();
>      }
>    }
>
>    @CommitAfter
>    Object onSuccess() {
>        Profile p = new Profile();
>        p.setEmail(email);
>                p.setName(name);
>                session.save(p);
>        }
>
> }
>
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/t5-create-a-record-from-another-table-what-is-best-practice-tp4307107p4307107.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>