package com.mason.server.model;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(detachable = "true")
public class DbUser {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	private String generatedKey;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
		this.generatedKey = KeyFactory.createKeyString(
				Project.class.getSimpleName(), id);
	}

	public String getGeneratedKey() {
		if (this.generatedKey == null && id != null) {
			this.generatedKey = KeyFactory.createKeyString(
					Project.class.getSimpleName(), id);
		}
		return generatedKey;
	}

	public void setGeneratedKey(String generatedKey) {
		this.generatedKey = generatedKey;
	}

	@Persistent
	private String username;

	@Persistent
	private String password;

	@Persistent
	private String email;

	/**
	 * Access level of the user. 1 = Admin user 2 = Regular user
	 */
	@Persistent
	private Integer access;

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getAccess() {
		return access;
	}

	public void setAccess(Integer access) {
		this.access = access;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUsername() {
		return username;
	}

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

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj == null || obj.getClass() != this.getClass()) {
            return false;
        }

        DbUser user = (DbUser) obj;
        return user.getId() == getId();
    }
   
    @Override
    public int hashCode() {
        final int prime = 31;
        long result = 1;
        result = prime * result
                + ((getUsername() == null) ? 0 : getUsername().hashCode());
        if (this.getId() != null ) {
        	result = prime * result + getId();
        }
        return (int) result;
    }
    
}
