Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.beans; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.TreeMap; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.jdom.Attribute; import org.jdom.DataConversionException; import org.jdom.Document; import org.jdom.Element; /** * The Class PrivilegesBean. * * @author David Harrison 3rd September 2004 - modified 4th June 2008 */ public class PrivilegesBean implements java.io.Serializable { /** The Constant serialVersionUID. */ private static final long serialVersionUID = 1L; /** The logger. */ private static Logger logger = Logger.getLogger(PrivilegesBean.class); /** The roles. */ private TreeMap<String, RoleBean> roles = new TreeMap<String, RoleBean>(); /** * Sets the roles. * * @param rolesMap the roles */ public final void setRoles(final TreeMap<String, RoleBean> rolesMap) { this.roles = rolesMap; } /** * Gets the roles. * * @return the roles */ public final TreeMap<String, RoleBean> getRoles() { return this.roles; } /** * Gets the role. * * @param name the name * * @return the role */ public final RoleBean getRole(final String name) { if (this.roles.containsKey(name)) { RoleBean role = (RoleBean) this.roles.get(name); return role; } return null; } /** * Sets the role. * * @param role the new role */ public final void setRole(final RoleBean role) { if (role != null && role.getName() != null) { this.roles.put(role.getName(), role); } } /** * Removes the role. * * @param role the role */ public final void removeRole(final RoleBean role) { if (role != null && role.getName() != null) { this.roles.remove(role.getName()); } } /** * Gets the privilege. * * @param user the user bean * @param typeVal the type * @param actionVal the action * * @return the privilege */ public final boolean getPrivilege(final UserBean user, final String typeVal, final String actionVal) { boolean allowed = false; if (user.getMemberOf() != null) { for (String role : user.getMemberOf()) { if (role != null && !allowed) { RoleBean roleBean = (RoleBean) this.roles.get(role); if (roleBean != null) { PrivilegeBean privilege = roleBean.getPrivilege(typeVal); if (StringUtils.equals(actionVal, "create")) { allowed = privilege.getCreate(); } if (StringUtils.equals(actionVal, "modify")) { allowed = privilege.getModify(); } if (StringUtils.equals(actionVal, "delete")) { allowed = privilege.getDelete(); } } } } } // If the override privilege flag is set default to true if (user.getOverridePrivileges()) { allowed = true; } return allowed; } /** * Gets a list of email addresses for the supplied user. * * @param user the user bean * * @return the list of email addresses */ public final Collection<String> getEmailAddresses(final UserBean user) { Collection<String> addresses = new ArrayList<String>(); TreeMap<String, String> unique = new TreeMap<String, String>(); if (user.getMemberOf() != null) { for (String role : user.getMemberOf()) { if (role != null) { RoleBean roleBean = (RoleBean) this.roles.get(role); StringBuffer address = new StringBuffer(); if (StringUtils.isNotBlank(roleBean.getEmailAddress())) { address.append(roleBean.getEmailAddress().trim()); if (StringUtils.isNotBlank(roleBean.getEmailName())) { address.insert(0, roleBean.getEmailName().trim() + " <"); address.append(">"); } } if (StringUtils.isNotBlank(address.toString())) { unique.put(address.toString(), address.toString()); } } } } StringBuffer userAddress = new StringBuffer(); if (StringUtils.isNotBlank(user.getEmail())) { userAddress.append(user.getEmail().trim()); if (StringUtils.isNotBlank(user.getLastName())) { userAddress.insert(0, user.getLastName().trim() + " <"); userAddress.append(">"); } if (StringUtils.isNotBlank(user.getPreferredName())) { userAddress.insert(0, user.getPreferredName().trim() + " "); } // Remove the address if it is already in the unique tree map if (unique.containsKey(userAddress.toString())) { unique.remove(userAddress.toString()); } } // Add the user name to the top of the list if (StringUtils.isNotBlank(userAddress.toString())) { addresses.add(userAddress.toString()); } for (String address : unique.keySet()) { addresses.add(address); } return addresses; } /** * Sets the xml document. * * @param document the new xml document */ public final void setXmlDocument(final Document document) { Iterator<?> loopUserClasses = document.getRootElement().getChildren().iterator(); while (loopUserClasses.hasNext()) { Element userclass = (Element) loopUserClasses.next(); RoleBean roleBean = new RoleBean(); try { roleBean.setName(userclass.getChild("name").getText()); } catch (Exception e) { logger.error("Error parsing name attribute: " + e.getMessage()); } try { roleBean.setDescription(userclass.getChild("description").getText()); } catch (Exception e) { logger.info("Error parsing name description: " + e.getMessage()); } try { roleBean.setEmailName(userclass.getChild("emailName").getText()); } catch (Exception e) { logger.info("Error parsing name emailName: " + e.getMessage()); } try { roleBean.setEmailAddress(userclass.getChild("emailAddress").getText()); } catch (Exception e) { logger.info("Error parsing name emailAddress: " + e.getMessage()); } Iterator<?> loopActions = userclass.getChild("actions").getChildren().iterator(); while (loopActions.hasNext()) { Element action = (Element) loopActions.next(); PrivilegeBean privilege = new PrivilegeBean(); privilege.setName(action.getText()); try { privilege.setCreate(action.getAttribute("create").getBooleanValue()); } catch (DataConversionException de) { privilege.setCreate(false); } try { privilege.setModify(action.getAttribute("modify").getBooleanValue()); } catch (DataConversionException de) { privilege.setCreate(false); } try { privilege.setDelete(action.getAttribute("delete").getBooleanValue()); } catch (DataConversionException de) { privilege.setCreate(false); } roleBean.setPrivilege(privilege); } this.setRole(roleBean); } } /** * Generates a JDOM Document form the values set in this PrivilegesBean. * * @return JDOM element representing the privilege values */ public final Document getXmlDocument() { Element rootElement = new Element("privileges"); TreeMap<String, RoleBean> roleMap = this.getRoles(); for (String roleName : roleMap.keySet()) { RoleBean role = (RoleBean) roleMap.get(roleName); if (role != null) { Element userclass = new Element("userClass"); Element name = new Element("name").setText(role.getName()); Element description = new Element("description").setText(role.getDescription()); Element emailName = new Element("emailName").setText(role.getEmailName()); Element emailAddress = new Element("emailAddress").setText(role.getEmailAddress()); Element actions = new Element("actions"); userclass.addContent(name); userclass.addContent(description); userclass.addContent(emailName); userclass.addContent(emailAddress); TreeMap<String, PrivilegeBean> privilegeRoles = role.getPrivileges(); for (String privilegeName : privilegeRoles.keySet()) { PrivilegeBean privilege = (PrivilegeBean) privilegeRoles.get(privilegeName); if (privilege != null) { Element action = new Element("action").setText(privilege.getName()); ArrayList<Attribute> attributes = new ArrayList<Attribute>(); String strCreate = "false"; String strModify = "false"; String strDelete = "false"; if (privilege.getCreate()) { strCreate = "true"; } if (privilege.getModify()) { strModify = "true"; } if (privilege.getDelete()) { strDelete = "true"; } Attribute create = new Attribute("create", strCreate); Attribute modify = new Attribute("modify", strModify); Attribute delete = new Attribute("delete", strDelete); attributes.add(create); attributes.add(modify); attributes.add(delete); action.setAttributes(attributes); actions.addContent(action); } } userclass.addContent(actions); rootElement.addContent(userclass); } } return new Document(rootElement); } }