testmanaction.java

来自「培训考试系统代码」· Java 代码 · 共 373 行

JAVA
373
字号
package com.huawei.icd30.agt.testman;

import javax.servlet.http.*;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import com.huawei.icd30.agt.util.*;
import com.huawei.icd30.common.db.*;

/**
 * <p> 从TestManForm类中提取试题信息,
 * 利用接口实现数据库中试题的删除与查询操作。
 *
 * 修改与删除成功定向到列表页面;
 * 操作失败统一定向到出错页面。</p>
 * <p> </p>
 * <p> </p>
 * @author 龙燕茜
 * @version 1.0
 */

public class TestManAction extends Action
{
    public ActionForward perform(ActionMapping mapping,
           ActionForm form, HttpServletRequest req,
           HttpServletResponse res)
    {
        /*
        //判断session是否超时
        ActionForward checkForword = Tools.checkSession(req,res);
        if(checkForword != null)
        {
             return checkForword;
        }*/

        //获取操作类型
        String opeType = ((TestManForm)form).getOpeType();//从form中获取

        if(opeType.equalsIgnoreCase(OperatorFlagCode.TEST_DELETE))
        {//删除操作
             return deleteTest(mapping,form,req,res);
        }
        else if(opeType.equalsIgnoreCase(OperatorFlagCode.TEST_QUERY)
                || opeType.equalsIgnoreCase(OperatorFlagCode.TEST_QUERY_FOREPAPER))
        {//查询操作
             return queryTest(mapping,form,req,res);
        }
        else if(opeType.equalsIgnoreCase(OperatorFlagCode.TEST_QDETAIL_SHOW)
                    || opeType.equalsIgnoreCase(OperatorFlagCode.TEST_QDETAIL_MODIFY))
        {//查询详细信息操作
             return queryTestDetail(mapping,form,req,res);
        }
        else
        {//操作类型错误,定向到错误页面, 错误编码为0601
             req.setAttribute("errorId",ErrorCode.TEST_OPERATORERROR);
             return (mapping.findForward("error"));
        }

    }

    /**
     * 删除试题操作
     */
    private ActionForward deleteTest(ActionMapping mapping,
           ActionForm form, HttpServletRequest req,
           HttpServletResponse res)
    {

       //从TestManFrom中获取试题ID号
       String[] testIds = ((TestManForm)form).getTestIds();

       //如果id数组为空,则定向到出错页面
       if(testIds == null || testIds.length==0)
       {
            //定向到错误页面,错误号为0604
            req.setAttribute("errorId",ErrorCode.TESTIDS_ISNULL);
            return (mapping.findForward("error"));
       }

       //以"#"号组合所要删除的试题,之间以"#"号分割
       String testIdStr = "";
       testIdStr = testIds[0];
       for(int i=1; i<testIds.length; i++)
       {
          testIdStr +=  "#" + testIds[i];
       }

       //定义连接的实例
       SysDbConn aplcoms = null;
       try
       {
           //得到一个连接的实例
           aplcoms = SysConnPool.getInstance().getAplComs();

           //调用存储过程 P_Agt_TestItemDel完成试题的删除
           aplcoms.preparedSP();
           aplcoms.setString(1 ,testIdStr);
           SysDataSet ds = aplcoms.csCommonSP("P_Agt_TestItemDel");
           SysRecord rc = ds.getParamSet() ;
           if(rc!=null && rc.getInt(0) == OperatorFlagCode.OPERATOR_SUCCESS)
           {//删除试题成功
               //删除试题部分失败
               if(rc.getString(1) != null && rc.getString(1).length()>10)
               {
                  req.setAttribute("errorId",ErrorCode.TEST_PARTDELERROR);

                  int testType = ((TestManForm)form).getTestType();
                  String content = ((TestManForm)form).getContent();
                  String explain = ((TestManForm)form).getExplain();
                  int degree = ((TestManForm)form).getDegree();
                  String classId = ((TestManForm)form).getClassId();
                  int testMeans = ((TestManForm)form).getTestMeans();
                  int useLevel = ((TestManForm)form).getUseLevel();
                  int useFrequency = ((TestManForm)form).getUseFrequency();

                  String backURL = "/QueryTest.do?opeType=" + OperatorFlagCode.TEST_QUERY;
                  backURL += "&testType=" +  testType;
                  backURL += "&content=" +  content;
                  backURL += "&explain=" +  explain;
                  backURL += "&degree=" +  degree;
                  backURL += "&classId=" +  classId;
                  backURL += "&testMeans=" +  testMeans;
                  backURL += "&useLevel=" +  useLevel;
                  backURL += "&useFrequency=" +  useFrequency;

                  req.setAttribute("backURL",backURL);
                  req.setAttribute("errorMes",rc.getString(1));
                  return (mapping.findForward("error"));
               }

               //调用queryTest函数进行重新查询后返回到列表页面
               ((TestManForm)form).setOpeType(OperatorFlagCode.TEST_QUERY);
               return queryTest(mapping,form,req,res);
           }
           else
           {
               //设置传送参数值
               //定向到错误页面
               req.setAttribute("errorId",ErrorCode.TEST_DELERROR);
              return (mapping.findForward("error"));

           }

       }
       catch (SysDbException aple)
       {//捕获CommonService系统异常,定向到出错页面
           //输出异常信息
           aple.printStackTrace();
           req.setAttribute("errorId",ErrorCode.COMMONSERVICE_ERROR);
           return (mapping.findForward("error"));
       }
       catch(java.sql.SQLException sqle)
       {//捕获调用aplcoms异常,定向到出错页面
           // 输出异常信息
           sqle.printStackTrace();
           req.setAttribute("errorId",ErrorCode.DATABASE_ERROR);
           return (mapping.findForward("error"));
       }
       catch(Exception e)
       {//捕获未知异常,定向到出错页面
           //输出异常信息
           e.printStackTrace();
           req.setAttribute("errorId",ErrorCode.UNKNOW_ERROR);
           return (mapping.findForward("error"));
       }
       finally
       {//关闭连接实例
           if(aplcoms != null)
           {
              aplcoms.close();
           }
       }

    }

    /**
     * 查询试题操作
     */
    private ActionForward queryTest(ActionMapping mapping,
           ActionForm form, HttpServletRequest req,
           HttpServletResponse res)
    {
       String opeType = ((TestManForm)form).getOpeType();//从form中获取
       //从TestManFrom中获取查询条件;
       int testType = ((TestManForm)form).getTestType();
       String content = ((TestManForm)form).getContent();
       String explain = ((TestManForm)form).getExplain();
       int degree = ((TestManForm)form).getDegree();
       String classId = ((TestManForm)form).getClassId();
       int testMeans = ((TestManForm)form).getTestMeans();
       int useLevel = ((TestManForm)form).getUseLevel();
       int useFrequency = ((TestManForm)form).getUseFrequency();

       //定义连接的实例
       SysDbConn aplcoms = null;
       try
       {
           //得到一个连接的实例
           aplcoms = SysConnPool.getInstance().getAplComs();

            //根据查询条件,从数据库中获取试题记录
            aplcoms.preparedSP();
            aplcoms.setString(1,content);
            aplcoms.setString(2,explain);
            aplcoms.setString(3,classId);
            aplcoms.setInt(4,testType);
            aplcoms.setInt(5,degree);
            aplcoms.setInt(6,testMeans);
            aplcoms.setInt(7,useLevel);

            SysResultSet rs1 = aplcoms.csCommonSP("P_Agt_TestItemSearch").getResultSet();
            rs1.next();
            aplcoms.preparedQuery(rs1.getString(1));

            SysResultSet rs2 = aplcoms.csCommonQuery("SELFSQL","1","-1").getResultSet();

             //定向到列表显示页面
             if(opeType.equalsIgnoreCase(OperatorFlagCode.TEST_QUERY))
             {
                 //把结果集存放到session中
                 req.getSession().setAttribute("TEST-QUERY-RESULTSET",rs2);
                 return (mapping.findForward("testlist"));
             }
             else
             {
                //把结果集存放到session中
                req.getSession().setAttribute("TEST-QUERYSELECT-RESULTSET",rs2);
                return (mapping.findForward("testselect"));
             }
       }
       catch (SysDbException aple)
       {//捕获CommonService系统异常,定向到出错页面
           //输出异常信息
           aple.printStackTrace();
           req.setAttribute("errorId",ErrorCode.COMMONSERVICE_ERROR);
           return (mapping.findForward("error"));
       }
       catch(java.sql.SQLException sqle)
       {//捕获调用aplcoms异常,定向到出错页面
           // 输出异常信息
           sqle.printStackTrace();
           req.setAttribute("errorId",ErrorCode.DATABASE_ERROR);
           return (mapping.findForward("error"));
       }
       catch(Exception e)
       {//捕获未知异常,定向到出错页面
           //输出异常信息
           e.printStackTrace();
           req.setAttribute("errorId",ErrorCode.UNKNOW_ERROR);
           return (mapping.findForward("error"));
       }
       finally
       {//关闭连接实例
           if(aplcoms != null)
           {
              aplcoms.close();
           }
       }
    }

    /**
     * 查询试题详细信息操作
     */
    private ActionForward queryTestDetail(ActionMapping mapping,
           ActionForm form, HttpServletRequest req,
           HttpServletResponse res)
    {
       //从TestManFrom中获取试题的ID号;
       String testId = ((TestManForm)form).getTestId();
       if(testId == null)
       {//从列表中传过来的参数
          testId = (((TestManForm)form).getTestIds())[0];
       }

       //从TestManFrom中获取操作类型;
       String opeType = ((TestManForm)form).getOpeType();//从form中获取

       //定义连接的实例
       SysDbConn aplcoms = null;
       try
       {
           //得到一个连接的实例
           aplcoms = SysConnPool.getInstance().getAplComs();

           //根据试题ID号,从数据库中获取试题详细信息
           aplcoms.preparedQuery("");
           aplcoms.setString(1,testId);
           SysResultSet rs = aplcoms.csCommonQuery("SQL_Agt_TestItemQueryDetail" ,"1" ,"-1").getResultSet();

           int testType = 0;
           //从结果集获取试题各字段的详细信息,只有一条记录,并设置传送到页面的参数值

           if(rs != null && rs.next())
           {
               rs = new ResUtil(rs);

               req.setAttribute("testId",rs.getString(0));
               req.setAttribute("content",rs.getString(1));
               req.setAttribute("explain",rs.getString(2));
               req.setAttribute("classId",rs.getString(3));
               req.setAttribute("testType",rs.getString(4));
               req.setAttribute("degree",rs.getString(5));
               req.setAttribute("testOption",rs.getString(6));
               req.setAttribute("result",rs.getString(7));
               req.setAttribute("testMeans",rs.getString(8));
               req.setAttribute("useLevel",rs.getString(9));
               req.setAttribute("useFrequency",rs.getString(10));
               req.setAttribute("referenceTime",rs.getString(11));
               req.setAttribute("typeName",rs.getString(12));
               req.setAttribute("degreeName",rs.getString(13));
               req.setAttribute("meansName",rs.getString(14));
               testType = rs.getInt(4);

           }
           else
           {
               req.setAttribute("errorId",ErrorCode.TEST_NOTFOUND_EORROR);
               return (mapping.findForward("error"));
           }

           //定向
           if(opeType.equalsIgnoreCase(OperatorFlagCode.TEST_QDETAIL_SHOW))
           { //定向到列表显示页面
                return (mapping.findForward("testdetail"));
           }
           else
           {//根据不同的试题类型定向到不同的修改页面
               SysResultSet typeRs = TestGetInfo.getTestTypeRs();
               if(typeRs != null)
               {
                  for(int i=0;typeRs.setRecord(i)&&i<typeRs.getMetaData().getRecordCount();i++)
                  {
                     if(testType == typeRs.getInt(0))
                     {
                       return mapping.findForward(typeRs.getString(3));
                     }
                  }
               }
           }
           req.setAttribute("errorId",ErrorCode.UNKNOW_ERROR);
           return (mapping.findForward("error"));
       }
       catch (SysDbException aple)
       {//捕获CommonService系统异常,定向到出错页面
           //输出异常信息
           aple.printStackTrace();
           req.setAttribute("errorId",ErrorCode.COMMONSERVICE_ERROR);
           return (mapping.findForward("error"));
       }
       catch(java.sql.SQLException sqle)
       {//捕获调用aplcoms异常,定向到出错页面
           // 输出异常信息
           sqle.printStackTrace();
           req.setAttribute("errorId",ErrorCode.DATABASE_ERROR);
           return (mapping.findForward("error"));
       }
       catch(Exception e)
       {//捕获未知异常,定向到出错页面
           //输出异常信息
           e.printStackTrace();
           req.setAttribute("errorId",ErrorCode.UNKNOW_ERROR);
           return (mapping.findForward("error"));
       }
       finally
       {//关闭连接实例
           if(aplcoms != null)
           {
              aplcoms.close();
           }
       }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?