|
@@ -15,16 +15,21 @@ import cn.iselab.mooctest.site.web.data.forMongo.BugDTO;
|
|
|
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.util.PdfUtils;
|
|
|
import com.itextpdf.text.*;
|
|
|
import com.itextpdf.text.pdf.*;
|
|
|
+import net.lingala.zip4j.core.ZipFile;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
+import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
+import org.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
+import java.io.*;
|
|
|
+import java.text.DecimalFormat;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
@@ -38,7 +43,7 @@ import java.util.stream.Collectors;
|
|
|
* @Modified By:
|
|
|
*/
|
|
|
@Service
|
|
|
-public class PdfServiceImpl implements PdfService {
|
|
|
+public class PdfServiceImpl extends BaseService implements PdfService {
|
|
|
|
|
|
private String templatePdfPath = FileUtils.getUserDirectory() + "/学生测试报告文档3.pdf";
|
|
|
private String templatePdfPathForGroup = FileUtils.getUserDirectory() + "/班级测试报告文档修改.pdf";
|
|
@@ -46,6 +51,8 @@ public class PdfServiceImpl implements PdfService {
|
|
|
private String SAVE_PATH = "/var/www/download/";
|
|
|
private String PDF_PATH = "pdf/";
|
|
|
|
|
|
+ @Autowired
|
|
|
+ PluginLogic pluginLogic;
|
|
|
|
|
|
@Autowired
|
|
|
TaskService taskService;
|
|
@@ -75,6 +82,12 @@ public class PdfServiceImpl implements PdfService {
|
|
|
Exam2CaseService exam2CaseService;
|
|
|
|
|
|
@Autowired
|
|
|
+ OSSService ossService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AppService appService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
private Task2GroupDao task2GroupDao;
|
|
|
|
|
|
@Autowired
|
|
@@ -142,11 +155,16 @@ public class PdfServiceImpl implements PdfService {
|
|
|
pdf.setDescription(caseExtends.getDescription());
|
|
|
for (AssignedCase assignedCase : cases) {
|
|
|
if (assignedCase.getCaseId() == caseId) {
|
|
|
- pdf.setCaseScore(String.valueOf(assignedCase.getMaxScore()));
|
|
|
+ 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());
|
|
|
+ pdf.setAnswerCode(this.getResultCode(downloadUrl));
|
|
|
+ }
|
|
|
pdfs.add(pdf);
|
|
|
}
|
|
|
reportPDF.setCases(pdfs);
|
|
@@ -220,33 +238,9 @@ public class PdfServiceImpl implements PdfService {
|
|
|
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()));
|
|
|
-
|
|
|
- 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) {
|
|
|
- generateReportCase(vo, document);
|
|
|
- }
|
|
|
- }
|
|
|
- List<BugDTO> dtos = report.getBugVOS();
|
|
|
- if (dtos != null) {
|
|
|
- document.add(new Paragraph("缺陷报告:", PdfUtils.getContent()));
|
|
|
- for (BugDTO dto : report.getBugVOS()) {
|
|
|
- generateBug(dto, document);
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- document.add(new Paragraph("考试结果:请下载zip文件包查看报告中的测试脚本等附件。", PdfUtils.getContent()));
|
|
|
- }
|
|
|
+ document.add(new Paragraph("案例描述:" + (casePDF.getDescription()!=null?casePDF.getDescription():"无"), PdfUtils.getContent()));
|
|
|
+ //生成本题学生的提交内容
|
|
|
+ generateCase(casePDF,document);
|
|
|
}
|
|
|
}
|
|
|
document.close();
|
|
@@ -456,7 +450,7 @@ public class PdfServiceImpl implements PdfService {
|
|
|
CaseExtends caseExtends = caseService.getCaseExtendsById(caseId);
|
|
|
casePDF.setCaseName(caseExtends.getName());
|
|
|
casePDF.setDescription(caseExtends.getDescription());
|
|
|
- casePDF.setContent(caseExtends.getContent());
|
|
|
+ getCaseContent(caseExtends,casePDF);
|
|
|
casePDFs.add(casePDF);
|
|
|
}
|
|
|
examPDF.setCasePDFs(casePDFs);
|
|
@@ -466,7 +460,7 @@ public class PdfServiceImpl implements PdfService {
|
|
|
|
|
|
@Override
|
|
|
public void generateExamPDF(ExamPDF examPDF, String fileName, Long examId, Long groupId) throws Exception {
|
|
|
-
|
|
|
+ //根据模板生成第一页统计部分
|
|
|
PdfReader reader = new PdfReader(templatePdfPathForGroup);
|
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
@@ -551,7 +545,7 @@ public class PdfServiceImpl implements PdfService {
|
|
|
FileOutputStream out = new FileOutputStream(FileUtils.getUserDirectory() + "/" + "2.pdf");
|
|
|
PdfWriter writer = PdfWriter.getInstance(document, out);
|
|
|
document.open();
|
|
|
-
|
|
|
+ //生成本次考试的案例介绍部分
|
|
|
List<CasePDF> casePDFS = examPDF.getCasePDFs();
|
|
|
if (casePDFS != null) {
|
|
|
for (CasePDF casePDF : casePDFS) {
|
|
@@ -560,11 +554,20 @@ public class PdfServiceImpl implements PdfService {
|
|
|
paragraph.setLeading(22f);
|
|
|
document.add(paragraph);
|
|
|
document.add(new Paragraph("案例名称:" + casePDF.getCaseName(), PdfUtils.getContent()));
|
|
|
- document.add(new Paragraph("案例描述:" + casePDF.getDescription(), PdfUtils.getContent()));
|
|
|
- document.add(new Paragraph("案例内容:" + casePDF.getContent()==null?"无":casePDF.getContent(), PdfUtils.getContent()));
|
|
|
+ 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){
|
|
|
+ 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()));
|
|
|
+ document.add(new Paragraph(str,PdfUtils.getContent()));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+ //生成每个学生的结果部分
|
|
|
List<Long> participantIds = group2WorkerDao.findByGroupId(groupId).stream().
|
|
|
map(group2Worker -> group2Worker.getParticipantId()).collect(Collectors.toList());
|
|
|
|
|
@@ -588,33 +591,9 @@ public class PdfServiceImpl 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()));
|
|
|
-
|
|
|
- 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) {
|
|
|
- generateReportCase(vo, document);
|
|
|
- }
|
|
|
- }
|
|
|
- List<BugDTO> dtos = report.getBugVOS();
|
|
|
- if (dtos != null) {
|
|
|
- document.add(new Paragraph("缺陷报告:", PdfUtils.getContent()));
|
|
|
- for (BugDTO dto : report.getBugVOS()) {
|
|
|
- generateBug(dto, document);
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- document.add(new Paragraph("考试结果:请下载zip文件包查看报告中的测试脚本等附件。", PdfUtils.getContent()));
|
|
|
- }
|
|
|
+ document.add(new Paragraph("案例描述:" + (casePDF.getDescription()==null?"无":casePDF.getDescription()), PdfUtils.getContent()));
|
|
|
+ //生成本题学生的提交内容
|
|
|
+ generateCase(casePDF,document);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -623,6 +602,40 @@ public class PdfServiceImpl implements PdfService {
|
|
|
mergePDFS(files, SAVE_PATH + PDF_PATH + "/" + fileName + ".pdf");
|
|
|
}
|
|
|
|
|
|
+ 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) {
|
|
|
+ generateReportCase(vo, document);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<BugDTO> dtos = report.getBugVOS();
|
|
|
+ if (dtos != null) {
|
|
|
+ document.add(new Paragraph("缺陷报告:", PdfUtils.getContent()));
|
|
|
+ for (BugDTO dto : report.getBugVOS()) {
|
|
|
+ generateBug(dto, document);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }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()));
|
|
|
+ document.add(new Paragraph(str,PdfUtils.getContent()));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ document.add(new Paragraph("考试结果:请下载zip文件包查看报告中的测试脚本等附件。", PdfUtils.getContent()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private Integer getRank(Long examId, Long participantId) {
|
|
|
List<AssignedTask> assignedTasks = assignedTaskService.getAssignedTasks(examId);
|
|
|
List<Double> scores = assignedTasks.stream().map(assignedTask -> assignedTask.getScore()).collect(Collectors.toList());
|
|
@@ -630,4 +643,132 @@ public class PdfServiceImpl implements PdfService {
|
|
|
AssignedTask assignedTask = assignedTaskService.getAssignedTask(examId, participantId);
|
|
|
return scores.indexOf(assignedTask.getScore());
|
|
|
}
|
|
|
+
|
|
|
+ private List<String> getResultCode(String downloadUrl) {
|
|
|
+ String path;
|
|
|
+ try {
|
|
|
+ path = ossService.download(downloadUrl);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ File extractDirectory = new File(FilenameUtils.removeExtension(path));
|
|
|
+
|
|
|
+ try {
|
|
|
+ ZipFile zipFile = new ZipFile(path);
|
|
|
+ FileUtils.forceMkdir(extractDirectory);
|
|
|
+ zipFile.extractAll(extractDirectory.getPath());
|
|
|
+ FileUtils.deleteQuietly(new File(path));
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error(String.format("Extract zip[%s] to path[%s] fail", path,
|
|
|
+ extractDirectory.getAbsolutePath()), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ File[] files = extractDirectory.listFiles();
|
|
|
+
|
|
|
+ File latestFile = null;
|
|
|
+ for (File file : files) {
|
|
|
+ if (latestFile == null || file.getName().compareTo(latestFile.getName()) > 0) {
|
|
|
+ latestFile = file;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ File projectDirectory = new File(FilenameUtils.removeExtension(latestFile.getAbsolutePath()));
|
|
|
+
|
|
|
+ try {
|
|
|
+ ZipFile zipFile = new ZipFile(latestFile);
|
|
|
+ FileUtils.forceMkdir(projectDirectory);
|
|
|
+ zipFile.extractAll(projectDirectory.getPath());
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error(String.format("Extract embedded zip[%s] to path[%s] fail", projectDirectory.getAbsoluteFile()
|
|
|
+ , projectDirectory.getAbsolutePath()), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ 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)
|
|
|
+ return;
|
|
|
+ for(File file:files){
|
|
|
+ if(file.isDirectory()){
|
|
|
+ getJavaCode(file,codes);
|
|
|
+ }
|
|
|
+ if(file.getName().contains(".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);
|
|
|
+ }
|
|
|
+ br.close();
|
|
|
+ }catch(Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ codes.add(stringBuilder.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> getTargetCode(String downloadUrl){
|
|
|
+ String path;
|
|
|
+ try {
|
|
|
+ path = ossService.download(downloadUrl);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ File file=new File(path);
|
|
|
+
|
|
|
+ try {
|
|
|
+ ZipFile zipFile = new ZipFile(path);
|
|
|
+ zipFile.extractAll(FileUtils.getUserDirectoryPath());
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error(String.format("Extract zip[%s] to path[%s] fail", path,
|
|
|
+ file.getAbsolutePath()), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ 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())));
|
|
|
+ return strs;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getCaseContent(CaseExtends caseExtends,CasePDF pdf){
|
|
|
+ JSONObject object=new JSONObject(caseExtends.getProperties());
|
|
|
+ if(caseExtends.getAnswerWay()==AnswerWayConstants.DEV_ECLIPSE){
|
|
|
+ App app=appService.getAppById(caseExtends.getAppId());
|
|
|
+ pdf.setCodes(getTargetCode(app.getUrl()));
|
|
|
+ }else {
|
|
|
+ if (object.has("require")) {
|
|
|
+ String path;
|
|
|
+ try {
|
|
|
+ path = ossService.download(object.getString("require"));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ File file = new File(path);
|
|
|
+ if (file.getName().contains(".doc") || file.getName().contains(".docx")) {
|
|
|
+ try {
|
|
|
+ FileInputStream fis = new FileInputStream(file);
|
|
|
+ XWPFDocument doc = new XWPFDocument(fis);
|
|
|
+ XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
|
|
|
+ pdf.setContent(extractor.getText());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FileUtils.deleteQuietly(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|