|
@@ -12,6 +12,7 @@ import cn.iselab.mooctest.site.service.common.MongoAPIService;
|
|
|
import cn.iselab.mooctest.site.service.common.PdfService;
|
|
|
import cn.iselab.mooctest.site.service.fromKibug.CaseTakeService;
|
|
|
import cn.iselab.mooctest.site.service.updownload.DownloadService;
|
|
|
+import cn.iselab.mooctest.site.util.DateUtil.DateUtil;
|
|
|
import cn.iselab.mooctest.site.util.MD5Util.MD5Util;
|
|
|
import cn.iselab.mooctest.site.util.data.PdfUtils;
|
|
|
import cn.iselab.mooctest.site.web.data.forMongo.BugDTO;
|
|
@@ -19,6 +20,7 @@ import cn.iselab.mooctest.site.web.data.forMongo.ReportForMongoDTO;
|
|
|
import cn.iselab.mooctest.site.web.data.fromKibug.ReportCaseVO;
|
|
|
import cn.iselab.mooctest.site.web.exception.HttpBadRequestException;
|
|
|
import cn.iselab.mooctest.site.web.logic.fromDev.PluginLogic;
|
|
|
+import cn.iselab.mooctest.site.web.logic.fromDev.UpDownloadLogic;
|
|
|
import com.itextpdf.text.*;
|
|
|
import com.itextpdf.text.pdf.*;
|
|
|
import net.lingala.zip4j.core.ZipFile;
|
|
@@ -28,6 +30,7 @@ import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
|
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
+import org.assertj.core.util.Lists;
|
|
|
import org.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -57,8 +60,18 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
public static String SAVE_PATH = "/var/www/download/";
|
|
|
public static String PDF_PATH = "pdf/";
|
|
|
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ GradeGeneralDao gradeGeneralDao;
|
|
|
+ @Autowired
|
|
|
+ GeneralCalculateScoreService generalCalculateScoreService;
|
|
|
+
|
|
|
@Autowired
|
|
|
ExamService examService;
|
|
|
+ @Autowired
|
|
|
+ private UploadRecordService uploadRecordService;
|
|
|
+ @Autowired
|
|
|
+ WeightGeneralDao weightGeneralDao;
|
|
|
|
|
|
@Autowired
|
|
|
PluginLogic pluginLogic;
|
|
@@ -111,39 +124,12 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
@Override
|
|
|
public ReportPDF getReportPDF(Long examId, Long workerId) throws Exception {
|
|
|
ReportPDF reportPDF = new ReportPDF();
|
|
|
- Exam exam = examService.getTask(examId);
|
|
|
- if (exam == null) {
|
|
|
- reportPDF.setFailReason("exam not exist");
|
|
|
- return reportPDF;
|
|
|
-
|
|
|
- }
|
|
|
- reportPDF.setExamName(exam.getName());
|
|
|
- SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
|
|
|
- reportPDF.setDateTime(exam.getBeginTime() + "-" + format.format(exam.getEndTime()));
|
|
|
- UserDTOForMT user = userService.findByUserId(workerId);
|
|
|
- if (user == null) {
|
|
|
- reportPDF.setFailReason("user not exist");
|
|
|
- return reportPDF;
|
|
|
- }
|
|
|
- reportPDF.setWorkerName(user.getName());
|
|
|
- reportPDF.setSchool(user.getSchool());
|
|
|
- List<Group> groups = groupService.getByExamId(exam.getId());
|
|
|
- if (groups.isEmpty()) {
|
|
|
- reportPDF.setFailReason("group not exits");
|
|
|
- return reportPDF;
|
|
|
- }else if(!groups.isEmpty()) {
|
|
|
- reportPDF.setGroupName(groups.get(0).getName());
|
|
|
- }
|
|
|
- UserDTOForMT teacher = userService.findByUserId(exam.getOwnerId());
|
|
|
- if (teacher == null) {
|
|
|
- reportPDF.setFailReason("teacher not exits");
|
|
|
- return reportPDF;
|
|
|
- }
|
|
|
- reportPDF.setTeacherName(teacher.getName());
|
|
|
+ //校验本场考试信息
|
|
|
+ verfiyParamter(reportPDF, examId, workerId);
|
|
|
SubmitRecord submitRecord = submitRecordService.getAssignedTask(examId, workerId);
|
|
|
- if (submitRecord == null) {
|
|
|
- reportPDF.setFailReason("score not exits");
|
|
|
- return reportPDF;
|
|
|
+
|
|
|
+ if (submitRecord == null || submitRecord.getScore() == null ||"".equals(submitRecord.getScore())) {
|
|
|
+ throw new HttpBadRequestException("score not exits");
|
|
|
}
|
|
|
/*
|
|
|
成绩保留两位小数。
|
|
@@ -152,55 +138,144 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
instance.setMaximumFractionDigits(2);
|
|
|
reportPDF.setScore(instance.format(submitRecord.getScore()));
|
|
|
|
|
|
+
|
|
|
List<SubmitRecord> submitRecords = submitRecordService.getAssignedTasks(examId);
|
|
|
List<Double> scores = calculateScoreService.calculateScores(submitRecords.stream().map(assignedTask1 -> assignedTask1.getScore()).collect(Collectors.toList()));
|
|
|
- reportPDF.setMax(scores.get(0).toString());
|
|
|
- reportPDF.setMin(scores.get(1).toString());
|
|
|
- reportPDF.setAvg(scores.get(2).toString());
|
|
|
- reportPDF.setVariance(scores.get(3).toString());
|
|
|
+ //初始化pdf的得分
|
|
|
+ initPDF(reportPDF, scores);
|
|
|
|
|
|
List<CasePDF> pdfs = new ArrayList<>();
|
|
|
List<AssignedCase> cases = submitRecordService.wrapAssignedCases(submitRecord);
|
|
|
- //List<Long> caseIds=cases.stream().map(casse-> casse.getCaseId()).collect(Collectors.toList());
|
|
|
+ List<Long> caseIds = cases.stream().map(casse -> casse.getCaseId()).collect(Collectors.toList());
|
|
|
List<CaseBlock> caseBlocks = exam2CaseService.getCasesByExamId(examId);
|
|
|
- for (CaseBlock caseBlock : caseBlocks) {
|
|
|
- if (caseBlock.getCaseIds().size() < 1) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- long caseId = caseBlock.getCaseIds().get(0);
|
|
|
+ for (long caseId : caseIds) {
|
|
|
CasePDF pdf = new CasePDF();
|
|
|
+ List<Double> scoreByUpload = new ArrayList<>();
|
|
|
+ Map<Long, List<GradeGeneral>> preScoreDetail = new HashMap<>();
|
|
|
CaseExtends caseExtends = caseService.getCaseExtendsById(caseId);
|
|
|
+ //获取当前用户当前case的所有提交记录
|
|
|
+ List<UploadRecord> uploadRecords = uploadRecordService.findByCaseId(workerId, examId, caseId);
|
|
|
+ //每一个case上传的所有id
|
|
|
+ List<Long> uploadIds = uploadRecords.stream().map(UploadRecord::getId).collect(Collectors.toList());//130-132
|
|
|
+ //所有提交的所有类型的得分。
|
|
|
+ List<GradeGeneral> gradeGenerals = gradeGeneralDao.findByUploadIdIn(uploadIds);
|
|
|
+ Map<Long, List<GradeGeneral>> collect = gradeGenerals.stream().collect(Collectors.groupingBy(GradeGeneral::getUploadId));
|
|
|
+
|
|
|
+ //得分规则
|
|
|
+ List<WeightGeneral> weightGeneralList = weightGeneralDao.findByExamIdAndCaseId(examId, caseId);
|
|
|
+ //计算每一次提交的所有得分详情
|
|
|
+ calculateAllScoresByUploadId(uploadIds, collect, preScoreDetail);
|
|
|
+ Map<Long, Double> finalScoreMap = new HashMap<>();
|
|
|
+ double finalScore = 0;
|
|
|
+ //每一次提交所有得分map
|
|
|
+ setFinalScoreMap(preScoreDetail, uploadIds, finalScoreMap, weightGeneralList);
|
|
|
+ pdf.setScoreMap(finalScoreMap);
|
|
|
+
|
|
|
+
|
|
|
+ //每一个case的提交列表
|
|
|
+ if (uploadRecords != null && uploadRecords.size() != 0) {
|
|
|
+ pdf.setUploadRecordList(uploadRecords);
|
|
|
+ }
|
|
|
+
|
|
|
pdf.setCaseName(caseExtends.getName());
|
|
|
pdf.setDescription(caseExtends.getDescription());
|
|
|
- for (AssignedCase assignedCase : cases) {
|
|
|
- if (assignedCase.getCaseId() == caseId) {
|
|
|
- DecimalFormat decimalFormat=new DecimalFormat("######0.00");
|
|
|
- pdf.setCaseScore(String.valueOf(decimalFormat.format(assignedCase.getMaxScore())));
|
|
|
- }
|
|
|
+ wrapPDF(caseId, cases, pdf, examId, workerId, caseExtends);
|
|
|
+ pdfs.add(pdf);
|
|
|
+ }
|
|
|
+ reportPDF.setCases(pdfs);
|
|
|
+ return reportPDF;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void wrapPDF(Long caseId, List<AssignedCase> cases, CasePDF pdf, Long examId, Long workerId, CaseExtends caseExtends) throws Exception {
|
|
|
+ for (AssignedCase assignedCase : cases) {
|
|
|
+ if (assignedCase.getCaseId() == caseId) {
|
|
|
+ DecimalFormat decimalFormat = new DecimalFormat("######0.00");
|
|
|
+ pdf.setCaseScore(String.valueOf(decimalFormat.format(assignedCase.getMaxScore())));
|
|
|
}
|
|
|
if (caseExtends.getAnswerWay() == AnswerWayConstants.REPORT)
|
|
|
pdf.setReport(this.getReport(examId, workerId, caseId));
|
|
|
if (caseExtends.getAnswerWay() == AnswerWayConstants.DEV_ECLIPSE) {
|
|
|
- String downloadUrl=pluginLogic.getAnalysisSignature(examId,workerId,caseExtends.getName());
|
|
|
+ String downloadUrl = pluginLogic.getAnalysisSignature(examId, workerId, caseExtends.getName());
|
|
|
pdf.setAnswerCode(this.getResultCode(downloadUrl));
|
|
|
}
|
|
|
- pdfs.add(pdf);
|
|
|
}
|
|
|
- reportPDF.setCases(pdfs);
|
|
|
- return reportPDF;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void generatePDF(ReportPDF pdf, String fileName) throws Exception {
|
|
|
- PdfReader reader = new PdfReader(templatePdfPath);
|
|
|
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
- PdfStamper ps = new PdfStamper(reader, bos);
|
|
|
+ //所有提交的总map
|
|
|
+ private void setFinalScoreMap(Map<Long, List<GradeGeneral>> preScoreDetail, List<Long> uploadIds, Map<Long, Double> finalScoreMap, List<WeightGeneral> weightGeneralList) {
|
|
|
+
|
|
|
+
|
|
|
+ for (int i = 0; i < preScoreDetail.size(); i++) {
|
|
|
+ double finalScore = 0;
|
|
|
+ Map<String, Double> typeScore = new HashMap<>();
|
|
|
+ if (preScoreDetail.get(uploadIds.get(i)) != null) {
|
|
|
+ for (GradeGeneral gradeGeneral : preScoreDetail.get(uploadIds.get(i))) {
|
|
|
+
|
|
|
+ typeScore.put(gradeGeneral.getType(), gradeGeneral.getScore());
|
|
|
+
|
|
|
+ }
|
|
|
+ for (WeightGeneral weightGeneral : weightGeneralList) {
|
|
|
+ if (typeScore.get(weightGeneral.getType()) == null) {
|
|
|
+ finalScore += 0;
|
|
|
+ } else {
|
|
|
+ finalScore += typeScore.get(weightGeneral.getType()) * weightGeneral.getWeight() / 100;
|
|
|
+ }
|
|
|
+ finalScoreMap.put(uploadIds.get(i), finalScore);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算每一次提交的所有得分
|
|
|
+ private void calculateAllScoresByUploadId(List<Long> uploadIds, Map<Long, List<GradeGeneral>> collect, Map<Long, List<GradeGeneral>> preScoreDetail) {
|
|
|
+ for (int i = 0; i < uploadIds.size(); i++) {
|
|
|
+ List<GradeGeneral> gradeGeneralList = new ArrayList<>();
|
|
|
+ gradeGeneralList = collect.get(uploadIds.get(i));
|
|
|
+ preScoreDetail.put(uploadIds.get(i), gradeGeneralList);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
|
|
|
- fontList.add(PdfUtils.getBaseFont());
|
|
|
- AcroFields fields = ps.getAcroFields();
|
|
|
- fields.setSubstitutionFonts(fontList);
|
|
|
|
|
|
+ //设置 成绩最大值,最小值,方差
|
|
|
+ private void initPDF(ReportPDF reportPDF, List<Double> scores) {
|
|
|
+ reportPDF.setMax(scores.get(0).toString());
|
|
|
+ reportPDF.setMin(scores.get(1).toString());
|
|
|
+ reportPDF.setAvg(scores.get(2).toString());
|
|
|
+ reportPDF.setVariance(scores.get(3).toString());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //校验考试 ,用户,班级,教师信息。
|
|
|
+ private void verfiyParamter(ReportPDF reportPDF, Long examId, Long workerId) {
|
|
|
+
|
|
|
+ Exam exam = examService.getTask(examId);
|
|
|
+ if (exam == null) {
|
|
|
+ throw new HttpBadRequestException("exam not exits");
|
|
|
+ }
|
|
|
+ reportPDF.setExamName(exam.getName());
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
|
|
|
+ reportPDF.setDateTime(exam.getBeginTime() + "-" + format.format(exam.getEndTime()));
|
|
|
+ UserDTOForMT user = userService.findByUserId(workerId);
|
|
|
+ if (user == null) {
|
|
|
+ throw new HttpBadRequestException("user not exits");
|
|
|
+ }
|
|
|
+ reportPDF.setWorkerName(user.getName());
|
|
|
+ reportPDF.setSchool(user.getSchool());
|
|
|
+ List<Group> groups = groupService.getByExamId(exam.getId());
|
|
|
+ if (groups.isEmpty()) {
|
|
|
+ throw new HttpBadRequestException("group not exits");
|
|
|
+ } else if (!groups.isEmpty()) {
|
|
|
+ reportPDF.setGroupName(groups.get(0).getName());
|
|
|
+ }
|
|
|
+ UserDTOForMT teacher = userService.findByUserId(exam.getOwnerId());
|
|
|
+ if (teacher == null) {
|
|
|
+ throw new HttpBadRequestException("teacher not exits");
|
|
|
+ }
|
|
|
+ reportPDF.setTeacherName(teacher.getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ //pdf上面的数据
|
|
|
+ private void copyPropertiesFromPdf(AcroFields fields, ReportPDF pdf) throws IOException, DocumentException {
|
|
|
fields.setField("examName", pdf.getExamName());
|
|
|
fields.setField("workerName", pdf.getWorkerName());
|
|
|
fields.setField("school", pdf.getSchool());
|
|
@@ -213,6 +288,19 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
fields.setField("avg", pdf.getAvg());
|
|
|
fields.setField("variance", pdf.getVariance());
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void generatePDF(ReportPDF pdf, String fileName) throws Exception {
|
|
|
+ PdfReader reader = new PdfReader(templatePdfPath);
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ PdfStamper ps = new PdfStamper(reader, bos);
|
|
|
+ ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
|
|
|
+ fontList.add(PdfUtils.getBaseFont());
|
|
|
+ AcroFields fields = ps.getAcroFields();
|
|
|
+ fields.setSubstitutionFonts(fontList);
|
|
|
+
|
|
|
+
|
|
|
//插入全班成绩柱状图
|
|
|
String barUrl;
|
|
|
if (pdf.getBarGraph() != null) {
|
|
@@ -229,10 +317,9 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
ps.setFormFlattening(true);
|
|
|
ps.close();
|
|
|
// 根据学生姓名和文件名进行MD5加密
|
|
|
+ String md5_fileName = MD5Util.string2MD5(fileName);
|
|
|
|
|
|
- String md5_fileName=MD5Util.string2MD5(fileName);
|
|
|
-
|
|
|
- FileOutputStream fos = new FileOutputStream(FileUtils.getUserDirectory() + "/" +md5_fileName+ "1.pdf");
|
|
|
+ FileOutputStream fos = new FileOutputStream(FileUtils.getUserDirectory() + "/" + md5_fileName + "1.pdf");
|
|
|
//方法一,生成的pdf文件较大
|
|
|
// fos.write(bos.toByteArray());
|
|
|
// fos.close();
|
|
@@ -248,28 +335,79 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
fos.close();
|
|
|
|
|
|
Document document = new Document();
|
|
|
- FileOutputStream out = new FileOutputStream(FileUtils.getUserDirectory() +"/" +md5_fileName + "2.pdf");
|
|
|
+ FileOutputStream out = new FileOutputStream(FileUtils.getUserDirectory() + "/" + md5_fileName + "2.pdf");
|
|
|
PdfWriter writer = PdfWriter.getInstance(document, out);
|
|
|
document.open();
|
|
|
-
|
|
|
List<CasePDF> casePDFS = pdf.getCases();
|
|
|
+ System.out.println(casePDFS.size() + "mmmmmmmmmmmm");
|
|
|
if (casePDFS != null) {
|
|
|
for (CasePDF casePDF : casePDFS) {
|
|
|
+
|
|
|
document.newPage();
|
|
|
Paragraph paragraph = new Paragraph("题目" + (pdf.getCases().indexOf(casePDF) + 1), PdfUtils.getTitle());
|
|
|
paragraph.setLeading(22f);
|
|
|
document.add(paragraph);
|
|
|
document.add(new Paragraph("案例名称:" + casePDF.getCaseName() + " 案例分数:" + casePDF.getCaseScore(), PdfUtils.getContent()));
|
|
|
- document.add(new Paragraph("案例描述:" + (casePDF.getDescription()!=null?casePDF.getDescription():"无"), PdfUtils.getContent()));
|
|
|
- //生成本题学生的提交内容
|
|
|
- generateCase(casePDF,document);
|
|
|
+ document.add(new Paragraph("案例描述:" + (casePDF.getDescription() != null ? casePDF.getDescription() : "无"), PdfUtils.getContent()));
|
|
|
+ generateCase(casePDF, document);
|
|
|
+ PdfPTable table = renderTable(casePDF);
|
|
|
+ document.add(new Paragraph("提交记录:", PdfUtils.getContent()));
|
|
|
+ document.add(Chunk.NEWLINE);
|
|
|
+ //提交记录表格 每一列的宽度 共3列
|
|
|
+ document.add(table);
|
|
|
}
|
|
|
}
|
|
|
document.close();
|
|
|
- String[] files = {FileUtils.getUserDirectory() + "/"+md5_fileName + "1.pdf", FileUtils.getUserDirectory() + "/"+md5_fileName + "2.pdf"};
|
|
|
- mergePDFS(files, SAVE_PATH + PDF_PATH + "/" + md5_fileName + ".pdf");
|
|
|
+ String[] files = {String.format("%s/%s1.pdf", FileUtils.getUserDirectory(), md5_fileName), String.format("%s/%s2.pdf", FileUtils.getUserDirectory(), md5_fileName)};
|
|
|
+ mergePDFS(files, String.format("%s%s/%s.pdf", SAVE_PATH, PDF_PATH, md5_fileName));
|
|
|
}
|
|
|
|
|
|
+ //生成表格
|
|
|
+ private PdfPTable renderTable(CasePDF casePDF) throws IOException, DocumentException {
|
|
|
+ float[] widths = {144, 113, 144};
|
|
|
+ PdfPTable table = new PdfPTable(widths);
|
|
|
+ /*
|
|
|
+ 提交记录表格第一行的数据 因为第一行数据固定 所以 后面增加了不是第一行的判断。
|
|
|
+ //每道题的提交次数
|
|
|
+ */
|
|
|
+
|
|
|
+ String uploadTime = "";
|
|
|
+ int uploadTimesCount = casePDF.getUploadRecordList().size();
|
|
|
+ Object[][] datas = new Object[uploadTimesCount + 1][3];
|
|
|
+ datas[0][0] = "提交次序";
|
|
|
+ datas[0][1] = "score";
|
|
|
+ datas[0][2] = "提交时间";
|
|
|
+ int columnWidth = 3;//表格的列数
|
|
|
+ for (int i = 0; i < uploadTimesCount + 1; i++) {
|
|
|
+ if (i != 0) {
|
|
|
+ uploadTime = DateUtil.timeStamp2Date(casePDF.getUploadRecordList().get(i - 1).getUploadTime());
|
|
|
+ datas[i][2] = uploadTime;
|
|
|
+ if (casePDF.getScoreMap().get(casePDF.getUploadRecordList().get(i - 1).getId()) != null) {
|
|
|
+ datas[i][1] = casePDF.getScoreMap().get(casePDF.getUploadRecordList().get(i - 1).getId());
|
|
|
+ } else {
|
|
|
+ datas[i][1] = "暂无分数信息";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int j = 0; j < columnWidth; j++) {
|
|
|
+
|
|
|
+ PdfPCell pdfCell = new PdfPCell(); //表格的单元格
|
|
|
+ if (j == 0 && i != 0) {
|
|
|
+ datas[i][j] = String.format("第%d次提交", i);
|
|
|
+ }
|
|
|
+ System.out.println(datas[i][j] + "hhhhhhhhhh");
|
|
|
+ Paragraph submit_paragraph = new Paragraph((datas[i][j]).toString(), getPdfChineseFont());
|
|
|
+ pdfCell.setPhrase(submit_paragraph);
|
|
|
+ table.addCell(pdfCell);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return table;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public void generateReportCase(ReportCaseVO vo, Document document) throws Exception {
|
|
|
PdfPTable table = new PdfPTable(2);
|
|
@@ -419,7 +557,7 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
|
|
|
private ReportForMongoDTO getReport(long examId, long participantId, long caseId) throws Exception {
|
|
|
CaseTake caseTake = CaseTakeService.getCaseTake(examId, caseId, participantId);
|
|
|
- if(caseTake==null){
|
|
|
+ if (caseTake == null) {
|
|
|
return null;
|
|
|
}
|
|
|
List<ReportForMongoDTO> reports = mongoAPIService.getReportFromMongo(caseTake.getId());
|
|
@@ -473,7 +611,7 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
CaseExtends caseExtends = caseService.getCaseExtendsById(caseId);
|
|
|
casePDF.setCaseName(caseExtends.getName());
|
|
|
casePDF.setDescription(caseExtends.getDescription());
|
|
|
- getCaseContent(caseExtends,casePDF);
|
|
|
+ getCaseContent(caseExtends, casePDF);
|
|
|
casePDFs.add(casePDF);
|
|
|
}
|
|
|
examPDF.setCasePDFs(casePDFs);
|
|
@@ -550,10 +688,11 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
over.addImage(img);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
ps.setFormFlattening(true);
|
|
|
ps.close();
|
|
|
- String md5_fileName=MD5Util.string2MD5(fileName);
|
|
|
- FileOutputStream fos = new FileOutputStream(FileUtils.getUserDirectory() + md5_fileName+ "1.pdf");
|
|
|
+ String md5_fileName = MD5Util.string2MD5(fileName);
|
|
|
+ FileOutputStream fos = new FileOutputStream(FileUtils.getUserDirectory() + md5_fileName + "1.pdf");
|
|
|
Document doc = new Document(PageSize.A4);
|
|
|
PdfCopy copy = new PdfCopy(doc, fos);
|
|
|
doc.open();
|
|
@@ -577,15 +716,15 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
paragraph.setLeading(22f);
|
|
|
document.add(paragraph);
|
|
|
document.add(new Paragraph("案例名称:" + casePDF.getCaseName(), PdfUtils.getContent()));
|
|
|
- document.add(new Paragraph("案例描述:" + (casePDF.getDescription()==null?"无":casePDF.getDescription()), PdfUtils.getContent()));
|
|
|
- if(casePDF.getContent()!=null){
|
|
|
+ document.add(new Paragraph("案例描述:" + (casePDF.getDescription() == null ? "无" : casePDF.getDescription()), PdfUtils.getContent()));
|
|
|
+ if (casePDF.getContent() != null) {
|
|
|
document.add(new Paragraph("案例内容:" + casePDF.getContent(), PdfUtils.getContent()));
|
|
|
- }else if(casePDF.getCodes()!=null&&casePDF.getCodes().size()>0){
|
|
|
+ } else if (casePDF.getCodes() != null && casePDF.getCodes().size() > 0) {
|
|
|
document.add(new Paragraph("案例内容:", PdfUtils.getContent()));
|
|
|
- List<String> codes=casePDF.getCodes();
|
|
|
- for(String str:codes){
|
|
|
- document.add(new Paragraph("类"+(codes.indexOf(str)+1), PdfUtils.getContent()));
|
|
|
- Paragraph paragraph1=new Paragraph(str,PdfUtils.getCode());
|
|
|
+ List<String> codes = casePDF.getCodes();
|
|
|
+ for (String str : codes) {
|
|
|
+ document.add(new Paragraph("类" + (codes.indexOf(str) + 1), PdfUtils.getContent()));
|
|
|
+ Paragraph paragraph1 = new Paragraph(str, PdfUtils.getCode());
|
|
|
paragraph.setIndentationLeft(25);
|
|
|
document.add(paragraph1);
|
|
|
}
|
|
@@ -599,15 +738,15 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
for (Long participantId : participantIds) {
|
|
|
ReportPDF reportPDF;
|
|
|
try {
|
|
|
- reportPDF = this.getReportPDF(examId, participantId );
|
|
|
- }catch (Exception e){
|
|
|
+ reportPDF = this.getReportPDF(examId, participantId);
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
continue;
|
|
|
}
|
|
|
if (casePDFS != null) {
|
|
|
document.newPage();
|
|
|
Paragraph paragraph1 = new Paragraph("编号:" + participantIds.indexOf(participantId) + " 考生姓名:" + userDao.findOne(participantId).getName()
|
|
|
- + " 考试成绩:" + reportPDF.getScore() + " , " + getRank(examId, participantId),PdfUtils.getTitle());
|
|
|
+ + " 考试成绩:" + reportPDF.getScore() + " , " + getRank(examId, participantId), PdfUtils.getTitle());
|
|
|
document.add(paragraph1);
|
|
|
document.add(new Paragraph(18f, " "));
|
|
|
for (CasePDF casePDF : reportPDF.getCases()) {
|
|
@@ -616,42 +755,41 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
document.add(paragraph);
|
|
|
|
|
|
document.add(new Paragraph("案例名称:" + casePDF.getCaseName() + " 案例分数:" + casePDF.getCaseScore(), PdfUtils.getContent()));
|
|
|
- document.add(new Paragraph("案例描述:" + (casePDF.getDescription()==null?"无":casePDF.getDescription()), PdfUtils.getContent()));
|
|
|
+ document.add(new Paragraph("案例描述:" + (casePDF.getDescription() == null ? "无" : casePDF.getDescription()), PdfUtils.getContent()));
|
|
|
//生成本题学生的提交内容
|
|
|
- generateCase(casePDF,document);
|
|
|
+ generateCase(casePDF, document);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
document.close();
|
|
|
- String[] files = {FileUtils.getUserDirectory() + "/" + md5_fileName+"1.pdf", FileUtils.getUserDirectory() + "/" +md5_fileName+ "2.pdf"};
|
|
|
+ String[] files = {FileUtils.getUserDirectory() + "/" + md5_fileName + "1.pdf", FileUtils.getUserDirectory() + "/" + md5_fileName + "2.pdf"};
|
|
|
mergePDFS(files, SAVE_PATH + PDF_PATH + "/" + fileName + ".pdf");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
* @param pdfUrl pdf路径
|
|
|
- * @param type 图片保存格式 png/jpg ...
|
|
|
+ * @param type 图片保存格式 png/jpg ...
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
public String pdf2imageList(String pdfUrl, String type) {
|
|
|
// 将pdf装图片 并且自定义图片得格式大小
|
|
|
File file = new File(pdfUrl);
|
|
|
- return (String)this.pdf2imageList(file,type).get("url");
|
|
|
+ return (String) this.pdf2imageList(file, type).get("url");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Map<String, Object> pdf2imageList(File file, String type) {
|
|
|
- String tmpPath = System.getProperty("java.io.tmpdir")+File.separator;
|
|
|
+ String tmpPath = System.getProperty("java.io.tmpdir") + File.separator;
|
|
|
String result_path = tmpPath;
|
|
|
- String parentPath = "mooctest." + UUID.randomUUID().toString().replaceAll("-","");
|
|
|
+ String parentPath = "mooctest." + UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
String imagesParentPath = result_path + parentPath + File.separator;
|
|
|
File dir = new File(imagesParentPath);
|
|
|
- if(!dir.exists()) {
|
|
|
+ if (!dir.exists()) {
|
|
|
dir.mkdirs();
|
|
|
}
|
|
|
HashMap<String, Object> result = new HashMap<>();
|
|
|
- result.put("url",imagesParentPath);
|
|
|
+ result.put("url", imagesParentPath);
|
|
|
try {
|
|
|
PDDocument doc = PDDocument.load(file);
|
|
|
PDFRenderer renderer = new PDFRenderer(doc);
|
|
@@ -660,7 +798,7 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
for (int i = 0; i < pageCount; i++) {
|
|
|
BufferedImage image = renderer.renderImageWithDPI(i, 80); // Windows native DPI
|
|
|
// BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
|
|
|
- ImageIO.write(image, type, new File(imagesParentPath + (i+1) +"."+type));
|
|
|
+ ImageIO.write(image, type, new File(imagesParentPath + (i + 1) + "." + type));
|
|
|
}
|
|
|
doc.close();
|
|
|
} catch (IOException e) {
|
|
@@ -669,16 +807,18 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- private void generateCase(CasePDF casePDF, Document document) throws Exception{
|
|
|
+ private void generateCase(CasePDF casePDF, Document document) throws Exception {
|
|
|
ReportForMongoDTO report = casePDF.getReport();
|
|
|
//如果case的answerWay为report,则将测试用例和缺陷报告写入pdf中
|
|
|
if (report != null) {
|
|
|
if (report.getBugVOS() == null && report.getReportCases() == null) {
|
|
|
document.add(new Paragraph("考试结果:请下载zip文件包查看报告中的测试脚本等附件。", PdfUtils.getContent()));
|
|
|
+
|
|
|
} else {
|
|
|
document.add(new Paragraph("考试结果:", PdfUtils.getContent()));
|
|
|
}
|
|
|
document.add(new Paragraph("测试用例:", PdfUtils.getContent()));
|
|
|
+
|
|
|
List<ReportCaseVO> caseVOS = report.getReportCases();
|
|
|
if (caseVOS != null) {
|
|
|
for (ReportCaseVO vo : caseVOS) {
|
|
@@ -692,11 +832,11 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
generateBug(dto, document);
|
|
|
}
|
|
|
}
|
|
|
- }else if(casePDF.getAnswerCode()!=null&&casePDF.getAnswerCode().size()>0){
|
|
|
+ } else if (casePDF.getAnswerCode() != null && casePDF.getAnswerCode().size() > 0) {
|
|
|
document.add(new Paragraph("考试结果:", PdfUtils.getContent()));
|
|
|
- for(String str:casePDF.getAnswerCode()){
|
|
|
- document.add(new Paragraph("类"+(casePDF.getAnswerCode().indexOf(str)+1)+":",PdfUtils.getContent()));
|
|
|
- Paragraph paragraph=new Paragraph(str,PdfUtils.getCode());
|
|
|
+ for (String str : casePDF.getAnswerCode()) {
|
|
|
+ document.add(new Paragraph("类" + (casePDF.getAnswerCode().indexOf(str) + 1) + ":", PdfUtils.getContent()));
|
|
|
+ Paragraph paragraph = new Paragraph(str, PdfUtils.getCode());
|
|
|
paragraph.setIndentationLeft(25);
|
|
|
document.add(paragraph);
|
|
|
}
|
|
@@ -717,7 +857,7 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
String path;
|
|
|
try {
|
|
|
path = downloadService.download(downloadUrl);
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}
|
|
@@ -752,31 +892,31 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
, projectDirectory.getAbsolutePath()), e);
|
|
|
return null;
|
|
|
}
|
|
|
- List<String> codes=new ArrayList<>();
|
|
|
- getJavaCode(new File(projectDirectory.getPath()),codes);
|
|
|
+ List<String> codes = new ArrayList<>();
|
|
|
+ getJavaCode(new File(projectDirectory.getPath()), codes);
|
|
|
FileUtils.deleteQuietly(projectDirectory);
|
|
|
FileUtils.deleteQuietly(extractDirectory);
|
|
|
return codes;
|
|
|
}
|
|
|
|
|
|
- private void getJavaCode(File projectFile,List<String> codes){
|
|
|
- File[] files=projectFile.listFiles();
|
|
|
- if(files==null||files.length==0)
|
|
|
+ private void getJavaCode(File projectFile, List<String> codes) {
|
|
|
+ File[] files = projectFile.listFiles();
|
|
|
+ if (files == null || files.length == 0)
|
|
|
return;
|
|
|
- for(File file:files){
|
|
|
- if(file.isDirectory()){
|
|
|
- getJavaCode(file,codes);
|
|
|
+ for (File file : files) {
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ getJavaCode(file, codes);
|
|
|
}
|
|
|
- if(FilenameUtils.getExtension(file.getName()).equals("java")){
|
|
|
- StringBuilder stringBuilder=new StringBuilder();
|
|
|
- try{
|
|
|
+ if (FilenameUtils.getExtension(file.getName()).equals("java")) {
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ try {
|
|
|
BufferedReader br = new BufferedReader(new FileReader(file));
|
|
|
String s;
|
|
|
- while((s = br.readLine())!=null){
|
|
|
- stringBuilder.append(System.lineSeparator()+s.replaceAll("\t"," "));
|
|
|
+ while ((s = br.readLine()) != null) {
|
|
|
+ stringBuilder.append(System.lineSeparator() + s.replaceAll("\t", " "));
|
|
|
}
|
|
|
br.close();
|
|
|
- }catch(Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
continue;
|
|
|
}
|
|
@@ -785,15 +925,15 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private List<String> getTargetCode(String downloadUrl){
|
|
|
+ private List<String> getTargetCode(String downloadUrl) {
|
|
|
String path;
|
|
|
try {
|
|
|
path = downloadService.download(downloadUrl);
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}
|
|
|
- File file=new File(path);
|
|
|
+ File file = new File(path);
|
|
|
|
|
|
try {
|
|
|
ZipFile zipFile = new ZipFile(path);
|
|
@@ -803,19 +943,19 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
file.getAbsolutePath()), e);
|
|
|
return null;
|
|
|
}
|
|
|
- List<String> strs=new ArrayList<>();
|
|
|
- getJavaCode(new File(FileUtils.getUserDirectory()+"/"+FilenameUtils.removeExtension(file.getName())+"/src"),strs);
|
|
|
+ List<String> strs = new ArrayList<>();
|
|
|
+ getJavaCode(new File(FileUtils.getUserDirectory() + "/" + FilenameUtils.removeExtension(file.getName()) + "/src"), strs);
|
|
|
FileUtils.deleteQuietly(file);
|
|
|
- FileUtils.deleteQuietly(new File(FileUtils.getUserDirectory()+"/"+FilenameUtils.removeExtension(file.getName())));
|
|
|
+ FileUtils.deleteQuietly(new File(FileUtils.getUserDirectory() + "/" + FilenameUtils.removeExtension(file.getName())));
|
|
|
return strs;
|
|
|
}
|
|
|
|
|
|
- private void getCaseContent(CaseExtends caseExtends,CasePDF pdf){
|
|
|
- JSONObject object=new JSONObject(caseExtends.getProperties());
|
|
|
- if(caseExtends.getAnswerWay()==AnswerWayConstants.DEV_ECLIPSE){
|
|
|
- Target target =targetService.getAppById(caseExtends.getAppId());
|
|
|
+ private void getCaseContent(CaseExtends caseExtends, CasePDF pdf) {
|
|
|
+ JSONObject object = new JSONObject(caseExtends.getProperties());
|
|
|
+ if (caseExtends.getAnswerWay() == AnswerWayConstants.DEV_ECLIPSE) {
|
|
|
+ Target target = targetService.getAppById(caseExtends.getAppId());
|
|
|
pdf.setCodes(getTargetCode(target.getUrl()));
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
if (object.has("require")) {
|
|
|
String path;
|
|
|
try {
|
|
@@ -839,4 +979,12 @@ public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public Font getPdfChineseFont() throws IOException, DocumentException {
|
|
|
+ BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
|
|
|
+ BaseFont.NOT_EMBEDDED);
|
|
|
+ Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
|
|
|
+ return fontChinese;
|
|
|
+
|
|
|
+ }
|
|
|
}
|