From 7be8c3e53b361c32440b44bd2749efe89014eae6 Mon Sep 17 00:00:00 2001 From: labkey-nicka Date: Fri, 12 Jan 2024 15:23:12 -0800 Subject: [PATCH] Plate Set Archive --- .../src/org/labkey/assay/PlateController.java | 55 +++++++++++++++++++ .../org/labkey/assay/plate/PlateManager.java | 42 ++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/assay/src/org/labkey/assay/PlateController.java b/assay/src/org/labkey/assay/PlateController.java index 73175a13aea..01e08deb7b6 100644 --- a/assay/src/org/labkey/assay/PlateController.java +++ b/assay/src/org/labkey/assay/PlateController.java @@ -926,4 +926,59 @@ public Object execute(CreatePlateSetForm form, BindException errors) throws Exce return null; } } + + public static class ArchiveForm + { + private List _plateSetIds; + private boolean _restore; + + public List getPlateSetIds() + { + return _plateSetIds; + } + + public void setPlateSetIds(List plateSetIds) + { + _plateSetIds = plateSetIds; + } + + public boolean isRestore() + { + return _restore; + } + + public void setRestore(boolean restore) + { + _restore = restore; + } + } + + @Marshal(Marshaller.JSONObject) + @RequiresPermission(DeletePermission.class) + public static class ArchivePlateSetsAction extends MutatingApiAction + { + @Override + public void validateForm(ArchiveForm form, Errors errors) + { + if (form.getPlateSetIds() == null) + errors.reject(ERROR_GENERIC, "\"plateSetIds\" is a required field."); + } + + @Override + public Object execute(ArchiveForm form, BindException errors) throws Exception + { + try + { + PlateManager.get().archivePlateSets(getContainer(), getUser(), form.getPlateSetIds(), !form.isRestore()); + return success(); + } + catch (Exception e) + { + String action = form.isRestore() ? "restore" : "archive"; + errors.reject(ERROR_GENERIC, e.getMessage() != null ? e.getMessage() : "Failed to " + action + " plate sets. An error has occurred."); + } + + return null; + } + } } diff --git a/assay/src/org/labkey/assay/plate/PlateManager.java b/assay/src/org/labkey/assay/plate/PlateManager.java index 5e466ffd204..54c71f93607 100644 --- a/assay/src/org/labkey/assay/plate/PlateManager.java +++ b/assay/src/org/labkey/assay/plate/PlateManager.java @@ -85,9 +85,11 @@ import org.labkey.api.query.ValidationException; import org.labkey.api.search.SearchService; import org.labkey.api.security.User; +import org.labkey.api.security.permissions.DeletePermission; import org.labkey.api.security.permissions.InsertPermission; import org.labkey.api.security.permissions.Permission; import org.labkey.api.security.permissions.ReadPermission; +import org.labkey.api.security.permissions.UpdatePermission; import org.labkey.api.util.GUID; import org.labkey.api.util.JunitUtil; import org.labkey.api.util.Pair; @@ -1772,6 +1774,46 @@ public PlateSetImpl createPlateSet(Container container, User user, @NotNull Plat return plateSet; } + public void archivePlateSets(Container container, User user, List plateSetIds, boolean archive) throws Exception + { + String action = archive ? "archive" : "restore"; + Class perm = archive ? DeletePermission.class : UpdatePermission.class; + + if (!container.hasPermission(user, perm)) + throw new UnauthorizedException("Failed to " + action + " plate sets. Insufficient permissions."); + + if (plateSetIds.isEmpty()) + throw new ValidationException("Failed to " + action + " plate sets. No plate sets specified."); + + try (DbScope.Transaction tx = ensureTransaction()) + { + TableInfo plateSetTable = AssayDbSchema.getInstance().getTableInfoPlateSet(); + + // Ensure user has permission in all containers + { + SQLFragment sql = new SQLFragment("SELECT DISTINCT container FROM ") + .append(plateSetTable, "PS") + .append(" WHERE rowId ") + .appendInClause(plateSetIds, plateSetTable.getSqlDialect()); + + for (String containerId : new SqlSelector(plateSetTable.getSchema(), sql).getCollection(String.class)) + { + Container c = ContainerManager.getForId(containerId); + if (c != null && !c.hasPermission(user, perm)) + throw new UnauthorizedException("Failed to " + action + " plate sets. Insufficient permissions in " + c.getPath()); + } + } + + SQLFragment sql = new SQLFragment("UPDATE ").append(plateSetTable, "PS") + .append(" SET archived = ?").add(archive) + .append(" WHERE rowId ").appendInClause(plateSetIds, plateSetTable.getSqlDialect()); + + new SqlExecutor(plateSetTable.getSchema()).execute(sql); + + tx.commit(); + } + } + public static final class TestCase { private static Container container;