You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Christoph Hermann <ch...@tu-clausthal.de> on 2005/01/15 18:23:42 UTC

Need help with Cforms binding

Hello

i really need help with a Cforms binding which i can't get to work.
I have two classes (User, Right) which have an m:n relation.

public class User implements Serializable {
    private int id;
    private String nick;
    private String firstname;
    private String lastname;
    private String email;
    private String pass;
    private Collection rights = new ArrayList();
    public int getId() {
        return this.id;
    }
    public void setId(int id) {
        this.id = id;
    }
...
    public Collection getRights() {
        return this.rights;
    }
    public void setRights(Collection rights) {
        this.rights = rights;
    }
    public void addRight(Right right) {
        right.addUser(this);
        this.rights.add(right);
    }
}

public class Right implements Serializable {
    private int id;
    private String name;
    private Collection users = new ArrayList();
    public int getId() {
        return this.id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Collection getUsers() {
        return this.users;
    }
    public void setUsers(Collection users) {
        this.users = users;
    }
    public void addUser(User user) {
        this.users.add(user);
    }
}

Now i have the following binding:
<fb:context
    xmlns:fb="http://apache.org/cocoon/forms/1.0#binding" path="/"
    >

 <fb:value id="id" path="id" direction="load"/>
 <fb:value id="nick" path="nick" />
...
 <fb:value id="confirmpass" path="pass" direction="load" />

  <fb:repeater id="rights" parent-path="." row-path="rights">
    <fb:identity>
      <fb:value id="id" path="@id"/>
    </fb:identity>

    <fb:on-bind>
      <!-- executed on updates AND right after the insert -->
      <fb:value id="id" path="id" />
      <fb:value id="name" path="name" />
    </fb:on-bind>

    <fb:on-delete-row>
      <fb:delete-node />
    </fb:on-delete-row>

    <fb:on-insert-row>
      <fb:insert-bean classname="papillon.Right" addmethod="addRight"/>
    </fb:on-insert-row>
  </fb:repeater>
</fb:context>

Inserting new Rights works fine, hte problem is that when i change a 
right the old value is deleted from the Rights Table instead of only 
deleting the entry in the reference table.
Can anyone of you help me how out on how an m:n relationship should be 
binded?

I wrote a flowscript to test if my classes are ok and this is working 
fine:
 user = new Packages.papillon.User();
 user.setId(15);
 user = dao.retrieve(user);
 firstname = user.getFirstName();
 right = new Packages.papillon.Right();
 right.setId(64);
 rights = new Packages.java.util.ArrayList();
 rights.add(right);
 user.setRights(rights);
 dao.update(user);

In my cforms i use the following flowscript:

 var bean = new Packages.papillon.User();
 id = 15;
 bean.setId (id);
 bean = dao.retrieve (bean);
 form.load(bean);
 form.showForm("form-display-pipeline-newuser",data);
 form.save(bean);
 dao.update (bean);

Christoph

Re: Need help with Cforms binding

Posted by Christoph Hermann <ch...@tu-clausthal.de>.
Christoph Hermann schrieb:

Hello,

> > Inserting new Rights works fine, the problem is that when i change
> > a right the old value is deleted from the Rights Table instead of
> > only deleting the entry in the reference table.
> > Can anyone of you help me how out on how an m:n relationship should
> > be binded?
>
> My repository.xml (I use ojb/jdo) looks like this:

>                 <collection-descriptor name="rights"
> element-class-ref="papillon.Right" auto-retrieve="true"
> auto-update="link" auto-delete="link"
> indirection-table="rel_users_rights" otm-dependent="false">
>                         <fk-pointing-to-this-class
> column="users_id"/> <fk-pointing-to-element-class
> column="rights_id"/>
>                 </collection-descriptor>

Thanks to Tim Larson, the problem is solved.
Just for the ones interested (and the archive) here's the Solution:
In the collection-descriptor i had to add:
 collection-class="org.apache.ojb.broker.util.collections.ManageableArrayList"
Then the Problem was gone :-).

Christoph

Re: Need help with Cforms binding

Posted by Christoph Hermann <ch...@tu-clausthal.de>.
Christoph Hermann schrieb:

Hello,

> i really need help with a Cforms binding which i can't get to work.
> I have two classes (User, Right) which have an m:n relation.

>     <fb:on-delete-row>
>       <fb:delete-node />
>     </fb:on-delete-row>

Maybe someone can tell me what happens when delete-node is executed?
I presume it takes the id value and deletes the corresponding object 
(Right)?
What i would need is that only the item in the Collection (User.rights) 
is deleted, and not the corresponding right (I think this causes the 
problem).

> Inserting new Rights works fine, hte problem is that when i change a
> right the old value is deleted from the Rights Table instead of only
> deleting the entry in the reference table.
> Can anyone of you help me how out on how an m:n relationship should
> be binded?

My repository.xml (I use ojb/jdo) looks like this:

<class-descriptor class="papillon.User" table="users">
                <field-descriptor name="id" primarykey="true" 
nullable="false" default-fetch="true" autoincrement="true" column="id" 
sequence-name="users_id_seq" jdbc-type="INTEGER"/>
                <field-descriptor name="nick" nullable="false" 
default-fetch="true" column="nick" jdbc-type="VARCHAR"/>
                <field-descriptor name="firstname" nullable="false" 
default-fetch="true" column="firstname" jdbc-type="VARCHAR"/>
                <field-descriptor name="lastname" nullable="false" 
default-fetch="true" column="lastname" jdbc-type="VARCHAR"/>
                <field-descriptor name="email" nullable="false" 
default-fetch="true" column="email" jdbc-type="VARCHAR"/>
                <field-descriptor name="pass" nullable="false" 
default-fetch="true" column="pass" jdbc-type="VARCHAR"/>
                <collection-descriptor name="rights" 
element-class-ref="papillon.Right" auto-retrieve="true" 
auto-update="link" auto-delete="link" 
indirection-table="rel_users_rights" otm-dependent="false">
                        <fk-pointing-to-this-class column="users_id"/>
                        <fk-pointing-to-element-class 
column="rights_id"/>
                </collection-descriptor>
        </class-descriptor>
 <class-descriptor class="papillon.Right" table="rights">
                <field-descriptor name="id" primarykey="true" 
nullable="false" default-fetch="true" autoincrement="true" column="id" 
sequence-name="rights_id_seq" jdbc-type="INTEGER"/>
                <field-descriptor name="name" nullable="false" 
default-fetch="true" column="name" jdbc-type="VARCHAR"/>
                <collection-descriptor name="users" 
element-class-ref="papillon.User" auto-retrieve="true" 
auto-update="link" auto-delete="link" 
indirection-table="rel_users_rights" otm-dependent="false">
                        <fk-pointing-to-this-class column="rights_id"/>
                        <fk-pointing-to-element-class 
column="users_id"/>
                </collection-descriptor>
        </class-descriptor>

As you can see i specified there auto-update="link" etc... according to:
Mapping m:n associations 
http://db.apache.org/ojb/docu/guides/basic-technique.html#m-to-n

> I wrote a flowscript to test if my classes are ok and this is working
> fine:
>  user = new Packages.papillon.User();
>  user.setId(15);
>  user = dao.retrieve(user);
>  firstname = user.getFirstName();
>  right = new Packages.papillon.Right();
>  right.setId(64);
>  rights = new Packages.java.util.ArrayList();
>  rights.add(right);
>  user.setRights(rights);
>  dao.update(user);

Here i just set new Rights, no Rights are deleted/removed from the 
Collection i think this is the difference.
Can someone tell me how to reproduce this behaviour with Cforms?

My "workaround" would be not to use a binding or not to call 
form.save(bean); then get the values out of the form in the flowscript 
set them to the bean (bean.setXY(value)) and then save the bean. But i 
don't think this is a good solution.

If you need more Information tell me please.
Christoph