|
@@ -1,362 +1,393 @@
|
|
|
-package com.qmth.cqb.paper.service.impl;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.regex.Matcher;
|
|
|
-import java.util.regex.Pattern;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.data.domain.Sort;
|
|
|
-import org.springframework.data.domain.Sort.Direction;
|
|
|
-import org.springframework.data.domain.Sort.Order;
|
|
|
-import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
-import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
-import org.springframework.data.mongodb.core.query.Query;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import com.google.gson.Gson;
|
|
|
-import com.qmth.cqb.paper.dao.PaperRepo;
|
|
|
-import com.qmth.cqb.paper.model.Paper;
|
|
|
-import com.qmth.cqb.paper.model.PaperDetail;
|
|
|
-import com.qmth.cqb.paper.model.PaperDetailUnit;
|
|
|
-import com.qmth.cqb.paper.model.computerTestModel.Block;
|
|
|
-import com.qmth.cqb.paper.model.computerTestModel.ComputerTestOption;
|
|
|
-import com.qmth.cqb.paper.model.computerTestModel.ComputerTestPaper;
|
|
|
-import com.qmth.cqb.paper.model.computerTestModel.ComputerTestPaperDetail;
|
|
|
-import com.qmth.cqb.paper.model.computerTestModel.ComputerTestQuestion;
|
|
|
-import com.qmth.cqb.paper.model.computerTestModel.Section;
|
|
|
-import com.qmth.cqb.paper.model.computerTestModel.Sections;
|
|
|
-import com.qmth.cqb.paper.service.ExportPaperService;
|
|
|
-import com.qmth.cqb.paper.service.PaperDetailService;
|
|
|
-import com.qmth.cqb.paper.service.PaperService;
|
|
|
-import com.qmth.cqb.paper.service.export.ExportPaperAbstractService;
|
|
|
-import com.qmth.cqb.question.model.QuesOption;
|
|
|
-import com.qmth.cqb.question.model.Question;
|
|
|
-import com.qmth.cqb.utils.CommonUtils;
|
|
|
-import com.qmth.cqb.utils.FileDisposeUtil;
|
|
|
-import com.qmth.cqb.utils.SpringContextUtils;
|
|
|
-import com.qmth.cqb.utils.enums.PaperType;
|
|
|
-
|
|
|
-@Service("exportPaperService")
|
|
|
-public class ExportPaperServiceImpl implements ExportPaperService{
|
|
|
-
|
|
|
- public static final String TEMP_FILE_EXP = "docxExport/";
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private PaperRepo paperRepo;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private PaperService paperService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private PaperDetailService paperDetailService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private MongoTemplate mongoTemplate;
|
|
|
-
|
|
|
- @Override
|
|
|
- public void exportPaperFile(String paperId,String serviceName,String exportContentList,HttpServletResponse response,String loginName) throws Exception {
|
|
|
- ExportPaperAbstractService exportPaperAbstractService = (ExportPaperAbstractService) SpringContextUtils.getBeanById(serviceName);
|
|
|
- //根据试卷id查询试卷
|
|
|
- Paper paper = paperRepo.findOne(paperId);
|
|
|
- List<Paper> papers = new ArrayList<Paper>();
|
|
|
- papers.add(paper);
|
|
|
- String zipFileName = loginName;
|
|
|
- File directory = new File(TEMP_FILE_EXP + File.separator + zipFileName);
|
|
|
- if(!directory.exists()){
|
|
|
- directory.mkdirs();
|
|
|
- }
|
|
|
- if(exportContentList.indexOf("COMPUTERTEST_PACKAGE")>-1){
|
|
|
- downJson(paper,zipFileName);
|
|
|
- }
|
|
|
- if(exportContentList.indexOf("PAPER")>-1){
|
|
|
- exportPaperAbstractService.downloadPaper(paperId,zipFileName);
|
|
|
- }
|
|
|
- if(exportContentList.indexOf("ANSWER")>-1){
|
|
|
- exportPaperAbstractService.downloadPaperAnswer(paperId,zipFileName);
|
|
|
- }
|
|
|
- FileDisposeUtil.fileToZip(TEMP_FILE_EXP+File.separator+zipFileName,TEMP_FILE_EXP,zipFileName);
|
|
|
- FileDisposeUtil.downloadFile(paper.getName()+"_"+paper.getCourse().getCode()+".zip", TEMP_FILE_EXP+File.separator+zipFileName+".zip",response);
|
|
|
- deteleFolder(TEMP_FILE_EXP,zipFileName);
|
|
|
- }
|
|
|
-
|
|
|
- private void deteleFolder(String sourceFilePath,String zipFileName) {
|
|
|
- File ComputerTestPaperfoler = new File(sourceFilePath + File.separator + "json");
|
|
|
- if(ComputerTestPaperfoler.exists()){
|
|
|
- FileUtils.deleteQuietly(ComputerTestPaperfoler);
|
|
|
- }
|
|
|
- File zipFolder = new File(sourceFilePath + File.separator + zipFileName);
|
|
|
- if(zipFolder.exists()){
|
|
|
- FileUtils.deleteQuietly(zipFolder);
|
|
|
- }
|
|
|
- File zipFile = new File(sourceFilePath + File.separator + zipFileName + ".zip");
|
|
|
- if(zipFile.exists()){
|
|
|
- FileUtils.deleteQuietly(zipFile);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void downJson(Paper paper,String zipFileName) {
|
|
|
- ComputerTestPaper computerTestPaper = buildComputerTestPapers(paper);
|
|
|
- String jsonDirectory = TEMP_FILE_EXP + File.separator + "json";
|
|
|
- //新建文件夹
|
|
|
- File dirFile = new File(jsonDirectory);
|
|
|
- if(!dirFile.exists()){
|
|
|
- dirFile.mkdirs();
|
|
|
- }
|
|
|
- makeComputerTestPaperToJsonFile(paper.getCourse().getCode(),computerTestPaper,jsonDirectory);
|
|
|
- //将文件夹打包成zip压缩包放在docxExport下
|
|
|
- FileDisposeUtil.fileToZip(jsonDirectory,TEMP_FILE_EXP+File.separator+zipFileName,paper.getCourse().getCode()+"_"+paper.getName());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 构建机考数据包实体类
|
|
|
- * @param extractConfig
|
|
|
- * @return
|
|
|
- */
|
|
|
- private ComputerTestPaper buildComputerTestPapers(Paper paper){
|
|
|
- //得到所有旧对象的大题对象
|
|
|
- List<PaperDetail> paperDetails = paperService.findPaperDetailsById(paper.getId());
|
|
|
- //通过 paper 对象 ,生成新的 ComputerTestPaper 对象
|
|
|
- ComputerTestPaper computerTestPaper = new ComputerTestPaper(paper,"");
|
|
|
- List<ComputerTestPaperDetail> details = new ArrayList<ComputerTestPaperDetail>();
|
|
|
- //遍历所有旧大题对象,得到小题对象的集合
|
|
|
- for(PaperDetail paperDetail:paperDetails){
|
|
|
- List<PaperDetailUnit> paperDetailUnits = paperDetailService.getUnitsByPaperDetailId(paperDetail.getId());
|
|
|
- ComputerTestPaperDetail computerTestPaperDetail = new ComputerTestPaperDetail(paperDetail);
|
|
|
- List<ComputerTestQuestion> questions = new ArrayList<ComputerTestQuestion>();
|
|
|
- //遍历所有的小题对象
|
|
|
- for(int i = 0;i<paperDetailUnits.size();i++){
|
|
|
- PaperDetailUnit paperDetailUnit = paperDetailUnits.get(i);
|
|
|
- //根据旧的小题对象,生成新的小题对象
|
|
|
- ComputerTestQuestion computerTestQuestion = new ComputerTestQuestion(paperDetailUnit);
|
|
|
- //设置小题题号
|
|
|
- computerTestQuestion.setNumber(i+1);
|
|
|
- //得到小题题干
|
|
|
- computerTestQuestion.setBody(getBody(paperDetailUnit.getQuestion()));
|
|
|
- //得到小题所有选项
|
|
|
- computerTestQuestion.setOptions(getOption(paperDetailUnit.getQuestion()));
|
|
|
- //查询小题中的 套题
|
|
|
- List<Question> subQuestionsList = paperDetailUnit.getQuestion().getSubQuestions();
|
|
|
- //判断这个小题中是否有套题
|
|
|
- if(subQuestionsList!=null&&subQuestionsList.size()>0){
|
|
|
- List<ComputerTestQuestion> subQuestions = new ArrayList<ComputerTestQuestion>();
|
|
|
- //遍历每个套题
|
|
|
- for(int j = 0;j<subQuestionsList.size();j++){
|
|
|
- Question subQuestion = subQuestionsList.get(j);
|
|
|
- ComputerTestQuestion subcomputerTestQuestion = new ComputerTestQuestion(subQuestion);
|
|
|
- //设置套题中小题题号
|
|
|
- subcomputerTestQuestion.setNumber(j+1);
|
|
|
- subcomputerTestQuestion.setBody(getBody(subQuestion));
|
|
|
- subcomputerTestQuestion.setOptions(getOption(subQuestion));
|
|
|
- subQuestions.add(subcomputerTestQuestion);
|
|
|
- }
|
|
|
- computerTestQuestion.setSubQuestions(subQuestions);
|
|
|
- }
|
|
|
- questions.add(computerTestQuestion);
|
|
|
-
|
|
|
- }
|
|
|
- computerTestPaperDetail.setQuestions(questions);
|
|
|
- //paperDetail中的题数(unitCount)可能不准确,这里以questions的实际size为准
|
|
|
- computerTestPaperDetail.setQuestionCount(questions.size());
|
|
|
- details.add(computerTestPaperDetail);
|
|
|
- }
|
|
|
- computerTestPaper.setDetails(details);
|
|
|
- return computerTestPaper;
|
|
|
- }
|
|
|
-
|
|
|
- private Sections getBody(Question question){
|
|
|
- Sections body = new Sections();
|
|
|
- List<Section> sections = new ArrayList<Section>();
|
|
|
- //得到小题题干
|
|
|
- String questionBodyString = question.getQuesBody();
|
|
|
- //得到小题题干行数
|
|
|
- String[] questionRowStrings = questionBodyString.split("</p>");
|
|
|
- for(int i = 0;i<questionRowStrings.length;i++){
|
|
|
- Section section = new Section();
|
|
|
- //将小题题干拆分为Block集合
|
|
|
- section.setBlocks(disposeQuestionBodyOrOption(questionRowStrings[i]));
|
|
|
- sections.add(section);
|
|
|
- }
|
|
|
- body.setSections(sections);
|
|
|
- return body;
|
|
|
- }
|
|
|
-
|
|
|
- private List<ComputerTestOption> getOption(Question question){
|
|
|
- //得到小题选项
|
|
|
- List<QuesOption> quesOptions = question.getQuesOptions();
|
|
|
- List<ComputerTestOption> options = new ArrayList<ComputerTestOption>();
|
|
|
- //遍历小题选项
|
|
|
- if(quesOptions!=null&&quesOptions.size()>0){
|
|
|
- for(QuesOption quesOption: quesOptions){
|
|
|
- ComputerTestOption option = new ComputerTestOption();
|
|
|
- option.setNumber(new Integer(quesOption.getNumber()));
|
|
|
- option.setCorrect(quesOption.getIsCorrect()==1?true:false);
|
|
|
- Sections body = new Sections();
|
|
|
-
|
|
|
- List<Section> sections = new ArrayList<Section>();
|
|
|
- //得到小题选项
|
|
|
- String optionString = quesOption.getOptionBody();
|
|
|
- String[] optionStrings = optionString.split("</p>");
|
|
|
- for(int i = 0;i<optionStrings.length;i++){
|
|
|
- Section section = new Section();
|
|
|
- section.setBlocks(disposeQuestionBodyOrOption(optionStrings[i]));
|
|
|
- sections.add(section);
|
|
|
- }
|
|
|
- body.setSections(sections);
|
|
|
- option.setBody(body);
|
|
|
- options.add(option);
|
|
|
- }
|
|
|
- }
|
|
|
- return options;
|
|
|
- }
|
|
|
-
|
|
|
- private List<Block> disposeQuestionBodyOrOption(String questionRow) {
|
|
|
- List<Block> blocks = new ArrayList<Block>();
|
|
|
- //去掉每行里面的<p>,<span>,</span>标签
|
|
|
- questionRow = questionRow.replaceAll("<p>", "")
|
|
|
- .replaceAll("</p>", "")
|
|
|
- .replaceAll("<span>", "")
|
|
|
- .replaceAll("</span>", "")
|
|
|
- .replaceAll("</a>", "");
|
|
|
- String[] questionRowStrings = questionRow.split("<|/>|>");
|
|
|
- for(int i = 0;i<questionRowStrings.length;i++){
|
|
|
- Block block = new Block();
|
|
|
- //判断是否有图片
|
|
|
- if(questionRowStrings[i].startsWith("img")){
|
|
|
- questionRowStrings[i] = "<"+questionRowStrings[i]+">";
|
|
|
- Map<String, Object> param = new HashMap<String, Object>();
|
|
|
- //需要继续做截取,取到Parma
|
|
|
- block.setType("image");
|
|
|
- //获取图片的路径
|
|
|
- List<String> strSrcList = getImg(questionRowStrings[i], "src");
|
|
|
- if(strSrcList.size()>0){
|
|
|
- String strSrc = strSrcList.get(0).replaceAll("src=\"", "").replaceAll("\"", "");
|
|
|
- block.setValue(strSrc);
|
|
|
- }
|
|
|
- //获取图片的高度
|
|
|
- List<String> strHeightList = getImg(questionRowStrings[i], "height");
|
|
|
- if(strHeightList.size()>0){
|
|
|
- String strHeight = strHeightList.get(0).replaceAll("height=\"", "").replaceAll("\"", "");
|
|
|
- param.put("height", strHeight);
|
|
|
- }
|
|
|
- //获取图片的宽度
|
|
|
- List<String> strWidthList = getImg(questionRowStrings[i], "width");
|
|
|
- if(strHeightList.size()>0){
|
|
|
- String strWidth = strWidthList.get(0).replaceAll("width=\"", "").replaceAll("\"", "");
|
|
|
- param.put("width", strWidth);
|
|
|
- }
|
|
|
- block.setParam(param);
|
|
|
- blocks.add(block);
|
|
|
- }else if(questionRowStrings[i].startsWith("a")
|
|
|
- &&questionRowStrings[i].contains("id")
|
|
|
- &&questionRowStrings[i].contains("name")){ //处理音频
|
|
|
- questionRowStrings[i] = "<"+questionRowStrings[i]+">";
|
|
|
- block.setType("audio");
|
|
|
- block.setValue(CommonUtils.getAttrValue(questionRowStrings[i],"id"));
|
|
|
- blocks.add(block);
|
|
|
- }else{
|
|
|
- block.setType("text");
|
|
|
- questionRowStrings[i] = questionRowStrings[i].replaceAll(" ","");
|
|
|
- block.setValue(questionRowStrings[i]);
|
|
|
- blocks.add(block);
|
|
|
- }
|
|
|
- }
|
|
|
- return blocks;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取图片里面的路径,长度,宽度
|
|
|
- * @param s
|
|
|
- * @param str
|
|
|
- * @return
|
|
|
- */
|
|
|
- private List<String> getImg(String s,String str)
|
|
|
- {
|
|
|
- String regex;
|
|
|
- List<String> list = new ArrayList<String>();
|
|
|
- regex = str + "=\"(.*?)\"";
|
|
|
- Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
|
|
|
- Matcher ma = pa.matcher(s);
|
|
|
- while (ma.find())
|
|
|
- {
|
|
|
- list.add(ma.group());
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将computerTestPaper对象生成JSON文件存放在jsonDirectoryPath中
|
|
|
- * @param extractConfig
|
|
|
- * @param computerTestPaper
|
|
|
- * @param jsonDirectoryPath
|
|
|
- */
|
|
|
- private void makeComputerTestPaperToJsonFile(String courseCode,ComputerTestPaper computerTestPaper,String jsonDirectoryPath){
|
|
|
- //创建新的JSON文件
|
|
|
- File file = new File(jsonDirectoryPath + File.separator+courseCode+".json");
|
|
|
- //将对象转成 json对象
|
|
|
- Gson gson = new Gson();
|
|
|
- String strJSON = gson.toJson(computerTestPaper);
|
|
|
-
|
|
|
- strJSON = CommonUtils.replaceUnicodeStr(strJSON);
|
|
|
- //生成文件流写入JSON文件
|
|
|
- FileOutputStream outputStream = null;
|
|
|
- try {
|
|
|
- outputStream = new FileOutputStream(file);
|
|
|
- byte b[] = strJSON.getBytes();
|
|
|
- outputStream.write(b);
|
|
|
- outputStream.flush();
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally{
|
|
|
- IOUtils.closeQuietly(outputStream);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void exportPaperFiles(List<String> paperIds,String serviceName,String exportContentList, HttpServletResponse response,String loginName) throws Exception {
|
|
|
- ExportPaperAbstractService exportPaperAbstractService = (ExportPaperAbstractService) SpringContextUtils.getBeanById(serviceName);
|
|
|
- //根据试卷id查询所有试卷
|
|
|
- List<Paper> papers = CommonUtils.toList(paperRepo.findAll(paperIds));
|
|
|
- String zipFileName = loginName;
|
|
|
- //创建压缩文件夹
|
|
|
- File directory = new File(TEMP_FILE_EXP + File.separator + zipFileName);
|
|
|
- if(!directory.exists()){
|
|
|
- directory.mkdirs();
|
|
|
- }
|
|
|
- //下载试卷
|
|
|
- if(exportContentList.indexOf("PAPER")>-1){
|
|
|
- for(Paper paper:papers){
|
|
|
- exportPaperAbstractService.downloadPaper(paper.getId(),zipFileName);
|
|
|
- }
|
|
|
- }
|
|
|
- //下载答案
|
|
|
- if(exportContentList.indexOf("ANSWER")>-1){
|
|
|
- for(Paper paper:papers){
|
|
|
- exportPaperAbstractService.downloadPaperAnswer(paper.getId(),zipFileName);
|
|
|
- }
|
|
|
- }
|
|
|
- //下载机考数据包
|
|
|
- if(exportContentList.indexOf("COMPUTERTEST_PACKAGE")>-1){
|
|
|
- for(Paper paper:papers){
|
|
|
- ComputerTestPaper computerTestPaper = buildComputerTestPapers(paper);
|
|
|
- makeComputerTestPaperToJsonFile(paper.getCourse().getCode()+ "_" +paper.getName(),computerTestPaper,TEMP_FILE_EXP+File.separator+zipFileName);
|
|
|
- }
|
|
|
- }
|
|
|
- FileDisposeUtil.fileToZip(TEMP_FILE_EXP+File.separator+zipFileName,TEMP_FILE_EXP,zipFileName);
|
|
|
- FileDisposeUtil.downloadFile(CommonUtils.getCurDate() +".zip", TEMP_FILE_EXP+File.separator+zipFileName+".zip",response);
|
|
|
- deteleFolder(TEMP_FILE_EXP,zipFileName);
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+package com.qmth.cqb.paper.service.impl;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import org.apache.bcel.generic.FCMPG;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.docx4j.Docx4J;
|
|
|
+import org.docx4j.openpackaging.exceptions.InvalidFormatException;
|
|
|
+import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
+import org.springframework.data.domain.Sort.Direction;
|
|
|
+import org.springframework.data.domain.Sort.Order;
|
|
|
+import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
+import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
+import org.springframework.data.mongodb.core.query.Query;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.qmth.cqb.paper.dao.PaperRepo;
|
|
|
+import com.qmth.cqb.paper.model.Paper;
|
|
|
+import com.qmth.cqb.paper.model.PaperDetail;
|
|
|
+import com.qmth.cqb.paper.model.PaperDetailUnit;
|
|
|
+import com.qmth.cqb.paper.model.computerTestModel.Block;
|
|
|
+import com.qmth.cqb.paper.model.computerTestModel.ComputerTestOption;
|
|
|
+import com.qmth.cqb.paper.model.computerTestModel.ComputerTestPaper;
|
|
|
+import com.qmth.cqb.paper.model.computerTestModel.ComputerTestPaperDetail;
|
|
|
+import com.qmth.cqb.paper.model.computerTestModel.ComputerTestQuestion;
|
|
|
+import com.qmth.cqb.paper.model.computerTestModel.Section;
|
|
|
+import com.qmth.cqb.paper.model.computerTestModel.Sections;
|
|
|
+import com.qmth.cqb.paper.service.ExportPaperService;
|
|
|
+import com.qmth.cqb.paper.service.PaperDetailService;
|
|
|
+import com.qmth.cqb.paper.service.PaperService;
|
|
|
+import com.qmth.cqb.paper.service.export.ExportPaperAbstractService;
|
|
|
+import com.qmth.cqb.question.model.QuesOption;
|
|
|
+import com.qmth.cqb.question.model.Question;
|
|
|
+import com.qmth.cqb.utils.CommonUtils;
|
|
|
+import com.qmth.cqb.utils.FileDisposeUtil;
|
|
|
+import com.qmth.cqb.utils.SpringContextUtils;
|
|
|
+import com.qmth.cqb.utils.enums.PaperType;
|
|
|
+import com.qmth.cqb.utils.word.DocxProcessUtil;
|
|
|
+
|
|
|
+import freemarker.template.Configuration;
|
|
|
+import freemarker.template.Template;
|
|
|
+
|
|
|
+@Service("exportPaperService")
|
|
|
+public class ExportPaperServiceImpl implements ExportPaperService{
|
|
|
+
|
|
|
+ public static final String TEMP_FILE_EXP = "docxExport/";
|
|
|
+
|
|
|
+ public static final String TEMP_FILE_NAME = "考试说明.docx";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PaperRepo paperRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PaperService paperService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PaperDetailService paperDetailService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MongoTemplate mongoTemplate;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exportPaperFile(String paperId,String serviceName,String exportContentList,HttpServletResponse response,String loginName) throws Exception {
|
|
|
+ ExportPaperAbstractService exportPaperAbstractService = (ExportPaperAbstractService) SpringContextUtils.getBeanById(serviceName);
|
|
|
+ //根据试卷id查询试卷
|
|
|
+ Paper paper = paperRepo.findOne(paperId);
|
|
|
+ List<Paper> papers = new ArrayList<Paper>();
|
|
|
+ papers.add(paper);
|
|
|
+ String zipFileName = loginName;
|
|
|
+ File directory = new File(TEMP_FILE_EXP + File.separator + zipFileName);
|
|
|
+ if(!directory.exists()){
|
|
|
+ directory.mkdirs();
|
|
|
+ }
|
|
|
+ if(exportContentList.indexOf("COMPUTERTEST_PACKAGE")>-1){
|
|
|
+ downJson(paper,zipFileName);
|
|
|
+ }
|
|
|
+ if(exportContentList.indexOf("PAPER")>-1){
|
|
|
+ exportPaperAbstractService.downloadPaper(paperId,zipFileName);
|
|
|
+ }
|
|
|
+ if(exportContentList.indexOf("ANSWER")>-1){
|
|
|
+ exportPaperAbstractService.downloadPaperAnswer(paperId,zipFileName);
|
|
|
+ }
|
|
|
+ //下载考试说明 2018-2-27 weiwehai
|
|
|
+ if(StringUtils.isNotBlank(paper.getExamRemark())){
|
|
|
+ downExamRemark(paper,zipFileName);
|
|
|
+ }
|
|
|
+ FileDisposeUtil.fileToZip(TEMP_FILE_EXP+File.separator+zipFileName,TEMP_FILE_EXP,zipFileName);
|
|
|
+ FileDisposeUtil.downloadFile(paper.getName()+"_"+paper.getCourse().getCode()+".zip", TEMP_FILE_EXP+File.separator+zipFileName+".zip",response);
|
|
|
+ deteleFolder(TEMP_FILE_EXP,zipFileName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载考试说明
|
|
|
+ * @param examRemark
|
|
|
+ * @param zipFileName
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private void downExamRemark(Paper paper,String zipFileName) throws Exception {
|
|
|
+ //1.考试说明html转成word
|
|
|
+ String title = "<p style=\"text-align:center\"><span style=\"font-size:26px\"><span style=\"font-family:宋体\">考试说明</span></span></p>";
|
|
|
+ WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
|
|
|
+ DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(title+paper.getExamRemark()));
|
|
|
+ //2.导出考试说明word
|
|
|
+ File file = new File(TEMP_FILE_EXP+File.separator+zipFileName+File.separator+TEMP_FILE_NAME);
|
|
|
+ Docx4J.save(wordMLPackage, file);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void deteleFolder(String sourceFilePath,String zipFileName) {
|
|
|
+ File ComputerTestPaperfoler = new File(sourceFilePath + File.separator + "json");
|
|
|
+ if(ComputerTestPaperfoler.exists()){
|
|
|
+ FileUtils.deleteQuietly(ComputerTestPaperfoler);
|
|
|
+ }
|
|
|
+ File zipFolder = new File(sourceFilePath + File.separator + zipFileName);
|
|
|
+ if(zipFolder.exists()){
|
|
|
+ FileUtils.deleteQuietly(zipFolder);
|
|
|
+ }
|
|
|
+ File zipFile = new File(sourceFilePath + File.separator + zipFileName + ".zip");
|
|
|
+ if(zipFile.exists()){
|
|
|
+ FileUtils.deleteQuietly(zipFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void downJson(Paper paper,String zipFileName) {
|
|
|
+ ComputerTestPaper computerTestPaper = buildComputerTestPapers(paper);
|
|
|
+ String jsonDirectory = TEMP_FILE_EXP + File.separator + "json";
|
|
|
+ //新建文件夹
|
|
|
+ File dirFile = new File(jsonDirectory);
|
|
|
+ if(!dirFile.exists()){
|
|
|
+ dirFile.mkdirs();
|
|
|
+ }
|
|
|
+ makeComputerTestPaperToJsonFile(paper.getCourse().getCode(),computerTestPaper,jsonDirectory);
|
|
|
+ //将文件夹打包成zip压缩包放在docxExport下
|
|
|
+ FileDisposeUtil.fileToZip(jsonDirectory,TEMP_FILE_EXP+File.separator+zipFileName,paper.getCourse().getCode()+"_"+paper.getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建机考数据包实体类
|
|
|
+ * @param extractConfig
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private ComputerTestPaper buildComputerTestPapers(Paper paper){
|
|
|
+ //得到所有旧对象的大题对象
|
|
|
+ List<PaperDetail> paperDetails = paperService.findPaperDetailsById(paper.getId());
|
|
|
+ //通过 paper 对象 ,生成新的 ComputerTestPaper 对象
|
|
|
+ ComputerTestPaper computerTestPaper = new ComputerTestPaper(paper,"");
|
|
|
+ List<ComputerTestPaperDetail> details = new ArrayList<ComputerTestPaperDetail>();
|
|
|
+ //遍历所有旧大题对象,得到小题对象的集合
|
|
|
+ for(PaperDetail paperDetail:paperDetails){
|
|
|
+ List<PaperDetailUnit> paperDetailUnits = paperDetailService.getUnitsByPaperDetailId(paperDetail.getId());
|
|
|
+ ComputerTestPaperDetail computerTestPaperDetail = new ComputerTestPaperDetail(paperDetail);
|
|
|
+ List<ComputerTestQuestion> questions = new ArrayList<ComputerTestQuestion>();
|
|
|
+ //遍历所有的小题对象
|
|
|
+ for(int i = 0;i<paperDetailUnits.size();i++){
|
|
|
+ PaperDetailUnit paperDetailUnit = paperDetailUnits.get(i);
|
|
|
+ //根据旧的小题对象,生成新的小题对象
|
|
|
+ ComputerTestQuestion computerTestQuestion = new ComputerTestQuestion(paperDetailUnit);
|
|
|
+ //设置小题题号
|
|
|
+ computerTestQuestion.setNumber(i+1);
|
|
|
+ //得到小题题干
|
|
|
+ computerTestQuestion.setBody(getBody(paperDetailUnit.getQuestion()));
|
|
|
+ //得到小题所有选项
|
|
|
+ computerTestQuestion.setOptions(getOption(paperDetailUnit.getQuestion()));
|
|
|
+ //查询小题中的 套题
|
|
|
+ List<Question> subQuestionsList = paperDetailUnit.getQuestion().getSubQuestions();
|
|
|
+ //判断这个小题中是否有套题
|
|
|
+ if(subQuestionsList!=null&&subQuestionsList.size()>0){
|
|
|
+ List<ComputerTestQuestion> subQuestions = new ArrayList<ComputerTestQuestion>();
|
|
|
+ //遍历每个套题
|
|
|
+ for(int j = 0;j<subQuestionsList.size();j++){
|
|
|
+ Question subQuestion = subQuestionsList.get(j);
|
|
|
+ ComputerTestQuestion subcomputerTestQuestion = new ComputerTestQuestion(subQuestion);
|
|
|
+ //设置套题中小题题号
|
|
|
+ subcomputerTestQuestion.setNumber(j+1);
|
|
|
+ subcomputerTestQuestion.setBody(getBody(subQuestion));
|
|
|
+ subcomputerTestQuestion.setOptions(getOption(subQuestion));
|
|
|
+ subQuestions.add(subcomputerTestQuestion);
|
|
|
+ }
|
|
|
+ computerTestQuestion.setSubQuestions(subQuestions);
|
|
|
+ }
|
|
|
+ questions.add(computerTestQuestion);
|
|
|
+
|
|
|
+ }
|
|
|
+ computerTestPaperDetail.setQuestions(questions);
|
|
|
+ //paperDetail中的题数(unitCount)可能不准确,这里以questions的实际size为准
|
|
|
+ computerTestPaperDetail.setQuestionCount(questions.size());
|
|
|
+ details.add(computerTestPaperDetail);
|
|
|
+ }
|
|
|
+ computerTestPaper.setDetails(details);
|
|
|
+ return computerTestPaper;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Sections getBody(Question question){
|
|
|
+ Sections body = new Sections();
|
|
|
+ List<Section> sections = new ArrayList<Section>();
|
|
|
+ //得到小题题干
|
|
|
+ String questionBodyString = question.getQuesBody();
|
|
|
+ //得到小题题干行数
|
|
|
+ String[] questionRowStrings = questionBodyString.split("</p>");
|
|
|
+ for(int i = 0;i<questionRowStrings.length;i++){
|
|
|
+ Section section = new Section();
|
|
|
+ //将小题题干拆分为Block集合
|
|
|
+ section.setBlocks(disposeQuestionBodyOrOption(questionRowStrings[i]));
|
|
|
+ sections.add(section);
|
|
|
+ }
|
|
|
+ body.setSections(sections);
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<ComputerTestOption> getOption(Question question){
|
|
|
+ //得到小题选项
|
|
|
+ List<QuesOption> quesOptions = question.getQuesOptions();
|
|
|
+ List<ComputerTestOption> options = new ArrayList<ComputerTestOption>();
|
|
|
+ //遍历小题选项
|
|
|
+ if(quesOptions!=null&&quesOptions.size()>0){
|
|
|
+ for(QuesOption quesOption: quesOptions){
|
|
|
+ ComputerTestOption option = new ComputerTestOption();
|
|
|
+ option.setNumber(new Integer(quesOption.getNumber()));
|
|
|
+ option.setCorrect(quesOption.getIsCorrect()==1?true:false);
|
|
|
+ Sections body = new Sections();
|
|
|
+
|
|
|
+ List<Section> sections = new ArrayList<Section>();
|
|
|
+ //得到小题选项
|
|
|
+ String optionString = quesOption.getOptionBody();
|
|
|
+ String[] optionStrings = optionString.split("</p>");
|
|
|
+ for(int i = 0;i<optionStrings.length;i++){
|
|
|
+ Section section = new Section();
|
|
|
+ section.setBlocks(disposeQuestionBodyOrOption(optionStrings[i]));
|
|
|
+ sections.add(section);
|
|
|
+ }
|
|
|
+ body.setSections(sections);
|
|
|
+ option.setBody(body);
|
|
|
+ options.add(option);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return options;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Block> disposeQuestionBodyOrOption(String questionRow) {
|
|
|
+ List<Block> blocks = new ArrayList<Block>();
|
|
|
+ //去掉每行里面的<p>,<span>,</span>标签
|
|
|
+ questionRow = questionRow.replaceAll("<p>", "")
|
|
|
+ .replaceAll("</p>", "")
|
|
|
+ .replaceAll("<span>", "")
|
|
|
+ .replaceAll("</span>", "")
|
|
|
+ .replaceAll("</a>", "");
|
|
|
+ String[] questionRowStrings = questionRow.split("<|/>|>");
|
|
|
+ for(int i = 0;i<questionRowStrings.length;i++){
|
|
|
+ Block block = new Block();
|
|
|
+ //判断是否有图片
|
|
|
+ if(questionRowStrings[i].startsWith("img")){
|
|
|
+ questionRowStrings[i] = "<"+questionRowStrings[i]+">";
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>();
|
|
|
+ //需要继续做截取,取到Parma
|
|
|
+ block.setType("image");
|
|
|
+ //获取图片的路径
|
|
|
+ List<String> strSrcList = getImg(questionRowStrings[i], "src");
|
|
|
+ if(strSrcList.size()>0){
|
|
|
+ String strSrc = strSrcList.get(0).replaceAll("src=\"", "").replaceAll("\"", "");
|
|
|
+ block.setValue(strSrc);
|
|
|
+ }
|
|
|
+ //获取图片的高度
|
|
|
+ List<String> strHeightList = getImg(questionRowStrings[i], "height");
|
|
|
+ if(strHeightList.size()>0){
|
|
|
+ String strHeight = strHeightList.get(0).replaceAll("height=\"", "").replaceAll("\"", "");
|
|
|
+ param.put("height", strHeight);
|
|
|
+ }
|
|
|
+ //获取图片的宽度
|
|
|
+ List<String> strWidthList = getImg(questionRowStrings[i], "width");
|
|
|
+ if(strHeightList.size()>0){
|
|
|
+ String strWidth = strWidthList.get(0).replaceAll("width=\"", "").replaceAll("\"", "");
|
|
|
+ param.put("width", strWidth);
|
|
|
+ }
|
|
|
+ block.setParam(param);
|
|
|
+ blocks.add(block);
|
|
|
+ }else if(questionRowStrings[i].startsWith("a")
|
|
|
+ &&questionRowStrings[i].contains("id")
|
|
|
+ &&questionRowStrings[i].contains("name")){ //处理音频
|
|
|
+ questionRowStrings[i] = "<"+questionRowStrings[i]+">";
|
|
|
+ block.setType("audio");
|
|
|
+ block.setValue(CommonUtils.getAttrValue(questionRowStrings[i],"id"));
|
|
|
+ blocks.add(block);
|
|
|
+ }else{
|
|
|
+ block.setType("text");
|
|
|
+ questionRowStrings[i] = questionRowStrings[i].replaceAll(" ","");
|
|
|
+ block.setValue(questionRowStrings[i]);
|
|
|
+ blocks.add(block);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return blocks;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取图片里面的路径,长度,宽度
|
|
|
+ * @param s
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<String> getImg(String s,String str)
|
|
|
+ {
|
|
|
+ String regex;
|
|
|
+ List<String> list = new ArrayList<String>();
|
|
|
+ regex = str + "=\"(.*?)\"";
|
|
|
+ Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
|
|
|
+ Matcher ma = pa.matcher(s);
|
|
|
+ while (ma.find())
|
|
|
+ {
|
|
|
+ list.add(ma.group());
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将computerTestPaper对象生成JSON文件存放在jsonDirectoryPath中
|
|
|
+ * @param extractConfig
|
|
|
+ * @param computerTestPaper
|
|
|
+ * @param jsonDirectoryPath
|
|
|
+ */
|
|
|
+ private void makeComputerTestPaperToJsonFile(String courseCode,ComputerTestPaper computerTestPaper,String jsonDirectoryPath){
|
|
|
+ //创建新的JSON文件
|
|
|
+ File file = new File(jsonDirectoryPath + File.separator+courseCode+".json");
|
|
|
+ //将对象转成 json对象
|
|
|
+ Gson gson = new Gson();
|
|
|
+ String strJSON = gson.toJson(computerTestPaper);
|
|
|
+
|
|
|
+ strJSON = CommonUtils.replaceUnicodeStr(strJSON);
|
|
|
+ //生成文件流写入JSON文件
|
|
|
+ FileOutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ outputStream = new FileOutputStream(file);
|
|
|
+ byte b[] = strJSON.getBytes();
|
|
|
+ outputStream.write(b);
|
|
|
+ outputStream.flush();
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally{
|
|
|
+ IOUtils.closeQuietly(outputStream);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exportPaperFiles(List<String> paperIds,String serviceName,String exportContentList, HttpServletResponse response,String loginName) throws Exception {
|
|
|
+ ExportPaperAbstractService exportPaperAbstractService = (ExportPaperAbstractService) SpringContextUtils.getBeanById(serviceName);
|
|
|
+ //根据试卷id查询所有试卷
|
|
|
+ List<Paper> papers = CommonUtils.toList(paperRepo.findAll(paperIds));
|
|
|
+ String zipFileName = loginName;
|
|
|
+ //创建压缩文件夹
|
|
|
+ File directory = new File(TEMP_FILE_EXP + File.separator + zipFileName);
|
|
|
+ if(!directory.exists()){
|
|
|
+ directory.mkdirs();
|
|
|
+ }
|
|
|
+ //下载试卷
|
|
|
+ if(exportContentList.indexOf("PAPER")>-1){
|
|
|
+ for(Paper paper:papers){
|
|
|
+ exportPaperAbstractService.downloadPaper(paper.getId(),zipFileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //下载答案
|
|
|
+ if(exportContentList.indexOf("ANSWER")>-1){
|
|
|
+ for(Paper paper:papers){
|
|
|
+ exportPaperAbstractService.downloadPaperAnswer(paper.getId(),zipFileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //下载机考数据包
|
|
|
+ if(exportContentList.indexOf("COMPUTERTEST_PACKAGE")>-1){
|
|
|
+ for(Paper paper:papers){
|
|
|
+ ComputerTestPaper computerTestPaper = buildComputerTestPapers(paper);
|
|
|
+ makeComputerTestPaperToJsonFile(paper.getCourse().getCode()+ "_" +paper.getName(),computerTestPaper,TEMP_FILE_EXP+File.separator+zipFileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FileDisposeUtil.fileToZip(TEMP_FILE_EXP+File.separator+zipFileName,TEMP_FILE_EXP,zipFileName);
|
|
|
+ FileDisposeUtil.downloadFile(CommonUtils.getCurDate() +".zip", TEMP_FILE_EXP+File.separator+zipFileName+".zip",response);
|
|
|
+ deteleFolder(TEMP_FILE_EXP,zipFileName);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|