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

package com.keyshop.shop.channel.controller;

import com.keyshop.pub.controller.PubAction;
import com.keyshop.pub.model.PubBean;
import com.keyshop.pub.util.*;
import com.keyshop.pub.util.image.ImageUtil;
import com.keyshop.shop.channel.bo.*;
import com.keyshop.shop.channel.manager.ChannelManager;
import com.keyshop.shop.channel.model.*;
import com.keyshop.shop.channel.util.ChannelConst;
import java.io.File;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;

// Referenced classes of package com.keyshop.shop.channel.controller:
//            ChannelConfigForm

public class ChannelConfigAction extends PubAction
{

    public ChannelConfigAction()
    {
    }

    public void initClassName()
    {
        boClass = "com.keyshop.shop.channel.bo.ChannelBO";
    }


    public ActionForward performEditccconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ChannelConfigForm cform = (ChannelConfigForm)form;
        cform.setPageType("edit");
        String configId = request.getParameter("channelCc.configId");
        String channelId = request.getParameter("channelCc.channelId");
        if(StringUtil.isEmpty(configId))
        {
            generalError(request, new Exception(getClass().getName() + "no_config_id"));
            return mapping.findForward("fail");
        }
        try
        {
            ChannelContentBO bo = new ChannelContentBO();
            List list = bo.list("from ChannelContent as cc where cc.configId='" + configId + "' and cc.channelId='" + channelId + "'");
            if(list == null || list.size() <= 0)
                performAddccconfig(mapping, form, request, response);
            else
                cform.setChannelContent((ChannelContent)list.iterator().next());
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return mapping.findForward("addCcConfig");
    }

    public ActionForward performAddadconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            ChannelConfigForm cform = (ChannelConfigForm)form;
            Channel channel = (Channel)cform.getBean();
            cform.setPageType("add");
            String contentType = request.getParameter("contentType");
            ChannelAd ad = cform.getChannelAd();
            ad.clear();
            String channelId = request.getParameter("channelAd.channelId");
            String pageViewKey = request.getParameter("channelAd.pageSearchKey");
            String pagePosition = request.getParameter("channelAd.position");
            String configId = request.getParameter("channelAd.configId");
            if(StringUtil.isEmpty(pagePosition))
                pagePosition = "0";
            ad.setChannelId(channelId);
            ad.setPageSearchKey(pageViewKey);
            ad.setPosition(Integer.parseInt(pagePosition));
            ad.setConfigId(configId);
            return mapping.findForward("addAdConfig");
        }
        catch(Exception e)
        {
            generalError(request, e);
            e.printStackTrace();
            return mapping.findForward("fail");
        }
    }

    public ActionForward performEditadconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ChannelConfigForm cform = (ChannelConfigForm)form;
        cform.setPageType("edit");
        String configId = request.getParameter("channelAd.configId");
        String channelId = request.getParameter("channelAd.channelId");
        if(StringUtil.isEmpty(configId))
        {
            generalError(request, new Exception(getClass().getName() + "no_config_id"));
            return mapping.findForward("fail");
        }
        try
        {
            ChannelAdBO bo = new ChannelAdBO();
            List list = bo.list("from ChannelAd as ad where ad.configId='" + configId + "' and ad.channelId='" + channelId + "'");
            if(list == null || list.size() <= 0)
                performAddadconfig(mapping, form, request, response);
            else
                cform.setChannelAd((ChannelAd)list.iterator().next());
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return mapping.findForward("addAdConfig");
    }

    public ActionForward performAddpcconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            ChannelConfigForm cform = (ChannelConfigForm)form;
            cform.setPageType("add");
            String contentType = request.getParameter("contentType");
            ChannelProductCategory pc = cform.getChannelPc();
            pc.clear();
            String channelId = request.getParameter("channelPc.channelId");
            String pageViewKey = request.getParameter("channelPc.pageSearchKey");
            String pagePosition = request.getParameter("channelPc.position");
          
            pc.setConfigId(configId);
            if(!StringUtil.isEmpty(rows))
                pc.setRows(Integer.parseInt(rows));
            return mapping.findForward("addPcConfig");
        }
        catch(Exception e)
        {
            generalError(request, e);
            e.printStackTrace();
            return mapping.findForward("fail");
        }
    }

    public ActionForward performEditpcconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            ChannelConfigForm cform = (ChannelConfigForm)form;
            cform.setPageType("edit");
            String configId = request.getParameter("channelPc.configId");
            String channelId = request.getParameter("channelPc.channelId");
            if(StringUtil.isEmpty(configId))
            {
                generalError(request, new Exception(getClass().getName() + "no_config_id"));
                return mapping.findForward("fail");
            }
     
        }
        catch(Exception e)
        {
            generalError(request, e);
            e.printStackTrace();
            return mapping.findForward("fail");
        }
        return mapping.findForward("addPcConfig");
    }

    public ActionForward performSavepcconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ChannelConfigForm cform = (ChannelConfigForm)form;
        updateImage();
        ChannelProductCategory pc = cform.getChannelPc();
        try
        {
            ChannelProductCategoryBO bo = new ChannelProductCategoryBO();
            bo.addBean(pc);
            ChannelManager.channelPcConfigMap.put(pc.getChannelId() + pc.getPageSearchKey() + pc.getPosition(), pc.copy());
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        String channelId = pc.getChannelId();
        try
        {
            Channel channel = ChannelManager.getChannel(channelId);
            updateEnableStatus(channel);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        request.setAttribute("save_success", "true");
        return mapping.findForward("addPcConfig");
    }

    private boolean updateCCImage()
    {
        ChannelConfigForm channelForm = (ChannelConfigForm)thisform;
        try
        {
            if(channelForm.getPicData() == null || channelForm.getPicData().getFileData().length <= 0)
                return true;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return false;
        }
        shopResource = ResourceBundle.getBundle("resources.shopapplication", new Locale("", ""));
        String fileRoot = shopResource.getString(ChannelConst.ADFILE_PATH_ROOT);
        if(channelForm.getPicData() != null && channelForm.getPicData().getFileSize() > ImageUtil.maxSize)
        {
            super.generalError(httpRequest, new Exception("image_size_too_long"));
            return false;
        }
        if(StringUtil.isEmpty(fileRoot))
        {
            super.generalError(httpRequest, new Exception());
            return false;
        }
        ChannelContent cc = channelForm.getChannelContent();
        String contentType = channelForm.getPicData().getContentType();
        String root = httpRequest.getRealPath(fileRoot + File.separator + "img");
        File rootDirectory = new File(root);
        if(!rootDirectory.exists() || !rootDirectory.isDirectory())
            rootDirectory.mkdirs();
        if(channelForm.getPicData() != null && channelForm.getPicData().getFileSize() > 0)
        {
            String fileName = FileUtil.updateImageFile(root, cc.getNavImage(), channelForm.getPicData());
            cc.setNavImage(fileRoot + File.separator + "img" + File.separator + fileName);
        }
        return true;
    }

    private boolean updateImage()
    {
        ChannelConfigForm channelForm = (ChannelConfigForm)thisform;
        try
        {
            if(channelForm.getPicData() == null || channelForm.getPicData().getFileData().length <= 0)
                return true;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return false;
        }
        shopResource = ResourceBundle.getBundle("resources.shopapplication", new Locale("", ""));
        String fileRoot = shopResource.getString(ChannelConst.ADFILE_PATH_ROOT);
        if(channelForm.getPicData() != null && channelForm.getPicData().getFileSize() > ImageUtil.maxSize)
        {
            super.generalError(httpRequest, new Exception("image_size_too_long"));
            return false;
        }
        if(StringUtil.isEmpty(fileRoot))
        {
            super.generalError(httpRequest, new Exception());
            return false;
        }
        ChannelProductCategory pc = channelForm.getChannelPc();
        String contentType = channelForm.getPicData().getContentType();
        String root = httpRequest.getRealPath(fileRoot + File.separator + "img");
        File rootDirectory = new File(root);
        if(!rootDirectory.exists() || !rootDirectory.isDirectory())
            rootDirectory.mkdirs();
        if(channelForm.getPicData() != null && channelForm.getPicData().getFileSize() > 0)
        {
            String fileName = FileUtil.updateImageFile(root, pc.getNavImage(), channelForm.getPicData());
            pc.setNavImage(fileRoot + File.separator + "img" + File.separator + fileName);
        }
        return true;
    }

    public ActionForward performUpdatepcconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ChannelConfigForm cform = (ChannelConfigForm)form;
        updateImage();
        ChannelProductCategory pc = cform.getChannelPc();
        try
        {
            ChannelProductCategoryBO bo = new ChannelProductCategoryBO();
            bo.updateBean(pc);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        String channelId = pc.getChannelId();
        try
        {
            Channel channel = ChannelManager.getChannel(channelId);
            updateEnableStatus(channel);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        ChannelManager.channelPcConfigMap.put(channelId + pc.getPageSearchKey() + pc.getPosition(), pc.copy());
        request.setAttribute("update_success", "true");
        return mapping.findForward("addPcConfig");
    }

    public ActionForward performSaveadconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ChannelConfigForm cform = (ChannelConfigForm)form;
        ChannelAd ad = cform.getChannelAd();
        try
        {
            ChannelAdBO bo = new ChannelAdBO();
            bo.addBean(ad);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        String channelId = ad.getChannelId();
        try
        {
            Channel channel = ChannelManager.getChannel(channelId);
            updateEnableStatus(channel);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        ChannelManager.channelAdConfigMap.put(ad.getChannelId() + ad.getPageSearchKey() + ad.getPosition(), ad.copy());
        request.setAttribute("save_success", "true");
        return mapping.findForward("addAdConfig");
    }

    public ActionForward performSaveccconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ChannelConfigForm cform = (ChannelConfigForm)form;
        updateCCImage();
        ChannelContent cc = cform.getChannelContent();
        try
        {
            ChannelContentBO bo = new ChannelContentBO();
            bo.addBean(cc);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        ChannelManager.channelCcConfigMap.put(cc.getChannelId() + cc.getPageSearchKey() + cc.getPosition(), cc.copy());
        request.setAttribute("save_success", "true");
        return mapping.findForward("addCcConfig");
    }

    public ActionForward performUpdateccconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ChannelConfigForm cform = (ChannelConfigForm)form;
        updateCCImage();
        ChannelContent cc = cform.getChannelContent();
        try
        {
            ChannelContentBO bo = new ChannelContentBO();
            bo.updateBean(cc);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        ChannelManager.channelCcConfigMap.put(cc.getChannelId() + cc.getPageSearchKey() + cc.getPosition(), cc.copy());
        request.setAttribute("update_success", "true");
        return mapping.findForward("addCcConfig");
    }

    public ActionForward performUpdateadconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ChannelConfigForm cform = (ChannelConfigForm)form;
        ChannelAd ad = cform.getChannelAd();
        try
        {
            ChannelAdBO bo = new ChannelAdBO();
            bo.updateBean(ad);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        String channelId = ad.getChannelId();
        try
        {
            Channel channel = ChannelManager.getChannel(channelId);
            ChannelManager.channelAdConfigMap.put(ad.getChannelId() + ad.getPageSearchKey() + ad.getPosition(), ad.copy());
            updateEnableStatus(channel);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        request.setAttribute("update_success", "true");
        return mapping.findForward("addAdConfig");
    }

    public ActionForward performRemoveadconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ChannelAd ad = null;
        try
        {
            String channelId = request.getParameter("bean.id");
            String configId = request.getParameter("configId");
            ChannelAdBO bo = new ChannelAdBO();
            String deleteSql = "from ChannelAd c where c.channelId='" + channelId + "' and c.configId='" + configId + "'";
            List list = bo.list(deleteSql);
            if(list != null && list.size() > 0)
                ad = (ChannelAd)list.get(0);
            if(ad != null)
            {
                bo.deleteById(ad.getId());
                ChannelManager.channelAdConfigMap.remove(ad.getChannelId() + ad.getPageSearchKey() + ad.getPosition());
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        ChannelManager.channelAdConfigMap.remove(ad.getChannelId() + ad.getPageSearchKey() + ad.getPosition());
        request.setAttribute("remove_success", "true");
        return performList(mapping, form, request, response);
    }

    public ActionForward performRemoveccconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ChannelContent cc = null;
        try
        {
            String channelId = request.getParameter("bean.id");
            String configId = request.getParameter("configId");
            ChannelContentBO bo = new ChannelContentBO();
            String deleteSql = "from ChannelContent c where c.channelId='" + channelId + "' and c.configId='" + configId + "'";
            List list = bo.list(deleteSql);
            if(list != null && list.size() > 0)
                cc = (ChannelContent)list.get(0);
            if(cc != null)
            {
                ChannelManager.channelCcConfigMap.remove(cc.getChannelId() + cc.getPageSearchKey() + cc.getPosition());
                bo.deleteById(cc.getId());
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        request.setAttribute("remove_success", "true");
        return performList(mapping, form, request, response);
    }

    public ActionForward performRemovepcconfig(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            String channelId = request.getParameter("bean.id");
            String configId = request.getParameter("configId");
            ChannelProductCategoryBO bo = new ChannelProductCategoryBO();
            ChannelProductCategory channelProductCategory = null;
            String sql = " from ChannelProductCategory c where c.channelId='" + channelId + "' and c.configId='" + configId + "'";
            List list = bo.list(sql);
            if(list != null && list.size() > 0)
                channelProductCategory = (ChannelProductCategory)list.get(0);
            if(channelProductCategory != null)
                bo.deleteById(channelProductCategory.getId());
            ChannelManager.channelPcConfigMap.remove(channelProductCategory.getChannelId() + channelProductCategory.getPageSearchKey() + channelProductCategory.getPosition());
            request.setAttribute("remove_success", "true");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return performList(mapping, form, request, response);
    }

    public void updateEnableStatus(Channel channel)
    {
        if(channel == null && StringUtil.isEmpty(channel.getId()))
            return;
        try
        {
            if(!channel.isEnableStatus())
            {
                ChannelBO bo = new ChannelBO();
                if(ChannelManager.isChannelEnable(channel.getId(), channel.getJspModelId()) && !channel.isEnableStatus())
                {
                    channel.setEnableStatus(true);
                    bo.updateBean(channel);
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

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

    public ActionForward performList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ChannelConfigForm cform = (ChannelConfigForm)thisform;
        Channel bean = (Channel)cform.getBean();
        if("true".equals(request.getParameter("init")))
            cform.clear();
        try
        {
            String channelId = bean.getId();
            if(StringUtil.isEmpty(channelId))
                channelId = request.getParameter("bean.id");
            if(StringUtil.isEmpty(channelId))
            {
                generalError(request, new Exception(getClass().getName() + "no_channel_id"));
                return mapping.findForward("fail");
            }
            Channel channel = ChannelManager.getChannel(channelId);
            if(channel == null)
            {
                generalError(request, new Exception(getClass().getName() + "no_channel_id"));
                return mapping.findForward("fail");
            }
            bean = channel;
            String modelId = bean.getJspModelId();
            String contentType = cform.getContentType();
            List channelAds = ChannelManager.getChannelAds(channelId);
            cform.setChannelAdList(channelAds);
            List channelPcs = ChannelManager.getChannelProductCategorys(channelId);
            cform.setChannelPrdouctCategoryList(channelPcs);
            List channelCcs = ChannelManager.getChannelCcs(channelId);
            cform.setChannelContentList(channelCcs);
            if(!StringUtil.isEmpty(modelId))
            {
                List configs = ChannelManager.getChannelJspModelConfigs(modelId, contentType, cform.getContentName());
                if(configs != null && configs.size() > 0)
                    setConfigStatus(configs, channelAds, channelPcs, channelCcs);
                Pager page = new Pager(40);
                page.setTotalRow(configs.size());
                request.setAttribute("pager", page);
                request.setAttribute("searchresult", configs);
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            generalError(request, new Exception(getClass().getName() + "no_channel_id"));
            return mapping.findForward("fail");
        }
        return mapping.findForward("list");
    }

 

    protected String getSelectSQL()
    {
        return "";
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            actionType = request.getParameter("actionType");
            if(!StringUtil.isEmpty(actionType) && !actionType.equalsIgnoreCase("add") && !actionType.equalsIgnoreCase("list") && !actionType.equalsIgnoreCase("edit") && !actionType.equalsIgnoreCase("update") && !actionType.equalsIgnoreCase("save"))
                actionType.equalsIgnoreCase("remove");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return super.execute(mapping, form, request, response);
    }

    protected boolean isObjectExisted(PubBean bean)
    {
        return false;
    }
}
