package com.mason.server.model;

import javax.jdo.annotations.EmbeddedOnly;
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 UmlRelationship {

	private String generatedKey;

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

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

	@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( 
				UmlRelationship.class.getSimpleName(), id);
	}

	@Persistent
	private String kind;

	public String getKind() {
		return kind;
	}

	public void setKind(String kind) {
		this.kind = kind;
	}
	
	@PersistenceCapable
    @EmbeddedOnly
    public static class UmlRelatedElement {
		
		@Persistent
		private String decorator;

		@Persistent
		private String port;
		
		public String getDecorator() {
			return decorator;
		}

		public void setDecorator(String decorator) {
			this.decorator = decorator;
		}
		
		public String getPort() {
			return port;
		}

		public void setPort(String port) {
			this.port = port;
		}
    }

	@Persistent
	@JsonIgnore
	private Long viewId;

	@JsonIgnore
	private String viewIdGenerated;

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

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

	public String getViewIdGenerated() {
		if (viewId != null && viewIdGenerated != null) {
			this.viewIdGenerated = KeyFactory.createKeyString(
					UmlModel.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
	@JsonIgnore
	private Long sourceId;

	private String sourceIdGenerated;

	public Long getSourceId() {
		if (sourceId == null && sourceIdGenerated != null) {
			this.sourceId = KeyFactory.stringToKey(sourceIdGenerated).getId();
		}
		return sourceId;
	}

	public void setSourceId(Long sourceId) {
		this.sourceId = sourceId;
		this.sourceIdGenerated = KeyFactory.createKeyString(
				UmlElementContainer.class.getSimpleName(), sourceId);
	}

	public String getSourceIdGenerated() {
		if (sourceId != null && sourceIdGenerated != null) {
			this.sourceIdGenerated = KeyFactory.createKeyString(
					UmlElementContainer.class.getSimpleName(), sourceId);
		}
		return sourceIdGenerated;
	}

	public void setSourceIdGenerated(String modelIdGenerated) {
		this.sourceIdGenerated = modelIdGenerated;
		if (sourceId == null && sourceIdGenerated != null) {
			this.sourceId = KeyFactory.stringToKey(sourceIdGenerated).getId();
		}
	}
	
	@Persistent
	private String sourceDecorator;

	@Persistent
	private String sourcePort;

	public String getSourceDecorator() {
		return sourceDecorator;
	}

	public void setSourceDecorator(String sourceDecorator) {
		this.sourceDecorator = sourceDecorator;
	}

	public String getSourcePort() {
		return sourcePort;
	}

	public void setSourcePort(String sourcePort) {
		this.sourcePort = sourcePort;
	}

	@Persistent
	@JsonIgnore
	private Long targetId;

	private String targetIdGenerated;

	public Long getTargetId() {
		if (targetId == null && targetIdGenerated != null) {
			this.targetId = KeyFactory.stringToKey(targetIdGenerated).getId();
		}
		return targetId;
	}

	public void setTargetId(Long targetId) {
		this.targetId = targetId;
		this.targetIdGenerated = KeyFactory.createKeyString(
				UmlElementContainer.class.getSimpleName(), targetId);
	}

	public String getTargetIdGenerated() {
		if (targetId != null && targetIdGenerated != null) {
			this.targetIdGenerated = KeyFactory.createKeyString(
					UmlElementContainer.class.getSimpleName(), targetId);
		}
		return targetIdGenerated;
	}

	public void setTargetIdGenerated(String targetIdGenerated) {
		this.targetIdGenerated = targetIdGenerated;
		if (targetId == null && targetIdGenerated != null) {
			this.targetId = KeyFactory.stringToKey(targetIdGenerated).getId();
		}
	}

	@Persistent
	private String targetDecorator;

	@Persistent
	private String targetPort;
	
	public String getTargetDecorator() {
		return targetDecorator;
	}

	public void setTargetDecorator(String targetDecorator) {
		this.targetDecorator = targetDecorator;
	}

	public String getTargetPort() {
		return targetPort;
	}

	public void setTargetPort(String targetPort) {
		this.targetPort = targetPort;
	}


}
