/* $Id: DAVItem.java,v 1.1 2004/01/09 16:54:18 burkhard Exp $
 * Created on 14.05.2003 by sell
 *
 */
package de.skyrix.zsp.logic;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Properties;

/**DAVResponseNode.
 * 
 * @author sell
 * @version 
 */
public class DAVItem extends Properties {
	private static final String ATTR_HASSUBFOLDERS = "x360A000B";
	private boolean isSpooled = false;
	private boolean isNew = false;
	private boolean isUpdated = false;

	public void setLocation(String location) {
		setProperty("location", location);
	}

	public String getLocation() {
		return getProperty("location");
	}

	public void setStatus(String status) {
		setProperty("status", status);
	}

	public String getStatus() {
		return getProperty("status");
	}

	public void setFolder(boolean folder) {
		setProperty("is_folder", "" + folder);
	}

	public boolean isFolder() {
		return "true".equals(getProperty("is_folder"));
	}

	public void setVersion(long version) {
		setProperty(
			"x0E1A0014",
			"" + version,
			"http://schemas.microsoft.com/mapi/proptag/",
			"pt");
	}

	public long getVersion() {
		try {
			return Long.parseLong(getValue("x0E1A0014"));
		}
		catch (Exception ex) {
		}

		return 0;
	}

	/*public void setVersion(long version) {
		setProperty("x0E1A0014", "" + version, "", "");
	}*/

	/*public void setVersion(long version) {
	  setProperty("DAVItemVersion", ""+version);
	}*/

	public String getValue(String key) {
		String property = getProperty(key);
		if (property != null) {
			try {
				return property.substring(property.indexOf("##") + 2);
			}
			catch (Exception e) {
			}
		}
		return null;
	}

	public String getNameSpaceURI(String key) {
		String property = getProperty(key);
		if (property != null) {
			try {
				return property.substring(
					property.indexOf(":") + 1,
					property.indexOf("##"));
			}
			catch (Exception e) {
			}
		}
		return null;
	}

	public String getNameSpacePrefix(String key) {
		String property = getProperty(key);
		if (property != null) {
			try {
				String prefix = property.substring(0, property.indexOf(":"));

				if (prefix != null && "".equals(prefix.trim())) {
					prefix = null;
				}

				return prefix;
			}
			catch (Exception e) {
			}
		}
		return null;
	}

	public boolean hasSubfolders() {
		if (!isFolder())
			return false;
		String value = getValue(ATTR_HASSUBFOLDERS);
		return (value != null && value.equals("1"));
	}

	public String toString() {
		ByteArrayOutputStream bOut = new ByteArrayOutputStream();
		PrintStream out = new PrintStream(bOut);
		list(out);
		String properties = new String(bOut.toByteArray());
		return "DAVItem:\nLocation: "
			+ getLocation()
			+ "\nstatus: "
			+ getStatus()
			+ "\nisFolder: "
			+ isFolder()
			+ "\nProperties: "
			+ properties;
	}

	public void setProperty(
		String key,
		String value,
		String prefix,
		String namespaceURI) {
		if (key == null || prefix == null || namespaceURI == null)
			return;
		super.setProperty(
			key,
			prefix + ":" + namespaceURI + "##" + ((value != null) ? value : ""));
	}

	public void setAll(DAVItem davItem) {
		if (davItem == null)
			return;
		try {
			putAll(davItem);
		}
		catch (Exception e) {
		}
	}

	/**Determines the ID of this DAVItem.
	 *  
	 * @return the ID or null if no ID set or this item is not a message.
	 */
	public String getID() {
		String ret = null;

		try {
			ret = getLocation();
			ret = ret.substring(ret.lastIndexOf("/") + 1);
		}
		catch (Exception e) {
			return null;
		}

		return ret;
	}
	/**
	 * @return
	 */
	public boolean isNew() {
		return isNew;
	}

	/**
	 * @return
	 */
	public boolean isSpooled() {
		return isSpooled;
	}

	/**
	 * @param b
	 */
	public void setNew(boolean b) {
		isNew = b;
	}

	/**
	 * @param b
	 */
	public void setSpooled(boolean b) {
		isSpooled = b;
	}

	public void load(InputStream inStream) throws IOException {
		super.load(inStream);

		//load internal properties
		isNew = "true".equals(getProperty("##internal.new", null));
		isSpooled = "true".equals(getProperty("##internal.spooled", null));
		isUpdated = "true".equals(getProperty("##internal.updated", null));

		//delete internat properties from property
		remove("##internal.new");
		remove("##internal.spooled");
		remove("##internal.updated");
	}

	public void store(OutputStream out, String header) throws IOException {
    //if(isNew) setpro
	}
}
