package com.dbxml.db.enterprise.jsp;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: QueryTag.java,v 1.2 2004/02/12 00:17:57 bradford Exp $
 */

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.w3c.dom.Node;

import com.dbxml.db.client.CollectionClient;
import com.dbxml.db.client.ResultSetClient;
import com.dbxml.xml.dom.TextWriter;

/**
 * QueryTag queries the specified Collection or Document key and either assigns
 * the result to a ResultSetClient or streams it to the JspWriter.
 * <br>
 * <br>
 * Example:
 * <br>
 * <pre>   &lt;dbxml:query name="myQuery" collection="myCol" style="XPath">
 *      /newDoc
 *   &lt;/dbxml:query></pre>
 */

public class QueryTag extends BodyTagSupport {
   static final String NAME = "name";
   static final String COLLECTION = "collection";
   static final String KEY = "key";
   static final String STYLE = "style";
   static final String HTML = "html";

   private String name;
   private String collection;
   private String key;
   private String style;
   private boolean html;

   public QueryTag() {
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getCollection() {
      return collection;
   }

   public void setCollection(String collection) {
      this.collection = collection;
   }

   public String getKey() {
      return key;
   }

   public void setKey(String key) {
      this.key = key;
   }

   public String getStyle() {
      return style;
   }

   public void setStyle(String style) {
      this.style = style;
   }

   public boolean isHtml() {
      return html;
   }

   public void setHtml(boolean html) {
      this.html = html;
   }

   public int doEndTag() throws JspException {
      Object obj = pageContext.getAttribute(collection);
      if ( obj == null || !(obj instanceof CollectionClient) )
         throw new JspException("No Collection named '" + collection + "' has been declared");
      CollectionClient col = (CollectionClient)obj;

      try {
         Map nsMap = new HashMap();
         Enumeration enum = getValues();
         while ( enum != null && enum.hasMoreElements() ) {
            String key = (String)enum.nextElement();
            Object value = getValue(key);
            if ( value != null && value instanceof String ) {
               if ( key.startsWith("xmlns:") ) {
                  String prefix = key.substring(6);
                  nsMap.put(prefix, value);
               }
               else if ( key.equals("xmlns") )
                  nsMap.put("", value);
            }
         }

         BodyContent content = getBodyContent();
         String query = content.getString();
         ResultSetClient rs;
         if ( key != null )
            rs = col.queryDocument(style, query, nsMap, key);
         else
            rs = col.queryCollection(style, query, nsMap);

         if ( name != null )
            pageContext.setAttribute(name, rs);
         else {
            while ( rs.next() ) {
               if ( html ) {
                  Node n = rs.getResult();
                  TextWriter.writeHTML(n, pageContext.getOut());
               }
               else
                  pageContext.getOut().write(rs.getResultAsText());
            }
         }
      }
      catch ( Exception e ) {
         System.out.println(e);
         throw new JspException("Can't perform query on Collection '" + collection + "'", e);
      }

      return SKIP_BODY;
   }
}
