Skip to content

Commit 04e2b40

Browse files
committed
Clean up trigger/customizer layer code
1 parent 2f7566a commit 04e2b40

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

Studies/src/org/labkey/studies/query/StudiesTriggerFactory.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,55 @@ public void beforeInsert(TableInfo table, Container c, User user, @Nullable Map<
3838
@Override
3939
public void beforeInsert(TableInfo table, Container c, User user, @Nullable Map<String, Object> newRow, ValidationException errors, Map<String, Object> extraContext, @Nullable Map<String, Object> existingRecord) throws ValidationException
4040
{
41-
possiblyResolveStudy(newRow, c);
41+
possiblyResolveStudy(table, newRow, existingRecord, c);
4242
}
4343

4444
@Override
4545
public void beforeUpdate(TableInfo table, Container c, User user, @Nullable Map<String, Object> newRow, @Nullable Map<String, Object> oldRow, ValidationException errors, Map<String, Object> extraContext) throws ValidationException
4646
{
47-
possiblyResolveStudy(newRow, c);
47+
possiblyResolveStudy(table, newRow, oldRow, c);
4848
}
4949

5050
/**
5151
* This allows incoming data to specify the study using the string name, which is resolved into the rowId
5252
*/
53-
private void possiblyResolveStudy(@Nullable Map<String, Object> row, Container c)
53+
private void possiblyResolveStudy(TableInfo table, @Nullable Map<String, Object> row, @Nullable Map<String, Object> oldRow, Container c)
5454
{
5555
if (row == null)
5656
{
5757
return;
5858
}
5959

60-
possiblyResolveStudy(row, c, "studyId");
61-
if (row.get("studyId") == null & row.get("studyName") != null)
60+
if (table.getColumn("studyId") != null)
6261
{
63-
possiblyResolveStudy(row, c, "studyName");
62+
possiblyResolveStudy(row, c, "studyId");
63+
if (row.get("studyId") == null & row.get("studyName") != null)
64+
{
65+
possiblyResolveStudy(row, c, "studyName");
66+
}
67+
}
68+
69+
if (table.getColumn("cohortId") != null)
70+
{
71+
possiblyResolveCohort(row, c, "cohortId");
72+
if (row.get("cohortId") == null & row.get("cohortName") != null)
73+
{
74+
possiblyResolveCohort(row, c, "cohortName");
75+
}
6476
}
6577
}
6678

6779
private void possiblyResolveStudy(@Nullable Map<String, Object> row, Container c, String sourceProperty)
80+
{
81+
possiblyResolveStudyOrCohort(row, c, sourceProperty, "studyId", "studyName");
82+
}
83+
84+
private void possiblyResolveCohort(@Nullable Map<String, Object> row, Container c, String sourceProperty)
85+
{
86+
possiblyResolveStudyOrCohort(row, c, sourceProperty, "cohortId", "cohortName");
87+
}
88+
89+
private void possiblyResolveStudyOrCohort(@Nullable Map<String, Object> row, Container c, String sourceProperty, String targetFieldName, String filterFieldName)
6890
{
6991
if (row == null)
7092
{
@@ -76,11 +98,11 @@ private void possiblyResolveStudy(@Nullable Map<String, Object> row, Container c
7698
if (!NumberUtils.isCreatable(row.get(sourceProperty).toString()))
7799
{
78100
Container target = c.isWorkbookOrTab() ? c.getParent() : c;
79-
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("container"), target.getEntityId()).addCondition(FieldKey.fromString("studyName"), row.get(sourceProperty));
80-
List<Integer> rowIds = new TableSelector(StudiesSchema.getInstance().getSchema().getTable(StudiesSchema.TABLE_STUDIES), PageFlowUtil.set("rowId"), filter, null).getArrayList(Integer.class);
101+
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("container"), target.getEntityId()).addCondition(FieldKey.fromString(filterFieldName), row.get(sourceProperty));
102+
List<Integer> rowIds = new TableSelector(StudiesSchema.getInstance().getSchema().getTable(StudiesSchema.TABLE_COHORTS), PageFlowUtil.set("rowId"), filter, null).getArrayList(Integer.class);
81103
if (rowIds.size() == 1)
82104
{
83-
row.put("studyId", rowIds.get(0));
105+
row.put(targetFieldName, rowIds.get(0));
84106
}
85107
}
86108
}

0 commit comments

Comments
 (0)