You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Francisco Diaz Trepat - gmail <fr...@gmail.com> on 2009/03/08 00:38:56 UTC

model for entity with checkgroup (user roles relation)

Hi all, it probably that this has been answered in the list, but I can't
figure out how to construct the query for gmail (I've been on the list for
some time now).

I have the following feature I don't know how to resolve. I am using wicket
1.4rc2 with activeobjects (orm).

I have User and Role entities, and User has a manytomany relationship.

I would like to have a form with a checkgroup to be able to select which
roles a user have.

User has:
String name
String password
@ManyToMany(UserRole.class)
Role[] roles

Role has:
String name

and UserRole has:
User user
Role role

all with corresponding setters and getters.

How could I combine this entities to develop a form in which user could see:
TextField
PasswordTextField
CheckGroup (with the names of the roles available)
?

Thanks
f(t)

PS: some extra info.

this is my model and form (comments other than the question would be grate
also):

      //create user entity model
      userModel = new LoadableDetachableModel<SqsUser>() {
         private static final long serialVersionUID = 0L;

         @Override
         protected SqsUser load() {
            Session session;
            session = (Session) getSession();
            SqsUser user;
            if (selectedUser != null)
               return selectedUser;
            user = null;
            try {
               user = session.getEntityManager().create(SqsUser.class);
            } catch (SQLException ex) {

Logger.getLogger(UsersPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (user != null)
               selectedUser = user;
            return user;
         }
      };

      //create user form overriding process to begin transaction
      usersForm = new Form<SqsUser>("users-form", new
CompoundPropertyModel<SqsUser>(userModel)) {
         private static final long serialVersionUID = 0L;
         private Transaction<SqsUser> transaction;

         @Override
         public void process(IFormSubmittingComponent submittingComponent) {

            super.process(submittingComponent);
            transaction = new Transaction<SqsUser>(((Session)
getSession()).getEntityManager()) {
               @Override
               protected SqsUser run() throws SQLException {
                  SqsUser user = getModelObject();
                  user.save();
                  return user;
               }
            };
         }

         @Override
         protected void onSubmit() {
            try {
               transaction.execute();
            } catch (SQLException ex) {

Logger.getLogger(UsersPanel.class.getName()).log(Level.SEVERE, null, ex);
               onError();
            }
         }

         @Override
         protected void onError() {

         }
      };

Re: model for entity with checkgroup (user roles relation)

Posted by Timo Rantalaiho <Ti...@ri.fi>.
On Sat, 07 Mar 2009, Francisco Diaz Trepat - gmail wrote:
> I would like to have a form with a checkgroup to be able to select which
> roles a user have.

I would have CheckBoxes with IModel<Boolean> bound to each
role so that they would do

  @Override
  public Boolean getObject() {
      return user.hasRole(thisRole);
  }

  @Override
  public void setObject(Boolean selected) {
     if (selected) {
         user.addRole(thisRole);
     } else {
         user.removeRole(thisRole);
     }
  }

Best wishes,
Timo


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