|
@@ -2,13 +2,10 @@ package com.qmth.cqb.paper.service.export;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import javax.xml.bind.JAXBElement;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.docx4j.XmlUtils;
|
|
@@ -50,7 +47,7 @@ import freemarker.template.Template;
|
|
|
* Created by songyue on 17/3/15.
|
|
|
*/
|
|
|
@Service("sydxExportPaperService")
|
|
|
-public class SydxExportPaperService{
|
|
|
+public class SydxExportPaperService extends ExportPaperAbstractService{
|
|
|
@Autowired
|
|
|
PaperRepo paperRepo;
|
|
|
|
|
@@ -75,8 +72,9 @@ public class SydxExportPaperService{
|
|
|
private static Configuration CONFIGURATION;
|
|
|
|
|
|
private static final String ENCODING = "UTF-8";
|
|
|
- //石油大学模板
|
|
|
- private static Template SYDX_TEMPLATE_NORMAL;
|
|
|
+
|
|
|
+ //石油大学word模板
|
|
|
+ private static Template SYDX_TEMPLATE_PAPER;
|
|
|
|
|
|
static {
|
|
|
try {
|
|
@@ -85,7 +83,7 @@ public class SydxExportPaperService{
|
|
|
CONFIGURATION.setDefaultEncoding(ENCODING);
|
|
|
// 设置ftl模板路径
|
|
|
CONFIGURATION.setClassForTemplateLoading(DocxProcessUtil.class, "/");
|
|
|
- SYDX_TEMPLATE_NORMAL = CONFIGURATION.getTemplate("sydx_paper_template.ftl", ENCODING);
|
|
|
+ SYDX_TEMPLATE_PAPER = CONFIGURATION.getTemplate("sydx_paper_template.ftl", ENCODING);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -96,9 +94,8 @@ public class SydxExportPaperService{
|
|
|
* @param id
|
|
|
* @return
|
|
|
*/
|
|
|
- public Map<String,Object> initExportPaper(String id) throws Exception{
|
|
|
- //创建返回Map
|
|
|
- Map<String,Object> returnMap = new HashMap<String,Object>();
|
|
|
+ @Override
|
|
|
+ public PaperExp initExportPaper(String id) throws Exception{
|
|
|
//获取paper
|
|
|
Paper paper = paperRepo.findOne(id);
|
|
|
paperService.formatPaper(paper,null);
|
|
@@ -112,8 +109,7 @@ public class SydxExportPaperService{
|
|
|
paperExp.setSubTitle(CommonUtils.PAPER_SUB_TITLE);
|
|
|
//获取大题
|
|
|
List<PaperDetail> paperDetails = paperDetailRepo.findByPaper(paper);
|
|
|
- List<PaperDetailExp> paperDetailExps =
|
|
|
- BeanCopierUtil.copyPropertiesOfList(paperDetails,PaperDetailExp.class);
|
|
|
+ List<PaperDetailExp> paperDetailExps = BeanCopierUtil.copyPropertiesOfList(paperDetails,PaperDetailExp.class);
|
|
|
paperExp.setPaperDetails(paperDetailExps);
|
|
|
|
|
|
//封装小题
|
|
@@ -124,17 +120,14 @@ public class SydxExportPaperService{
|
|
|
|
|
|
setAnswerWord(paperDetailUnits);
|
|
|
|
|
|
- List<PaperDetailUnitExp> paperDetailUnitExps =
|
|
|
- BeanCopierUtil.copyPropertiesOfList(paperDetailUnits,PaperDetailUnitExp.class);
|
|
|
+ List<PaperDetailUnitExp> paperDetailUnitExps = BeanCopierUtil.copyPropertiesOfList(paperDetailUnits,PaperDetailUnitExp.class);
|
|
|
//选择题,套题下选择题 选项顺序重新排列
|
|
|
paperService.reorderChoicequestionOption(paperDetailUnitExps);
|
|
|
paperDetailExps.get(i).setPaperDetailUnits(paperDetailUnitExps);
|
|
|
}
|
|
|
//初始化试卷序号
|
|
|
initPaperNum(paperExp);
|
|
|
- returnMap.put("paper", paperExp);
|
|
|
- returnMap.put("fileName", paperExp.getName());
|
|
|
- return returnMap;
|
|
|
+ return paperExp;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -273,12 +266,13 @@ public class SydxExportPaperService{
|
|
|
* 下载试卷
|
|
|
* @param id
|
|
|
*/
|
|
|
+ @Override
|
|
|
public void downloadPaper(String id, HttpServletResponse response) throws Exception {
|
|
|
List<String> fileNames = new ArrayList<String>();
|
|
|
- Map<String,Object> dataMap = initExportPaper(id);
|
|
|
- if (dataMap.get("fileName") != null) {
|
|
|
- String fileName = (String) dataMap.get("fileName")+".docx";
|
|
|
- DocxProcessUtil.exportWord(dataMap, fileName,SYDX_TEMPLATE_NORMAL);
|
|
|
+ PaperExp paperExp = initExportPaper(id);
|
|
|
+ if (paperExp!=null) {
|
|
|
+ String fileName = paperExp.getName()+DOCX_SUFFIX;
|
|
|
+ DocxProcessUtil.exportWord(paperExp,fileName,SYDX_TEMPLATE_PAPER);
|
|
|
DocxProcessUtil.processImage(fileName, getPkgList(id));
|
|
|
fileNames.add(fileName);
|
|
|
DocxProcessUtil.processDownload(fileNames, response);
|
|
@@ -318,9 +312,7 @@ public class SydxExportPaperService{
|
|
|
Question question = paperDetailUnit.getQuestion();
|
|
|
//套题序号
|
|
|
if(subQuesList != null && subQuesList.size() > 0){
|
|
|
-
|
|
|
question.setQuesBodyWord(replaceQuesBlank(question.getQuesBodyWord(),subNum + 1));
|
|
|
-
|
|
|
for(Question subQues:subQuesList){
|
|
|
int curSubNum = ++subNum;
|
|
|
subQues.setQuesBodyWord(setSubQuesNum(subQues.getQuesBodyWord(),curSubNum));
|
|
@@ -429,68 +421,4 @@ public class SydxExportPaperService{
|
|
|
return pWordMl.toString();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 替换填空
|
|
|
- * @param wordMl
|
|
|
- * @param num
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public String replaceQuesBlank(String wordMl,int num) throws Exception {
|
|
|
- String tmpStr = DocxProcessUtil.BODY_HEADER + wordMl + DocxProcessUtil.BODY_TAIL;
|
|
|
- Body body = (Body)XmlUtils.unmarshalString(tmpStr,
|
|
|
- Context.jc,Body.class);
|
|
|
- List<Object> pList = body.getContent();
|
|
|
- int index = 0;
|
|
|
- int cur = 0;
|
|
|
- Map<Integer,String> curMap = new HashMap<Integer,String>();
|
|
|
- for(Object pObj:pList){
|
|
|
- if(pObj.getClass().equals(P.class)){
|
|
|
- List<Object> pContent = ((P)pObj).getContent();
|
|
|
- for(Object rObj:pContent){
|
|
|
- if(rObj.getClass().equals(R.class)){
|
|
|
- List<Object> rContent = ((R)rObj).getContent();
|
|
|
- for(Object tObj:rContent){
|
|
|
- if (tObj instanceof JAXBElement)
|
|
|
- tObj = ((JAXBElement<?>) tObj).getValue();
|
|
|
- if(tObj.getClass().equals(Text.class)){
|
|
|
- Text text = (Text)tObj;
|
|
|
- String str = text.getValue().trim();
|
|
|
- if(str.equals("###")){
|
|
|
- text.setValue("______");
|
|
|
- }else if(str.equals("(###)")){
|
|
|
- text.setValue("(______)");
|
|
|
- }else if(str.equals("(###)")){
|
|
|
- text.setValue("(______)");
|
|
|
- }else if(str.matches("##\\d{1,}##")){
|
|
|
- int curNum = num + index;
|
|
|
- text.setValue("___"+(curNum)+"___");
|
|
|
- index++;
|
|
|
- }else if(str.startsWith("#") || str.equals("___")){
|
|
|
- curMap.put(cur,str);
|
|
|
- text.setValue("");
|
|
|
- }else if(str.matches("^\\d{1,}$")){
|
|
|
- String preStr = curMap.get(cur - 1);
|
|
|
- if(!StringUtils.isEmpty(preStr) && preStr.startsWith("#")){
|
|
|
- int curNum = num + index;
|
|
|
- text.setValue("___"+(curNum)+"___");
|
|
|
- index++;
|
|
|
- }
|
|
|
- }
|
|
|
- cur++;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- StringBuffer pWordMl = new StringBuffer();
|
|
|
- for(Object pObj:pList){
|
|
|
- if(pObj instanceof P){
|
|
|
- pWordMl.append(DocxProcessUtil.formatPWordMl(XmlUtils.marshaltoString(pObj)));
|
|
|
- }
|
|
|
- }
|
|
|
- return pWordMl.toString();
|
|
|
- }
|
|
|
-
|
|
|
}
|