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 ;
@@ -120,35 +126,52 @@ public Map<String, Object> saveTemplate(User u, Container c, ExpProtocol protoco
120126 {
121127 validateTemplate (u , c , protocol , templateId , title , importMethod , json );
122128
123- TableInfo ti = LaboratorySchema .getInstance ().getSchema ().getTable (LaboratorySchema .TABLE_ASSAY_RUN_TEMPLATES );
129+ UserSchema us = QueryService .get ().getUserSchema (u , c , "laboratory" );
130+ if (us == null )
131+ {
132+ throw new IllegalStateException ("Could not find the laboratory schema in container: " + c .getPath ());
133+ }
134+
135+ TableInfo ti = us .getTable (LaboratorySchema .TABLE_ASSAY_RUN_TEMPLATES );
124136 Map <String , Object > row = new HashMap <>();
125137 row .put ("assayId" , protocol .getRowId ());
126138 row .put ("title" , title );
127139 row .put ("importMethod" , importMethod );
128140 row .put ("json" , json .toString ());
129- row .put ("container" , c .getId ());
130141
131142 if (templateId == null )
132143 {
133- row = Table .insert (u , ti , row );
144+ BatchValidationException bve = new BatchValidationException ();
145+ List <Map <String , Object >> rows = ti .getUpdateService ().insertRows (u , c , Arrays .asList (row ), bve , null , null );
146+ if (bve .hasErrors ())
147+ {
148+ throw bve ;
149+ }
150+
151+ return rows .get (0 );
134152 }
135153 else
136154 {
137155 row .put ("rowid" , templateId );
138- row = Table .update (u , ti , row , templateId );
156+ List <Map <String , Object >> rows = ti .getUpdateService ().updateRows (u , c , Arrays .asList (row ), Arrays .asList (Map .of ("rowid" , templateId )), null , null );
157+ return rows .get (0 );
139158 }
140-
141- return row ;
142159 }
143- catch (RuntimeSQLException e )
160+ catch (BatchValidationException e )
161+ {
162+ // Expected validation failures (e.g. from validateTemplate() or insertRows()) should propagate to the
163+ // client as-is, not be logged as server errors.
164+ throw e ;
165+ }
166+ catch (Exception e )
144167 {
145168 _log .error (e .getMessage (), e );
146169 errors .addRowError (new ValidationException (e .getMessage ()));
147170 throw errors ;
148171 }
149172 }
150173
151- public void validateTemplate (User u , Container c , ExpProtocol protocol , Integer templateId , String title , String importMethod , JSONObject json ) throws BatchValidationException
174+ public void validateTemplate (User u , Container c , ExpProtocol protocol , @ Nullable Integer templateId , String title , String importMethod , JSONObject json ) throws BatchValidationException
152175 {
153176 BatchValidationException errors = new BatchValidationException ();
154177
@@ -173,6 +196,49 @@ public void validateTemplate(User u, Container c, ExpProtocol protocol, Integer
173196 throw errors ;
174197 }
175198
199+ // Verify if this template exists and permissions. This expects any existing row to be present in the current container:
200+ if (templateId != null )
201+ {
202+ // This queries the current container+workbooks to identify the existence of rows in other reasonable containers:
203+ UserSchema us = QueryService .get ().getUserSchema (u , c .getContainerFor (ContainerType .DataType .tabParent ), "laboratory" );
204+ TableInfo ti = us .getTable ("assay_run_templates" );
205+ TableSelector ts = new TableSelector (ti , PageFlowUtil .set ("container" ), new SimpleFilter (FieldKey .fromString ("rowId" ), templateId ), null );
206+ if (ts .exists ())
207+ {
208+ Container rowContainer = ContainerManager .getForId (ts .getObject (String .class ));
209+ if (rowContainer == null )
210+ {
211+ errors .addRowError (new ValidationException ("Unable to determine the container for template: " + templateId ));
212+ throw errors ;
213+ }
214+
215+ if (!rowContainer .hasPermission ("AssayHelper.validateTemplate()" , u , UpdatePermission .class ))
216+ {
217+ errors .addRowError (new ValidationException ("The current user does not have permission to edit template: " + templateId ));
218+ throw errors ;
219+ }
220+
221+ if (!c .equals (rowContainer ))
222+ {
223+ errors .addRowError (new ValidationException ("Template " + templateId + " is not from this folder" ));
224+ throw errors ;
225+ }
226+ }
227+ else
228+ {
229+ errors .addRowError (new ValidationException ("Unable to find template with ID: " + templateId ));
230+ throw errors ;
231+ }
232+ }
233+ else
234+ {
235+ if (!c .hasPermission ("AssayHelper.validateTemplate()" , u , UpdatePermission .class ))
236+ {
237+ errors .addRowError (new ValidationException ("The current user does not have permission to creates templates in folder: " + c .getName ()));
238+ throw errors ;
239+ }
240+ }
241+
176242 method .validateTemplate (u , c , protocol , templateId , title , json , errors );
177243
178244 if (errors .hasErrors ())
0 commit comments