Skip to content

Commit bef9938

Browse files
committed
Change pattern used in AutoCreateDemographicsTrigger
1 parent 8c5c597 commit bef9938

File tree

1 file changed

+11
-32
lines changed

1 file changed

+11
-32
lines changed

SivStudies/src/org/labkey/sivstudies/query/AutoCreateDemographicsTrigger.java

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Collection;
2828
import java.util.List;
2929
import java.util.Map;
30+
import java.util.Set;
3031

3132
public class AutoCreateDemographicsTrigger extends DefaultDatasetTrigger
3233
{
@@ -46,55 +47,33 @@ public Factory()
4647
}
4748
}
4849

49-
private static final String CACHE_KEY = "~~AutoCreateDemographicsTrigger.IdsToCreate~~";
50+
private final Set<String> _idsToCheck = new CaseInsensitiveHashSet();
5051

5152
@Override
5253
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
5354
{
54-
if (extraContext == null)
55+
String idField = getIdField(c);
56+
String id = newRow.get(idField) != null ? newRow.get(idField).toString() : null;
57+
if (id != null)
5558
{
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-
}
59+
_idsToCheck.add(id);
7360
}
7461
}
7562

7663
@Override
7764
public void complete(TableInfo table, Container c, User user, TableInfo.TriggerType event, BatchValidationException errors, Map<String, Object> extraContext)
7865
{
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)
66+
if (!_idsToCheck.isEmpty())
8667
{
87-
s = new CaseInsensitiveHashSet(s);
88-
8968
String idField = getIdField(c);
9069
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);
70+
List<String> existingIds = new TableSelector(ti, PageFlowUtil.set(idField), new SimpleFilter(FieldKey.fromString(idField), _idsToCheck, CompareType.IN), null).getArrayList(String.class);
9271

93-
s.removeAll(existingIds);
72+
_idsToCheck.removeAll(existingIds);
9473

95-
if (!s.isEmpty())
74+
if (!_idField.isEmpty())
9675
{
97-
List<Map<String, Object>> toInsert = s.stream().map(id -> Map.of(idField, (Object)id)).toList();
76+
List<Map<String, Object>> toInsert = _idsToCheck.stream().map(id -> Map.of(idField, (Object)id)).toList();
9877
try
9978
{
10079
ti.getUpdateService().insertRows(user, c, toInsert, null, null, null);

0 commit comments

Comments
 (0)