1616
1717package org .labkey .hdrl ;
1818
19+ import jakarta .servlet .http .HttpServletResponse ;
1920import org .apache .commons .beanutils .BeanUtils ;
2021import org .apache .commons .io .IOUtils ;
2122import org .json .JSONArray ;
2223import org .json .JSONException ;
2324import org .json .JSONObject ;
2425import org .labkey .api .action .ApiSimpleResponse ;
26+ import org .labkey .api .action .ApiUsageException ;
2527import org .labkey .api .action .ExportAction ;
2628import org .labkey .api .action .FormViewAction ;
2729import org .labkey .api .action .Marshal ;
2830import org .labkey .api .action .Marshaller ;
2931import org .labkey .api .action .MutatingApiAction ;
3032import org .labkey .api .action .ReadOnlyApiAction ;
3133import org .labkey .api .action .SimpleApiJsonForm ;
32- import org .labkey .api .action .SimpleErrorView ;
3334import org .labkey .api .action .SimpleViewAction ;
3435import org .labkey .api .action .SpringActionController ;
3536import org .labkey .api .admin .AdminUrls ;
7475import org .springframework .validation .Errors ;
7576import org .springframework .web .servlet .ModelAndView ;
7677
77- import jakarta .servlet .http .HttpServletResponse ;
7878import java .io .IOException ;
7979import java .util .ArrayList ;
8080import java .util .Collections ;
8181import java .util .Date ;
8282import java .util .List ;
8383import java .util .Map ;
84+ import java .util .Objects ;
8485
8586public class HDRLController extends SpringActionController
8687{
@@ -137,11 +138,11 @@ public static class RequestDetailsAction extends SimpleViewAction<Object>
137138 @ Override
138139 public ModelAndView getView (Object o , BindException errors )
139140 {
140- String requestId = getViewContext ().getRequest ().getParameter ("requestId" );
141- if (requestId != null )
141+ try
142142 {
143- InboundRequestBean bean = HDRLManager .get ().getInboundRequest (getUser (), getContainer (), Integer .parseInt (requestId ));
144- JspView jsp = new JspView <>("/org/labkey/hdrl/view/requestDetails.jsp" , bean );
143+ int requestId = Integer .parseInt (getViewContext ().getRequest ().getParameter ("requestId" ));
144+ InboundRequestBean bean = HDRLManager .get ().getInboundRequest (getUser (), getContainer (), requestId );
145+ JspView <?> jsp = new JspView <>("/org/labkey/hdrl/view/requestDetails.jsp" , bean );
145146 jsp .setTitle ("Test Request" );
146147
147148 UserSchema schema = QueryService .get ().getUserSchema (getUser (), getContainer (), HDRLQuerySchema .NAME );
@@ -151,10 +152,9 @@ public ModelAndView getView(Object o, BindException errors)
151152 jsp .setView ("queryView" , queryView );
152153 return jsp ;
153154 }
154- else
155+ catch ( NumberFormatException x )
155156 {
156- errors .reject ("RequestId is required" );
157- return new SimpleErrorView (errors );
157+ throw new ApiUsageException ("RequestId is required" );
158158 }
159159 }
160160
@@ -175,10 +175,18 @@ public ModelAndView getView(RequestForm form, BindException errors) throws Excep
175175 {
176176 if (form .getRequestId () != -1 )
177177 {
178- _navLabel = "Edit a Test Request" ;
179-
180178 TableSelector selector = new TableSelector (org .labkey .hdrl .HDRLSchema .getInstance ().getTableInfoInboundRequest ());
181- BeanUtils .copyProperties (form , selector .getObject (form .getRequestId (), RequestForm .class ));
179+ RequestForm object = selector .getObject (form .getRequestId (), RequestForm .class );
180+ if (object != null )
181+ {
182+ _navLabel = "Edit a Test Request" ;
183+ BeanUtils .copyProperties (form , object );
184+ }
185+ else
186+ {
187+ form .setTestTypeId (1 ); // default to first test type
188+ errors .reject (ERROR_MSG , "Request with id " + form .getRequestId () + " not found." );
189+ }
182190 }
183191 else
184192 {
@@ -265,7 +273,11 @@ public static class VerifySpecimenAction extends ReadOnlyApiAction<VerifyForm>
265273 public Object execute (VerifyForm form , BindException errors )
266274 {
267275 ApiSimpleResponse response = new ApiSimpleResponse ();
268- JSONArray rows = form .getJsonObject ().getJSONArray ("rows" );
276+ JSONArray rows = Objects .requireNonNullElseGet (form .getJsonObject (), JSONObject ::new ).optJSONArray ("rows" );
277+ if (rows == null )
278+ {
279+ throw new ApiUsageException ("rows not provided." );
280+ }
269281 List <Map <String , Object >> rowsToValidate = new ArrayList <>();
270282
271283 for (int idx = 0 ; idx < rows .length (); ++idx )
@@ -277,7 +289,7 @@ public Object execute(VerifyForm form, BindException errors)
277289 }
278290 catch (JSONException x )
279291 {
280- throw new IllegalArgumentException ("rows[" + idx + "] is not an object." );
292+ throw new ApiUsageException ("rows[" + idx + "] is not an object." );
281293 }
282294 if (null != jsonObj )
283295 {
0 commit comments