|
| 1 | +package org.labkey.sivstudies.query; |
| 2 | + |
| 3 | +import org.apache.logging.log4j.Logger; |
| 4 | +import org.jetbrains.annotations.NotNull; |
| 5 | +import org.jetbrains.annotations.Nullable; |
| 6 | +import org.labkey.api.collections.CaseInsensitiveHashSet; |
| 7 | +import org.labkey.api.data.CompareType; |
| 8 | +import org.labkey.api.data.Container; |
| 9 | +import org.labkey.api.data.SimpleFilter; |
| 10 | +import org.labkey.api.data.TableInfo; |
| 11 | +import org.labkey.api.data.TableSelector; |
| 12 | +import org.labkey.api.data.triggers.Trigger; |
| 13 | +import org.labkey.api.data.triggers.TriggerFactory; |
| 14 | +import org.labkey.api.query.BatchValidationException; |
| 15 | +import org.labkey.api.query.DuplicateKeyException; |
| 16 | +import org.labkey.api.query.FieldKey; |
| 17 | +import org.labkey.api.query.QueryService; |
| 18 | +import org.labkey.api.query.QueryUpdateServiceException; |
| 19 | +import org.labkey.api.query.ValidationException; |
| 20 | +import org.labkey.api.security.User; |
| 21 | +import org.labkey.api.study.Study; |
| 22 | +import org.labkey.api.study.StudyService; |
| 23 | +import org.labkey.api.util.PageFlowUtil; |
| 24 | +import org.labkey.api.util.logging.LogHelper; |
| 25 | + |
| 26 | +import java.sql.SQLException; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +public class AutoCreateDemographicsTrigger extends DefaultDatasetTrigger |
| 32 | +{ |
| 33 | + protected static final Logger _log = LogHelper.getLogger(DefaultDatasetTrigger.class, "Messages related to CreateDemographicsTrigger"); |
| 34 | + |
| 35 | + public static class Factory implements TriggerFactory |
| 36 | + { |
| 37 | + public Factory() |
| 38 | + { |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public @NotNull Collection<Trigger> createTrigger(@Nullable Container c, TableInfo table, Map<String, Object> extraContext) |
| 44 | + { |
| 45 | + return List.of(new AutoCreateDemographicsTrigger()); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static final String CACHE_KEY = "~~AutoCreateDemographicsTrigger.IdsToCreate~~"; |
| 50 | + |
| 51 | + @Override |
| 52 | + protected void afterUpsert(TableInfo table, Container c, User user, @Nullable Map<String, Object> newRow, @Nullable Map<String, Object> oldRow, ValidationException errors, Map<String, Object> extraContext) throws ValidationException |
| 53 | + { |
| 54 | + if (extraContext == null) |
| 55 | + { |
| 56 | + _log.error("extraContext is null in AutoCreateDemographicsTrigger.afterUpsert()"); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (!extraContext.containsKey(AutoCreateDemographicsTrigger.CACHE_KEY)) |
| 61 | + { |
| 62 | + extraContext.put(CACHE_KEY, new CaseInsensitiveHashSet()); |
| 63 | + } |
| 64 | + |
| 65 | + if (extraContext.get(CACHE_KEY) instanceof CaseInsensitiveHashSet s) |
| 66 | + { |
| 67 | + String idField = getIdField(c); |
| 68 | + String id = newRow.get(idField) != null ? newRow.get(idField).toString() : null; |
| 69 | + if (id != null) |
| 70 | + { |
| 71 | + s.add(id); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void complete(TableInfo table, Container c, User user, TableInfo.TriggerType event, BatchValidationException errors, Map<String, Object> extraContext) |
| 78 | + { |
| 79 | + if (extraContext == null) |
| 80 | + { |
| 81 | + _log.error("extraContext is null in AutoCreateDemographicsTrigger.complete()"); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (extraContext.get(CACHE_KEY) instanceof CaseInsensitiveHashSet s) |
| 86 | + { |
| 87 | + s = new CaseInsensitiveHashSet(s); |
| 88 | + |
| 89 | + String idField = getIdField(c); |
| 90 | + TableInfo ti = QueryService.get().getUserSchema(user, getTargetContainer(c), "study").getTable("demographics"); |
| 91 | + List<String> existingIds = new TableSelector(ti, PageFlowUtil.set(idField), new SimpleFilter(FieldKey.fromString(idField), s, CompareType.IN), null).getArrayList(String.class); |
| 92 | + |
| 93 | + s.removeAll(existingIds); |
| 94 | + |
| 95 | + if (!s.isEmpty()) |
| 96 | + { |
| 97 | + List<Map<String, Object>> toInsert = s.stream().map(id -> Map.of(idField, (Object)id)).toList(); |
| 98 | + try |
| 99 | + { |
| 100 | + ti.getUpdateService().insertRows(user, c, toInsert, null, null, null); |
| 101 | + } |
| 102 | + catch (SQLException | BatchValidationException | QueryUpdateServiceException | DuplicateKeyException e) |
| 103 | + { |
| 104 | + _log.error("Error creating demographics records", e); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private String _idField = null; |
| 111 | + |
| 112 | + private String getIdField(Container c) |
| 113 | + { |
| 114 | + if (_idField == null) |
| 115 | + { |
| 116 | + Study s = StudyService.get().getStudy(getTargetContainer(c)); |
| 117 | + if (s == null) |
| 118 | + { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + _idField = s.getSubjectColumnName(); |
| 123 | + } |
| 124 | + |
| 125 | + return _idField; |
| 126 | + } |
| 127 | +} |
0 commit comments