2020import org .labkey .api .data .ExcelWriter ;
2121import org .labkey .api .data .RenderContext ;
2222import org .labkey .api .data .Results ;
23+ import org .labkey .api .data .RuntimeSQLException ;
2324import org .labkey .api .data .Table ;
2425import org .labkey .api .query .FieldKey ;
2526import org .labkey .api .query .QueryParam ;
2627import org .labkey .api .query .QueryService ;
2728import org .labkey .api .query .QuerySettings ;
2829import org .labkey .api .query .UserSchema ;
30+ import org .labkey .api .query .ValidationException ;
2931import org .labkey .api .reports .Report ;
3032import org .labkey .api .reports .report .AbstractReport ;
3133import org .labkey .api .reports .report .ReportDescriptor ;
3234import org .labkey .api .reports .report .ReportUrls ;
3335import org .labkey .api .reports .report .view .ReportQueryView ;
3436import org .labkey .api .util .PageFlowUtil ;
37+ import org .labkey .api .util .ResultSetUtil ;
3538import org .labkey .api .view .ActionURL ;
3639import org .labkey .api .view .DataView ;
3740import org .labkey .api .view .HttpView ;
3841import org .labkey .api .view .Stats ;
3942import org .labkey .api .view .ViewContext ;
4043
44+ import java .sql .SQLException ;
4145import java .util .LinkedHashSet ;
4246import java .util .Set ;
4347
@@ -67,7 +71,7 @@ public String getDescriptorType()
6771 return CrosstabReportDescriptor .TYPE ;
6872 }
6973
70- protected ReportQueryView createQueryView (ViewContext context , ReportDescriptor descriptor ) throws Exception
74+ protected ReportQueryView createQueryView (ViewContext context , ReportDescriptor descriptor )
7175 {
7276 final String queryName = descriptor .getProperty (QueryParam .queryName .toString ());
7377 final String viewName = descriptor .getProperty (QueryParam .viewName .toString ());
@@ -111,7 +115,11 @@ public HttpView<?> renderReport(ViewContext context)
111115 return new CrosstabView (crosstab , exportAction );
112116 }
113117 }
114- catch (Exception e )
118+ catch (SQLException e )
119+ {
120+ throw new RuntimeSQLException (e );
121+ }
122+ catch (ValidationException e )
115123 {
116124 throw new RuntimeException (e );
117125 }
@@ -124,7 +132,7 @@ public HttpView<?> renderReport(ViewContext context)
124132 }
125133
126134 @ Override
127- public Results generateResults (ViewContext context , boolean allowAsyncQuery ) throws Exception
135+ public Results generateResults (ViewContext context , boolean allowAsyncQuery ) throws SQLException , ValidationException
128136 {
129137 ReportQueryView view = createQueryView (context , getDescriptor ());
130138 validateQueryView (view );
@@ -141,37 +149,33 @@ public Results generateResults(ViewContext context, boolean allowAsyncQuery) thr
141149 return null ;
142150 }
143151
144- protected Crosstab createCrosstab (ViewContext context , boolean allowAsyncQuery ) throws Exception
152+ protected Crosstab createCrosstab (ViewContext context , boolean allowAsyncQuery ) throws SQLException , ValidationException
145153 {
146154 CrosstabReportDescriptor descriptor = (CrosstabReportDescriptor )getDescriptor ();
147155 Results results = generateResults (context , allowAsyncQuery );
148156 if (results != null )
149157 {
150- FieldKey rowFieldKey = FieldKey .decode (descriptor .getProperty ("rowField" ));
151- FieldKey colFieldKey = FieldKey .decode (descriptor .getProperty ("colField" ));
152- FieldKey statFieldKey = FieldKey .decode (descriptor .getProperty ("statField" ));
158+ try
159+ {
160+ FieldKey rowFieldKey = FieldKey .decode (descriptor .getProperty ("rowField" ));
161+ FieldKey colFieldKey = FieldKey .decode (descriptor .getProperty ("colField" ));
162+ FieldKey statFieldKey = FieldKey .decode (descriptor .getProperty ("statField" ));
153163
154- Set <Stats .StatDefinition > statSet = new LinkedHashSet <>();
155- for (String stat : descriptor .getStats ())
164+ Set <Stats .StatDefinition > statSet = new LinkedHashSet <>();
165+ for (String stat : descriptor .getStats ())
166+ {
167+ try
168+ {
169+ statSet .add (Stats .getStatFromString (stat ));
170+ }
171+ catch (IllegalArgumentException _) {}
172+ }
173+ return new Crosstab (results , rowFieldKey , colFieldKey , statFieldKey , statSet );
174+ }
175+ finally
156176 {
157- if ("Count" .equals (stat ))
158- statSet .add (Stats .COUNT );
159- else if ("Sum" .equals (stat ))
160- statSet .add (Stats .SUM );
161- else if ("Mean" .equals (stat ))
162- statSet .add (Stats .MEAN );
163- else if ("Min" .equals (stat ))
164- statSet .add (Stats .MIN );
165- else if ("Max" .equals (stat ))
166- statSet .add (Stats .MAX );
167- else if ("StdDev" .equals (stat ))
168- statSet .add (Stats .STDDEV );
169- else if ("Var" .equals (stat ))
170- statSet .add (Stats .VAR );
171- else if ("Median" .equals (stat ))
172- statSet .add (Stats .MEDIAN );
177+ ResultSetUtil .close (results );
173178 }
174- return new Crosstab (results , rowFieldKey , colFieldKey , statFieldKey , statSet );
175179 }
176180 return null ;
177181 }
0 commit comments