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

package com.keyshop.pub.controller;

import com.keyshop.pub.dao.BaseInfoDAO;
import com.keyshop.pub.model.BaseInfo;
import com.keyshop.pub.util.DBUtil;
import com.keyshop.pub.util.StringUtil;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

// Referenced classes of package com.keyshop.pub.controller:
//            PubAction, BaseInfoForm

public class BaseInfoAction extends PubAction
{

    public BaseInfoAction()
    {
    }

    public String getSelectSQL()
    {
        return null;
    }

    public void initClassName()
    {
    }

    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();
        }
        try
        {
            ds = DBUtil.getDataSource();
        }
        catch(Exception exception) { }
        return super.execute(mapping, form, request, response);
    }

    public ActionForward performList(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
    {
        BaseInfoForm baseInfoForm = (BaseInfoForm)thisform;
        BaseInfo baseInfo = (BaseInfo)baseInfoForm.getBean();
        String tableName = baseInfo.getTableName();
        BaseInfoDAO baseInfoDAO = new BaseInfoDAO(ds);
        try
        {
            baseInfoDAO.putColumParamters(tableName, baseInfo);
            baseInfo.setRs(baseInfoDAO.listAll(tableName, baseInfo.getColumNames()));
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        baseInfoForm.setPageType("add");
        return mapping.findForward("list");
    }

    public ActionForward performAdd(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
    {
        BaseInfoForm baseInfoForm = (BaseInfoForm)actionForm;
        baseInfoForm.setPageType("add");
        return performList(mapping, actionForm, request, response);
    }

    public ActionForward performEdit(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
    {
        BaseInfoForm baseInfoForm = (BaseInfoForm)thisform;
        BaseInfo baseInfo = (BaseInfo)baseInfoForm.getBean();
        String tableName = baseInfo.getTableName();
        BaseInfoDAO baseInfoDAO = new BaseInfoDAO(ds);
        try
        {
            baseInfoDAO.putColumParamters(tableName, baseInfo);
            baseInfo.setRs(baseInfoDAO.listAll(tableName, baseInfo.getColumNames()));
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        baseInfoForm.setPageType("edit");
        return mapping.findForward("list");
    }

    public ActionForward performSave(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
    {
        BaseInfoForm baseInfoForm = (BaseInfoForm)actionForm;
        baseInfoForm.setPageType("list");
        BaseInfo baseInfo = (BaseInfo)baseInfoForm.getBean();
        String tableName = baseInfo.getTableName();
        baseInfoForm.setPageType("add");
        BaseInfoDAO baseInfoDAO = new BaseInfoDAO(ds);
        try
        {
            baseInfoDAO.putColumParamters(tableName, baseInfo);
            String columNames[] = baseInfo.getColumNames();
            String values[] = new String[columNames.length];
            for(int i = 0; i < columNames.length; i++)
                values[i] = request.getParameter(columNames[i]);

            values[0] = (new StringBuffer(String.valueOf((new Date()).getTime()))).toString();
            baseInfoDAO.insert(tableName, columNames, values);
            request.setAttribute("save_success", "true");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return performList(mapping, actionForm, request, response);
    }

    public ActionForward performUpdate(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
    {
        BaseInfoForm baseInfoForm = (BaseInfoForm)actionForm;
        baseInfoForm.setPageType("list");
        BaseInfo baseInfo = (BaseInfo)baseInfoForm.getBean();
        String tableName = baseInfo.getTableName();
        baseInfoForm.setPageType("add");
        BaseInfoDAO baseInfoDAO = new BaseInfoDAO(ds);
        try
        {
            baseInfoDAO.putColumParamters(tableName, baseInfo);
            String columNames[] = baseInfo.getColumNames();
            String values[] = new String[columNames.length];
            values[0] = baseInfo.getId();
            for(int i = 1; i < columNames.length; i++)
                values[i] = request.getParameter(columNames[i]);

            baseInfoDAO.update(tableName, columNames, values);
            request.setAttribute("update_success", "true");
        }
        catch(Exception ex)
        {
            request.setAttribute("update_error", "true");
            ex.printStackTrace();
        }
        return performList(mapping, actionForm, request, response);
    }

    public ActionForward performRemove(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
    {
        BaseInfoForm baseInfoForm = (BaseInfoForm)actionForm;
        BaseInfo baseInfo = (BaseInfo)baseInfoForm.getBean();
        String id = baseInfo.getId();
        try
        {
            BaseInfoDAO baseInfoDAO = new BaseInfoDAO(ds);
            if(!StringUtil.isEmpty(id))
            {
                String tableName = baseInfo.getTableName();
                baseInfoForm.setPageType("add");
                baseInfoDAO.delete(tableName, id);
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return performList(mapping, actionForm, request, response);
    }
}
