001package org.apache.fulcrum.security.entity.impl;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023import org.apache.commons.lang3.builder.HashCodeBuilder;
024import org.apache.fulcrum.security.entity.SecurityEntity;
025
026/**
027 * Base class for all objects implementing SecurityEnitity. This class
028 * automatically lowercases the name. So the permission "EDIT" will equal "eDit"
029 * and "edit";
030 * 
031 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
032 * @version $Id$
033 */
034public class SecurityEntityImpl implements SecurityEntity
035{
036    /**
037         * Serial id
038         */
039        private static final long serialVersionUID = 6949229336753158100L;
040
041        private String name;
042
043    private Object id;
044
045    /**
046     * @return object id
047     */
048    public Object getId()
049    {
050        return id;
051    }
052
053    /**
054     * @param id the object id
055     */
056    public void setId(Object id)
057    {
058        this.id = id;
059    }
060
061    /**
062     * @return object name
063     */
064    public String getName()
065    {
066        return name;
067    }
068
069    /**
070     * Pass in the name for this entity. Also lowercases it.
071     * 
072     * @param name name of entity
073     * @throws IllegalArgumentException must provide a name
074     */
075    public void setName(String name) throws IllegalArgumentException
076    {
077        if ( name == null )
078        {
079            throw new IllegalArgumentException("Must provide a valid name for all SecurityEntities.");
080        } else {
081                this.name = name.toLowerCase();
082        }
083    }
084
085    @Override
086    public String toString()
087    {
088        return getClass().getName() + " (id:" + getId() + " name:" + getName() + ")";
089    }
090
091    /**
092     * Check if this object is equal to another
093     * 
094     * @see java.lang.Object#equals(java.lang.Object)
095     */
096    @Override
097    public boolean equals(Object o)
098    {
099        boolean equals = true;
100        Object id = getId();
101
102        if (o == null || id == null)
103        {
104            equals = false;
105        }
106        else if (!(o instanceof SecurityEntity))
107        {
108            equals = false;
109        }
110        else
111        {
112            equals = id.equals(((SecurityEntity) o).getId());
113        }
114        return equals;
115    }
116
117    /**
118     * Calculate a hash code for this object
119     * 
120     * @see java.lang.Object#hashCode()
121     */
122    @Override
123    public int hashCode()
124    {
125        return new HashCodeBuilder(47, 11).append(getId()).append(getName()).toHashCode();
126    }
127}