forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStudiesTriggerFactory.java
More file actions
116 lines (102 loc) · 4.79 KB
/
StudiesTriggerFactory.java
File metadata and controls
116 lines (102 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package org.labkey.studies.query;
import org.apache.commons.lang3.math.NumberUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.labkey.api.data.Container;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.TableSelector;
import org.labkey.api.data.triggers.Trigger;
import org.labkey.api.data.triggers.TriggerFactory;
import org.labkey.api.query.FieldKey;
import org.labkey.api.query.ValidationException;
import org.labkey.api.security.User;
import org.labkey.api.util.PageFlowUtil;
import org.labkey.studies.StudiesSchema;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class StudiesTriggerFactory implements TriggerFactory
{
@Override
public @NotNull Collection<Trigger> createTrigger(@Nullable Container c, TableInfo table, Map<String, Object> extraContext)
{
return List.of(new StudyTrigger());
}
public static class StudyTrigger implements Trigger
{
@Override
public void beforeInsert(TableInfo table, Container c, User user, @Nullable Map<String, Object> newRow, ValidationException errors, Map<String, Object> extraContext) throws ValidationException
{
beforeInsert(table, c, user, newRow, errors, extraContext, null);
}
@Override
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
{
possiblyResolveStudy(table, newRow, existingRecord, c);
}
@Override
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
{
possiblyResolveStudy(table, newRow, oldRow, c);
}
/**
* This allows incoming data to specify the study using the string name, which is resolved into the rowId
*/
private void possiblyResolveStudy(TableInfo table, @Nullable Map<String, Object> row, @Nullable Map<String, Object> oldRow, Container c)
{
if (row == null)
{
return;
}
if (table.getColumn("studyId") != null)
{
possiblyResolveStudy(row, c, "studyId");
if (row.get("studyId") == null & row.get("studyName") != null)
{
possiblyResolveStudy(row, c, "studyName");
}
}
if (table.getColumn("cohortId") != null)
{
possiblyResolveCohort(row, c, "cohortId");
if (row.get("cohortId") == null & row.get("cohortName") != null)
{
possiblyResolveCohort(row, c, "cohortName");
}
}
}
private void possiblyResolveStudy(@Nullable Map<String, Object> row, Container c, String sourceProperty)
{
possiblyResolveStudyOrCohort(StudiesSchema.TABLE_STUDIES, row, c, sourceProperty, "studyId", "studyName");
}
private void possiblyResolveCohort(@Nullable Map<String, Object> row, Container c, String sourceProperty)
{
possiblyResolveStudyOrCohort(StudiesSchema.TABLE_COHORTS, row, c, sourceProperty, "cohortId", "cohortName");
}
private void possiblyResolveStudyOrCohort(String tableToQuery, @Nullable Map<String, Object> row, Container c, String sourceProperty, String targetFieldName, String filterFieldName)
{
if (row == null)
{
return;
}
if (row.get(sourceProperty) instanceof Integer)
{
return;
}
if (row.get(sourceProperty) != null & row.get(sourceProperty) instanceof String & !String.valueOf(row.get(sourceProperty)).isEmpty())
{
if (!NumberUtils.isCreatable(row.get(sourceProperty).toString()))
{
Container target = c.isWorkbookOrTab() ? c.getParent() : c;
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("container"), target.getEntityId()).addCondition(FieldKey.fromString(filterFieldName), row.get(sourceProperty));
List<Integer> rowIds = new TableSelector(StudiesSchema.getInstance().getSchema().getTable(tableToQuery), PageFlowUtil.set("rowId"), filter, null).getArrayList(Integer.class);
if (rowIds.size() == 1)
{
row.put(targetFieldName, rowIds.get(0));
}
}
}
}
}
}