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

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

	private String generatedKey;

	public Long getId() {
		if (id == null && generatedKey != null) {
			this.id = KeyFactory.stringToKey(generatedKey).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;
		if (id == null && generatedKey != null) {
			this.id = KeyFactory.stringToKey(this.generatedKey).getId();
		}
	}
	
	@Persistent
	private Long ownerId;
	
	private String ownerIdGenerated;

	public Long getOwnerId() {
		if (ownerId == null && ownerIdGenerated != null) {
			this.ownerId = KeyFactory.stringToKey(ownerIdGenerated).getId();
		}
		return ownerId;
	}

	public void setOwnerId(Long ownerId) {
		this.ownerId = ownerId;
		this.ownerIdGenerated = KeyFactory.createKeyString(
				DbUser.class.getSimpleName(), ownerId);
	}

	public String getOwnerIdGenerated() {
		if (ownerId != null && ownerIdGenerated != null) {
			this.ownerIdGenerated = KeyFactory.createKeyString(
					DbUser.class.getSimpleName(), ownerId);
		}
		return ownerIdGenerated;
	}

	public void setOwnerIdGenerated(String ownerIdGenerated) {
		this.ownerIdGenerated = ownerIdGenerated;
		if (ownerId == null && ownerIdGenerated != null) {
			this.ownerId = KeyFactory.stringToKey(ownerIdGenerated).getId();
		}
	}

	@Persistent
	private String slug;

	public String getSlug() {
		return slug;
	}

	public void setSlug(String slug) {
		this.slug = slug;
	}

	@Persistent
	private String description;

	public String getDescription() {
		return description;
	}

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

}
