结合EasyJWeb写的一个考试选题

public Page doSave(WebForm form, Module module) {
  Integer fillBox_num = Integer
    .parseInt((String) form.get("FillBox_num"));
  Integer singleChoice_num = Integer.parseInt((String) form
    .get("SingleChoice_num"));
  Integer mulitiChoice_num = Integer.parseInt((String) form
    .get("MulitiChoice_num"));
  Integer estimate_num = Integer.parseInt((String) form
    .get("Estimate_num"));
  Integer other_num = Integer.parseInt((String) form.get("Other_num"));
  int fillBox_totalNum = 0;
  int singleChoice_totalNum = 0;
  int mulitiChoice_totalNum = 0;
  int estimate_totalNum = 0;
  int other_totalNum = 0;
  List fillBox_totalList = new java.util.ArrayList();
  List singleChoice_totalList = new java.util.ArrayList();
  List mulitiChoice_totalList = new java.util.ArrayList();
  List estimate_totalList = new java.util.ArrayList();
  List other_totalList = new java.util.ArrayList();
  Random random = new Random();
  TestPaperCommand cmd = new TestPaperCommand();
  TestPaper testPaper = new TestPaper();
  List items = new java.util.ArrayList();
  form.toPo(cmd);
  errors = cmd.vaild();
  if (errors != null && errors.size() > 0) {
   form.addResult("errors", errors);
   return module.findPage("edit");
  }
  java.util.Iterator it = form.getTextElement().keySet().iterator();
  List typeList = new ArrayList();
  List scoreList = new ArrayList();
  int inputTotalScore = CommUtil.null2Int(form.get("totalScore"));
  int singleScore = 0;
  int totalScore = 0;
  while (it.hasNext()) {
   String name = (String) it.next();
   System.out.println(name);
   if (name.indexOf("_num") > 0) {
    typeList.add(name.substring(0, name.length() - 4));
   }
   if (name.indexOf("_score") > 0) {
    scoreList.add(name);
   }

  }
  for (String s : typeList) {
   int score = 0;
   int num = 0;
   score = CommUtil.null2Int(form.get(s + "_score"));
   num = CommUtil.null2Int(form.get(s + "_num"));
   singleScore = score * num;
   System.out.println(s + "类型的分数是" + singleScore);
   totalScore = totalScore + singleScore;
  }
  if (totalScore != inputTotalScore) {
   form.addResult("msg", "请检查总分和题型分数累加是否相等!");
   return this.doAdd(form, module);
  }

  CourseInfo courseInfo = new CourseInfo();
  courseInfo.setCourse(cmd.getCourse());
  courseInfo.setChapter(cmd.getCourseChapter());
  courseInfo.setWare(cmd.getCourseWare());
  testPaper.setCourseInfo(courseInfo);
  // 自动生成填空题
  QuestionQueryObject fqqo = new QuestionQueryObject();
  fqqo.addQuery("obj.ttype", QuestionType.FillBox, "=");
  fqqo.addQuery("obj.courseInfo.course", courseInfo.getCourse(), "=");
  fqqo.addQuery("obj.courseInfo.chapter", courseInfo.getChapter(), "=");
  fqqo.addQuery("obj.courseInfo.ware", courseInfo.getWare(), "=");
  if (fillBox_num != 0) {
   if (this.questionService.getQuestionBy(fqqo).getResult() != null) {
    System.out.println("开始生成填空题...");
    fillBox_totalList = this.questionService.getQuestionBy(fqqo)
      .getResult();
    fillBox_totalNum = fillBox_totalList.size();

    if (fillBox_num > fillBox_totalNum) {
     form.addResult("msg", "对不起,您选择的课程中没有足够的填空题!");
     return this.doAdd(form, module);
    } else {
     int f = 0;
     while (f < fillBox_num) {
      TestPaperItem item = new TestPaperItem();
      int count = random.nextInt(fillBox_totalList.size());
      item.setQuestion(fillBox_totalList.get(count));
      int fill_score = CommUtil.null2Int(form
        .get("FillBox_score"));
      item.setScore(fill_score);
      item.setPaper(testPaper);
      items.add(item);
      System.out.println(items.size());
      fillBox_totalList.remove(count);
      f++;
     }
    }

   } else {
    form.addResult("msg", "对不起,您选择的课程中没有填空题!");
    return this.doAdd(form, module);
   }
  }

  // 自动生成判断题
  QuestionQueryObject eqqo = new QuestionQueryObject();
  eqqo.addQuery("obj.ttype", QuestionType.Estimate, "=");
  eqqo.addQuery("obj.courseInfo.course", courseInfo.getCourse(), "=");
  eqqo.addQuery("obj.courseInfo.chapter", courseInfo.getChapter(), "=");
  eqqo.addQuery("obj.courseInfo.ware", courseInfo.getWare(), "=");
  if (estimate_num != 0) {
   if (this.questionService.getQuestionBy(eqqo).getResult() != null) {
    estimate_totalList = this.questionService.getQuestionBy(eqqo)
      .getResult();
    estimate_totalNum = estimate_totalList.size();

    if (estimate_num > estimate_totalNum) {
     form.addResult("msg", "对不起,您选择的课程中没有足够的判断题!");
     return this.doAdd(form, module);
    } else {
     int e = 0;
     while (e < estimate_num) {
      TestPaperItem item = new TestPaperItem();
      int count = random.nextInt(estimate_totalList.size());
      item.setQuestion(estimate_totalList.get(count));
      int estimate_score = CommUtil.null2Int(form
        .get("Estimate_score"));
      item.setScore(estimate_score);
      items.add(item);
      estimate_totalList.remove(count);
      e++;
     }
    }
   } else {
    form.addResult("msg", "对不起,您选择的课程中没有判断题!");
    return this.doAdd(form, module);
   }
  }

  // 自动生成单选题
  QuestionQueryObject sqqo = new QuestionQueryObject();
  sqqo.addQuery("obj.ttype", QuestionType.SingleChoice, "=");
  sqqo.addQuery("obj.courseInfo.course", courseInfo.getCourse(), "=");
  sqqo.addQuery("obj.courseInfo.chapter", courseInfo.getChapter(), "=");
  sqqo.addQuery("obj.courseInfo.ware", courseInfo.getWare(), "=");
  if (singleChoice_num != 0) {
   if (this.questionService.getQuestionBy(sqqo).getResult() != null) {
    singleChoice_totalList = this.questionService.getQuestionBy(
      sqqo).getResult();
    singleChoice_totalNum = singleChoice_totalList.size();

    if (singleChoice_num > singleChoice_totalNum) {
     form.addResult("msg", "对不起,您选择的课程中没有足够的单选题!");
     return this.doAdd(form, module);
    } else {
     int s = 0;
     while (s < singleChoice_num) {
      TestPaperItem item = new TestPaperItem();
      int count = random.nextInt(singleChoice_totalList
        .size());
      item.setQuestion(singleChoice_totalList.get(count));
      int singleChoice_score = CommUtil
        .null2Int("SingleChoice_score");
      item.setScore(singleChoice_score);
      items.add(item);
      singleChoice_totalList.remove(count);
      s++;
     }
    }
   } else {
    form.addResult("msg", "对不起,您选择的课程中没有单选题!");
    return this.doAdd(form, module);
   }
  }

  // 自动生成多选题
  QuestionQueryObject mqqo = new QuestionQueryObject();
  mqqo.addQuery("obj.ttype", QuestionType.MulitiChoice, "=");
  mqqo.addQuery("obj.courseInfo.course", courseInfo.getCourse(), "=");
  mqqo.addQuery("obj.courseInfo.chapter", courseInfo.getChapter(), "=");
  mqqo.addQuery("obj.courseInfo.ware", courseInfo.getWare(), "=");
  if (mulitiChoice_num != 0) {
   if (this.questionService.getQuestionBy(mqqo).getResult() != null) {
    mulitiChoice_totalList = this.questionService.getQuestionBy(
      mqqo).getResult();
    mulitiChoice_totalNum = mulitiChoice_totalList.size();

    if (mulitiChoice_num > mulitiChoice_totalNum) {
     form.addResult("msg", "对不起,您选择的课程中没有足够的多选题!");
     return this.doAdd(form, module);
    } else {
     int m = 0;
     while (m < mulitiChoice_num) {
      TestPaperItem item = new TestPaperItem();
      int count = random.nextInt(mulitiChoice_totalList
        .size());
      item.setQuestion(mulitiChoice_totalList.get(count));
      int mulitiChoice_score = CommUtil.null2Int(form
        .get("MulitiChoice_score"));
      item.setScore(mulitiChoice_score);
      items.add(item);
      mulitiChoice_totalList.remove(count);
      m++;
     }
    }
   } else {
    form.addResult("msg", "对不起,您选择的课程中没有多选题!");
    return this.doAdd(form, module);
   }
  }

  // 自动生成其他题
  QuestionQueryObject oqqo = new QuestionQueryObject();
  oqqo.addQuery("obj.ttype", QuestionType.Other, "=");
  oqqo.addQuery("obj.courseInfo.course", courseInfo.getCourse(), "=");
  oqqo.addQuery("obj.courseInfo.chapter", courseInfo.getChapter(), "=");
  oqqo.addQuery("obj.courseInfo.ware", courseInfo.getWare(), "=");
  if (other_num != 0) {
   if (this.questionService.getQuestionBy(oqqo).getResult() != null) {
    other_totalList = this.questionService.getQuestionBy(oqqo)
      .getResult();
    other_totalNum = other_totalList.size();

    if (other_num > other_totalNum) {
     form.addResult("msg", "对不起,您选择的课程中没有足够的其他题!");
     return this.doAdd(form, module);
    } else {
     int m = 0;
     while (m < mulitiChoice_num) {
      TestPaperItem item = new TestPaperItem();
      int count = random.nextInt(other_totalList.size());
      item.setQuestion(other_totalList.get(count));
      int other_score = CommUtil.null2Int(form
        .get("Other_score"));
      item.setScore(other_score);
      items.add(item);
      other_totalList.remove(count);
      m++;
     }
    }
   } else {
    form.addResult("msg", "对不起,您选择的课程中没有其他题!");
    return this.doAdd(form, module);
   }
  }

  com.easyjf.beans.BeanUtils.copyProperties(cmd, testPaper);
  for (TestPaperItem testItem : items) {
   System.out.println(testItem.getQuestion().getTtype()
     + testItem.getQuestion().getContent());
   testPaper.addTestPaperItem(testItem);
  }
  Long ret = this.service.addTestPaper(testPaper);
  return this.doList(form, module);
 } 

你可能感兴趣的:(程序设计,算法驿站,module,integer,string,random,null,webform)