2727import java .util .Collection ;
2828import java .util .List ;
2929import java .util .Map ;
30+ import java .util .Set ;
3031
3132public 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