Java tutorial
/******************************************************************************* * Educational Online Test Delivery System * Copyright (c) 2013 American Institutes for Research * * Distributed under the AIR Open Source License, Version 1.0 * See accompanying file AIR-License-1_0.txt or at * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf ******************************************************************************/ package org.opentestsystem.shared.permissions.data; import java.util.ArrayList; import java.util.List; import javax.faces.bean.ManagedBean; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; import org.apache.commons.lang3.StringUtils; @ManagedBean public class Role { private String _role = null; private List<AllowableEntityType> _allowableEntities = new ArrayList<AllowableEntityType>(); private List<Component> _mappings = new ArrayList<Component>(); @XmlAttribute(name = "name") public String getRole() { return _role; } public void setRole(String value) { _role = value; } @XmlElement(name = "allowableentitytype") public List<AllowableEntityType> getAllowableEntities() { return _allowableEntities; } public void setAllowableEntities(List<AllowableEntityType> value) { _allowableEntities = value; } @XmlElement(name = "mappings") public List<Component> getMappings() { return _mappings; } public void addComponent(Component c) { _mappings.add(c); } public boolean hasEntity(final String entityName) { List<AllowableEntityType> entities = this.getAllowableEntities(); if (entities == null) return false; AllowableEntityType entityType = (AllowableEntityType) CollectionUtils.find(entities, new Predicate() { @Override public boolean evaluate(Object object) { String entity = ((AllowableEntityType) object).getEntity().toLowerCase(); if (StringUtils.equals(entityName.toLowerCase(), entity)) return true; return false; } }); if (entityType == null) return false; return true; } public boolean exists(List<Role> roles) { if (roles == null) return false; final Role r = this; Role role = (Role) CollectionUtils.find(roles, new Predicate() { @Override public boolean evaluate(Object object) { if (StringUtils.equals(r.getRole().toLowerCase(), ((Role) object).getRole().toLowerCase())) return true; return false; } }); if (role == null) return false; return true; } @Override public boolean equals(Object o) { if (o instanceof Role) { Role candidate = (Role) o; if (StringUtils.equals(candidate.getRole(), getRole())) return true; return false; } else return false; } }