|
| 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 | +} |
0 commit comments