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

package com.keyshop.shop.channel.manager;

import com.keyshop.pub.bo.PubHibernate;
import com.keyshop.pub.util.StringUtil;
import com.keyshop.shop.channel.bo.*;
import com.keyshop.shop.channel.model.*;
import com.keyshop.shop.channel.util.ChannelConst;
import com.keyshop.shop.content.bo.ContentCategoryBO;
import com.keyshop.shop.content.model.ContentCategory;
import com.keyshop.shop.product.bo.ProductCategoryBO;
import com.keyshop.shop.product.model.ProductCategory;
import java.util.*;
import javax.servlet.http.HttpServletRequest;

public class ChannelManager
{

    public static Map channelAdConfigMap = new HashMap();
    public static Map channelPcConfigMap = new HashMap();
    public static Map channelCcConfigMap = new HashMap();
    public static List channels = new ArrayList();
    public static Channel defaultChannel;

    public ChannelManager()
    {
    }

    public static List getChannelJspModelConfigs(String modelId, String contentType, String contentName)
        throws Exception
    {
        if(StringUtil.isEmpty(modelId))
            return null;
        String sql = " from ChannelJspModelConfig as m where m.modelId='" + modelId + "'";
        if(!StringUtil.isEmpty(contentType))
            sql = sql + " and m.contentType='" + contentType + "'";
        if(!StringUtil.isEmpty(contentName))
            sql = sql + " and m.contentName like'%" + contentName + "%'";
        sql = sql + " order by m.contentName";
        ChannelJspModelBO bo = new ChannelJspModelBO();
        return bo.list(sql);
    }

    public static List getChannelProductCategorys(String channelId)
        throws Exception
    {
        if(StringUtil.isEmpty(channelId))
        {
            return null;
        } else
        {
            String sql = " from ChannelProductCategory as pc where pc.channelId='" + channelId + "'";
            ChannelProductCategoryBO bo = new ChannelProductCategoryBO();
            return bo.list(sql);
        }
    }

    public static List getChannelAds(String channelId)
        throws Exception
    {
        if(StringUtil.isEmpty(channelId))
        {
            return null;
        } else
        {
            String sql = " from ChannelAd as channelAd where channelAd.channelId='" + channelId + "'";
            ChannelAdBO bo = new ChannelAdBO();
            return bo.list(sql);
        }
    }

    public static List getChannelCcs(String channelId)
        throws Exception
    {
        if(StringUtil.isEmpty(channelId))
        {
            return null;
        } else
        {
            String sql = " from ChannelContent as cc where cc.channelId='" + channelId + "'";
            ChannelContentBO bo = new ChannelContentBO();
            return bo.list(sql);
        }
    }



    public static Channel getChannel(String channelId)
        throws Exception
    {
        ChannelBO bo = new ChannelBO();
        Channel channel = (Channel)bo.get(channelId);
        return channel;
    }

    public static void main(String args[])
    {
        Channel channel;
        try
        {
            channel = getChannel("com");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public static boolean isChannelEnable(String channelId, String modelId)
    {
        if(StringUtil.isEmpty(channelId))
            return false;
        try
        {
            String sql = "select count(*) from ChannelJspModelConfig config  where config.isneed='1'  and config.modelId='" + modelId + "'";
            String sql1 = sql + " and config.contentType='adcontent'";
            String sql2 = sql + " and config.contentType='product_category'";
            String contentsql = "select count(distinct config.id) from ChannelJspModelConfig config, ChannelAd ad where config.isneed='1'  and config.modelId='" + modelId + "'";
            contentsql = contentsql + " and config.id=ad.configId" + " and ad.channelId='" + channelId + "'";
            String pcsql = "select count(distinct config.id) from ChannelJspModelConfig config, ChannelProductCategory pc where config.isneed='1'  and config.modelId='" + modelId + "'";
            pcsql = pcsql + "  and config.id=pc.configId" + " and pc.channelId='" + channelId + "'";
            PubHibernate bo = new PubHibernate();
            List list1 = bo.list(sql1);
            List list2 = bo.list(sql2);
            List contentList = bo.list(contentsql);
            List pcList = bo.list(pcsql);
            boolean adflag = false;
            if(list1 != null && list1.size() > 0)
            {
             
            } else
            {
                adflag = true;
            }
            boolean pcflag = false;
            if(list2 != null && list2.size() > 0)
            {
           
            } else
            {
                pcflag = true;
            }
            return adflag && pcflag;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return false;
    }

    public static AdContent getAdContent(String contentId)
        throws Exception
    {
        if(StringUtil.isEmpty(contentId))
        {
            return null;
        } else
        {
            AdContentBO bo = new AdContentBO();
            return (AdContent)bo.get(contentId);
        }
    }

    public static ContentCategory getContentCategory(String contentCategoryId)
        throws Exception
    {
       
    }

    public static ProductCategory getProductCategory(String categoryId)
        throws Exception
    {
        if(StringUtil.isEmpty(categoryId))
        {
            return null;
        } else
        {
            ProductCategoryBO bo = new ProductCategoryBO();
            return (ProductCategory)bo.get(categoryId);
        }
    }

    public static AdContent getConfigAdContent(String channelId, String searchKey, int position, HttpServletRequest request)
    {
        if(StringUtil.isEmpty(channelId) || StringUtil.isEmpty(searchKey))
            return null;
        try
        {
            ChannelAd ad = getChannelAdConfig(channelId, searchKey, position);
           
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }

    public static ChannelProductCategory getConfigPcContent(String channelId, String searchKey, int position)
    {
        if(StringUtil.isEmpty(channelId) || StringUtil.isEmpty(searchKey))
            return null;
        ChannelProductCategory productCategory = (ChannelProductCategory)channelPcConfigMap.get(channelId + searchKey + position);
        if(productCategory != null)
            return productCategory;
        String sql = " from ChannelProductCategory cpc  where cpc.channelId='" + channelId + "' and cpc.display='1'";
        sql = sql + " and cpc.pageSearchKey='" + searchKey + "'";
        sql = sql + " and cpc.position=" + position;
        try
        {
            ChannelProductCategoryBO bo = new ChannelProductCategoryBO();
            List list = bo.list(sql);
            if(list != null && list.size() > 0)
            {
                ChannelProductCategory pc = (ChannelProductCategory)list.iterator().next();
                channelPcConfigMap.put(channelId + searchKey + position, pc);
                return pc;
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }

    public static ChannelAd getChannelAdConfig(String channelId, String pageSearchKey, int position)
        throws Exception
    {

        ChannelAd channelAd = (ChannelAd)channelAdConfigMap.get(channelId + pageSearchKey + position);
   
        String sql = " from ChannelAd as ad where ad.channelId='" + channelId + "' and ad.display='1'";
        sql = sql + " and ad.pageSearchKey='" + pageSearchKey + "'";
        sql = sql + " and ad.position=" + position;
        ChannelAdBO bo = new ChannelAdBO();
        List list = bo.list(sql);
        if(list != null && list.size() > 0)
        {
            channelAd = (ChannelAd)list.iterator().next();
            channelAdConfigMap.put(channelId + pageSearchKey + position, channelAd);
            return channelAd;
        } else
        {
            return null;
        }
    }

    public static ChannelContent getChannelCcConfig(String channelId, String pageSearchKey, int position)
        throws Exception
    {
        if(StringUtil.isEmpty(channelId) || StringUtil.isEmpty(pageSearchKey))
            return null;
        ChannelContent channelContent = (ChannelContent)channelPcConfigMap.get(channelId + pageSearchKey + position);
        if(channelContent != null)
            return channelContent;
        String sql = " from ChannelContent cc  where cc.channelId='" + channelId + "' and cc.display='1'";
        sql = sql + " and cc.pageSearchKey='" + pageSearchKey + "'";
        sql = sql + " and cc.position=" + position;
        ChannelContentBO bo = new ChannelContentBO();
        List list = bo.list(sql);
        if(list != null && list.size() > 0)
        {
            channelContent = (ChannelContent)list.iterator().next();
            channelCcConfigMap.put(channelId + pageSearchKey + position, channelContent);
            return channelContent;
        } else
        {
            return null;
        }
    }

    public static List getChannelAdConfig(String channelId)
        throws Exception
    {
        String sql = " from ChannelAd as ad where ad.channelId='" + channelId + "' and ad.display='1'";
        ChannelAdBO bo = new ChannelAdBO();
        List list = bo.list(sql);
        return list;
    }

    public static List getChannelPcConfig(String channelId)
        throws Exception
    {
        String sql = " from ChannelProductCategory as pc where pc.channelId='" + channelId + "' and pc.display='1'";
        ChannelProductCategoryBO bo = new ChannelProductCategoryBO();
        List list = bo.list(sql);
        return list;
    }

    public static List getChannels()
        throws Exception
    {
        String sql = "from Channel as channel where channel.status='1' and (channel.parentId is null or channel.parentId='') order by channel.rowIndex, channel.colIndex";
        ChannelBO bo = new ChannelBO();
        List list = bo.list(sql);
        return list;
    }

    public static List getAllChannels()
        throws Exception
    {
        String sql = "from Channel as channel  order by channel.rowIndex, channel.colIndex";
        ChannelBO bo = new ChannelBO();
        List list = bo.list(sql);
        return list;
    }

    public static List getNavChannels()
    {
        try
        {
            channels = getChannels();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return channels;
    }

    public static void changeDefaultChannel(String channelId)
        throws Exception
    {
        String sql = "from Channel as c where c.isdefault='1'";
        ChannelBO bo = new ChannelBO();
        List list = bo.list(sql);
        for(int i = 0; i < list.size(); i++)
        {
            Channel channel = (Channel)list.get(i);
            channel.setIsdefault(false);
            bo.updateBean(channel);
        }

        Channel defaultChannel = getChannel(channelId);
        if(defaultChannel != null)
        {
            defaultChannel.setIsdefault(true);
            defaultChannel = defaultChannel;
            bo.updateBean(defaultChannel);
        }
    }

    public static Channel getDefaultChannel()
    {
        if(defaultChannel != null)
            return defaultChannel;
        channels = getNavChannels();
        Channel firstChannel = null;
        for(int i = 0; i < channels.size(); i++)
        {
            Channel channel = (Channel)channels.get(i);
            if(firstChannel == null && !"SYSTEM".equals(channel.getId()))
                firstChannel = channel;
            if(channel.isIsdefault() && !"SYSTEM".equals(channel.getId()))
            {
                defaultChannel = channel;
                return defaultChannel;
            }
        }

        if(defaultChannel == null && channels.size() > 0)
            defaultChannel = firstChannel;
        return defaultChannel;
    }

}
