package com.mason.server.model;

import java.util.Date;

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 Log {

	@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) {
		if (id == 0) {
			this.id = KeyFactory.stringToKey(this.generatedKey).getId();
		}
		this.generatedKey = generatedKey;
	}

	public Log(long id, String type, Action action) {
		this(id, type, action, new Date(), null);
	}

	public Log(long id, String type, Action action, String description) {
		this(id, type, action, new Date(), description);
	}

	public Log(long id, String type, Action action, Date date,
			String description) {
		this.setObjectId(id);
		this.setObjectType(type);
		this.setAction(action);
		this.setDate(date);
		this.setDescription(description);
	}

	public enum Action {
		CREATE, UPDATE, DELETE, CHECKOUT, CHECKIN
	}

	@Persistent
	private String description;

	@Persistent
	private Date date;

	@Persistent
	private long objectId;

	@Persistent
	private String objectType;

	@Persistent
	private Action action;

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Action getAction() {
		return action;
	}

	public void setAction(Action action) {
		this.action = action;
	}

	public long getObjectId() {
		return objectId;
	}

	public void setObjectId(long objectId) {
		this.objectId = objectId;
	}

	public String getObjectType() {
		return objectType;
	}

	public void setObjectType(String objectType) {
		this.objectType = objectType;
	}

}
