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

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

/**
 * A Class class.
 * <P>
 * @author Juan Oropeza
 */
public class InputTag extends TagSupport
{
   String sDataSource;
   String sName;
   String sControlType = "DEFAULT";
   String sClassRenderer = null;
   String sFormName = null;
   String sSrcDataItem = null;
   String sDisplayAttribute = null;

   public InputTag()
   {
   }

   public void setItemname(String sname)
   {
      String sItemName = sname;

      this.sDataSource = sItemName.substring(0 , sItemName.indexOf('.'));
      this.sName = sItemName.substring(sItemName.indexOf('.')+ 1);
   }

   public void setFormname(String sName)
   {
      this.sFormName = sName;
   }

   public void setSourcedataitem(String sValue)
   {
      this.sSrcDataItem = sValue;
   }

   public void setDisplayattribute(String sValue)
   {
      this.sDisplayAttribute = sValue;
   }

   public void setType(String sType)
   {
      if(sType.equalsIgnoreCase("TEXT"))
      {
         sClassRenderer = "oracle.jdeveloper.html.TextField";
      }
      else if(sType.equalsIgnoreCase("TEXTAREA"))
      {
         sClassRenderer = "oracle.jdeveloper.html.TextArea";
      }
      else if(sType.equalsIgnoreCase("HIDDEN"))
      {
         sClassRenderer = "oracle.jdeveloper.html.HiddenField";
      }
      else if(sType.equalsIgnoreCase("READONLY"))
      {
         sClassRenderer = "oracle.jdeveloper.html.ReadOnlyField";
      }
      else if(sType.equalsIgnoreCase("ROWKEY"))
      {
         sClassRenderer = "oracle.jdeveloper.html.ReadOnlyField";
      }
      else if(sType.equalsIgnoreCase("DATE"))
      {
         sClassRenderer = "oracle.jdeveloper.html.DateField";
      }
      else if(sType.equalsIgnoreCase("LOV"))
      {
         sClassRenderer = "oracle.jdeveloper.html.LOVField";
      }
      else if(sType.equalsIgnoreCase("LISTBOX"))
      {
         sClassRenderer = "oracle.jdeveloper.html.PickList";
      }
      else if(sType.equalsIgnoreCase("COMBOBOX"))
      {
         sClassRenderer = "oracle.jdeveloper.html.PickList";
      }
      else if(sType.equalsIgnoreCase("RADIOGROUP"))
      {
         sClassRenderer = "oracle.jdeveloper.html.PickList";
      }
      else if(sType.equalsIgnoreCase("CHECKBOXGROUP"))
      {
         sClassRenderer = "oracle.jdeveloper.html.PickList";
      }
      else if(sType.equalsIgnoreCase("DEFAULT"))
      {
         // need to provide a class name
      }
      else
      {
         sType = "TEXT";
      }

      this.sControlType = sType;
   }

   protected void validateFieldType()
   {
      if(sControlType.equalsIgnoreCase("DATE"))
      {
         if(sFormName == null)
            throw new RuntimeException("Date input fields require a value for the formname attribute");
      }
      else if(sControlType.equalsIgnoreCase("LOV"))
      {
         if(sFormName == null)
            throw new RuntimeException("LOV input fields require a value for the formname attribute");

         if(sSrcDataItem == null)
            throw new RuntimeException("LOV input fields require a value for the sourcedataitem attribute");
      }
      else if(sControlType.equalsIgnoreCase("COMBOBOX") ||
               sControlType.equalsIgnoreCase("LISTBOX") ||
               sControlType.equalsIgnoreCase("RADIOGROUP") ||
               sControlType.equalsIgnoreCase("CHECKBOXGROUP"))
      {
         if(sSrcDataItem == null)
            throw new RuntimeException("List input fields require a value for the sourcedataitem attribute");
         if(sDisplayAttribute == null)
            throw new RuntimeException("List input fields require a value for the displayitem attribute");
      }
   }
   
   /**
     * 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
         {
            validateFieldType();

            RowSet            rs = (RowSet)pageContext.getAttribute(sDataSource + "_RowSet");
            DataWebBeanImpl   dwb = (DataWebBeanImpl)pageContext.getAttribute(sDataSource);

            if(rs == null)
            {
               throw new Exception("Data Source was not found in page context.");
            }

            Row row = rs.getCurrentRow();

            if( row == null)
               row = rs.first();

            if( row == null)
            {
               pageContext.getOut().print("&nbsp");
            }
            else
            {
               // if it's the rowkey, add a hidden field and get out
               if(sControlType.equalsIgnoreCase("ROWKEY"))
               {
                    // post the row key
                  Key      key = row.getKey();
                  String   sKey = key.toStringFormat(false);
                  String   sField = "<input type=\"hidden\" value=\"" + sKey + "\" name=\"" + sDataSource +"\">";

                  pageContext.getOut().print(sField);
                  
                  return Tag.SKIP_BODY;
               }

               // get the renderer for the attribute type
               Object obj = row.getAttribute(sName);
               HTMLRenderingContext ctx = null;

               if(pageContext.getAttribute("RenderingContext") == null)
               {
                  ctx = new HTMLRenderingContext(pageContext);
                  pageContext.setAttribute("RenderingContext", ctx);
               }
               else
               {
                  ctx = (HTMLRenderingContext)pageContext.getAttribute("RenderingContext");
               }

               // get the renderer
               HTMLFieldRenderer rndObj = null;

               if(sClassRenderer == null)
               {
                  rndObj = dwb.getEditFieldRenderer(row, rs.getViewObject().findAttributeDef(sName));
               }
               else
               {
                  try
                  {
                     Class cls = Class.forName(sClassRenderer);
                     rndObj = (HTMLFieldRenderer)cls.newInstance();
                  }
                  catch(Exception ex)
                  {
                     ex.printStackTrace();
                     rndObj = dwb.getEditFieldRenderer(row, rs.getViewObject().findAttributeDef(sName));
                  }
               }

               if(sControlType != null)
                  ctx.userData.put("control_type", sControlType);

               if(this.sFormName != null)
                  ctx.userData.put("form_name", this.sFormName);

               if(sSrcDataItem != null)
               {
                  // locate the data source
                  String source = sSrcDataItem.substring(0 , sSrcDataItem.indexOf('.'));
                  String item = sSrcDataItem.substring(sSrcDataItem.indexOf('.')+ 1);

                  oracle.jdeveloper.html.DataWebBeanImpl ds = (oracle.jdeveloper.html.DataWebBeanImpl)pageContext.getAttribute(source);

                  if(ds == null)
                  {
                    throw new Exception("Data Source was not found in page context.");
                  }

                  String dsPath = ds.getApplicationName() + "." + ds.getViewObjectName();

                  if(dsPath != null)
                     ctx.userData.put("lov_vo",  dsPath);

                  if(item != null)
                     ctx.userData.put("data_attribute", item);

                  if(sDisplayAttribute != null)
                     ctx.userData.put("display_attribute", sDisplayAttribute);
               }

               String sOutput = rndObj.renderToString(ctx, rs, row , sName);

               pageContext.getOut().print(sOutput);



            }

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

   /**
     * release() called after doEndTag() to reset state
     */
   public void release()
   {
      sDataSource = null;
      sName = null;
      sControlType = null;
      sFormName = null;
      sClassRenderer = "oracle.jdeveloper.html.TextField";
      sFormName = null;
      sSrcDataItem = null;
      sDisplayAttribute = null;

      super.release();
   }

}

 