// 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:   ProductCategoryAction.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.*;
import com.keyshop.shop.product.bo.ProductCategoryBO;
import com.keyshop.shop.product.model.ProductCategory;
import com.keyshop.shop.product.util.ProductHelper;
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.product.controller:
//            ProductCategoryForm

public class ProductCategoryAction extends PubAction
{

    public ProductCategoryAction()
    {
    }

    public String getSelectSQL()
    {
        String sql = "from ProductCategory as productCategory where productCategory.parentId='' ";
        return sql;
    }

    public void initClassName()
    {
        boClass = "com.keyshop.shop.product.bo.ProductCategoryBO";
        beanClass = "com.keyshop.shop.product.model.ProductCategory";
        formBeanClass = "com.keyshop.shop.product.controller.ProductCategoryForm";
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            actionType = request.getParameter("actionType");
        }
        catch(Exception exception) { }
        return super.execute(mapping, form, request, response);
    }

    public ActionForward performSel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ProductCategoryForm productCategoryForm = (ProductCategoryForm)form;
        String selCategory = request.getParameter("sel");
        if(!StringUtil.isEmpty(selCategory))
            productCategoryForm.selCategory(selCategory);
        return performList(mapping, form, request, response);
    }

    public ActionForward performCheck(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ProductCategoryForm productCategoryForm = (ProductCategoryForm)form;
        String checkCategory = request.getParameter("check");
        if(!StringUtil.isEmpty(checkCategory))
            productCategoryForm.checkCategory(checkCategory);
        return performList(mapping, form, request, response);
    }

    public ActionForward performEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ProductCategoryForm productCategoryForm = (ProductCategoryForm)form;
        String id = request.getParameter("bean.id");
        productCategoryForm.setCurrentCategory(id);
        return super.performEdit(mapping, form, request, response);
    }

    public ActionForward performInit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ProductCategoryForm productCategoryForm = (ProductCategoryForm)form;
        productCategoryForm.clear();
        return performList(mapping, form, request, response);
    }

    public ActionForward performAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            ProductCategoryForm productCategoryForm = (ProductCategoryForm)form;
            String categoryType = request.getParameter("categoryType");
            if("root".equals(categoryType))
                productCategoryForm.setCurrentCategory("");
            ProductCategory productCategory = (ProductCategory)productCategoryForm.getBean();
            productCategory.clear();
            String foreProductId = request.getParameter("bean.parentId");
            int index = getMaxIndex(productCategoryForm.getCurrentCategory());
            productCategory.setIndex(index + 1);
            productCategoryForm.setPageType("add");
        }
        catch(Exception e)
        {
            generalError(request, e);
            return mapping.findForward("fail");
        }
        return mapping.findForward("add");
    }

    private int getMaxIndex(String parentId)
    {
        String sql = "select max(category.index) from ProductCategory category where category.parentId='" + parentId + "'";
        try
        {
            ProductCategoryBO bo = new ProductCategoryBO();
            List list = bo.list(sql);
            if(list != null && list.size() > 0)
            {
                Integer index = (Integer)list.get(0);
                return index.intValue();
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return 0;
    }

    public ActionForward performRemove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            ProductCategoryForm pForm = (ProductCategoryForm)form;
            String selected = request.getParameter("selected");
            ProductCategory curcategory = ProductHelper.getProductCategory(selected);
            if(curcategory != null && "SYSTEM".equals(curcategory.getType()))
            {
                request.setAttribute("remove_error", "true");
                return performList(mapping, form, request, response);
            }
            pForm.setCurrentCategory("");
            List allSubs = ProductHelper.getAllSubCategorys(selected, new ArrayList());
            ProductCategoryBO bo = new ProductCategoryBO();
            for(int i = 0; i < allSubs.size(); i++)
                try
                {
                    ProductCategory subcategory = (ProductCategory)allSubs.get(i);
                    bo.deleteById(subcategory.getId());
                    bo.delete(" from ProductCategoryRelation c where c.categoryId='" + subcategory.getId() + "'");
                }
                catch(Exception exception1) { }

            try
            {
                bo.deleteById(selected);
                bo.delete(" from ProductCategoryRelation c where c.categoryId='" + selected + "'");
                pForm.setBean(new ProductCategory());
            }
            catch(Exception exception) { }
            request.setAttribute("remove_success", "true");
        }
        catch(Exception e)
        {
            generalError(request, e);
            return mapping.findForward("fail");
        }
        return performList(mapping, form, request, response);
    }

    public ActionForward performSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            ProductCategoryForm pForm = (ProductCategoryForm)form;
            ProductCategory productCategory = (ProductCategory)pForm.getBean();
            if(!pForm.isOpen(productCategory.getId()))
                pForm.getOpenCategorys().add(productCategory.getId());
            Object existedObject = null;
            if(existedObject != null || isObjectExisted(pForm.getBean()))
            {
                request.setAttribute("existed", "true");
                return mapping.findForward("add");
            }
            if(pForm.getNavImageData() != null && pForm.getNavImageData().getFileSize() > 0)
            {
                FormFile centerPicFile = pForm.getNavImageData();
                String fileName = updateAdElement(centerPicFile, "", "navImage");
                productCategory.setNavImage(fileName);
            }
            invokeBOAddBeanMethod(pForm.getBean());
            PubForm _tmp = (PubForm)form;
            pForm.setPageType("add");
            request.setAttribute("save_success", "true");
        }
        catch(Exception e)
        {
            generalError(request, e);
            return mapping.findForward("fail");
        }
        request.setAttribute("countinue_add", "true");
        return mapping.findForward("add");
    }

    public ActionForward performUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            ProductCategoryForm pForm = (ProductCategoryForm)form;
            ProductCategory bean = (ProductCategory)pForm.getBean();
            String removePopPhoto = request.getParameter("removepopphoto");
            if(removePopPhoto != null && removePopPhoto.equals("true"))
            {
                try
                {
                    FileUtil.deleteFile(request.getRealPath(bean.getNavImage()));
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
                bean.setNavImage(null);
            } else
            if(pForm.getNavImageData() != null && pForm.getNavImageData().getFileSize() > 0)
            {
                String fileName = updateAdElement(pForm.getNavImageData(), bean.getNavImage(), "pop");
                bean.setNavImage(fileName);
            }
        }
        catch(Exception e)
        {
            generalError(request, e);
            return mapping.findForward("fail");
        }
        return super.performUpdate(mapping, form, request, response);
    }

    public ActionForward performList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        try
        {
            return mapping.findForward("list");
        }
        catch(Exception e)
        {
            generalError(request, e);
        }
        return mapping.findForward("fail");
    }

    protected boolean isObjectExisted(PubBean bean)
    {
        if(bean != null)
        {
            ProductCategory productCategory = (ProductCategory)bean;
            try
            {
                ProductCategoryBO productCategoryBO = new ProductCategoryBO();
                String ProductCategorySql = " from ProductCategory as productCategory where productCategory.categoryName ='" + productCategory.getCategoryName() + "'";
                ProductCategorySql = ProductCategorySql + " and productCategory.parentId='" + productCategory.getParentId() + "'";
                List ProductCategoryList = productCategoryBO.list(ProductCategorySql);
                if(ProductCategoryList != null && ProductCategoryList.size() > 0)
                {
                    ProductCategory productCategoryobject = (ProductCategory)ProductCategoryList.get(0);
                    if(!productCategoryobject.getId().equals(productCategory.getId()))
                        return true;
                }
            }
            catch(Exception exception) { }
        }
        return false;
    }

    private String updateAdElement(FormFile file, String oldFileName, String type)
    {
        shopResource = ResourceBundle.getBundle("resources.shopapplication", new Locale("", ""));
        String fileRoot = shopResource.getString("product.file.root");
        try
        {
            if(file != null && file.getFileData() != null && file.getFileSize() > 0)
            {
                String currentPath = fileRoot + DateUtil.getNoSpSysDateString() + "/" + type + "/";
                String currentRoot = httpRequest.getRealPath(currentPath);
                String oldFilePath = "";
                if(!StringUtil.isEmpty(oldFileName))
                    oldFilePath = httpRequest.getRealPath(oldFileName);
                File currentFolder = new File(currentRoot);
                if(!currentFolder.exists())
                    currentFolder.mkdirs();
                if(file != null && file.getFileSize() > 0)
                {
                    String fileName = FileUtil.updateImageFileByRelPath(currentRoot + "/", oldFilePath, file);
                    return currentPath + fileName;
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return "";
    }
}
