Skip to content

Commit

Permalink
Plate Set Archive (#5129)
Browse files Browse the repository at this point in the history
  • Loading branch information
labkey-nicka authored Jan 16, 2024
1 parent 7736e67 commit 052f52e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
55 changes: 55 additions & 0 deletions assay/src/org/labkey/assay/PlateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -926,4 +926,59 @@ public Object execute(CreatePlateSetForm form, BindException errors) throws Exce
return null;
}
}

public static class ArchiveForm
{
private List<Integer> _plateSetIds;
private boolean _restore;

public List<Integer> getPlateSetIds()
{
return _plateSetIds;
}

public void setPlateSetIds(List<Integer> plateSetIds)
{
_plateSetIds = plateSetIds;
}

public boolean isRestore()
{
return _restore;
}

public void setRestore(boolean restore)
{
_restore = restore;
}
}

@Marshal(Marshaller.JSONObject)
@RequiresPermission(UpdatePermission.class)
public static class ArchivePlateSetsAction extends MutatingApiAction<ArchiveForm>
{
@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;
}
}
}
42 changes: 42 additions & 0 deletions assay/src/org/labkey/assay/plate/PlateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1772,6 +1774,46 @@ public PlateSetImpl createPlateSet(Container container, User user, @NotNull Plat
return plateSet;
}

public void archivePlateSets(Container container, User user, List<Integer> plateSetIds, boolean archive) throws Exception
{
String action = archive ? "archive" : "restore";
Class<? extends Permission> perm = 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;
Expand Down

0 comments on commit 052f52e

Please sign in to comment.