Skip to content

Commit 19e61e8

Browse files
Improve generics for plate code
1 parent 0622405 commit 19e61e8

11 files changed

Lines changed: 147 additions & 134 deletions

File tree

assay/api-src/org/labkey/api/assay/plate/PlateService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static PlateService get()
156156
*/
157157
@Nullable Plate getPlate(ContainerFilter cf, Long plateSetId, Object plateIdentifier);
158158

159-
@NotNull List<Plate> getPlates(Container container);
159+
@NotNull List<? extends Plate> getPlates(Container container);
160160

161161
/**
162162
* Gets the plate set by ID

assay/api-src/org/labkey/api/assay/plate/PlateSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public interface PlateSet extends Identifiable
2626

2727
boolean isTemplate();
2828

29-
List<Plate> getPlates();
29+
List<? extends Plate> getPlates();
3030

3131
PlateSetType getType();
3232

assay/src/org/labkey/assay/PlateController.java

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,45 @@ public class PlateController extends SpringActionController
112112
private static final SpringActionController.DefaultActionResolver _actionResolver = new DefaultActionResolver(PlateController.class);
113113
private static final Logger LOG = LogHelper.getLogger(PlateController.class, "Controller for plate related actions");
114114

115+
record SubmittedGroup(int rowId, String type, String name, List<PlatePosition> positions, Map<String, Object> properties)
116+
{
117+
public static SubmittedGroup from(JSONObject g)
118+
{
119+
int rowId = g.optInt("rowId", -1);
120+
String type = g.getString("type");
121+
String name = g.getString("name");
122+
JSONArray posArr = g.optJSONArray("positions");
123+
List<PlatePosition> positions = new ArrayList<>();
124+
if (posArr != null)
125+
{
126+
for (int j = 0; j < posArr.length(); j++)
127+
{
128+
JSONObject p = posArr.getJSONObject(j);
129+
positions.add(PlatePosition.from(p));
130+
}
131+
}
132+
JSONObject propsObj = g.optJSONObject("properties");
133+
Map<String, Object> props = new HashMap<>();
134+
if (propsObj != null)
135+
{
136+
for (String key : propsObj.keySet())
137+
{
138+
Object val = propsObj.get(key);
139+
props.put(key, val == JSONObject.NULL ? null : val);
140+
}
141+
}
142+
return new SubmittedGroup(rowId, type, name, positions, props);
143+
}
144+
}
145+
146+
record PlatePosition(int row, int col)
147+
{
148+
public static PlatePosition from(JSONObject p)
149+
{
150+
return new PlatePosition(p.getInt("row"), p.getInt("col"));
151+
}
152+
}
153+
115154
public PlateController()
116155
{
117156
setActionResolver(_actionResolver);
@@ -165,7 +204,7 @@ public static class PlateListAction extends SimpleViewAction<ReturnUrlForm>
165204
public ModelAndView getView(ReturnUrlForm form, BindException errors)
166205
{
167206
setHelpTopic("editPlateTemplate");
168-
List<Plate> plateTemplates = PlateService.get().getPlates(getContainer())
207+
List<? extends Plate> plateTemplates = PlateService.get().getPlates(getContainer())
169208
.stream()
170209
.filter(p -> !TsvPlateLayoutHandler.TYPE.equalsIgnoreCase(p.getAssayType()))
171210
.toList();
@@ -180,6 +219,7 @@ public void addNavTrail(NavTree root)
180219
}
181220
}
182221

222+
/** Delete soon! */
183223
@RequiresAnyOf({InsertPermission.class, DesignAssayPermission.class})
184224
public static class DesignerServiceAction extends GWTServiceAction
185225
{
@@ -417,7 +457,7 @@ public Object execute(SaveTemplateForm form, BindException errors) throws Except
417457
}
418458

419459
boolean updateExisting = false;
420-
Plate plate;
460+
PlateImpl plate;
421461
if (rowId > 0)
422462
{
423463
plate = PlateManager.get().getPlate(getContainer(), rowId);
@@ -447,42 +487,16 @@ public Object execute(SaveTemplateForm form, BindException errors) throws Except
447487
plate.setProperties(plateProperties);
448488

449489
// Parse groups from JSON
450-
List<Map<String, Object>> submittedGroups = new ArrayList<>();
490+
List<SubmittedGroup> submittedGroups = new ArrayList<>();
451491
Set<Integer> submittedGroupIds = new HashSet<>();
452492
if (groupsJson != null)
453493
{
454494
for (int i = 0; i < groupsJson.length(); i++)
455495
{
456-
JSONObject g = groupsJson.getJSONObject(i);
457-
Map<String, Object> gm = new HashMap<>();
458-
gm.put("rowId", g.optInt("rowId", -1));
459-
gm.put("type", g.getString("type"));
460-
gm.put("name", g.getString("name"));
461-
JSONArray posArr = g.optJSONArray("positions");
462-
List<int[]> positions = new ArrayList<>();
463-
if (posArr != null)
464-
{
465-
for (int j = 0; j < posArr.length(); j++)
466-
{
467-
JSONObject p = posArr.getJSONObject(j);
468-
positions.add(new int[]{p.getInt("row"), p.getInt("col")});
469-
}
470-
}
471-
gm.put("positions", positions);
472-
JSONObject propsObj = g.optJSONObject("properties");
473-
Map<String, Object> props = new HashMap<>();
474-
if (propsObj != null)
475-
{
476-
for (String key : propsObj.keySet())
477-
{
478-
Object val = propsObj.get(key);
479-
props.put(key, val == JSONObject.NULL ? null : val);
480-
}
481-
}
482-
gm.put("properties", props);
483-
submittedGroups.add(gm);
484-
if ((int) gm.get("rowId") > 0)
485-
submittedGroupIds.add((int) gm.get("rowId"));
496+
SubmittedGroup g = SubmittedGroup.from(groupsJson.getJSONObject(i));
497+
submittedGroups.add(g);
498+
if (g.rowId > 0)
499+
submittedGroupIds.add(g.rowId);
486500
}
487501
}
488502

@@ -491,14 +505,14 @@ public Object execute(SaveTemplateForm form, BindException errors) throws Except
491505
for (WellGroup existingGroup : existingWellGroups)
492506
{
493507
if (existingGroup.getRowId() != null && !submittedGroupIds.contains(existingGroup.getRowId()))
494-
((PlateImpl) plate).markWellGroupForDeletion(existingGroup);
508+
plate.markWellGroupForDeletion(existingGroup);
495509
}
496510

497511
// Update or create well groups
498-
for (Map<String, Object> gm : submittedGroups)
512+
for (SubmittedGroup gm : submittedGroups)
499513
{
500-
int gRowId = (int) gm.get("rowId");
501-
String groupTypeName = (String) gm.get("type");
514+
int gRowId = gm.rowId();
515+
String groupTypeName = gm.type();
502516
WellGroup.Type groupType;
503517
try
504518
{
@@ -508,14 +522,12 @@ public Object execute(SaveTemplateForm form, BindException errors) throws Except
508522
{
509523
throw new ApiUsageException("Unknown well group type: '" + groupTypeName + "'");
510524
}
511-
@SuppressWarnings("unchecked")
512-
List<int[]> posList = (List<int[]>) gm.get("positions");
525+
List<PlatePosition> posList = gm.positions();
513526
List<Position> positions = new ArrayList<>();
514-
for (int[] p : posList)
515-
positions.add(plate.getPosition(p[0], p[1]));
527+
for (PlatePosition p : posList)
528+
positions.add(plate.getPosition(p.row, p.col));
516529

517-
@SuppressWarnings("unchecked")
518-
Map<String, Object> props = (Map<String, Object>) gm.get("properties");
530+
Map<String, Object> props = gm.properties();
519531

520532
WellGroupImpl group;
521533
if (updateExisting && gRowId > 0)
@@ -524,15 +536,15 @@ public Object execute(SaveTemplateForm form, BindException errors) throws Except
524536
if (existing == null)
525537
throw new Exception("Well group " + gRowId + " was not found.");
526538
if (existing.getType() != groupType)
527-
throw new Exception("Well group type cannot be changed: " + gm.get("name"));
528-
existing.setName((String) gm.get("name"));
539+
throw new Exception("Well group type cannot be changed: " + gm.name());
540+
existing.setName(gm.name);
529541
existing.setPositions(positions);
530-
((PlateImpl) plate).storeWellGroup(existing);
542+
plate.storeWellGroup(existing);
531543
group = existing;
532544
}
533545
else
534546
{
535-
group = (WellGroupImpl) plate.addWellGroup((String) gm.get("name"), groupType, positions);
547+
group = plate.addWellGroup(gm.name, groupType, positions);
536548
}
537549
group.setProperties(props);
538550
}
@@ -579,6 +591,7 @@ public void addNavTrail(NavTree root)
579591
}
580592
}
581593

594+
/** Delete soon! */
582595
@RequiresAnyOf({InsertPermission.class, DesignAssayPermission.class})
583596
public class DesignerGwtAction extends SimpleViewAction<DesignerForm>
584597
{
@@ -664,7 +677,7 @@ public static class CopyTemplateBean
664677
private HtmlString _treeHtml;
665678
private Plate _plate;
666679
private String _selectedDestination;
667-
private List<Plate> _destinationTemplates;
680+
private List<? extends Plate> _destinationTemplates;
668681

669682
public CopyTemplateBean(final Container container, final User user, final Integer plateId, final String selectedDestination)
670683
{
@@ -1083,7 +1096,7 @@ public Object execute(CreatePlateForm form, BindException errors) throws Excepti
10831096
PlateImpl newPlate = new PlateImpl(getContainer(), form.getName(), form.getBarcode(), form.getAssayType(), _plateType);
10841097
if (form.getData() == null && form.getTemplateId() != null && TsvPlateLayoutHandler.TYPE.equalsIgnoreCase(newPlate.getAssayType()))
10851098
{
1086-
newPlate = (PlateImpl) PlateManager.get().copyPlate(
1099+
newPlate = PlateManager.get().copyPlate(
10871100
getContainer(),
10881101
getUser(),
10891102
form.getTemplateId(),
@@ -1104,7 +1117,7 @@ public Object execute(CreatePlateForm form, BindException errors) throws Excepti
11041117
if (form.isTemplate() && data == null)
11051118
data = PlateManager.get().prepareEmptyPlateTemplateData(getContainer(), _plateType);
11061119

1107-
newPlate = (PlateImpl) PlateManager.get().createAndSavePlate(getContainer(), getUser(), newPlate, form.getPlateSetId(), data);
1120+
newPlate = PlateManager.get().createAndSavePlate(getContainer(), getUser(), newPlate, form.getPlateSetId(), data);
11081121
}
11091122

11101123
return success(newPlate);

assay/src/org/labkey/assay/plate/AssayPlateMetadataServiceImpl.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public Map<String, Object> apply(Map<String, Object> row)
238238
});
239239
}
240240

241-
private List<Plate> getPlatesForPlateSet(
241+
private List<? extends Plate> getPlatesForPlateSet(
242242
Container container,
243243
User user,
244244
Long plateSetId,
@@ -270,7 +270,7 @@ public DataIteratorBuilder parsePlateData(
270270
) throws ExperimentException
271271
{
272272
// get the ordered list of plates for the plate set
273-
List<Plate> plates = getPlatesForPlateSet(container, user, plateSetId, protocol);
273+
List<? extends Plate> plates = getPlatesForPlateSet(container, user, plateSetId, protocol);
274274
if (plates.isEmpty())
275275
throw new ExperimentException("No plates were found for the plate set (" + plateSetId + ").");
276276
PlateSet plateSet = plates.get(0).getPlateSet();
@@ -297,7 +297,7 @@ private List<Map<String, Object>> _parsePlateData(
297297
AssayProvider provider,
298298
ExpProtocol protocol,
299299
PlateSet plateSet,
300-
List<Plate> plates,
300+
List<? extends Plate> plates,
301301
FileLike dataFile,
302302
DataLoaderSettings settings
303303
) throws ExperimentException
@@ -356,7 +356,7 @@ public DataIteratorBuilder mergeReRunData(
356356
) throws ExperimentException
357357
{
358358
Long plateSetId = getPlateSetId(context, provider, protocol);
359-
List<Plate> plates = getPlatesForPlateSet(container, user, plateSetId, protocol);
359+
List<? extends Plate> plates = getPlatesForPlateSet(container, user, plateSetId, protocol);
360360
if (plates.isEmpty())
361361
throw new ExperimentException("No plates were found for the plate set (" + plateSetId + ").");
362362

@@ -540,7 +540,7 @@ private boolean isGridFormat(List<Map<String, Object>> data)
540540
private List<Map<String, Object>> parsePlateRows(
541541
AssayProvider provider,
542542
ExpProtocol protocol,
543-
List<Plate> plates,
543+
List<? extends Plate> plates,
544544
List<Map<String, Object>> data
545545
) throws ExperimentException
546546
{
@@ -604,7 +604,7 @@ private List<Map<String, Object>> parsePlateRows(
604604
}
605605

606606
// Resolves a pre-calculated "plateIdField" to a plate rowId and furnishes new "data" rows with the plate rowId.
607-
private List<Map<String, Object>> resolvePlateIdentifier(List<Plate> plates, List<Map<String, Object>> data, String plateIdField)
607+
private List<Map<String, Object>> resolvePlateIdentifier(List<? extends Plate> plates, List<Map<String, Object>> data, String plateIdField)
608608
{
609609
var newData = new ArrayList<Map<String, Object>>();
610610
var plateIdentifiers = new HashMap<Object, Long>();
@@ -664,7 +664,7 @@ public PlateGridInfo(PlateUtils.GridInfo info, PlateSet plateSet, Set<String> me
664664

665665
// locate the plate in the plate set this grid is associated with plus an optional
666666
// measure name
667-
List<Plate> plates = PlateManager.get().getPlatesForPlateSet(plateSet);
667+
List<? extends Plate> plates = PlateManager.get().getPlatesForPlateSet(plateSet);
668668
List<String> annotations = getAnnotations();
669669

670670
// if the plate set only has one plate, then treat a single annotation as the measure
@@ -694,7 +694,7 @@ public PlateGridInfo(PlateUtils.GridInfo info, PlateSet plateSet, Set<String> me
694694
}
695695
}
696696

697-
private @NotNull Plate getPlateForId(String annotation, List<Plate> platesetPlates) throws ExperimentException
697+
private @NotNull Plate getPlateForId(String annotation, List<? extends Plate> platesetPlates) throws ExperimentException
698698
{
699699
Plate plate = platesetPlates.stream().filter(p -> p.isIdentifierMatch(annotation)).findFirst().orElse(null);
700700
if (plate == null)
@@ -734,7 +734,7 @@ private List<Map<String, Object>> parsePlateGrids(
734734
AssayProvider provider,
735735
ExpProtocol protocol,
736736
PlateSet plateSet,
737-
List<Plate> plates,
737+
List<? extends Plate> plates,
738738
FileLike dataFile
739739
) throws ExperimentException
740740
{
@@ -1754,7 +1754,7 @@ public void testGridAnnotations() throws Exception
17541754
);
17551755

17561756
PlateSet plateSet = PlateManager.get().createPlateSet(container, user, new PlateSetImpl(), plates, null, null);
1757-
List<Plate> plateSetPlates = PlateManager.get().getPlatesForPlateSet(plateSet);
1757+
List<? extends Plate> plateSetPlates = PlateManager.get().getPlatesForPlateSet(plateSet);
17581758
assertEquals("Expected two plates to be created.", 2, plateSetPlates.size());
17591759
Plate plate = plateSetPlates.get(0);
17601760

0 commit comments

Comments
 (0)