/*
 * Copyright 2006 Wyona
 */

package org.wyona.yanel.impl.resources;

import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.api.attributes.ViewableV2;
import org.wyona.yanel.core.attributes.viewable.View;
import org.wyona.yanel.core.attributes.viewable.ViewDescriptor;
import org.wyona.yanel.core.navigation.Node;
import org.wyona.yanel.core.navigation.Sitetree;

import org.apache.log4j.Category;

/**
 *
 */
public class DataRepoSitetreeResource extends Resource implements ViewableV2 {

    private static Category log = Category.getInstance(DataRepoSitetreeResource.class);

    /**
     *
     */
    public DataRepoSitetreeResource() {
    }

    /**
     *
     */
    public String getMimeType(String viewId) throws Exception {
        if (viewId != null && viewId.equals("xml")) return "application/xml";
        return "application/xhtml+xml";
    }

    /**
     *
     */
    public long getSize() throws Exception {
        return -1;
    }

    /**
     *
     */
    public boolean exists() throws Exception {
        return true;
    }

    /**
     *
     */
    public View getView(String viewId) throws Exception {

        StringBuffer sb = new StringBuffer("<?xml version=\"1.0\"?>");
        if (viewId != null && viewId.equals("xml")) {
            sb.append(getSitetreeAsXML());
            //sb.append(getSitetreeAsXML(getPath().toString()));
        } else {
            sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"><head><title>Browse Data Repository Sitetree</title></head><body><a href=\"?yanel.resource.viewid=xml\">Show XML</a><br/><br/>This content is being generated by the resource <![CDATA[" + getResourceTypeUniversalName() + "]]></body></html>");
        }

        View view = new View();
        view.setMimeType(getMimeType(viewId));
        view.setInputStream(new java.io.StringBufferInputStream(sb.toString()));
        return view;
    }

    /**
     *
     */
    public ViewDescriptor[] getViewDescriptors() {
        try {
            ViewDescriptor[] vd = new ViewDescriptor[2];
            vd[0] = new ViewDescriptor("default");
            vd[0].setMimeType(getMimeType(null));
            vd[1] = new ViewDescriptor("xml");
            vd[1].setMimeType(getMimeType("xml"));
            return vd;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * Get sitetree as XML
     */
    private String getSitetreeAsXML() {
        StringBuffer sb = new StringBuffer("<sitetree>");
        if (getRequest().getParameter("path") != null) {
            sb.append(getNodeAsXML(request.getParameter("path")));
        } else {
            sb.append(getNodeAsXML("/"));
        }

        // TODO: Sitetree generated out of RDF resources and statements
        /*
        com.hp.hpl.jena.rdf.model.Resource rootResource = getRealm().getSitetreeRootResource();
        sb.append(getNodeAsXML(rootResource));
        */
        sb.append("</sitetree>");

        return sb.toString();
    }

    /**
     * Get node as XML
     */
    private String getNodeAsXML(String path) {
    //private String getNodeAsXML(com.hp.hpl.jena.rdf.model.Resource resource) {
        //log.error("DEBUG: Path: " + path);
        Sitetree sitetree = getRealm().getRepoNavigation();
        Node node = sitetree.getNode(getRealm(), path);
        StringBuffer sb = new StringBuffer("");

        // TODO: Check for statements "parentOf" for this resource
        /*
        Statement[] st = resource.getStatements("parentOf");
        if (st.length > 0) {
            for (int i = 0; i < st.length; i++) {
                Resource child = st.getObject();
                URL url = getReal().getURLBuilder(child);
            }
        } else {
            // Is not a collection, there are no children
        }
        */

        if (node != null) {
            if (node.isCollection()) {
                sb.append("<collection path=\"" + path + "\" name=\" " + node.getName() + "\">");
                Node[] children = node.getChildren();
                for (int i = 0; i < children.length; i++) {
                    if (children[i].isCollection()) {
                        sb.append(getNodeAsXML(children[i].getPath()));
                    } else if (children[i].isResource()) {
                        sb.append("<resource path=\"" + children[i].getPath() + "\" name=\"" + children[i].getName() + "\"/>");
                    } else {
                        sb.append("<neither-resource-nor-collection path=\"" + children[i].getPath() + "\" name=\"" + children[i].getName() + "\"/>");
                    }
                }
                sb.append("</collection>");
            } else {
                sb.append("<resource path=\"" + path + "\" name=\"" + node.getName() + "\"/>");
            }
        } else {
            String errorMessage = "node is null for path: " + path;
            sb.append("<exception>" + errorMessage + "</exception>");
            log.error(errorMessage);
        }
        return sb.toString();
    }
}

@@ -4022,17 +4022,16 @@
  name=%5C%22
- 
 %22 + node
@@ -4157,32 +4157,301 @@
 .length; i++) %7B%0A
+                    String childPath = path + %22/%22 + children%5Bi%5D.getName();%0A                    if (path.equals(%22/%22)) %7B%0A                        childPath = path + children%5Bi%5D.getName();%0A                    %7D%0A                    //log.debug(%22Child path: %22 + childPath);%0A%0A
                 
@@ -4504,32 +4504,94 @@
                 
+sb.append(getNodeAsXML(childPath));%0A                        //
 sb.append(getNod
@@ -4728,32 +4728,150 @@
 ource path=%5C%22%22 +
+ childPath + %22%5C%22 name=%5C%22%22 + children%5Bi%5D.getName() + %22%5C%22/%3E%22);%0A                        //sb.append(%22%3Cresource path=%5C%22%22 +
  children%5Bi%5D.get
@@ -5026,32 +5026,173 @@
 ction path=%5C%22%22 +
+ childPath + %22%5C%22 name=%5C%22%22 + children%5Bi%5D.getName() + %22%5C%22/%3E%22);%0A                        //sb.append(%22%3Cneither-resource-nor-collection path=%5C%22%22 +
  children%5Bi%5D.get

construct child path