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: IteratorTag.java,v 1.2 2004/02/12 00:17:57 bradford Exp $
 */

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

import com.dbxml.db.client.ResultSetClient;
import com.dbxml.util.dbXMLException;

/**
 * IteratorTag iterates over a specified QueryTag's ResultSet.
 * <br>
 * <br>
 * Example:
 * <br>
 * <pre>   &lt;dbxml:iterator query="myQuery" text="true">
 *      &lt;resValue>
 *         &lt;%= result %>
 *      &lt;/resValue>
 *   &lt;/dbxml:iterator></pre>
 */

public class IteratorTag extends BodyTagSupport {
   static final String QUERY = "query";
   static final String COL = "col";
   static final String KEY = "key";
   static final String RESULT = "result";
   static final String TEXT = "text";

   private String query;
   private ResultSetClient rs;
   private boolean text;

   public IteratorTag() {
   }

   public String getQuery() {
      return query;
   }

   public void setQuery(String query) {
      this.query = query;
   }

   public boolean isText() {
      return text;
   }

   public void setText(boolean text) {
      this.text = text;
   }

   public int doStartTag() throws JspException {
      Object obj = pageContext.getAttribute(query);
      if ( obj == null && !(obj instanceof ResultSetClient) )
         throw new JspException("No query named '" + query + "' has been declared");
      rs = (ResultSetClient)obj;
      try {
         if ( rs.next() ) {
            setVariables();
            return EVAL_BODY_BUFFERED;
         }
         else
            return SKIP_BODY;
      }
      catch ( dbXMLException e ) {
         throw new JspException("Error processing ResultSet from '" + query + "'", e);
      }
   }

   private void setVariables() throws dbXMLException {
      pageContext.setAttribute(COL, rs.getResultCollection().getCanonicalName());
      String key = rs.getResultKey();
      if ( key != null )
         pageContext.setAttribute(KEY, key);
      if ( text )
         pageContext.setAttribute(RESULT, rs.getResultAsText());
      else
         pageContext.setAttribute(RESULT, rs.getResult());
   }

   public int doAfterBody() throws JspException {
      try {
         String body = bodyContent.getString();
         bodyContent.clearBody();
         bodyContent.getEnclosingWriter().write(body);
         if ( rs.next() ) {
            setVariables();
            return EVAL_BODY_AGAIN;
         }
         else
            return SKIP_BODY;
      }
      catch ( Exception e ) {
         throw new JspException("Error processing ResultSet from '" + query + "'", e);
      }
   }
}
