/*
   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you under the Apache License, Version 2.0 (the
   "License"); you may not use this file except in compliance
   with the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an
   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   KIND, either express or implied.  See the License for the
   specific language governing permissions and limitations
   under the License.    
 */

package org.opentides.bean.user;

import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.opentides.bean.BaseEntity;

@Entity
@AttributeOverride(name = "ID", column = @Column(insertable = false, updatable = false))
@Table(name = "AUTHORITIES")
public class UserRole extends BaseEntity {
	private static final long serialVersionUID = -2779918759002560767L;

	@Column(name = "USERNAME")
	private String username;

	@Column(name = "AUTHORITY")
	private String role;

	@ManyToOne(optional = false)
	@JoinColumn(name = "USERGROUP_ID", nullable = false)
	private UserGroup userGroup;

	public UserRole() {
		super();
	}

	public UserRole(UserGroup userGroup, String role) {
		Date now = new Date();
		this.setUserGroup(userGroup);
		this.setRole(role);
		this.setCreateDate(now);
		this.setUpdateDate(now);
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the userGroup
	 */
	public UserGroup getUserGroup() {
		return userGroup;
	}

	/**
	 * @param userGroup
	 *            the userGroup to set
	 */
	public void setUserGroup(UserGroup userGroup) {
		this.userGroup = userGroup;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((role == null) ? 0 : role.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		final UserRole other = (UserRole) obj;
		if (role == null) {
			if (other.role != null)
				return false;
		} else if (!role.equals(other.role))
			return false;
		return true;
	}
}

@@ -1499,24 +1499,81 @@
 ring role;%0D%0A
+%09%0D%0A%09// userGroup is nullable, to support username linkage
 %0D%0A%09@ManyToOn
@@ -1640,20 +1640,19 @@
 lable = 
-fals
+tru
 e)%0D%0A%09pri

make usergroup nullable to allow link of roles to user