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;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

@PersistenceCapable()
@JsonSerialize
public class UmlElementContainer {
 
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@JsonIgnore
	private Long id;


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

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

	private String generatedKey;

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

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

	@Persistent
	@JsonIgnore
	private Long elementId;

    @JsonSerialize
	private String elementIdGenerated;

	public Long getElementId() {
		if (elementId == null && elementIdGenerated != null) {
			this.elementId = KeyFactory.stringToKey(generatedKey).getId();
		}
		return elementId;
	}

	public void setElementId(Long elementId) {
		this.elementId = elementId;
		this.elementIdGenerated = KeyFactory.createKeyString(
				UmlElement.class.getSimpleName(), elementId);
	}

	public String getElementIdGenerated() {
		if (elementId != null && elementIdGenerated == null) {
			this.elementIdGenerated = KeyFactory.createKeyString(
					UmlElement.class.getSimpleName(), elementId);
		}
		return elementIdGenerated;
	}

	public void setElementIdGenerated(String elementIdGenerated) {
		this.elementIdGenerated = elementIdGenerated;
		if (elementId == null && elementIdGenerated != null) {
			this.elementId = KeyFactory.stringToKey(elementIdGenerated).getId();
		}
	}

	@Persistent
	@JsonIgnore
	private Long viewId;

    @JsonSerialize
	private String viewIdGenerated;

	public Long getViewId() {
		if (viewId == null && viewIdGenerated != null) {
			this.viewId = KeyFactory.stringToKey(viewIdGenerated).getId();
		}
		return viewId;
	}

	public void setViewId(Long ViewId) {
		this.viewId = ViewId;
		this.viewIdGenerated = KeyFactory.createKeyString(
				View.class.getSimpleName(), ViewId);
	}

	public String getViewIdGenerated() {
		if (viewId != null && viewIdGenerated == null) {
			this.viewIdGenerated = KeyFactory.createKeyString(
					View.class.getSimpleName(), viewId);
		}
		return viewIdGenerated;
	}

	public void setViewIdGenerated(String viewIdGenerated) {
		this.viewIdGenerated = viewIdGenerated;
		if (viewId == null && viewIdGenerated != null) {
			this.viewId = KeyFactory.stringToKey(viewIdGenerated).getId();
		}
	}

	@Persistent
    @JsonSerialize
	private long x;

	@Persistent
    @JsonSerialize
	private long y;

	@Persistent
    @JsonSerialize
	private long height;

	@Persistent
    @JsonSerialize
	private long width;

	public long getX() {
		return x;
	}

	public void setX(long x) {
		this.x = x;
	}

	public long getY() {
		return y;
	}

	public void setY(long y) {
		this.y = y;
	}

	public long getHeight() {
		return height;
	}

	public void setHeight(long height) {
		this.height = height;
	}

	public long getWidth() {
		return width;
	}

	public void setWidth(long width) {
		this.width = width;
	}

}