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

package com.keyshop.shop.system.controller;

import com.keyshop.pub.controller.PubAction;
import com.keyshop.pub.controller.PubForm;
import com.keyshop.pub.model.PubBean;
import com.keyshop.pub.util.*;
import com.keyshop.shop.product.bo.ProductTemplateBO;
import com.keyshop.shop.product.controller.ProductCategoryForm;
import com.keyshop.shop.system.bo.ProviderBO;
import com.keyshop.shop.system.model.Provider;
import com.keyshop.shop.system.util.SystemConst;
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;

// Referenced classes of package com.keyshop.shop.system.controller:
//            ProviderForm

public class ProviderAction extends PubAction
{

    public static final String PROVIDER = "Provider";

    public ProviderAction()
    {
    }

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

    public void initClassName()
    {
        boClass = "com.keyshop.shop.system.bo.ProviderBO";
        beanClass = "com.keyshop.shop.system.model.Provider";
        formBeanClass = "com.keyshop.shop.system.controller.ProviderForm";
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        return super.execute(mapping, form, request, response);
    }

    public ActionForward performList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            PubForm pForm = (PubForm)form;
            Pager page = new Pager(pForm.getCurrentPageNo());
            request.setAttribute("pager", page);
            List list = new ArrayList();
            if(!"".equals(getSelectSQL()))
            {
                ProviderBO providerbo = new ProviderBO();
                list = providerbo.list(page, getSelectSQL());
            }
            request.setAttribute("searchresult", list);
            return mapping.findForward("list");
        }
        catch(Exception e)
        {
            generalError(request, e);
        }
        return mapping.findForward("fail");
    }

    public ActionForward performLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        String providerId = request.getParameter("bean.id");
        String password = request.getParameter("bean.password");
        if(StringUtil.isEmpty(providerId))
        {
            request.setAttribute("login_error", "true");
            return mapping.findForward("loginoff");
        }
        if(StringUtil.isEmpty(password))
        {
            request.setAttribute("login_error", "true");
            return mapping.findForward("loginoff");
        }
        String sql = "from Provider as provider where provider.id='" + providerId + "'";
        sql = sql + " and provider.password='" + password + "'";
        try
        {
            ProviderBO bo = new ProviderBO();
            List list = bo.list(sql);
            if(list != null && list.size() > 0)
            {
                Provider provider = (Provider)list.get(0);
                if(provider.isStatus())
                {
                    Provider sessionprovider = (Provider)list.iterator().next();
                    String s = sessionprovider.getProductroles();
                    Provider xmlProvider = XMLHelper.unmarshalProvider(request.getRealPath(SystemConst.PROVIDERCONFIG_FILENAME), s);
                    sessionprovider.setProductCategorys(xmlProvider.getProductCategorys());
                    sessionprovider.setProductCategorys(xmlProvider.getProductTemplates());
                    if(request.getSession() != null)
                        request.getSession().setAttribute(SystemConst.PROVIDER, xmlProvider);
                    return mapping.findForward("providerloginsuccess");
                }
                request.setAttribute("provider_overdue", "true");
            } else
            {
                request.setAttribute("login_error", "true");
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return mapping.findForward("providerloginoff");
    }

    public ActionForward performLogout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        session.removeAttribute("Provider");
        try
        {
            response.sendRedirect(request.getContextPath() + "/provider/login.jsp");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }

    private void goUrl(String requestPageUrl, String param)
        throws Exception
    {
        if(requestPageUrl != null && requestPageUrl.length() > 0)
        {
            if(requestPageUrl.indexOf("?") != -1)
                requestPageUrl = requestPageUrl + "&";
            else
                requestPageUrl = requestPageUrl + "?";
            requestPageUrl = requestPageUrl + param;
            httpResponse.sendRedirect(requestPageUrl);
        } else
        {
            requestPageUrl = requestPageUrl + "?" + param;
            httpResponse.sendRedirect((new StringBuffer(String.valueOf(httpRequest.getContextPath()))).toString());
        }
    }

    public ActionForward performEditself(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        Provider provider = (Provider)request.getSession().getAttribute(SystemConst.PROVIDER);
        ProviderForm mform = (ProviderForm)form;
        mform.setBean(provider);
        return mapping.findForward("updateself");
    }

    public ActionForward performAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        List templates = null;
        ProductCategoryForm cform = (ProductCategoryForm)request.getSession().getAttribute("providerProductTreeForm");
        if(cform != null)
        {
            cform.setCheckCategorys(new ArrayList());
            cform.setOpenCategorys(new ArrayList());
        }
        putTemplates(request);
        return super.performAdd(mapping, form, request, response);
    }

    public ActionForward performEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        super.performEdit(mapping, form, request, response);
        ProviderForm providerForm = (ProviderForm)form;
        Provider provider = (Provider)providerForm.getBean();
        putTemplates(request);
        if(!StringUtil.isEmpty(provider.getProductroles()))
            try
            {
                Provider newProvider = XMLHelper.unmarshalProvider(request.getRealPath(SystemConst.PROVIDERCONFIG_FILENAME), provider.getProductroles());
                if(newProvider != null && newProvider.getProductCategorys() != null && newProvider.getProductCategorys().size() > 0)
                    provider.setProductCategorys(newProvider.getProductCategorys());
                if(newProvider != null && newProvider.getProductTemplates() != null && newProvider.getProductTemplates().size() > 0)
                    provider.setProductTemplates(newProvider.getProductTemplates());
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        if(provider.getProductTemplates() != null)
        {
            String selected[] = new String[provider.getProductTemplates().size()];
            for(int i = 0; i < provider.getProductTemplates().size(); i++)
                selected[i] = (String)provider.getProductTemplates().get(i);

            providerForm.setSelected(selected);
        }
        ProductCategoryForm cform = (ProductCategoryForm)request.getSession().getAttribute("providerProductTreeForm");
        if(cform != null)
        {
            cform.setOpenCategorys(new ArrayList());
            cform.setCheckCategorys(provider.getProductCategorys());
        }
        return mapping.findForward("add");
    }

    public ActionForward performSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ProviderForm providerForm = (ProviderForm)form;
        Provider provider = (Provider)providerForm.getBean();
        putTemplates(request);
        String selected[] = providerForm.getSelected();
        ProductCategoryForm cform = (ProductCategoryForm)request.getSession().getAttribute("providerProductTreeForm");
        provider.setProductCategorys(cform.getCheckCategorys());
        if(selected != null && selected.length > 0)
        {
            for(int i = 0; i < selected.length; i++)
                provider.addProductTemplate(selected[i]);

        }
        String roleStr = XMLHelper.marshalProvider(request.getRealPath(SystemConst.PROVIDERCONFIG_FILENAME), provider);
        provider.setProductroles(roleStr);
        return super.performSave(mapping, form, request, response);
    }

    public ActionForward performUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ProviderForm providerForm = (ProviderForm)form;
        Provider provider = (Provider)providerForm.getBean();
        putTemplates(request);
        putTemplates(request);
        String selected[] = providerForm.getSelected();
        ProductCategoryForm cform = (ProductCategoryForm)request.getSession().getAttribute("providerProductTreeForm");
        provider.setProductCategorys(cform.getCheckCategorys());
        if(selected != null && selected.length > 0)
        {
            for(int i = 0; i < selected.length; i++)
                provider.addProductTemplate(selected[i]);

        }
        String roleStr = XMLHelper.marshalProvider(request.getRealPath(SystemConst.PROVIDERCONFIG_FILENAME), provider);
        provider.setProductroles(roleStr);
        return super.performUpdate(mapping, form, request, response);
    }

    public ActionForward performRemove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            String selected = request.getParameter("selected");
            if(!StringUtil.isEmpty(selected))
            {
                ProviderBO userbo = new ProviderBO();
                Provider user = (Provider)userbo.get(selected);
                return super.performRemove(mapping, form, request, response);
            } else
            {
                return super.performList(mapping, form, request, response);
            }
        }
        catch(Exception e)
        {
            generalError(request, e);
            e.printStackTrace();
            return mapping.findForward("fail");
        }
    }

    protected boolean isObjectExisted(PubBean bean)
    {
        try
        {
            String tempSql = " from Provider as provider where 1=1 ";
            ProviderBO providerBO = new ProviderBO();
            Provider provider = (Provider)bean;
            if(!StringUtil.isEmpty(provider.getId()))
            {
                String providerSql = tempSql + " and provider.id='" + provider.getId().trim() + "'";
                List providerList = providerBO.list(providerSql);
                if(providerList != null && providerList.size() > 0)
                {
                    Provider m = (Provider)providerList.iterator().next();
                    if(!m.getId().equals(provider.getId()))
                        return true;
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return false;
    }

    private void putTemplates(HttpServletRequest request)
    {
        List templates = null;
        try
        {
            ProductTemplateBO bo = new ProductTemplateBO();
            templates = bo.list("from ProductTemplate");
            request.setAttribute("TEMPLATES", templates);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}
