diff --git a/src/org/labkey/snd/query/EventNotesDataIterator.java b/src/org/labkey/snd/query/EventNotesDataIterator.java new file mode 100644 index 0000000..a1bea49 --- /dev/null +++ b/src/org/labkey/snd/query/EventNotesDataIterator.java @@ -0,0 +1,92 @@ +package org.labkey.snd.query; + +import org.apache.logging.log4j.Logger; +import org.labkey.api.data.ColumnInfo; +import org.labkey.api.data.Container; +import org.labkey.api.data.DbScope; +import org.labkey.api.dataiterator.AbstractDataIterator; +import org.labkey.api.dataiterator.DataIterator; +import org.labkey.api.dataiterator.DataIteratorContext; +import org.labkey.api.dataiterator.DataIteratorUtil; +import org.labkey.api.query.BatchValidationException; +import org.labkey.api.query.UserSchema; +import org.labkey.api.security.User; +import org.labkey.api.util.logging.LogHelper; +import org.labkey.snd.SNDManager; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +public class EventNotesDataIterator extends AbstractDataIterator +{ + private static final SNDManager _sndManager = SNDManager.get(); + private static final String EVENT_ID_COL = "eventId"; + private static final String EVENT_NOTE_ID_COL = "eventNoteId"; + private User _user; + private Container _container; + private int _eventIdColIndex; + private Set _eventIds = new HashSet<>(); + private DataIterator _in; + private Logger log = LogHelper.getLogger(EventNotesDataIterator.class, "Fill out event notes"); + + public static DataIterator wrap(DataIterator in, DataIteratorContext context, Container c, User u) + { + return new EventNotesDataIterator(in, context, c, u); + } + + private EventNotesDataIterator(DataIterator in, DataIteratorContext context, Container c, User u) + { + super(context); + _user = u; + _container = c; + _in = in; + + _eventIdColIndex = DataIteratorUtil.createColumnNameMap(in).get(EVENT_ID_COL); + } + + @Override + public int getColumnCount() + { + return _in.getColumnCount(); + } + + @Override + public ColumnInfo getColumnInfo(int i) + { + return _in.getColumnInfo(i); + } + + @Override + public boolean next() throws BatchValidationException + { + boolean hasNext = _in.next(); + if (hasNext) + { + Integer eventId = (Integer)_in.get(_eventIdColIndex); + _eventIds.add(eventId); + } + else + { + UserSchema schema = SNDManager.getSndUserSchema(_container, _user); + + // Add a post commit task to update the narrative cache after the transaction updating the notes is committed. + SNDManager.get().getTableInfo(schema, "EventNotes").getSchema().getScope().addCommitTask(() -> { + _sndManager.updateNarrativeCache(_container, _user, _eventIds, log); + }, DbScope.CommitTaskOption.POSTCOMMIT); + } + return hasNext; + } + + @Override + public Object get(int i) + { + return _in.get(i); + } + + @Override + public void close() throws IOException + { + _in.close(); + } +} diff --git a/src/org/labkey/snd/query/EventNotesDataIteratorBuilder.java b/src/org/labkey/snd/query/EventNotesDataIteratorBuilder.java new file mode 100644 index 0000000..a3190fb --- /dev/null +++ b/src/org/labkey/snd/query/EventNotesDataIteratorBuilder.java @@ -0,0 +1,33 @@ +package org.labkey.snd.query; + +import org.jetbrains.annotations.NotNull; +import org.labkey.api.data.Container; +import org.labkey.api.dataiterator.DataIterator; +import org.labkey.api.dataiterator.DataIteratorBuilder; +import org.labkey.api.dataiterator.DataIteratorContext; +import org.labkey.api.dataiterator.DataIteratorUtil; +import org.labkey.api.security.User; + +public class EventNotesDataIteratorBuilder implements DataIteratorBuilder +{ + private final DataIteratorBuilder in; + private final User user; + private final Container container; + + public EventNotesDataIteratorBuilder(@NotNull DataIteratorBuilder in, User user, Container container) + { + this.in = in; + this.user = user; + this.container = container; + } + + @Override + public DataIterator getDataIterator(DataIteratorContext context) + { + DataIterator it = in.getDataIterator(context); + DataIterator in = DataIteratorUtil.wrapMap(it, false); + return EventNotesDataIterator.wrap(in, context, container, user); + } +} + + diff --git a/src/org/labkey/snd/query/EventNotesTable.java b/src/org/labkey/snd/query/EventNotesTable.java index 5b389dc..a0bd74d 100644 --- a/src/org/labkey/snd/query/EventNotesTable.java +++ b/src/org/labkey/snd/query/EventNotesTable.java @@ -39,8 +39,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; public class EventNotesTable extends SimpleUserSchema.SimpleTable { @@ -98,7 +96,7 @@ public int mergeRows(User user, Container container, DataIteratorBuilder rows, B { Logger log = SNDManager.getLogger(configParameters, EventNotesTable.class); // Large merge triggers importRows path - int result = 0; + int result; if (getRowCount(rows, configParameters, errors) > SNDManager.MAX_MERGE_ROWS) { log.info("More than " + SNDManager.MAX_MERGE_ROWS + " rows. using importRows method."); @@ -107,17 +105,9 @@ public int mergeRows(User user, Container container, DataIteratorBuilder rows, B else { log.info("Merging rows."); + DataIteratorBuilder dib = new EventNotesDataIteratorBuilder(rows, user, container); - DataIteratorContext context = getDataIteratorContext(errors, QueryUpdateService.InsertOption.MERGE, configParameters); - - Set eventIds = rows.getDataIterator(context).stream() - .filter(row -> row.containsKey("eventId")) - .map(row -> (Integer) row.get("eventId")) - .collect(Collectors.toSet()); - - result = super.mergeRows(user, container, rows, errors, configParameters, extraScriptContext); - - _sndManager.updateNarrativeCache(container, user, eventIds, log); + result = super.mergeRows(user, container, dib, errors, configParameters, extraScriptContext); } return result; }