Package com.tll.service.entity.user

Source Code of com.tll.service.entity.user.AuthorityEditor

package com.tll.service.entity.user;

import java.beans.PropertyEditorSupport;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.tll.model.Authority;
import com.tll.util.StringUtil;

/**
* @author jpk
*/
public class AuthorityEditor extends PropertyEditorSupport {

  protected List<Authority> masterAuthorities;

  /**
   * Constructor
   * @param masterAuthorities
   */
  public AuthorityEditor(List<Authority> masterAuthorities) {
    super();
    this.masterAuthorities = masterAuthorities;
  }

  protected String formatText(String text) {
    return StringUtil.enumStyleToPresentation(StringUtils.replace(text, "ROLE_", ""));
  }

  @Override
  public String getAsText() {
    final Authority a = (Authority) getValue();
    return a == null ? "" : formatText(a.getAuthority());
  }

  @Override
  public void setAsText(String text) throws IllegalArgumentException {
    if(text == null || text.length() < 1) throw new IllegalArgumentException("No authority specified");
    for(final Authority a : masterAuthorities) {
      if(text.equals(formatText(a.getAuthority()))) {
        setValue(a);
        return;
      }
    }
    throw new IllegalArgumentException("Unrecognized authority: '" + text + "'");
  }

}
TOP

Related Classes of com.tll.service.entity.user.AuthorityEditor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.