001package org.apache.fulcrum.security.model.turbine.entity;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.Serializable;
023
024import org.apache.commons.lang3.builder.HashCodeBuilder;
025import org.apache.fulcrum.security.entity.Group;
026import org.apache.fulcrum.security.entity.Role;
027import org.apache.fulcrum.security.entity.User;
028
029/**
030 * Represents the "turbine" model where permissions are in a many to many
031 * relationship to roles, roles are related to groups are related to users, all
032 * in many to many relationships.
033 * 
034 * @author <a href="mailto:epugh@upstate.com">Eric Pugh </a>
035 * @version $Id$
036 */
037public class TurbineUserGroupRole implements Serializable
038{
039    /**
040     * Serial number
041     */
042    private static final long serialVersionUID = 265670888102016829L;
043
044    private User user;
045
046    private Group group;
047
048    private Role role;
049
050    /**
051     * Get the group
052     * 
053     * @return Returns the group.
054     */
055    public Group getGroup()
056    {
057        return group;
058    }
059
060    /**
061     * Get the role
062     * 
063     * @return Returns the role.
064     */
065    public Role getRole()
066    {
067        return role;
068    }
069
070    /**
071     * Get the user
072     * 
073     * @return Returns the user.
074     */
075    public User getUser()
076    {
077        return user;
078    }
079
080    /**
081     * Set the group
082     * 
083     * @param group
084     *            The group to set.
085     */
086    public void setGroup(Group group)
087    {
088        this.group = group;
089    }
090
091    /**
092     * Set the role
093     * 
094     * @param role
095     *            The role to set.
096     */
097    public void setRole(Role role)
098    {
099        this.role = role;
100    }
101
102    /**
103     * Set the user
104     * 
105     * @param user
106     *            The user to set.
107     */
108    public void setUser(User user)
109    {
110        this.user = user;
111    }
112
113    @Override
114    public boolean equals(Object obj)
115    {
116        if (null == obj)
117        {
118            return false;
119        }
120        if (!(obj instanceof TurbineUserGroupRole))
121        {
122            return false;
123        }
124        else
125        {
126            TurbineUserGroupRole mObj = (TurbineUserGroupRole) obj;
127            if (null != this.getRole() && null != mObj.getRole())
128            {
129                if (!this.getRole().equals(mObj.getRole()))
130                {
131                    return false;
132                }
133            }
134            else
135            {
136                return false;
137            }
138            if (null != this.getUser() && null != mObj.getUser())
139            {
140                if (!this.getUser().equals(mObj.getUser()))
141                {
142                    return false;
143                }
144            }
145            else
146            {
147                return false;
148            }
149            if (null != this.getGroup() && null != mObj.getGroup())
150            {
151                if (!this.getGroup().equals(mObj.getGroup()))
152                {
153                    return false;
154                }
155            }
156            else
157            {
158                return false;
159            }
160            return true;
161        }
162    }
163
164    @Override
165    public int hashCode()
166    {
167        HashCodeBuilder hcBuilder = new HashCodeBuilder(39, 17);
168
169        Role role = getRole();
170        if (null != role)
171        {
172            hcBuilder.append(role);
173        }
174
175        User user = getUser();
176        if (null != user)
177        {
178            hcBuilder.append(user);
179        }
180
181        Group group = getGroup();
182        if (null != group)
183        {
184            hcBuilder.append(group);
185        }
186
187        return hcBuilder.toHashCode();
188    }
189
190    @Override
191    public String toString()
192    {
193        StringBuilder sb = new StringBuilder();
194
195        sb.append(null != getUser() ? getUser().toString() : "null");
196        sb.append('\n');
197        sb.append(null != getGroup() ? getGroup().toString() : "null");
198        sb.append('\n');
199        sb.append(null != getRole() ? getRole().toString() : "null");
200        sb.append('\n');
201
202        return sb.toString();
203    }
204}