package de.terrestris.shogun2.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * @author Nils Bühner
 *
 */
@Entity
@Table
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class UserGroup extends PersistentObject {

	private static final long serialVersionUID = 1L;

	@Column
	private String name;

	@ManyToOne
	private User owner;

	@ManyToMany
	@JoinTable(
		name = "USERGROUP_USER",
		joinColumns = { @JoinColumn(name = "USERGROUP_ID") },
		inverseJoinColumns = { @JoinColumn(name = "USER_ID") }
	)
	private Set<User> members = new HashSet<User>();

	@ManyToMany
	@JoinTable(
		name = "USERGROUP_ROLE",
		joinColumns = { @JoinColumn(name = "USERGROUP_ID") },
		inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") }
	)
	private Set<Role> roles = new HashSet<Role>();

	/**
	 * Default Constructor
	 */
	public UserGroup() {
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the owner
	 */
	public User getOwner() {
		return owner;
	}

	/**
	 * @param owner
	 *            the owner to set
	 */
	public void setOwner(User owner) {
		this.owner = owner;
	}

	/**
	 * @return the members
	 */
	public Set<User> getMembers() {
		return members;
	}

	/**
	 * @param members the members to set
	 */
	public void setMembers(Set<User> members) {
		this.members = members;
	}

	/**
	 * @return the roles
	 */
	public Set<Role> getRoles() {
		return roles;
	}

	/**
	 * @param roles the roles to set
	 */
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 *
	 *      According to
	 *      http://stackoverflow.com/questions/27581/overriding-equals
	 *      -and-hashcode-in-java it is recommended only to use getter-methods
	 *      when using ORM like Hibernate
	 */
	@Override
	public int hashCode() {
		// two randomly chosen prime numbers
		return new HashCodeBuilder(53, 19).appendSuper(super.hashCode())
				.append(getName())
				.append(getOwner())
				.toHashCode();
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 *
	 *      According to
	 *      http://stackoverflow.com/questions/27581/overriding-equals
	 *      -and-hashcode-in-java it is recommended only to use getter-methods
	 *      when using ORM like Hibernate
	 */
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof UserGroup))
			return false;
		UserGroup other = (UserGroup) obj;

		return new EqualsBuilder().appendSuper(super.equals(other))
				.append(getName(), other.getName())
				.append(getOwner(), other.getOwner())
				.isEquals();
	}

	/**
	 *
	 */
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE);
	}
}

@@ -2639,24 +2639,74 @@
 getOwner())%0A
+%09%09%09%09.append(getMembers())%0A%09%09%09%09.append(getRoles())%0A
 %09%09%09%09.toHashC
@@ -3264,24 +3264,112 @@
 getOwner())%0A
+%09%09%09%09.append(getMembers(), other.getMembers())%0A%09%09%09%09.append(getRoles(), other.getRoles())%0A
 %09%09%09%09.isEqual

Add missing properties to hashCode() and equals()