
// Copyright (c) 2000 Oracle Corporation
package oracle.jbo.html.jsp.datatags;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import oracle.jbo.*;
import oracle.jdeveloper.html.*;

/**
 * A Class class.
 * <P>
 * @author Juan Oropeza
 */
public class DataValueTag extends BodyTagSupport
{
   protected String            sDataSource;
   protected String            sDataItem = null;

   public DataValueTag()
   {
   }

   public void setDatasource(String sDataSource)
   {
      this.sDataSource = sDataSource;
   }

   public void setDataitem(String sValue)
   {
      this.sDataItem = sValue;
   }

   /**
     * Process the start tag for this instance.
     *
     * The doStartTag() method assumes that all setter methods have been
     * invoked before.
     *
     * When this method is invoked, the body has not yet been invoked.
     *
     * @returns EVAL_BODY_INCLUDE if the tag wants to process body, SKIP_BODY if it
     * does ont want to process it.
     */
   public int doStartTag() throws JspException
   {
         try
         {
            RowSet rs = (RowSet)pageContext.getAttribute(sDataSource + "_RowSet");

            if(rs == null)
            {
               throw new Exception(Res.format(Res.DATA_SOURCE_NOT_FOUND,sDataSource));
            }

            Row row = rs.getCurrentRow();

            if( row == null)
            {
              throw new Exception(Res.format(Res.CURRENT_ROW_IS_NULL,sDataSource));
            }

            if( row == null)
            {
               pageContext.getOut().print("&nbsp");
            }
            else
            {
               Object obj = null;

               if(!sDataItem.equals("RowKey"))
               {
                  int nDotIndex = sDataItem.indexOf('.');

                  if( nDotIndex != -1)
                  {
                    String sAttrName = null;
                    String sSubAttrName = null;

                    sAttrName = sDataItem.substring(0,nDotIndex);
                    sSubAttrName = sDataItem.substring(nDotIndex + 1);

                    AttributeList attrList = (AttributeList)row.getAttribute(sAttrName);

                    if(attrList != null)
                      obj = attrList.getAttribute(sSubAttrName);

                  }
                  else
                  {
                    obj = row.getAttribute(sDataItem);
                  }
               }
               else
               {
                  Key key = row.getKey();

                  obj = key.toStringFormat(false);
               }

               if(obj == null)
               {
                  pageContext.getOut().print("&nbsp");
               }
               else
               {
                  pageContext.getOut().print(obj);
               }
            }

         }
         catch(Exception ex)
         {
            throw new JspException(ex.getMessage());
         }
         return Tag.SKIP_BODY;
   }

   /**
     * release() called after doEndTag() to reset state
     */
   public void release()
   {
      sDataSource = null;
      sDataItem = null;

      super.release();
   }
}

