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

package com.keyshop.shop.product.controller;

import com.keyshop.pub.bo.PubHibernate;
import com.keyshop.pub.controller.PubAction;
import com.keyshop.pub.util.Pager;
import com.keyshop.pub.util.StringUtil;
import com.keyshop.shop.product.bo.ProductBO;
import com.keyshop.shop.product.bo.ProductRelationBO;
import com.keyshop.shop.product.model.Product;
import com.keyshop.shop.product.model.ProductRelation;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

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

public class ProductRelationAction extends PubAction
{

    public ProductRelationAction()
    {
    }

    public String getSelectSQL()
    {
        ProductRelationForm rform = (ProductRelationForm)thisform;
        String productName = rform.getProductName();
        String key = rform.getKey();
        String categoryId = rform.getCategoryId();
        String startPrice = rform.getStartPrice();
        String endPrice = rform.getEndPrice();
        String startCreateTime = rform.getStartCreateTime();
        String endCreateTime = rform.getEndCreateTime();
        String startUpdateTime = rform.getStartUpdateTime();
        String endUpdateTime = rform.getEndCreateTime();
        String sql = "select distinct p.id, p.name,p.unitPrice, p.status, p.key, p.createTime, p.updateTime from Product as p";
        if(!StringUtil.isEmpty(categoryId))
            sql = sql + " , ProductCategoryRelation r ";
        sql = sql + " where 1=1 ";
        if(!StringUtil.isEmpty(productName))
        {
            sql = sql + " and (p.name like'%" + productName.trim() + "%'";
            sql = sql + " or p.note like'%" + productName.trim() + "%')";
        }
        if(!StringUtil.isEmpty(key))
            sql = sql + " and p.key like'%" + key.trim() + "%'";
        if(!StringUtil.isEmpty(categoryId))
            sql = sql + " and p.id=r.productId and r.categoryId='" + categoryId + "'";
        if(!StringUtil.isEmpty(startPrice))
            sql = sql + " and p.unitPrice >= " + startPrice;
        if(!StringUtil.isEmpty(endPrice))
            sql = sql + " and p.unitPrice <= " + endPrice;
        if(!StringUtil.isEmpty(startCreateTime))
            sql = sql + " and p.createTime >='" + startCreateTime + "'";
        if(!StringUtil.isEmpty(endCreateTime))
            sql = sql + " and p.createTime <='" + endCreateTime + "'";
        if(!StringUtil.isEmpty(startUpdateTime))
            sql = sql + " and p.updateTime >='" + startUpdateTime + "'";
        if(!StringUtil.isEmpty(endUpdateTime))
            sql = sql + " and p.updateTime <='" + endUpdateTime + "'";
        if(!StringUtil.isEmpty(rform.getCorpId()))
            sql = sql + " and p.corpId='" + rform.getCorpId() + "'";
        sql = sql + " order by p.updateTime desc";
        return sql;
    }

    public void initClassName()
    {
        boClass = "com.keyshop.shop.product.bo.ProductRelationBO";
        beanClass = "com.keyshop.shop.product.model.ProductRelation";
        formBeanClass = "com.keyshop.shop.product.controller.ProductRelationForm";
    }

    public ActionForward performList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ProductRelationForm pForm = (ProductRelationForm)form;
        Pager page = new Pager(pForm.getCurrentPageNo());
        request.setAttribute("pager", page);
        List list = new ArrayList();
        String sql = getSelectSQL();
        if(!"".equals(sql))
            try
            {
                ProductBO bo = new ProductBO();
                list = bo.list(page, sql);
                list = convertProductList(list);
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        request.setAttribute("searchresult", list);
        return mapping.findForward("list");
    }

    public ActionForward performAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        String productId = request.getParameter("productId");
        if(StringUtil.isEmpty(productId))
        {
            request.setAttribute("no_product_id", "true");
            return mapping.findForward("fail");
        }
        try
        {
            ProductBO bo = new ProductBO();
            Product p = (Product)bo.get(productId);
            if(p == null)
            {
                request.setAttribute("no_product_id", "true");
                return mapping.findForward("fail");
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        List existedList = getRelationProducts(productId);
        getEnableProducts(productId, existedList);
        return mapping.findForward("add");
    }

    private List getRelationProducts(String productId)
    {
        if(StringUtil.isEmpty(productId))
            return null;
        Pager page = new Pager(((ProductRelationForm)thisform).getExistedPageNo());
        httpRequest.setAttribute("existedPager", page);
        String sql = "select distinct p.id, p.name,p.unitPrice, p.status, p.key, p.createTime, p.updateTime";
        sql = sql + " from Product as p, ProductRelation r where 1=1 ";
        sql = sql + "  and p.id=r.reproductId";
        sql = sql + " and r.productId='" + productId + "'";
        try
        {
            PubHibernate bo = new PubHibernate();
            List list = bo.list(page, sql);
            list = convertProductList(list);
            if(list != null && list.size() > 0)
                httpRequest.setAttribute("existedlist", list);
            else
                httpRequest.setAttribute("existedlist", new ArrayList());
            return list;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }

    private List getEnableProducts(String productId, List existedList)
    {
        if(StringUtil.isEmpty(productId))
            return null;
        Pager page = new Pager(((ProductRelationForm)thisform).getEnablePageNo());
        httpRequest.setAttribute("enablePager", page);
        String tempsql = getSelectSQL();
        String sql = "";
        int index = tempsql.indexOf("order by");
        if(index > 0)
            sql = tempsql.substring(0, index);
        else
            sql = tempsql;
        sql = sql + " and p.id not in('" + productId + "'";
        if(existedList != null && existedList.size() > 0)
        {
            sql = sql + ",";
            for(int i = 0; i < existedList.size(); i++)
            {
                Product p = (Product)existedList.get(i);
                if(p != null)
                    sql = sql + "'" + p.getId() + "'";
                if(i != existedList.size() - 1)
                    sql = sql + ",";
            }

        }
        sql = sql + ")";
        if(index > 0)
            sql = sql + " " + tempsql.substring(tempsql.indexOf("order by"));
        try
        {
            PubHibernate bo = new PubHibernate();
            List list = bo.list(page, sql);
            if(list != null && list.size() > 0)
                httpRequest.setAttribute("enablelist", convertProductList(list));
            else
                httpRequest.setAttribute("enablelist", new ArrayList());
            return list;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }

    public ActionForward performAddproduct(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ProductRelationForm pform = (ProductRelationForm)form;
        String productId = pform.getProductId();
        String selected[] = pform.getEnableselected();
        if(StringUtil.isEmpty(productId) || selected == null || selected.length < 0)
            return performAdd(mapping, form, request, response);
        try
        {
            ProductRelationBO bo = new ProductRelationBO();
            for(int i = 0; i < selected.length; i++)
            {
                String reProductId = selected[i];
                ProductRelation pr = new ProductRelation();
                pr.setProductId(productId);
                pr.setReproductId(reProductId);
                try
                {
                    bo.addBean(pr);
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
            }

            pform.setEnableselected(null);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return performAdd(mapping, form, request, response);
    }

    public ActionForward performRemoveproduct(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ProductRelationForm pform = (ProductRelationForm)form;
        String productId = pform.getProductId();
        String selected[] = pform.getExistedselected();
        if(StringUtil.isEmpty(productId) || selected == null || selected.length < 0)
            return performAdd(mapping, form, request, response);
        try
        {
            String sql = " from ProductRelation r where r.productId='" + productId + "'";
            ProductRelationBO bo = new ProductRelationBO();
            for(int i = 0; i < selected.length; i++)
            {
                if(i == 0)
                    sql = sql + " and r.reproductId in (";
                sql = sql + "'" + selected[i] + "'";
                if(i != selected.length - 1)
                    sql = sql + ",";
                if(i == selected.length - 1)
                    sql = sql + ")";
            }

            bo.delete(sql);
            pform.setExistedselected(null);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return performAdd(mapping, form, request, response);
    }

    private List convertProductList(List searchResult)
    {
        if(searchResult == null || searchResult.size() <= 0)
            return new ArrayList();
        List rlist = new ArrayList();
        for(int i = 0; i < searchResult.size(); i++)
        {
            Product p = new Product();
            Object o[] = (Object[])searchResult.get(i);
            String id = (String)o[0];
            String name = (String)o[1];
            Double unitPrice = (Double)o[2];
            Boolean status = (Boolean)o[3];
            String key = (String)o[4];
            String createTime = (String)o[5];
            String updateTime = (String)o[6];
            p.setId(id);
            p.setName(name);
            p.setUnitPrice(unitPrice.doubleValue());
            p.setStatus(status.booleanValue());
            p.setKey(key);
            p.setCreateTime(createTime);
            p.setUpdateTime(updateTime);
            rlist.add(p);
        }

        return rlist;
    }
}
