I am trying to create an edit form which models the hashset of roles in a
CheckBoxMultipleChoice; so many roles can be selected and displayed. How can
I achieve this? Here is the class that that I am modelling and the EditForm
page.
*************Borrower.java**********************
package libhib.model;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
/**
* User: jamesperry
* Date: 20-Mar-2007
* Time: 11:48:06
*/
public class Borrower implements Serializable, UserDetails
{
private Long id;
private int version;
private String firstName;
private String lastName;
private String password;
private String confirmPassword;
private String address;
private String email;
private BigDecimal debt;
private Set roles = new HashSet();
private boolean enabled = true;
private boolean accountExpired = false;
private boolean accountLocked = false;
private boolean credentialsExpired = false;
// contains the information about the book copies currently on loan to
this Borrower
private Set<Borrow> borrows = new HashSet<Borrow>();
private Set<Reservation> reservations = new HashSet<Reservation>();
public Borrower()
{
}
public Borrower(String firstName, String lastName, String password,
String address, String email)
{
assert email != null;
this.firstName = firstName;
this.lastName = lastName;
this.password = password;
this.address = address;
this.email = email;
this.debt = new BigDecimal(0);
}
public Long getId()
{
return id;
}
@SuppressWarnings({"UNUSED_SYMBOL"})
private void setId(Long id)
{
this.id = id;
}
public int getVersion()
{
return version;
}
public void setVersion(int version)
{
this.version = version;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public BigDecimal getDebt()
{
return debt;
}
public void setDebt(BigDecimal debt)
{
this.debt = debt;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public Set<Borrow> getBorrows()
{
return borrows;
}
public void setBorrows(Set<Borrow> borrows)
{
this.borrows = borrows;
}
public Set<Reservation> getReservations()
{
return reservations;
}
private void setReservations(Set<Reservation> reservations)
{
this.reservations = reservations;
}
public void addReservation(Reservation rsv)
{
reservations.add(rsv);
}
public void removeReservation(Reservation rsv)
{
reservations.remove(rsv);
}
public void addBorrow(Borrow bor)
{
borrows.add(bor);
}
public void removeBorrow(Borrow bor)
{
borrows.remove(bor);
}
public String getPassword()
{
return password;
}
public void setPassword(String passworld)
{
this.password = passworld;
}
public void addRole(Role role)
{
this.getRoles().add(role);
role.getUsers().add(this);
}
public GrantedAuthority[] getAuthorities()
{
return (GrantedAuthority[]) getRoles().toArray(new
GrantedAuthority[0]);
}
public String getUsername()
{
return email;
}
public boolean isEnabled()
{
return enabled;
}
public boolean isAccountExpired()
{
return accountExpired;
}
public boolean isAccountNonExpired()
{
return !isAccountExpired();
}
public boolean isAccountLocked()
{
return accountLocked;
}
public boolean isAccountNonLocked()
{
return !isAccountLocked();
}
public boolean isCredentialsExpired()
{
return credentialsExpired;
}
public boolean isCredentialsNonExpired()
{
return !credentialsExpired;
}
public void setEnabled(boolean enabled)
{
this.enabled = enabled;
}
public boolean getEnabled()
{
return this.enabled;
}
public void setAccountExpired(boolean accountExpired)
{
this.accountExpired = accountExpired;
}
public boolean getAccountExpired()
{
return this.accountExpired;
}
public void setAccountLocked(boolean accountLocked)
{
this.accountLocked = accountLocked;
}
public boolean getAccountLocked()
{
return this.accountLocked;
}
public void setCredentialsExpired(boolean credentialsExpired)
{
this.credentialsExpired = credentialsExpired;
}
public boolean getCredentialsExpired()
{
return this.credentialsExpired;
}
public boolean equals(Object o)
{
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
final Borrower borrower = (Borrower) o;
return email.equals(borrower.email);
}
public int hashCode()
{
return email.hashCode();
}
public String toString()
{
ToStringBuilder sb = new ToStringBuilder(this,
ToStringStyle.DEFAULT_STYLE).append("borrower",
this.id)
.append("firstname", this.firstName)
.append("lastname", this.lastName)
.append("email", this.email)
.append("debt", this.debt)
.append("enabled", this.enabled)
.append("accountExpired", this.accountExpired)
.append("credentialsExpired", this.credentialsExpired)
.append("accountLocked", this.accountLocked);
if (borrows != null)
{
sb.append("Borrow Records: ");
boolean afterFirst = false;
for (Borrow el : borrows)
{
if (afterFirst)
sb.append(", ");
else
afterFirst = true;
sb.append(el);
}
}
else
{
sb.append("No Borrow Records");
}
if (reservations != null)
{
sb.append("Reservation Records: ");
boolean afterFirst = false;
for (Reservation el : reservations)
{
if (afterFirst)
sb.append(", ");
else
afterFirst = true;
sb.append(el);
}
}
else
{
sb.append("No Reservation Records");
}
GrantedAuthority[] auths = this.getAuthorities();
if (auths != null)
{
sb.append("Granted Authorities: ");
for (int i = 0; i < auths.length ; i++)
{
if (i > 0)
{
sb.append(", ");
}
sb.append(auths[i].toString());
}
}
else
{
sb.append("No Granted Authorities");
}
return sb.toString();
}
public Set getRoles()
{
return roles;
}
public void setRoles(Set roles)
{
this.roles = roles;
}
}
*****************EditForm Page***********************
package libpage;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.Logger;
import wicket.Application;
import
wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import wicket.Session;
import wicket.Page;
import wicket.PageParameters;
import wicket.RequestCycle;
import wicket.markup.html.basic.Label;
import wicket.markup.html.form.CheckBox;
import wicket.markup.html.form.DropDownChoice;
import wicket.markup.html.form.Form;
import wicket.markup.html.form.ListMultipleChoice;
import wicket.markup.html.form.RadioChoice;
import wicket.markup.html.form.TextField;
import wicket.markup.html.form.validation.FormComponentFeedbackBorder;
import wicket.markup.html.form.validation.StringValidator;
import wicket.markup.html.form.CheckBoxMultipleChoice;
import wicket.markup.html.link.IPageLink;
import wicket.markup.html.link.PageLink;
import wicket.markup.html.panel.FeedbackPanel;
import wicket.markup.html.WebPage;
import wicket.markup.html.panel.FeedbackPanel;
import wicket.markup.html.border.Border;
import wicket.markup.html.link.Link;
import wicket.model.CompoundPropertyModel;
import wicket.model.PropertyModel;
import wicket.model.LoadableDetachableModel;
import libhib.model.Borrower;
import libhib.service.LibraryService;
@AuthorizeInstantiation("admin")
public final class EditBorrower extends WebPage
{
private Borrower borrower;
private static final Logger LOG = Logger.getLogger(ViewBorrowers.class);
/**
* Constructs a page that edits a borrower's security details
*
* @param id
* The unique identifier of the borrower
*/
public EditBorrower(final long id)
{
LOG.debug("ViewBorrowers Construction is called");
Border border = new LibBorder("libBorder");
add(border);
this.borrower = getService().getBorrowerById(id);
border.add(new FeedbackPanel("feedback"));
border.add(new EditBorrowerForm("editBorrowerForm", borrower,
getService().getRoles()));
}
/**
* Gets a link to a page that will edit a borrower
*
* @param name
* The name of the link
* @param id
* The id of the borrower that the page will edit
* @return The page link
*/
public static PageLink link(final String name, final long id)
{
return new PageLink(name, new IPageLink()
{
public Page getPage()
{
return new EditBorrower(id);
}
public Class getPageIdentity()
{
return EditBorrower.class;
}
});
}
static public final class EditBorrowerForm extends Form
{
/**
* Constructor
*
* @param id
* id of form
* @param book
* Book model
* @param roles
* the security roles
*/
public EditBorrowerForm(final String id, final Borrower borrower, final
List roles)
{
super(id, new CompoundPropertyModel(borrower));
add(new Label("firstName"));
add(new Label("lastName"));
add(new Label("email"));
// Create a required text field that edits the borrowers's
password
final TextField password = new TextField("password");
password.setRequired(true);
final FormComponentFeedbackBorder passwordFeedback = new
FormComponentFeedbackBorder(
"passwordFeedback");
add(passwordFeedback);
passwordFeedback.add(password);
add(new CheckBox("enabled"));
add(new CheckBox("accountExpired"));
add(new CheckBox("accountLocked"));
add(new CheckBox("credentialsExpired"));
add(new CheckBoxMultipleChoice("roles", roles));
}
/**
* Show the resulting valid edit back at the Home page in the
*/
public final void onSubmit()
{
final RequestCycle cycle = getRequestCycle();
PageParameters parameters = new PageParameters();
final Borrower borrower = (Borrower) getModelObject();
parameters.put("Borrower", borrower);
cycle.setResponsePage(getPageFactory().newPage(Home.class,
parameters));
cycle.setRedirect(true);
}
}
public LibraryService getService()
{
return ((LibraryApplication) Application.get()).getLibraryService();
}
}
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user