// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   PubOneToManyAction.java

package com.keyshop.pub.controller;

import com.keyshop.pub.model.PubOneToManyBean;
import com.keyshop.pub.model.PubOneToManyChildrenBean;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

// Referenced classes of package com.keyshop.pub.controller:
//            PubAction, PubOneToManyForm

public abstract class PubOneToManyAction extends PubAction
{

    private static final String ADDCHILD = "addchild";
    private static final String REMOVECHILD = "removechild";
    private static final String TEMPCHILDID = "tempchildid";
    private static final String CHILDID = "childid";

    public PubOneToManyAction()
    {
    }

    protected ActionForward performRemovechild(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            String tempId = request.getParameter("tempid");
            String childid = request.getParameter("childid");
            PubOneToManyForm pForm = (PubOneToManyForm)form;
            PubOneToManyBean pBean = (PubOneToManyBean)pForm.getBean();
            Set childList = pBean.getChildList();
            Object tempList[] = childList.toArray();
            for(int i = 0; i < tempList.length; i++)
            {
                PubOneToManyChildrenBean childBean = (PubOneToManyChildrenBean)tempList[i];
                if(childBean.getTempId() == tempId)
                {
                    childList.remove(childBean);
                    if(!"0".equals(childid))
                        pBean.getDeleteChilds().add(childBean);
                }
            }

        }
        catch(Exception e)
        {
            generalError(httpRequest, e);
            e.printStackTrace();
            return map.findForward("fail");
        }
        return map.findForward("add");
    }

    protected ActionForward performAddchild(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            PubOneToManyForm pForm = (PubOneToManyForm)form;
            PubOneToManyBean pBean = (PubOneToManyBean)pForm.getBean();
            PubOneToManyChildrenBean child = new PubOneToManyChildrenBean();
            child.setTempId(String.valueOf(++pBean.initTempId));
        }
        catch(Exception e)
        {
            generalError(httpRequest, e);
            e.printStackTrace();
            return map.findForward("fail");
        }
        return map.findForward("add");
    }

    public boolean isInArray(String id, String idarray[])
    {
        if(idarray == null || idarray.length <= 0 || id == null || id.trim().length() <= 0)
            return false;
        for(int i = 0; i < idarray.length; i++)
            if(id.equals(idarray[i]))
                return true;

        return false;
    }
}
