Skip to content

Commit 85a45dc

Browse files
committed
Add studies trigger
1 parent bade9bd commit 85a45dc

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

Studies/src/org/labkey/studies/StudiesSchema.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ public class StudiesSchema
99
private static final StudiesSchema _instance = new StudiesSchema();
1010
public static final String NAME = "studies";
1111

12+
public static final String TABLE_STUDIES = "studies";
13+
public static final String TABLE_COHORTS = "studyCohorts";
14+
public static final String TABLE_ANCHOR_EVENTS = "anchorEvents";
15+
public static final String TABLE_EXPECTED_TIMEPOINTS = "expectedTimepoints";
16+
public static final String TABLE_TIMEPOINT_TO_DATE = "timepointToDate";
17+
1218
public static StudiesSchema getInstance()
1319
{
1420
return _instance;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.labkey.studies.query;
2+
3+
import org.apache.commons.lang3.math.NumberUtils;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
6+
import org.labkey.api.data.Container;
7+
import org.labkey.api.data.SimpleFilter;
8+
import org.labkey.api.data.TableInfo;
9+
import org.labkey.api.data.TableSelector;
10+
import org.labkey.api.data.triggers.Trigger;
11+
import org.labkey.api.data.triggers.TriggerFactory;
12+
import org.labkey.api.query.FieldKey;
13+
import org.labkey.api.query.ValidationException;
14+
import org.labkey.api.security.User;
15+
import org.labkey.api.util.PageFlowUtil;
16+
import org.labkey.studies.StudiesSchema;
17+
18+
import java.util.Collection;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
public class StudiesTriggerFactory implements TriggerFactory
23+
{
24+
@Override
25+
public @NotNull Collection<Trigger> createTrigger(@Nullable Container c, TableInfo table, Map<String, Object> extraContext)
26+
{
27+
return List.of(new StudyTrigger());
28+
}
29+
30+
public static class StudyTrigger implements Trigger
31+
{
32+
@Override
33+
public void beforeInsert(TableInfo table, Container c, User user, @Nullable Map<String, Object> newRow, ValidationException errors, Map<String, Object> extraContext) throws ValidationException
34+
{
35+
beforeInsert(table, c, user, newRow, errors, extraContext, null);
36+
}
37+
38+
@Override
39+
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
40+
{
41+
possiblyResolveStudy(newRow, c);
42+
}
43+
44+
@Override
45+
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
46+
{
47+
possiblyResolveStudy(newRow, c);
48+
}
49+
50+
/**
51+
* This allows incoming data to specify the study using the string name, which is resolved into the rowId
52+
*/
53+
private void possiblyResolveStudy(@Nullable Map<String, Object> row, Container c)
54+
{
55+
if (row == null)
56+
{
57+
return;
58+
}
59+
60+
possiblyResolveStudy(row, c, "studyId");
61+
if (row.get("studyId") == null & row.get("studyName") != null)
62+
{
63+
possiblyResolveStudy(row, c, "studyName");
64+
}
65+
}
66+
67+
private void possiblyResolveStudy(@Nullable Map<String, Object> row, Container c, String sourceProperty)
68+
{
69+
if (row == null)
70+
{
71+
return;
72+
}
73+
74+
if (row.get(sourceProperty) != null & row.get(sourceProperty) instanceof String)
75+
{
76+
if (!NumberUtils.isCreatable(row.get(sourceProperty).toString()))
77+
{
78+
Container target = c.isWorkbookOrTab() ? c.getParent() : c;
79+
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("container"), target.getEntityId()).addCondition(FieldKey.fromString("name"), row.get(sourceProperty));
80+
List<Integer> rowIds = new TableSelector(StudiesSchema.getInstance().getSchema().getTable(StudiesSchema.TABLE_STUDIES), PageFlowUtil.set("rowId"), filter, null).getArrayList(Integer.class);
81+
if (rowIds.size() == 1)
82+
{
83+
row.put("studyId", rowIds.get(0));
84+
}
85+
}
86+
}
87+
}
88+
}
89+
}

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.labkey.api.data.SimpleFilter;
1212
import org.labkey.api.data.TableInfo;
1313
import org.labkey.api.data.TableSelector;
14-
import org.labkey.api.data.dialect.SqlDialect;
1514
import org.labkey.api.ldk.table.ContainerScopedTable;
1615
import org.labkey.api.ldk.table.CustomPermissionsTable;
1716
import org.labkey.api.query.FieldKey;
@@ -35,6 +34,11 @@
3534
import java.util.Map;
3635
import java.util.Set;
3736

37+
import static org.labkey.studies.StudiesSchema.TABLE_ANCHOR_EVENTS;
38+
import static org.labkey.studies.StudiesSchema.TABLE_COHORTS;
39+
import static org.labkey.studies.StudiesSchema.TABLE_EXPECTED_TIMEPOINTS;
40+
import static org.labkey.studies.StudiesSchema.TABLE_STUDIES;
41+
import static org.labkey.studies.StudiesSchema.TABLE_TIMEPOINT_TO_DATE;
3842
import static org.labkey.studies.query.LookupSetsManager.TABLE_LOOKUPS;
3943
import static org.labkey.studies.query.LookupSetsManager.TABLE_LOOKUP_SETS;
4044

@@ -118,6 +122,26 @@ else if (TABLE_LOOKUPS.equalsIgnoreCase(name))
118122
ret.addPermissionMapping(ReadPermission.class, StudiesDataAdminPermission.class);
119123
return ret.init();
120124
}
125+
else if (TABLE_STUDIES.equalsIgnoreCase(name))
126+
{
127+
return createStudyDesignTable(name, cf);
128+
}
129+
else if (TABLE_COHORTS.equalsIgnoreCase(name))
130+
{
131+
return createStudyDesignTable(name, cf);
132+
}
133+
else if (TABLE_ANCHOR_EVENTS.equalsIgnoreCase(name))
134+
{
135+
return createStudyDesignTable(name, cf);
136+
}
137+
else if (TABLE_EXPECTED_TIMEPOINTS.equalsIgnoreCase(name))
138+
{
139+
return createStudyDesignTable(name, cf);
140+
}
141+
else if (TABLE_TIMEPOINT_TO_DATE.equalsIgnoreCase(name))
142+
{
143+
return createStudyDesignTable(name, cf);
144+
}
121145
else if (TABLE_EVENT_TYPES.equalsIgnoreCase(name))
122146
{
123147
return createEventTypesTable(getContainer());
@@ -131,6 +155,18 @@ else if (TABLE_EVENT_TYPES.equalsIgnoreCase(name))
131155
return super.createTable(name, cf);
132156
}
133157

158+
private TableInfo createStudyDesignTable(String name, ContainerFilter cf)
159+
{
160+
CustomPermissionsTable<SimpleUserSchema> ret = new CustomPermissionsTable<>(this, createSourceTable(name), cf);
161+
ret.addPermissionMapping(InsertPermission.class, StudiesDataAdminPermission.class);
162+
ret.addPermissionMapping(UpdatePermission.class, StudiesDataAdminPermission.class);
163+
ret.addPermissionMapping(DeletePermission.class, StudiesDataAdminPermission.class);
164+
ret.addPermissionMapping(ReadPermission.class, StudiesDataAdminPermission.class);
165+
ret.addTriggerFactory(new StudiesTriggerFactory());
166+
167+
return ret.init();
168+
}
169+
134170
private LookupSetTable createForPropertySet(StudiesUserSchema us, ContainerFilter cf, String setName, Map<String, Object> map)
135171
{
136172
SchemaTableInfo table = _dbSchema.getTable(TABLE_LOOKUPS);

0 commit comments

Comments
 (0)