|
23 | 23 |
|
24 | 24 | import java.io.ByteArrayOutputStream;
|
25 | 25 | import java.io.File;
|
| 26 | +import java.io.IOException; |
26 | 27 | import java.util.List;
|
27 | 28 | import java.util.Optional;
|
| 29 | +import java.util.stream.Collectors; |
28 | 30 | import java.util.zip.ZipEntry;
|
29 | 31 | import java.util.zip.ZipOutputStream;
|
30 | 32 |
|
@@ -60,51 +62,50 @@ public class ExportRubrics implements ActionListener {
|
60 | 62 | * Standard process action method.
|
61 | 63 | * @param ae ActionEvent
|
62 | 64 | * @throws AbortProcessingException
|
63 |
| - */ |
| 65 | + */ |
64 | 66 | public void processAction(ActionEvent ae) throws AbortProcessingException {
|
65 |
| - ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
66 |
| - |
67 |
| - try { |
68 |
| - ZipOutputStream out = new ZipOutputStream(baos); |
69 |
| - |
70 |
| - QuestionScoresBean bean = (QuestionScoresBean) ContextUtil.lookupBean("questionScores"); |
71 |
| - TotalScoresBean tBean = (TotalScoresBean) ContextUtil.lookupBean("totalScores"); |
72 |
| - String templateFilename = rb.getString("question") + "_" + bean.getItemName(); |
73 |
| - String toolId = "sakai.samigo"; |
74 |
| - List<AgentResults> agents = (List<AgentResults>) bean.getAgents(); |
75 |
| - |
76 |
| - for (AgentResults ar : agents) { |
77 |
| - String itemId = "pub." + bean.getPublishedId() + "." + ContextUtil.lookupParam("itemId"); |
78 |
| - User user = userDirectoryService.getUserByEid(ar.getAgentEid()); |
79 |
| - |
80 |
| - Optional<ToolItemRubricAssociation> optAssociation = associationRepository.findByToolIdAndItemId(toolId, itemId); |
81 |
| - long rubricId = optAssociation.isPresent()? optAssociation.get().getRubric().getId() : 0l; |
82 |
| - |
83 |
| - String evaluatedItemId = rubricsService.getRubricEvaluationObjectId(itemId, user.getId(), toolId, AgentFacade.getCurrentSiteId()); |
84 |
| - byte[] pdf = rubricsService.createPdf(AgentFacade.getCurrentSiteId(), rubricId, toolId, itemId, evaluatedItemId); |
85 |
| - final ZipEntry zipEntryPdf = new ZipEntry(user.getEid() + "_" + templateFilename + ".pdf"); |
86 |
| - |
87 |
| - out.putNextEntry(zipEntryPdf); |
88 |
| - out.write(pdf); |
89 |
| - out.closeEntry(); |
90 |
| - } |
91 |
| - |
92 |
| - out.finish(); |
93 |
| - out.close(); |
94 |
| - |
95 |
| - FacesContext faces = FacesContext.getCurrentInstance(); |
96 |
| - HttpServletResponse response = (HttpServletResponse)faces.getExternalContext().getResponse(); |
97 |
| - String fileName = tBean.getAssessmentName().replaceAll(" ", "_") + "_" + templateFilename; |
98 |
| - |
99 |
| - response.reset(); // Eliminate the added-on stuff |
100 |
| - response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "_" + rb.getString("rubrics") + ".zip\""); |
101 |
| - response.setContentType("application/zip"); |
102 |
| - response.setContentLength(baos.size()); |
103 |
| - |
104 |
| - baos.writeTo(response.getOutputStream()); |
105 |
| - faces.responseComplete(); |
| 67 | + QuestionScoresBean bean = (QuestionScoresBean) ContextUtil.lookupBean("questionScores"); |
| 68 | + TotalScoresBean tBean = (TotalScoresBean) ContextUtil.lookupBean("totalScores"); |
| 69 | + String templateFilename = rb.getString("question") + "_" + bean.getItemName(); |
| 70 | + String toolId = "sakai.samigo"; |
| 71 | + |
| 72 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 73 | + ZipOutputStream out = new ZipOutputStream(baos)) { |
| 74 | + List<AgentResults> agents = (List<AgentResults>) bean.getAgents(); |
| 75 | + List<User> users = userDirectoryService.getUsersByEids(agents.stream() |
| 76 | + .map(AgentResults::getAgentEid) |
| 77 | + .collect(Collectors.toList())); |
| 78 | + |
| 79 | + for (User user : users) { |
| 80 | + String itemId = "pub." + bean.getPublishedId() + "." + ContextUtil.lookupParam("itemId"); |
| 81 | + Optional<ToolItemRubricAssociation> optAssociation = associationRepository.findByToolIdAndItemId(toolId, itemId); |
| 82 | + long rubricId = optAssociation.isPresent() ? optAssociation.get().getRubric().getId() : 0L; |
| 83 | + String evaluatedItemId = rubricsService.getRubricEvaluationObjectId(itemId, user.getId(), toolId, AgentFacade.getCurrentSiteId()); |
| 84 | + byte[] pdf = rubricsService.createPdf(AgentFacade.getCurrentSiteId(), rubricId, toolId, itemId, evaluatedItemId); |
| 85 | + final ZipEntry zipEntryPdf = new ZipEntry(user.getEid() + "_" + templateFilename + ".pdf"); |
| 86 | + out.putNextEntry(zipEntryPdf); |
| 87 | + out.write(pdf); |
| 88 | + out.closeEntry(); |
| 89 | + } |
| 90 | + |
| 91 | + out.finish(); |
| 92 | + |
| 93 | + FacesContext faces = FacesContext.getCurrentInstance(); |
| 94 | + HttpServletResponse response = (HttpServletResponse)faces.getExternalContext().getResponse(); |
| 95 | + String fileName = tBean.getAssessmentName().replaceAll(" ", "_") + "_" + templateFilename; |
| 96 | + |
| 97 | + response.reset(); |
| 98 | + response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "_" + rb.getString("rubrics") + ".zip\""); |
| 99 | + response.setContentType("application/zip"); |
| 100 | + response.setContentLength(baos.size()); |
| 101 | + |
| 102 | + try (var outputStream = response.getOutputStream()) { |
| 103 | + baos.writeTo(outputStream); |
| 104 | + } |
| 105 | + |
| 106 | + faces.responseComplete(); |
106 | 107 | } catch (Exception e) {
|
107 |
| - log.error(e.toString(), e); |
| 108 | + log.error("Error exporting rubrics", e); |
108 | 109 | }
|
109 | 110 | }
|
110 | 111 | }
|
0 commit comments