// 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:   ProductTemplateAction.java

package com.keyshop.shop.product.controller;

import com.keyshop.pub.controller.PubAction;
import com.keyshop.pub.controller.PubForm;
import com.keyshop.pub.model.PubBean;
import com.keyshop.pub.util.StringUtil;
import com.keyshop.shop.product.bo.ProductTemplateBO;
import com.keyshop.shop.product.model.ProductTemplate;
import com.keyshop.shop.product.model.ProductTemplateFields;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

// Referenced classes of package com.keyshop.shop.product.controller:
//            ProductTemplateForm

public class ProductTemplateAction extends PubAction
{

    public ProductTemplateAction()
    {
    }

    public String getSelectSQL()
    {
        String sql = " from ProductTemplate as ProductTemplate where 1=1 ";
        return sql;
    }

    public void initClassName()
    {
        boClass = "com.keyshop.shop.product.bo.ProductTemplateBO";
        beanClass = "com.keyshop.shop.product.model.ProductTemplate";
        formBeanClass = "com.keyshop.shop.product.controller.ProductTemplateForm";
    }

    public ActionForward performAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            ((ProductTemplateForm)form).clear();
            PubForm _tmp = (PubForm)form;
            ((PubForm)form).setPageType("add");
        }
        catch(Exception e)
        {
            generalError(request, e);
            e.printStackTrace();
            return mapping.findForward("fail");
        }
        return mapping.findForward("add");
    }

    public ActionForward performSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            ProductTemplateForm pForm = (ProductTemplateForm)form;
            Object existedObject = null;
            if(!StringUtil.isEmpty(pForm.getBean().getId()))
                existedObject = invokeBOGetMethod(pForm.getBean().getId());
            if(existedObject != null || isObjectExisted(pForm.getBean()))
            {
                request.setAttribute("existed", "true");
                return mapping.findForward("add");
            }
            ProductTemplate ProductTemplate = (ProductTemplate)pForm.getBean();
            ProductTemplateBO ProductTemplateBO = new ProductTemplateBO();
            ProductTemplateBO.addBean(ProductTemplate);
            PubForm _tmp = (PubForm)form;
            pForm.setPageType("add");
            request.setAttribute("save_success", "true");
        }
        catch(Exception e)
        {
            generalError(request, e);
            e.printStackTrace();
            return mapping.findForward("fail");
        }
        return mapping.findForward("add");
    }

    public ActionForward performAddchild(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            ProductTemplateForm pForm = (ProductTemplateForm)form;
            ProductTemplate bean = (ProductTemplate)pForm.getBean();
            ProductTemplateFields child = new ProductTemplateFields();
            String name = request.getParameter("name");
            if(!StringUtil.isEmpty(name))
                name = name.trim();
            String type = request.getParameter("type");
            boolean isDisplay = true;
            String strIsDisplay = "0";
            if(!StringUtil.isEmpty(request.getParameter("isDisplay")))
                strIsDisplay = request.getParameter("isDisplay");
            child.setName(name);
            child.setDisplay(strIsDisplay);
            child.setType(type);
            child.setTemplate(bean);
            child.setTempId(String.valueOf(++bean.initTempId));
            if(!childExisted(child))
                bean.getChildList().add(child);
            else
                request.setAttribute("fieldname_existed", "true");
        }
        catch(Exception e)
        {
            generalError(httpRequest, e);
            e.printStackTrace();
            return map.findForward("fail");
        }
        request.setAttribute("addChild", "true");
        return map.findForward("add");
    }

    public ActionForward performRemovechild(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            String tempId = request.getParameter("tempchildid");
            String childid = request.getParameter("childid");
            ProductTemplateForm pForm = (ProductTemplateForm)form;
            ProductTemplate pBean = (ProductTemplate)pForm.getBean();
            Set childList = pBean.getChildList();
            Object tempList[] = childList.toArray();
            for(int i = 0; i < tempList.length; i++)
            {
                ProductTemplateFields childBean = (ProductTemplateFields)tempList[i];
                if(childBean.getTempId().equals(tempId))
                {
                    if(!"".equals(childBean.getTempId()))
                        childList.remove(childBean);
                    if(childBean.getId() != null && !"".equals(childBean.getId()) && childBean.getId().equals(childid))
                    {
                        childList.remove(childBean);
                        pBean.getDeleteChilds().add(childBean);
                    }
                }
            }

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

    public ActionForward performEditchild(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        String childid = request.getParameter("childid");
        request.setAttribute("eidtChild", childid);
        return map.findForward("add");
    }

    public ActionForward performUpdatechild(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            String tempId = request.getParameter("tempchildid");
            String childid = request.getParameter("childid");
            String isDisplay = request.getParameter("isDisplay");
            String name = request.getParameter("name");
            if(!StringUtil.isEmpty(name))
                name = name.trim();
            ProductTemplateForm pForm = (ProductTemplateForm)form;
            ProductTemplate pBean = (ProductTemplate)pForm.getBean();
            Set childList = pBean.getChildList();
            Object tempList[] = childList.toArray();
            ProductTemplateFields updateChild = new ProductTemplateFields();
            updateChild.setName(name);
            updateChild.setId(childid);
            updateChild.setTempId(tempId);
            if(childExisted(updateChild))
            {
                request.setAttribute("fieldname_existed", "true");
                request.setAttribute("eidtChild", childid);
                return map.findForward("add");
            }
            for(int i = 0; i < tempList.length; i++)
            {
                ProductTemplateFields childBean = (ProductTemplateFields)tempList[i];
                if(childBean.getTempId() != null && childBean.getTempId().length() > 0 && childBean.getTempId().equals(tempId))
                {
                    childBean.setDisplay(isDisplay);
                    childBean.setName(name);
                }
                if(childBean.getId() != null && !"".equals(childBean.getId()) && childBean.getId().equals(childid))
                {
                    childBean.setDisplay(isDisplay);
                    childBean.setName(name);
                }
            }

        }
        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;
    }

    protected boolean isObjectExisted(PubBean bean)
    {
        if(bean != null)
        {
            ProductTemplate ProductTemplate = (ProductTemplate)bean;
            if(!StringUtil.isEmpty(ProductTemplate.getName()))
                try
                {
                    ProductTemplateBO ProductTemplateBO = new ProductTemplateBO();
                    String ProductTemplateSql = " from ProductTemplate as ProductTemplate where ProductTemplate.name ='" + ProductTemplate.getName() + "'";
                    List ProductTemplateList = ProductTemplateBO.list(ProductTemplateSql);
                    if(ProductTemplateList != null && ProductTemplateList.size() > 0)
                    {
                        ProductTemplate ProductTemplateobject = (ProductTemplate)ProductTemplateList.get(0);
                        if(!ProductTemplateobject.getId().equals(ProductTemplate.getId()))
                            return true;
                    }
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
        }
        return false;
    }

    private boolean childExisted(ProductTemplateFields child)
    {
        ProductTemplateForm pform = (ProductTemplateForm)thisform;
        ProductTemplate bean = (ProductTemplate)pform.getBean();
        Collection childs = bean.getChildList();
        if(childs != null && childs.size() > 0)
        {
            for(Iterator it = childs.iterator(); it.hasNext();)
            {
                ProductTemplateFields tchild = (ProductTemplateFields)it.next();
                if(tchild.getName().equals(child.getName()))
                {
                    if(tchild.getTempId() != null && tchild.getTempId().length() > 0 && tchild.getTempId().equals(child.getTempId()))
                        return false;
                    return tchild.getId() == null || tchild.getId().length() <= 0 || !tchild.getId().equals(child.getId());
                }
            }

        }
        return false;
    }
}
