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
@@ -64,18 +66,23 @@ public class ExportRubrics implements ActionListener {
64
66
public void processAction (ActionEvent ae ) throws AbortProcessingException {
65
67
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
66
68
69
+ QuestionScoresBean bean = (QuestionScoresBean ) ContextUtil .lookupBean ("questionScores" );
70
+ TotalScoresBean tBean = (TotalScoresBean ) ContextUtil .lookupBean ("totalScores" );
71
+ String templateFilename = rb .getString ("question" ) + "_" + bean .getItemName ();
72
+ String toolId = "sakai.samigo" ;
73
+
74
+ ZipOutputStream out = null ;
75
+
67
76
try {
68
- ZipOutputStream out = new ZipOutputStream (baos );
77
+ out = new ZipOutputStream (baos );
69
78
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
79
List <AgentResults > agents = (List <AgentResults >) bean .getAgents ();
80
+ List <User > users = userDirectoryService .getUsersByEids (agents .stream ()
81
+ .map (AgentResults ::getAgentEid )
82
+ .collect (Collectors .toList ()));
75
83
76
- for (AgentResults ar : agents ) {
84
+ for (User user : users ) {
77
85
String itemId = "pub." + bean .getPublishedId () + "." + ContextUtil .lookupParam ("itemId" );
78
- User user = userDirectoryService .getUserByEid (ar .getAgentEid ());
79
86
80
87
Optional <ToolItemRubricAssociation > optAssociation = associationRepository .findByToolIdAndItemId (toolId , itemId );
81
88
long rubricId = optAssociation .isPresent ()? optAssociation .get ().getRubric ().getId () : 0l ;
@@ -88,23 +95,39 @@ public void processAction(ActionEvent ae) throws AbortProcessingException {
88
95
out .write (pdf );
89
96
out .closeEntry ();
90
97
}
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 ();
106
98
} catch (Exception e ) {
107
99
log .error (e .toString (), e );
100
+ } finally {
101
+ if (out != null ) {
102
+ try {
103
+ out .finish ();
104
+ out .flush ();
105
+ } catch (IOException e ) {
106
+ log .error ("Error ocurred while finish ZipOutputStream: " + e .toString (), e );
107
+ }
108
+ try {
109
+ out .close ();
110
+ } catch (IOException e ) {
111
+ log .error ("Error ocurred while closing ZipOutputStream: " + e .toString (), e );
112
+ }
113
+
114
+ FacesContext faces = FacesContext .getCurrentInstance ();
115
+ HttpServletResponse response = (HttpServletResponse )faces .getExternalContext ().getResponse ();
116
+ String fileName = tBean .getAssessmentName ().replaceAll (" " , "_" ) + "_" + templateFilename ;
117
+
118
+ response .reset (); // Eliminate the added-on stuff
119
+ response .setHeader ("Content-Disposition" , "attachment; filename=\" " + fileName + "_" + rb .getString ("rubrics" ) + ".zip\" " );
120
+ response .setContentType ("application/zip" );
121
+ response .setContentLength (baos .size ());
122
+
123
+ try {
124
+ baos .writeTo (response .getOutputStream ());
125
+ } catch (IOException e ) {
126
+ log .error ("Error ocurred while writing ByteArrayOutputStream: " + e .toString (), e );
127
+ }
128
+
129
+ faces .responseComplete ();
130
+ }
108
131
}
109
132
}
110
133
}
0 commit comments