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 org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

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

@PersistenceCapable(detachable = "true")
@JsonSerialize
public class View {

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

    @JsonSerialize
	private String generatedKey;

	@JsonIgnore
	public Long getId() {
		return id;
	}

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

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

	public void setGeneratedKey(String generatedKey) {
		if (id == 0) {
			this.id = KeyFactory.stringToKey(this.generatedKey).getId();
		}
		this.generatedKey = generatedKey;
	}

	@Persistent
    @JsonSerialize
	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;
	}

	@Persistent

	@JsonIgnore
	private Long modelId;

    @JsonSerialize
	private String modelIdGenerated;

	public Long getModelId() {
		if (modelId == null && modelIdGenerated != null) {
			this.modelId = KeyFactory.stringToKey(modelIdGenerated).getId();
		}
		return modelId;
	}

	public void setModelId(Long modelId) {
		this.modelId = modelId;
		this.modelIdGenerated = KeyFactory.createKeyString(
				UmlModel.class.getSimpleName(), modelId);
	}

	public String getModelIdGenerated() {
		if (modelId != null && modelIdGenerated != null) {
			this.modelIdGenerated = KeyFactory.createKeyString(
					UmlModel.class.getSimpleName(), modelId);
		}
		return modelIdGenerated;
	}

	public void setModelIdGenerated(String modelIdGenerated) {
		this.modelIdGenerated = modelIdGenerated;
		if (modelId == null && modelIdGenerated != null) {
			this.modelId = KeyFactory.stringToKey(modelIdGenerated).getId();
		}
	}

}
