/* $Id: GSResource.java 372 2011-10-01 02:32:51Z minao $ */
package smart_gs.logical;


import javax.swing.JPanel;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import reasoning_web.logical.NotMarkupException;
import reasoning_web.logical.inter_face.NoWayToShowException;
import smart_gs.GSConstants;
import smart_gs.GSVersion;
import smart_gs.debugprint.Debugprint;
import smart_gs.logical.visitor.Acceptor;
import smart_gs.logical.visitor.CreateXMLElementVisitor;
import smart_gs.logical.visitor.ShowResourceVisitor;
import smart_gs.logical.visitor.Visitor;
import smart_gs.reasoning_web.URIBearer;
import smart_gs.reasoning_web.logical.GS_RWElement;

public abstract class GSResource implements GS_RWElement,Acceptor,URIBearer{

	protected String uri;
	protected String originalURI;
	protected String oldVersionURI;
	protected String name;
	protected int id;
	protected String version;

	public static final String NAME = "name";
	public static final String ID = "id";
	public static final String URI = "uri";
	public static final String ORIGINAL_URI = "originalURI";
	public static final String VERSION = "version";
	public static final String OLD_VERSION_URI = "oldVersionURI";

//	2011.03.29 shayashi deleted
//	//2007/11/20 kazuhiro kobayashi
//	protected String userName;
//	protected String systemName;
//
	public GSResource() {
		this.name = "";
		this.id = this.hashCode() % 1000;
		this.version = GSVersion.getVersion();
	}

	public void setId(int id) {
		this.id = id;
	}
	public int getId() {
		return id;
	}
	public String getURI(){
		return this.uri;
	}	
	public void setURI(String uri){
		this.uri = uri;
	}
	public void extendURIBy(String extension) {
		this.uri = this.uri + extension;
	}
	public void extendURIBy(int extension) {
		this.uri = this.uri + extension;
	}
	public void extendOriginalUriBy(String extension) {
		this.originalURI = this.originalURI + extension;
	}
	public void extendOriginalUriBy(int extension) {
		this.originalURI = this.originalURI + extension;
	}
	public String getName(){
		return this.name;
	}
	public void setName(String name){
		this.name = name;
	}

	public final void show() {
		ShowResourceVisitor visitor = new ShowResourceVisitor();
		this.accept(visitor);
	}
	public String getSource() throws NotMarkupException{
		throw new NotMarkupException();
	}
	
	// TODO 表示できないものの表示を統一せよ！！
	public JPanel createElementViewPanel() throws NoWayToShowException {
		throw new NoWayToShowException();
	}
	public JPanel createElementViewPanel(int width,int height) throws NoWayToShowException {
		throw new NoWayToShowException();
	}
	@Override
	public String toString() {
		return this.uri;
	}
	public final Element createXMLElement(Document document){
		CreateXMLElementVisitor visitor = new CreateXMLElementVisitor(document);
		this.accept(visitor);
		return visitor.getLastElement();
	}

	public abstract void accept(Visitor visitor);

//	2011.03.28 shayashi deleted
//	//2007/11/20 kazuhiro kobayashi
//	/**
//	 * String配列を受けて、systemNameが一致するものはuserNameをそこから設定する。
//	 */
//	public void setUserName(String[] names){
//		if(names[0].equals(this.systemName)){
//			this.userName = names[1];
//		}
//	}

	public abstract String createURI();
	public abstract String getTypeString();

	public void setOriginalURI(String originalURI) {
		this.originalURI = originalURI;
	}
	public String getOriginalURI() {
		return originalURI;
	}
	public void setURIs() {
		this.originalURI = this.createURI();
		this.uri = this.originalURI;
	}

	public void setURIs(String uri, String originalURI) {
		this.uri = uri;
		this.originalURI = originalURI;
	}	
	public String getVersion() {
		return this.version;
	}
	public void setVersion(String version) {
		this.version = version;
	}

	public boolean isZombie() {
		String type = getTypeString();
		if (type == null) {
			System.out.printf("For %s getTypeString is not defined", this);
			return false;
		}
		if (type == URIObject.ZOMBIE) return true;
		return false;
	}

	@Override
	public String getOldVersionURI() {
		return this.oldVersionURI;
	}
	@Override
	public void setOldVersionURI(String oldVersionURI) {
		this.oldVersionURI = oldVersionURI;
	}

}
