|
49 | 49 | import org.labkey.api.ehr.dataentry.DataEntryForm; |
50 | 50 | import org.labkey.api.ehr.demographics.AnimalRecord; |
51 | 51 | import org.labkey.api.ehr.history.HistoryRow; |
| 52 | +import org.labkey.api.ehr.security.EHRDataAdminPermission; |
52 | 53 | import org.labkey.api.ehr.security.EHRDataEntryPermission; |
53 | 54 | import org.labkey.api.exp.api.ExperimentService; |
54 | 55 | import org.labkey.api.gwt.client.AuditBehaviorType; |
|
60 | 61 | import org.labkey.api.query.BatchValidationException; |
61 | 62 | import org.labkey.api.query.DetailsURL; |
62 | 63 | import org.labkey.api.query.FieldKey; |
| 64 | +import org.labkey.api.query.InvalidKeyException; |
63 | 65 | import org.labkey.api.query.QueryAction; |
64 | 66 | import org.labkey.api.query.QueryForm; |
65 | 67 | import org.labkey.api.query.QueryParseException; |
@@ -331,6 +333,137 @@ public ApiResponse execute(DiscardFormForm form, BindException errors) |
331 | 333 | } |
332 | 334 | } |
333 | 335 |
|
| 336 | + public static class DiscardEmptyTasksForm |
| 337 | + { |
| 338 | + private String[] taskIds; |
| 339 | + |
| 340 | + public String[] getTaskIds() |
| 341 | + { |
| 342 | + return taskIds; |
| 343 | + } |
| 344 | + |
| 345 | + public void setTaskIds(String[] taskIds) |
| 346 | + { |
| 347 | + this.taskIds = taskIds; |
| 348 | + } |
| 349 | + } |
| 350 | + |
| 351 | + @RequiresPermission(EHRDataAdminPermission.class) |
| 352 | + public static class DiscardEmptyTasksAction extends MutatingApiAction<DiscardEmptyTasksForm> |
| 353 | + { |
| 354 | + private List<String> _selectedTaskIds; |
| 355 | + private TableInfo _tasksTable; |
| 356 | + |
| 357 | + @Override |
| 358 | + public void validateForm(DiscardEmptyTasksForm form, Errors errors) |
| 359 | + { |
| 360 | + super.validateForm(form, errors); |
| 361 | + |
| 362 | + if (form.getTaskIds() == null || form.getTaskIds().length == 0) |
| 363 | + { |
| 364 | + errors.reject(ERROR_MSG, "No tasks selected."); |
| 365 | + return; |
| 366 | + } |
| 367 | + _selectedTaskIds = Arrays.asList(form.getTaskIds()); |
| 368 | + |
| 369 | + UserSchema ehrSchema = QueryService.get().getUserSchema(getUser(), getContainer(), EHRSchema.EHR_SCHEMANAME); |
| 370 | + if (ehrSchema == null) |
| 371 | + { |
| 372 | + errors.reject(ERROR_MSG, "EHR schema is not available in this container."); |
| 373 | + return; |
| 374 | + } |
| 375 | + |
| 376 | + _tasksTable = ehrSchema.getTable(EHRSchema.TABLE_TASKS); |
| 377 | + if (_tasksTable == null) |
| 378 | + { |
| 379 | + errors.reject(ERROR_MSG, "ehr.tasks table is not available in this container."); |
| 380 | + return; |
| 381 | + } |
| 382 | + |
| 383 | + Set<String> nonEmpty = new LinkedHashSet<>(); |
| 384 | + UserSchema studySchema = QueryService.get().getUserSchema(getUser(), getContainer(), "study"); |
| 385 | + if (studySchema != null) |
| 386 | + { |
| 387 | + TableInfo studyData = studySchema.getTable("StudyData"); |
| 388 | + if (studyData != null && studyData.getColumn("taskid") != null) |
| 389 | + { |
| 390 | + SimpleFilter filter = new SimpleFilter(FieldKey.fromString("taskid"), _selectedTaskIds, CompareType.IN); |
| 391 | + String[] ids = new TableSelector(studyData, Collections.singleton("taskid"), filter, null).getArray(String.class); |
| 392 | + if (ids != null) |
| 393 | + { |
| 394 | + for (String id : ids) |
| 395 | + { |
| 396 | + if (id != null) |
| 397 | + nonEmpty.add(id); |
| 398 | + } |
| 399 | + } |
| 400 | + } |
| 401 | + } |
| 402 | + |
| 403 | + if (!nonEmpty.isEmpty()) |
| 404 | + { |
| 405 | + errors.reject(ERROR_MSG, "Cannot delete: " + nonEmpty.size() + " of the selected task(s) have associated dataset records. Task ID(s): " + String.join(", ", nonEmpty)); |
| 406 | + } |
| 407 | + } |
| 408 | + |
| 409 | + @Override |
| 410 | + public ApiResponse execute(DiscardEmptyTasksForm form, BindException errors) |
| 411 | + { |
| 412 | + int deleted; |
| 413 | + try (DbScope.Transaction transaction = ExperimentService.get().ensureTransaction()) |
| 414 | + { |
| 415 | + deleted = deleteRowsByTaskIds(_tasksTable, _selectedTaskIds); |
| 416 | + transaction.commit(); |
| 417 | + } |
| 418 | + catch (SQLException e) |
| 419 | + { |
| 420 | + throw new RuntimeSQLException(e); |
| 421 | + } |
| 422 | + |
| 423 | + Map<String, Object> resultProperties = new HashMap<>(); |
| 424 | + resultProperties.put("success", true); |
| 425 | + resultProperties.put("deletedCount", deleted); |
| 426 | + return new ApiSimpleResponse(resultProperties); |
| 427 | + } |
| 428 | + |
| 429 | + private int deleteRowsByTaskIds(TableInfo ti, List<String> taskIds) throws SQLException |
| 430 | + { |
| 431 | + QueryUpdateService qus = ti.getUpdateService(); |
| 432 | + if (qus == null) |
| 433 | + return 0; |
| 434 | + |
| 435 | + int total = 0; |
| 436 | + final int chunkSize = 1000; |
| 437 | + for (int start = 0; start < taskIds.size(); start += chunkSize) |
| 438 | + { |
| 439 | + List<String> chunk = taskIds.subList(start, Math.min(start + chunkSize, taskIds.size())); |
| 440 | + SimpleFilter filter = new SimpleFilter(FieldKey.fromString("taskid"), chunk, CompareType.IN); |
| 441 | + String[] pkColumns = ti.getPkColumnNames().toArray(new String[0]); |
| 442 | + Set<String> selectCols = new LinkedHashSet<>(Arrays.asList(pkColumns)); |
| 443 | + selectCols.add("taskid"); |
| 444 | + |
| 445 | + Map<String, Object>[] rows = new TableSelector(ti, selectCols, filter, null).getMapArray(); |
| 446 | + if (rows == null || rows.length == 0) |
| 447 | + continue; |
| 448 | + |
| 449 | + List<Map<String, Object>> keys = new ArrayList<>(rows.length); |
| 450 | + for (Map<String, Object> row : rows) |
| 451 | + keys.add(new HashMap<>(row)); |
| 452 | + |
| 453 | + try |
| 454 | + { |
| 455 | + qus.deleteRows(getUser(), getContainer(), keys, null, new HashMap<>()); |
| 456 | + } |
| 457 | + catch (InvalidKeyException | QueryUpdateServiceException | BatchValidationException e) |
| 458 | + { |
| 459 | + throw new RuntimeException(e); |
| 460 | + } |
| 461 | + total += rows.length; |
| 462 | + } |
| 463 | + return total; |
| 464 | + } |
| 465 | + } |
| 466 | + |
334 | 467 | public static class EHRQueryForm extends QueryForm |
335 | 468 | { |
336 | 469 | private boolean _showImport = false; |
|
0 commit comments