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

import java.util.List;
import java.util.Properties;

import de.skyrix.zsp.logic.DAVItem;
import de.skyrix.zsp.logic.Proxy;
import de.skyrix.zsp.logic.ZidelookQueries;
import de.skyrix.zsp.logic.httpserver.MethodModule;
import de.skyrix.zsp.logic.httpserver.Response;
import de.skyrix.zsp.logic.parser.ElementCreationParser;
import de.skyrix.zsp.logic.parser.PropfindQueryParser;

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

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

	/**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("PROPFIND "+uri);
      System.out.println(body+"\n\n");
      
      
		Proxy proxy = Proxy.getInstance();
		if (proxy == null) {
			System.err.println("ERROR: No proxy instance found.");
			return null;
		}

		ElementCreationParser ecParser = new ElementCreationParser();
		PropfindQueryParser cqParser = null;
		List attributeList = null;

		if (body != null) {
			cqParser = new PropfindQueryParser(body);
			attributeList = cqParser.parseDocument();
		}

		String responseBody = null;
		String depth = (String) header.get("Depth");

		if (depth != null) {
			if (uri.endsWith("/") && uri.indexOf("_range_") < 0) {
				//Folder
				//check the depth
				if ("0".equals(depth)) {
					//Depth = 0 --> get folder properties
					DAVItem item = proxy.getFolder(uri);

					if (item != null) {
						responseBody = ecParser.createPropfindResponse(item, attributeList);
					}
					else
						responseBody = ZidelookQueries.createEmptyResponse(uri);
				}
				else {
					//Depth = 1 --> list folder items
					List items = proxy.listFolderItems(uri);

          /*if (!uri.endsWith("/zidestore/so/" + ZSPConfig.getProperty("zidestore_user") + "/")) {
            //attributeList = Arrays.asList(ZidelookQueries.listAttributes);
          }*/
          
					responseBody = ecParser.createPropfindRespone(items, attributeList);
				}
			}
			else {
				//Message
				try {
					//if ("0".equals(depth)) {
					//Depth = 0 --> get message properties

					if (uri.endsWith("/"))
						uri = uri.substring(0, uri.length() - 1);

					List messages = proxy.getMessages(uri);
					if (messages != null) {
						responseBody = ecParser.createPropfindRespone(messages, attributeList);
					}
					//}
					//else
					//  ; //Depth = 1 not allowed on messages
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}

			if (responseBody != null) {
				return prepareResponse("207 Multistatus", "text/xml", responseBody);
			}
		}

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