/* $ID: $
 * Created on 14.08.2003
 */
package de.skyrix.zsp.logic.httpserver.modules;

import java.util.Properties;

import de.skyrix.zsp.logic.DAVItem;
import de.skyrix.zsp.logic.Proxy;
import de.skyrix.zsp.logic.httpserver.MethodModule;
import de.skyrix.zsp.logic.httpserver.Response;
import de.skyrix.zsp.logic.parser.ProppatchQueryParser;

/**Module to hande PROPPATCH queries.
 * 
 * @author Burkhard Sell
 */
public class PropPatchModule extends MethodModule {

	/**Returns "PROPPATCH"
	 *  
	 * @see de.skyrix.zsp.logic.httpserver.MethodModule#getMethodName()
	 */
	public String getMethodName() {
		return "PROPPATCH";
	}

	/**Processing the query.
	 * 
	 * @see de.skyrix.zsp.logic.httpserver.MethodModule#processQuery(java.lang.String, java.util.Properties, java.util.Properties, java.lang.String)
	 * @return the response object;
	 */
	public Response processQuery(
		String uri,
		Properties header,
		Properties parms,
		String body) {

		System.out.println("proppatch module invoked");
		System.out.println("uri = " + uri);
		System.out.println("header = " + header);
		System.out.println("body = " + body);

		Proxy proxy = Proxy.getInstance();
		if (proxy == null) {
			System.err.println("ERROR: No proxy instance found.");
			return null;
		}

		if (body != null) {

			//Parse element
			ProppatchQueryParser ppqParser = new ProppatchQueryParser(body);
			DAVItem davItem = ppqParser.parseDocument();

			if (davItem != null) {
				try {
					String location = uri.substring(0, uri.lastIndexOf("/"));

					boolean result = false;

					//Determine wheter the element is new or updated
					if (uri.endsWith("/NEW")) {
						//create new message
						System.out.println("!!!!!NEW ELEMENT");
						result = proxy.createMessage(davItem, location);
					}
					else {
						//update existing message
						System.out.println("EDITING EXISTING ELEMENT");
						String idString = uri.substring(uri.lastIndexOf("/") + 1);
						System.out.println("!!!!!idstr = " + idString);
					}

					//create response object
					if (result) {
						System.out.println("creating response...");
						String messageID = davItem.getProperty("message_id");

						String responseBody =
							"<?xml version=\"1.0\"?>"
								+ "<D:multistatus xmlns:a=\"http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-c000-000000000046}/\" xmlns:b=\"http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-c000-000000000046}/\" xmlns:pt=\"http://schemas.microsoft.com/mapi/proptag/\" xmlns:D=\"DAV:\">"
								+ "<D:response><D:href>/zidestore/so/sell/IPM/public/Contacts/NEW</D:href>"
								+ "<D:propstat><D:status>HTTP/1.1 200 OK</D:status>"
								+ "<D:prop>"
								+ "</D:prop></D:propstat></D:response>"
								+ "</D:multistatus>";
						Response response =
							prepareResponse("207 Multistatus", "text/xml", responseBody);

						response.addHeader("x-skyrix-newname", messageID);
						return response;
					}
				}
				catch (Exception e) {
					e.printStackTrace();
				}

				//save element at disk

				//start thread to publish changes

				//create response
			}
		}

		System.out.println("PROPPATCH MODULE END!!!");

		return new Response("404 Not found", "text/html", (String) null);
	}
}
