package org.owasp.appsensor.core;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

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

/**
 * The standard User object. This represents the end user in the system, 
 * NOT the client application. 
 * 
 * The base implementation assumes the username is provided by the client application. 
 * 
 * It is up to the client application to manage the username. 
 * The username could be anything, an actual username, an IP address, 
 * or any other identifier desired. The core notion is that any desired 
 * correlation on the user is done by comparing the username.
 * 
 * @see java.io.Serializable
 *
 * @author John Melton (jtmelton@gmail.com) http://www.jtmelton.com/
 */
@Entity
public class User implements Serializable {

	private static final long serialVersionUID = 5084152023446797592L;

	@Id
	@Column
	@GeneratedValue
	private Integer id;
	
	private String username;

	public User() {}
	
	public User(String username) {
		setUsername(username);
	}
	
	public String getUsername() {
		return username;
	}

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

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17,31).
				append(username).
				toHashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		
		User other = (User) obj;
		
		return new EqualsBuilder().
				append(username, other.getUsername()).
				isEquals();
	}
	
	@Override
	public String toString() {
		return new ToStringBuilder(this).
				append("username", username).
			    toString();
	}
	
}

@@ -54,24 +54,52 @@
 ializable;%0A%0A
+import javax.inject.Inject;%0A
 import javax
@@ -119,16 +119,16 @@
 Column;%0A
-
 import j
@@ -393,16 +393,61 @@
 ilder;%0A%0A
+import com.google.common.net.InetAddresses;%0A%0A
 /**%0A * T
@@ -1223,16 +1223,97 @@
 sername;
+%0A%09%0A%09private IPAddress ipAddress;%0A%09%0A%09@Inject%0A%09private transient IPAddress locator;
 %0A%0A%09publi
@@ -1384,24 +1384,134 @@
 name);%0A%09%7D%0A%09%0A
+%09public User(String username, IPAddress ipAddress) %7B%0A%09%09setUsername(username);%0A%09%09setIPAddress(ipAddress);%0A%09%7D%0A%09%0A
 %09public Stri
@@ -1623,24 +1623,353 @@
 sername;%0A%09%09%0A
+%09%09// if IP is used as username, setup IP address w/ geolocation%0A%09%09if (InetAddresses.isInetAddress(username)) %7B%0A%09%09%09this.ipAddress = locator.fromString(username);%0A%09%09%7D%0A%09%09%0A%09%09return this;%0A%09%7D%0A%09%0A%09public IPAddress getIPAddress() %7B%0A%09%09return ipAddress;%0A%09%7D%0A%0A%09public User setIPAddress(IPAddress ipAddress) %7B%0A%09%09this.ipAddress = ipAddress;%0A%09%09%0A
 %09%09return thi
@@ -2467,16 +2467,16 @@
 (this).%0A
-
 %09%09%09%09appe
@@ -2501,16 +2501,52 @@
 rname).%0A
+%09%09%09%09append(%22ipAddress%22, ipAddress).%0A
 %09%09%09    t

add ip address to user