com.neocotic.bloggaer.account.Role.java Source code

Java tutorial

Introduction

Here is the source code for com.neocotic.bloggaer.account.Role.java

Source

/* Copyright (C) 2012 Alasdair Mercer, http://neocotic.com/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.neocotic.bloggaer.account;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.persistence.Id;
import javax.persistence.Transient;

import org.apache.commons.lang3.ArrayUtils;

import com.googlecode.objectify.annotation.NotSaved;
import com.googlecode.objectify.condition.IfDefault;
import com.neocotic.bloggaer.common.Persistable;

/**
 * A {@code Role} is a group of {@link Capability Capabilities} that are applicable to {@link Account Accounts}, which
 * are required to fulfil a specific purpose..
 * 
 * @author Alasdair Mercer
 */
public class Role implements Persistable {

    private List<Capability> capabilities = new ArrayList<Capability>();
    @Transient
    private Date dateLoaded;
    @Id
    private String name;
    @NotSaved(IfDefault.class)
    private Boolean removable = Boolean.TRUE;
    private String title;

    /**
     * Creates a new {@link Role} with no title or {@code Capability Capabilities}.
     */
    public Role() {
    }

    /**
     * Creates a new {@link Role} based on the {@code role} provided.
     * 
     * @param role
     *            the {@link PredefinedRole} to base the {@code Role} on
     */
    public Role(PredefinedRole role) {
        Collections.addAll(capabilities, role.getCapabilities());
        name = role.name();
        removable = Boolean.FALSE;
        title = role.getTitle();
    }

    /**
     * Returns the {@link Capability Capabilities} included in this {@link Role}.
     * 
     * @return The included {@code Capabilities}.
     */
    public List<Capability> getCapabilities() {
        return capabilities;
    }

    /*
     * @see Persistable#getDateLoaded()
     */
    @Override
    public Date getDateLoaded() {
        return dateLoaded;
    }

    /**
     * Returns any {@link Capability Capabilities} within {@code capabilities} that are not included in this
     * {@link Role}.
     * 
     * @param capabilities
     *            the {@code Capabilities} to be checked
     * @return A {@link List} of excluded {@code Capabilities}.
     */
    public List<Capability> getMissingCapabilities(Capability... capabilities) {
        List<Capability> missing = new ArrayList<Capability>();
        if (ArrayUtils.isNotEmpty(capabilities))
            for (Capability capability : capabilities)
                if (!this.capabilities.contains(capability))
                    missing.add(capability);
        return missing;
    }

    /**
     * Returns the unique name of this {@link Role}.
     * 
     * @return The unique name.
     */
    public String getName() {
        return name;
    }

    /**
     * Returns the title of this {@link Role}.
     * 
     * @return The title.
     */
    public String getTitle() {
        return title;
    }

    /**
     * Indicates whether or not this {@link Role} includes all of the {@code capabilities} provided.
     * 
     * @param capabilities
     *            the {@link Capability Capabilities} to be checked
     * @return {@code true} if this {@code Role} includes all {@code capabilities} or {@code capabilities} is
     *         {@code null} or empty; otherwise {@code false}.
     */
    public boolean hasAllCapabilities(Capability... capabilities) {
        if (ArrayUtils.isEmpty(capabilities))
            return true;

        for (Capability capability : capabilities)
            if (!this.capabilities.contains(capability))
                return false;
        return true;
    }

    /**
     * Indicates whether or not this {@link Role} includes any of the {@code capabilities} provided.
     * 
     * @param capabilities
     *            the {@link Capability Capabilities} to be checked
     * @return {@code true} if {@code capabilities} isn't {@code null} or empty and this {@code Role} includes any
     *         {@code capabilities}; otherwise {@code false}.
     */
    public boolean hasAnyCapabilities(Capability... capabilities) {
        if (ArrayUtils.isEmpty(capabilities))
            return false;

        for (Capability capability : capabilities)
            if (this.capabilities.contains(capability))
                return true;
        return false;
    }

    /**
     * Indicates whether or not this {@link Role} includes the {@code capability} provided.
     * 
     * @param capability
     *            the {@link Capability} to be checked
     * @return {@code true} if this {@code Role} includes {@code capability}; otherwise {@code false}.
     */
    public boolean hasCapability(Capability capability) {
        return capabilities.contains(capability);
    }

    /**
     * Indicates whether or not this {@link Role} can be deleted from the underyling datastore.
     * <p>
     * Only {@code Roles} that have been created from {@link PredefinedRole PredefinedRoles} should return a affirmative
     * result.
     * 
     * @return {@link Boolean#TRUE} if this {@code Role} can be deleted; otherwise {@link Boolean#FALSE}.
     */
    public Boolean isRemovable() {
        return removable;
    }

    /**
     * Sets the {@link Capability Capabilities} included in this {@link Role} to {@code capabilities}.
     * 
     * @param capabilities
     *            the {@code Capabilities} to be set
     */
    public void setCapabilities(List<Capability> capabilities) {
        this.capabilities = capabilities;
    }

    /**
     * Sets the unique name of this {@link Role} to {@code name}.
     * <p>
     * This cannot be changed once this {@code Role} has been persisted.
     * 
     * @param name
     *            the unique name to be set
     * @throws IllegalStateException
     *             If this {@code Role} already has a name.
     */
    public void setName(String name) {
        if (this.name != null)
            throw new IllegalStateException("Role already has name: " + this.name);

        this.name = name;
    }

    /**
     * Sets whether or not this {@link Role} can be deleted from the underlying datastore to {@code removable}.
     * <p>
     * {@code removable} should only be affirmative for {@code Roles} that have been created from {@link PredefinedRole
     * PredefinedRoles}.
     * 
     * @param removable
     *            {@link Boolean#TRUE} if this {@code Role} should be removable; otherwise {@link Boolean#FALSE}
     */
    public void setRemovable(Boolean removable) {
        this.removable = removable;
    }

    /**
     * Sets the title of this {@link Role} to {@code title}.
     * 
     * @param title
     *            the title to be set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /*
     * @see Object#equals(Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Role other = (Role) obj;
        if (capabilities == null) {
            if (other.capabilities != null)
                return false;
        } else if (!capabilities.equals(other.capabilities))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (removable == null) {
            if (other.removable != null)
                return false;
        } else if (!removable.equals(other.removable))
            return false;
        if (title == null) {
            if (other.title != null)
                return false;
        } else if (!title.equals(other.title))
            return false;
        return true;
    }

    /*
     * @see Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((capabilities == null) ? 0 : capabilities.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((removable == null) ? 0 : removable.hashCode());
        result = prime * result + ((title == null) ? 0 : title.hashCode());
        return result;
    }
}