package edu.mssm.pharm.maayanlab.Enrichr;

// Generated Dec 13, 2012 12:26:26 PM by Hibernate Tools 4.0.0

import static javax.persistence.GenerationType.IDENTITY;

import java.io.Serializable;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import java.util.Random;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * User generated by hbm2java
 */
@Entity
@Table(name = "users", catalog = "enrichr")
public class User implements Serializable {

	private static final long serialVersionUID = 1893085998342363733L;
	
	private Integer userid;
	private String email;
	private String salt;
	private String password;
	private String first;
	private String last;
	private String institute;
	private Date accessed;
	
	public User() {
	}

	public User(String email, String password) {
		this.setEmail(email);
		this.setPassword(password);
	}

	public User(String email, String password, String first, String last, String institute) {
		this.setEmail(email);
		this.setPassword(password);
		this.setFirst(first);
		this.setLast(last);
		this.setInstitute(institute);
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "userid", unique = true, nullable = false)
	public Integer getUserid() {
		return this.userid;
	}

	// Shouldn't be used because auto-incremented by db
	public void setUserid(Integer userid) {
		this.userid = userid;
	}	
	
	@Column(name = "email", unique = true, nullable = false, length = 100)
	public String getEmail() {		
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	@Column(name = "salt", nullable = false, length = 16)
	public String getSalt() {
		return this.salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}
	
	private String generateSalt() {
		Random r = new SecureRandom();
		byte[] saltBytes = new byte[8];
		r.nextBytes(saltBytes);
		return new BigInteger(1, saltBytes).toString(16);
	}

	@Column(name = "password", nullable = false, length = 32)
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.setSalt(generateSalt());
		this.password = hashPassword(this.salt + password);
	}
	
	public boolean checkPassword(String password) {
		return this.password.equals(hashPassword(this.salt + password)); 
	}
	
	private String hashPassword(String password) {
		String hash = null;
		
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(password.getBytes());
			hash = new BigInteger(1, digest.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		return hash;
	}

	@Column(name = "first", length = 50)
	public String getFirst() {
		return this.first;
	}

	public void setFirst(String first) {
		if (!first.isEmpty())
			this.first = first;
	}

	@Column(name = "last", length = 200)
	public String getLast() {
		return this.last;
	}

	public void setLast(String last) {
		if (!last.isEmpty())
			this.last = last;
	}

	@Column(name = "institute", length = 200)
	public String getInstitute() {
		return this.institute;
	}

	public void setInstitute(String institute) {
		if (!institute.isEmpty())
			this.institute = institute;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "accessed", nullable = false, length = 19)
	public Date getAccessed() {
		return this.accessed;
	}

	// Shouldn't be used because it uses default timestamp by db
	public void setAccessed(Date accessed) {
		this.accessed = accessed;
	}
	
	// Use this to update timestamp
	public void updateAccessed() {
		this.accessed = null;
	}
}

@@ -2512,32 +2512,24 @@
 sword = hash
-Password
 (this.salt +
@@ -2632,24 +2632,16 @@
 als(hash
-Password
 (this.sa
@@ -2686,32 +2686,24 @@
 ing hash
-Password
 (String 
 password
@@ -2686,33 +2686,39 @@
 ing hash(String 
-p
+saltedP
 assword) %7B%0D%0A%09%09St
@@ -2827,17 +2827,23 @@
 .update(
-p
+saltedP
 assword.

Refactor to better reflect what the function and variables are.