|
28 | 28 | import org.labkey.api.action.ReadOnlyApiAction; |
29 | 29 | import org.labkey.api.action.SimpleViewAction; |
30 | 30 | import org.labkey.api.action.SpringActionController; |
| 31 | +import org.labkey.api.data.CompareType; |
31 | 32 | import org.labkey.api.data.Container; |
32 | 33 | import org.labkey.api.data.ContainerManager; |
33 | 34 | import org.labkey.api.data.CoreSchema; |
| 35 | +import org.labkey.api.data.SQLFragment; |
34 | 36 | import org.labkey.api.data.SimpleFilter; |
| 37 | +import org.labkey.api.data.Sort; |
| 38 | +import org.labkey.api.data.SqlExecutor; |
| 39 | +import org.labkey.api.data.Table; |
35 | 40 | import org.labkey.api.data.TableInfo; |
36 | 41 | import org.labkey.api.data.TableSelector; |
| 42 | +import org.labkey.api.ehr.EHRService; |
37 | 43 | import org.labkey.api.ehr.security.EHRDataEntryPermission; |
| 44 | +import org.labkey.api.exp.property.Domain; |
38 | 45 | import org.labkey.api.files.FileContentService; |
39 | 46 | import org.labkey.api.query.FieldKey; |
| 47 | +import org.labkey.api.query.QueryService; |
40 | 48 | import org.labkey.api.security.AdminConsoleAction; |
| 49 | +import org.labkey.api.security.LimitedUser; |
41 | 50 | import org.labkey.api.security.RequiresPermission; |
42 | 51 | import org.labkey.api.security.RequiresSiteAdmin; |
43 | 52 | import org.labkey.api.security.User; |
44 | 53 | import org.labkey.api.security.permissions.AdminPermission; |
45 | 54 | import org.labkey.api.security.permissions.ReadPermission; |
| 55 | +import org.labkey.api.study.Dataset; |
| 56 | +import org.labkey.api.study.DatasetTable; |
| 57 | +import org.labkey.api.study.StudyService; |
46 | 58 | import org.labkey.api.util.PageFlowUtil; |
47 | 59 | import org.labkey.api.util.URLHelper; |
48 | 60 | import org.labkey.api.view.ActionURL; |
49 | 61 | import org.labkey.api.view.HtmlView; |
50 | 62 | import org.labkey.api.view.NavTree; |
| 63 | +import org.springframework.dao.DataIntegrityViolationException; |
51 | 64 | import org.springframework.validation.BindException; |
52 | 65 | import org.springframework.validation.Errors; |
53 | 66 | import org.springframework.web.servlet.ModelAndView; |
54 | 67 |
|
55 | 68 | import java.io.File; |
56 | 69 | import java.lang.reflect.Method; |
| 70 | +import java.sql.ResultSet; |
| 71 | +import java.sql.SQLException; |
57 | 72 | import java.util.ArrayList; |
58 | 73 | import java.util.HashMap; |
59 | 74 | import java.util.List; |
60 | 75 | import java.util.Map; |
| 76 | +import java.util.Objects; |
61 | 77 |
|
62 | 78 | /** |
63 | 79 | * User: bbimber |
@@ -512,4 +528,112 @@ public void setStartingId(Integer startingId) |
512 | 528 | _startingId = startingId; |
513 | 529 | } |
514 | 530 | } |
| 531 | + |
| 532 | + @RequiresPermission(EHRDataEntryPermission.class) |
| 533 | + public class PopulateCaseNumbersAction extends MutatingApiAction<Object> |
| 534 | + { |
| 535 | + private String _casesProvisionedName; |
| 536 | + |
| 537 | + @Override |
| 538 | + public void validateForm(Object o, Errors errors) |
| 539 | + { |
| 540 | + super.validateForm(o, errors); |
| 541 | + |
| 542 | + StudyService ss = StudyService.get(); |
| 543 | + if (ss == null) |
| 544 | + { |
| 545 | + errors.reject(ERROR_REQUIRED, "No study"); |
| 546 | + return; |
| 547 | + } |
| 548 | + |
| 549 | + int datasetId = ss.getDatasetIdByName(getContainer(), "cases"); |
| 550 | + Dataset dataset = ss.getDataset(getContainer(), datasetId); |
| 551 | + |
| 552 | + if (dataset == null) |
| 553 | + { |
| 554 | + errors.reject(ERROR_REQUIRED, "Cases dataset not found."); |
| 555 | + return; |
| 556 | + } |
| 557 | + |
| 558 | + Domain domain = dataset.getDomain(); |
| 559 | + if (domain == null) |
| 560 | + { |
| 561 | + errors.reject(ERROR_REQUIRED, "Cases dataset domain not found."); |
| 562 | + return; |
| 563 | + } |
| 564 | + |
| 565 | + _casesProvisionedName = domain.getStorageTableName(); |
| 566 | + if (_casesProvisionedName == null) |
| 567 | + { |
| 568 | + errors.reject(ERROR_REQUIRED, "Cases dataset provisioned name not found."); |
| 569 | + } |
| 570 | + } |
| 571 | + |
| 572 | + @Override |
| 573 | + public ApiResponse execute(Object form, BindException errors) throws SQLException |
| 574 | + { |
| 575 | + Container c = EHRService.get().getEHRStudyContainer(getContainer()); |
| 576 | + TableInfo ti = QueryService.get().getUserSchema(getUser(), c, "study").getTable("cases"); |
| 577 | + |
| 578 | + SQLFragment sqlStart = new SQLFragment("UPDATE c SET c.caseNo = updates.caseNo FROM studydataset."); |
| 579 | + sqlStart.appendIdentifier(_casesProvisionedName); |
| 580 | + sqlStart.append(" c JOIN (VALUES "); |
| 581 | + |
| 582 | + SQLFragment sqlEnd = new SQLFragment(") AS updates(dsrowid, caseNo) "); |
| 583 | + sqlEnd.append("ON c.dsrowid = updates.dsrowid"); |
| 584 | + |
| 585 | + logger.info("Starting case number updates."); |
| 586 | + |
| 587 | + // Stream cases with no case number, sorted by date |
| 588 | + SimpleFilter filter = SimpleFilter.createContainerFilter(getContainer()); |
| 589 | + filter.addCondition(FieldKey.fromParts("caseNo"), null, CompareType.ISBLANK); |
| 590 | + ResultSet rs = new TableSelector(ti, filter, new Sort("date")).getResultSet(false, false); |
| 591 | + |
| 592 | + boolean newBatch = true; |
| 593 | + int counter = 0; |
| 594 | + SQLFragment sql = new SQLFragment().append(sqlStart); |
| 595 | + |
| 596 | + while( rs.next() ) |
| 597 | + { |
| 598 | + if (newBatch) { |
| 599 | + newBatch = false; |
| 600 | + } |
| 601 | + else { |
| 602 | + sql.append(","); |
| 603 | + } |
| 604 | + sql.append("(?, ?)"); |
| 605 | + sql.add(rs.getInt("dsrowid")); |
| 606 | + sql.add(ONPRC_EHRManager.get().getNextCaseNo(c)); |
| 607 | + counter++; |
| 608 | + |
| 609 | + // Batch boundary |
| 610 | + if (counter % 1000 == 0) |
| 611 | + { |
| 612 | + newBatch = true; |
| 613 | + sql.append(sqlEnd); |
| 614 | + |
| 615 | + new SqlExecutor(ti.getSchema()).execute(sql); |
| 616 | + |
| 617 | + logger.info(counter + " total case numbers added."); |
| 618 | + |
| 619 | + sql = new SQLFragment().append(sqlStart); |
| 620 | + } |
| 621 | + } |
| 622 | + |
| 623 | + if (!newBatch) |
| 624 | + { |
| 625 | + sql.append(sqlEnd); |
| 626 | + new SqlExecutor(ti.getSchema()).execute(sql); |
| 627 | + } |
| 628 | + |
| 629 | + JSONObject ret = new JSONObject(); |
| 630 | + ret.put("rows", counter); |
| 631 | + ret.put("success", true); |
| 632 | + |
| 633 | + logger.info("Case number update completed. " + counter + " total case numbers updated."); |
| 634 | + |
| 635 | + |
| 636 | + return new ApiSimpleResponse(ret); |
| 637 | + } |
| 638 | + } |
515 | 639 | } |
0 commit comments