3232import org .labkey .api .collections .CollectionUtils ;
3333import org .labkey .api .data .Container ;
3434import org .labkey .api .data .ContainerManager ;
35- import org .labkey .api .data .RuntimeSQLException ;
35+ import org .labkey .api .data .ContainerType ;
36+ import org .labkey .api .data .SimpleFilter ;
3637import org .labkey .api .data .TSVMapWriter ;
37- import org .labkey .api .data .Table ;
3838import org .labkey .api .data .TableInfo ;
3939import org .labkey .api .data .TableSelector ;
4040import org .labkey .api .exp .ChangePropertyDescriptorException ;
5151import org .labkey .api .laboratory .assay .AssayDataProvider ;
5252import org .labkey .api .laboratory .assay .AssayImportMethod ;
5353import org .labkey .api .query .BatchValidationException ;
54+ import org .labkey .api .query .FieldKey ;
55+ import org .labkey .api .query .QueryService ;
56+ import org .labkey .api .query .UserSchema ;
5457import org .labkey .api .query .ValidationException ;
5558import org .labkey .api .security .User ;
59+ import org .labkey .api .security .permissions .UpdatePermission ;
5660import org .labkey .api .util .FileUtil ;
61+ import org .labkey .api .util .PageFlowUtil ;
5762import org .labkey .api .util .Pair ;
5863import org .labkey .api .view .ActionURL ;
5964import org .labkey .api .view .ViewContext ;
6671import java .io .File ;
6772import java .io .IOException ;
6873import java .util .ArrayList ;
74+ import java .util .Arrays ;
6975import java .util .Collections ;
7076import java .util .HashMap ;
7177import java .util .List ;
@@ -125,35 +131,52 @@ public Map<String, Object> saveTemplate(User u, Container c, ExpProtocol protoco
125131 {
126132 validateTemplate (u , c , protocol , templateId , title , importMethod , json );
127133
128- TableInfo ti = LaboratorySchema .getInstance ().getSchema ().getTable (LaboratorySchema .TABLE_ASSAY_RUN_TEMPLATES );
134+ UserSchema us = QueryService .get ().getUserSchema (u , c , "laboratory" );
135+ if (us == null )
136+ {
137+ throw new IllegalStateException ("Could not find the laboratory schema in container: " + c .getPath ());
138+ }
139+
140+ TableInfo ti = us .getTable (LaboratorySchema .TABLE_ASSAY_RUN_TEMPLATES );
129141 Map <String , Object > row = new HashMap <>();
130142 row .put ("assayId" , protocol .getRowId ());
131143 row .put ("title" , title );
132144 row .put ("importMethod" , importMethod );
133145 row .put ("json" , json .toString ());
134- row .put ("container" , c .getId ());
135146
136147 if (templateId == null )
137148 {
138- row = Table .insert (u , ti , row );
149+ BatchValidationException bve = new BatchValidationException ();
150+ List <Map <String , Object >> rows = ti .getUpdateService ().insertRows (u , c , Arrays .asList (row ), bve , null , null );
151+ if (bve .hasErrors ())
152+ {
153+ throw bve ;
154+ }
155+
156+ return rows .get (0 );
139157 }
140158 else
141159 {
142160 row .put ("rowid" , templateId );
143- row = Table .update (u , ti , row , templateId );
161+ List <Map <String , Object >> rows = ti .getUpdateService ().updateRows (u , c , Arrays .asList (row ), Arrays .asList (Map .of ("rowid" , templateId )), null , null );
162+ return rows .get (0 );
144163 }
145-
146- return row ;
147164 }
148- catch (RuntimeSQLException e )
165+ catch (BatchValidationException e )
166+ {
167+ // Expected validation failures (e.g. from validateTemplate() or insertRows()) should propagate to the
168+ // client as-is, not be logged as server errors.
169+ throw e ;
170+ }
171+ catch (Exception e )
149172 {
150173 _log .error (e .getMessage (), e );
151174 errors .addRowError (new ValidationException (e .getMessage ()));
152175 throw errors ;
153176 }
154177 }
155178
156- public void validateTemplate (User u , Container c , ExpProtocol protocol , Integer templateId , String title , String importMethod , JSONObject json ) throws BatchValidationException
179+ public void validateTemplate (User u , Container c , ExpProtocol protocol , @ Nullable Integer templateId , String title , String importMethod , JSONObject json ) throws BatchValidationException
157180 {
158181 BatchValidationException errors = new BatchValidationException ();
159182
@@ -178,6 +201,49 @@ public void validateTemplate(User u, Container c, ExpProtocol protocol, Integer
178201 throw errors ;
179202 }
180203
204+ // Verify if this template exists and permissions. This expects any existing row to be present in the current container:
205+ if (templateId != null )
206+ {
207+ // This queries the current container+workbooks to identify the existence of rows in other reasonable containers:
208+ UserSchema us = QueryService .get ().getUserSchema (u , c .getContainerFor (ContainerType .DataType .tabParent ), "laboratory" );
209+ TableInfo ti = us .getTable ("assay_run_templates" );
210+ TableSelector ts = new TableSelector (ti , PageFlowUtil .set ("container" ), new SimpleFilter (FieldKey .fromString ("rowId" ), templateId ), null );
211+ if (ts .exists ())
212+ {
213+ Container rowContainer = ContainerManager .getForId (ts .getObject (String .class ));
214+ if (rowContainer == null )
215+ {
216+ errors .addRowError (new ValidationException ("Unable to determine the container for template: " + templateId ));
217+ throw errors ;
218+ }
219+
220+ if (!rowContainer .hasPermission ("AssayHelper.validateTemplate()" , u , UpdatePermission .class ))
221+ {
222+ errors .addRowError (new ValidationException ("The current user does not have permission to edit template: " + templateId ));
223+ throw errors ;
224+ }
225+
226+ if (!c .equals (rowContainer ))
227+ {
228+ errors .addRowError (new ValidationException ("Template " + templateId + " is not from this folder" ));
229+ throw errors ;
230+ }
231+ }
232+ else
233+ {
234+ errors .addRowError (new ValidationException ("Unable to find template with ID: " + templateId ));
235+ throw errors ;
236+ }
237+ }
238+ else
239+ {
240+ if (!c .hasPermission ("AssayHelper.validateTemplate()" , u , UpdatePermission .class ))
241+ {
242+ errors .addRowError (new ValidationException ("The current user does not have permission to creates templates in folder: " + c .getName ()));
243+ throw errors ;
244+ }
245+ }
246+
181247 method .validateTemplate (u , c , protocol , templateId , title , json , errors );
182248
183249 if (errors .hasErrors ())
0 commit comments