/*
 * @author <a href="mailto:novotny@aei.mpg.de">Jason Novotny</a>
 * @version $Id$
 */
package org.gridlab.gridsphere.provider.portletui.tags;

import org.gridlab.gridsphere.provider.portletui.beans.ActionParamBean;
import org.gridlab.gridsphere.provider.portletui.beans.ActionSubmitBean;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * An <code>ActionSubmitTag</code> provides a button element that includes a <code>DefaultPortletAction</code> and may
 * also include nested <code>ActionParamTag</code>s
 */
public class ActionSubmitTag extends ActionTag {

    protected String key = "";

    protected ActionSubmitBean actionSubmitBean = null;

    protected String onClick = null;

    /**
     * Returns the action link key used to locate localized text
     *
     * @return the action link key
     */
    public String getKey() {
        return key;
    }

    /**
     * Sets the action link key used to locate localized text
     *
     * @param key the action link key
     */
    public void setKey(String key) {
        this.key = key;
    }

    /**
     * Returns the onClick JavaScript function
     *
     * @return onClick JavaScript function
     */
    public String getOnClick() {
        return onClick;
    }

    /**
     * Sets the onClick JavaScript function
     *
     * @param onClick the onClick JavaScript function
     */
    public void setOnClick(String onClick) {
        this.onClick = onClick;
    }

    public int doStartTag() throws JspException {
        if (!beanId.equals("")) {
            actionSubmitBean = (ActionSubmitBean) getTagBean();
            if (actionSubmitBean == null) {
                actionSubmitBean = new ActionSubmitBean(beanId);
                paramBeans = new ArrayList();
            } else {

                if (actionSubmitBean.getAction() != null) {
                    action = actionSubmitBean.getAction();
                }
                if (actionSubmitBean.getValue() != null) {
                    value = actionSubmitBean.getValue();
                }
                if (actionSubmitBean.getKey() != null) {
                    key = actionSubmitBean.getKey();
                }
                if (actionSubmitBean.getParamBeanList() != null) {
                    paramBeans = actionSubmitBean.getParamBeanList();
                }

            }
        } else {
            actionSubmitBean = new ActionSubmitBean();
            paramBeans = new ArrayList();
        }

        actionSubmitBean.setName(createActionURI());

        if (anchor != null) actionSubmitBean.setAnchor(anchor);

        if (!key.equals("")) {
            actionSubmitBean.setKey(key);
            value = getLocalizedText(key);
        }

        if (onClick != null) {
            actionSubmitBean.setOnClick(onClick);
        }
        
        if (!beanId.equals("")) {
            this.updateBaseComponentBean(actionSubmitBean);
        } else {
            this.setBaseComponentBean(actionSubmitBean);
        }

        if (action != null) actionSubmitBean.setAction(action);
        if (trackMe != null) actionSubmitBean.setTrackme(trackMe);
        
        if (cssStyle != null) {
            actionSubmitBean.setCssStyle(cssStyle);
        }
        if (cssClass != null) {
            actionSubmitBean.setCssClass(cssClass);
        }

        Object parentTag = getParent();
        if (parentTag instanceof ContainerTag) {
            ContainerTag containerTag = (ContainerTag) parentTag;
            containerTag.addTagBean(actionSubmitBean);
        }
        try {
            JspWriter out = pageContext.getOut();
            out.print(actionSubmitBean.toStartString());
        } catch (Exception e) {
            throw new JspException(e.getMessage());
        }
        return EVAL_BODY_INCLUDE;
    }

    public int doEndTag() throws JspException {

        Iterator it = paramBeans.iterator();
        while (it.hasNext()) {
            ActionParamBean pbean = (ActionParamBean) it.next();
            portletAction.addParameter(pbean.getName(), pbean.getValue());
        }

        String actionURI = createActionURI().toString();
        actionSubmitBean.setName(actionURI);

        if (portletAction != null) actionSubmitBean.setAction(portletAction.toString());

        if ((bodyContent != null) && (value == null)) {
            actionSubmitBean.setValue(bodyContent.getString());
        }

        try {
            JspWriter out = pageContext.getOut();
            out.print(actionSubmitBean.toEndString());
        } catch (Exception e) {
            throw new JspException(e.getMessage());
        }

        return EVAL_PAGE;
    }

}

@@ -283,16 +283,79 @@
 mitBean;
+%0Aimport org.gridlab.gridsphere.portlet.impl.SportletProperties;
 %0A%0Aimport
@@ -427,46 +427,8 @@
 er;%0A
-import javax.servlet.jsp.PageContext;%0A
 impo
@@ -712,40 +712,8 @@
  %7B%0A%0A
-    protected String key = %22%22;%0A%0A
     
@@ -769,798 +769,8 @@
 l;%0A%0A
-    protected String onClick = null;%0A%0A    /**%0A     * Returns the action link key used to locate localized text%0A     *%0A     * @return the action link key%0A     */%0A    public String getKey() %7B%0A        return key;%0A    %7D%0A%0A    /**%0A     * Sets the action link key used to locate localized text%0A     *%0A     * @param key the action link key%0A     */%0A    public void setKey(String key) %7B%0A        this.key = key;%0A    %7D%0A%0A    /**%0A     * Returns the onClick JavaScript function%0A     *%0A     * @return onClick JavaScript function%0A     */%0A    public String getOnClick() %7B%0A        return onClick;%0A    %7D%0A%0A    /**%0A     * Sets the onClick JavaScript function%0A     *%0A     * @param onClick the onClick JavaScript function%0A     */%0A    public void setOnClick(String onClick) %7B%0A        this.onClick = onClick;%0A    %7D%0A%0A
     
@@ -1089,17 +1089,16 @@
  else %7B%0A
-%0A
         
@@ -1918,23 +1918,19 @@
 if (
-!
 key
-.equals(%22%22)
+ != null
 ) %7B%0A
@@ -2017,32 +2017,234 @@
 ey);%0A        %7D%0A%0A
+        if (useAjax) %7B%0A            String cid = (String)pageContext.getRequest().getAttribute(SportletProperties.COMPONENT_ID);%0A            onClick = %22startRequest(%22 + cid + %22, this.form);%22;%0A        %7D%0A%0A
         if (onCl
@@ -2637,25 +2637,17 @@
 ackMe);%0A
-        
 %0A
+
         
@@ -4109,32 +4109,32 @@
 ());%0A        %7D%0A%0A
-
         return E
@@ -4150,10 +4150,103 @@
 %0A    %7D%0A%0A
+    public void release() %7B%0A        super.release();%0A        actionSubmitBean = null;%0A    %7D%0A%0A
 %7D%0A

add support for ajax with useAjax attribute