

// Copyright (c) 1999, 2000 Oracle Corporation

package oracle.jbo.html.jsp.datatags;



import javax.servlet.jsp.tagext.*;

import javax.servlet.jsp.JspException;

import oracle.jdeveloper.html.*;

import oracle.jbo.*;

import oracle.ord.im.*;



/**

 * A Class class.

 * <P>

 * @author Juan Oropeza

 */

public class SetAttributeTag extends TagSupport {



  protected String sDataItem;

  protected String sValue;

  protected boolean useMultipartForm = false;

  

  /**

   * Constructor

   */

  public SetAttributeTag() {

  }



  public void setDataitem(String sValue)

  {

    this.sDataItem = sValue;

  }



  public void setValue(String sValue)

  {

    this.sValue = sValue;

  }



  public void setUsemultipartformat(String sValue)

  {

    if(sValue.equalsIgnoreCase("yes") || sValue.equalsIgnoreCase("true"))

    {

      this.useMultipartForm = true;

    }

    else

    {

      this.useMultipartForm = false;

    }

  }



  /**

   * doStartTag

   * @return int

   * @exception javax.servlet.jsp.JspException

   */

  public int doStartTag() throws JspException

  {

    try

    {

      Tag tag = this.getParent();



      if(!(tag instanceof RowTag))

      {

        throw new RuntimeException("SetAttribute can only be used within a Row tag");

      }



      RowTag rtag = (RowTag)tag;



      if(!sDataItem.equals("*"))

      {

         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)rtag.getRow().getAttribute(sAttrName);



            if(attrList == null)

            {

              AttributeDef aDef = rtag.getRowSet().getViewObject().findAttributeDef(sAttrName);

              

              attrList = (AttributeList)aDef.getJavaType().newInstance();

            }



            attrList.setAttribute(sSubAttrName, sValue);

            

            rtag.getRow().setAttribute(sAttrName, attrList);



         }

         else

         {

            rtag.getRow().setAttribute(sDataItem, sValue);

         }

      }

      else

      {

         DataWebBeanImpl   dwb = (DataWebBeanImpl)pageContext.getAttribute(rtag.sDataSource);

         RowSet            rs = dwb.getRowSet();

         AttributeDef      aDefs[] = rs.getViewObject().getAttributeDefs();

         String            sValue;

         String            sName;

         

         OrdHttpUploadRequest ordRequest = new OrdHttpUploadRequest(pageContext.getRequest());



         if(this.useMultipartForm)

         {

            // enable parameter transaltion

            ordRequest.enableParameterTranslation(pageContext.getResponse().getCharacterEncoding());



            ordRequest.parseFormData();

         }



         for(int i = 0; i < aDefs.length ; i ++)

         {

            sName = aDefs[i].getName();

            Row row = rtag.getRow();

            

            if(this.useMultipartForm)

            {

               if (! OrdDomainFactory.isOrdDomainType(aDefs[i]))

               {

                  sValue = ordRequest.getParameter(aDefs[i].getName());



                  if( sValue != null)

                  {

                     rtag.getRow().setAttribute(aDefs[i].getIndex(), sValue);

                  }

               }

               else

               {

                  OrdHttpUploadFile mFile = ordRequest.getFileParameter(sName);



                  if ((mFile != null) && (! mFile.getOriginalFileName().equals("")) && ( mFile.getContentLength() > 0 ))

                  {

                    Object obj = OrdDomainFactory.newInstance(aDefs[i]);

                    String className = obj.getClass().getName();



                    if(className.equals(OrdDomainFactory.ordImageDomain))

                    {

                      ((OrdImageDomain) obj).getLoader().setOrdHttpUploadFile(mFile);

                    }

                    else if(className.equals(OrdDomainFactory.ordAudioDomain))

                    {

                      ((OrdAudioDomain) obj).getLoader().setOrdHttpUploadFile(mFile);

                    }

                    else if(className.equals(OrdDomainFactory.ordVideoDomain))

                    {

                      ((OrdVideoDomain) obj).getLoader().setOrdHttpUploadFile(mFile);

                    }

                    else if(className.equals(OrdDomainFactory.ordVirDomain))

                    {

                      ((OrdVirDomain) obj).getLoader().setOrdHttpUploadFile(mFile);

                    }



                    row.setAttribute(aDefs[i].getIndex(), obj );

                  }else

                  {

                    // if "attrName_DELETE" was set as ON, this means user

                    // wants to delete the interMedia object

                    String attrDel = ordRequest.getParameter(sName + "_DELETE");

                    if(attrDel != null && attrDel.equalsIgnoreCase("ON"))

                    {

                      Object obj = OrdDomainFactory.newInstance(aDefs[i]);



                      row.setAttribute(aDefs[i].getIndex(), obj );

                    }

                  }



               }

            }

            else

            {

               sValue = pageContext.getRequest().getParameter(aDefs[i].getName());



               if( sValue != null)

               {

                  rtag.getRow().setAttribute(aDefs[i].getIndex(), sValue);

               }

            }





         }

      }

      

      return Tag.SKIP_BODY;

    }

    catch(Exception ex)

    {

      throw new JspException(ex.getMessage());

    }

  }

}



