Skip to content

Commit

Permalink
EventNotesDataIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
labkey-martyp committed Oct 30, 2024
1 parent 5cd0482 commit 7c38cde
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 13 deletions.
92 changes: 92 additions & 0 deletions src/org/labkey/snd/query/EventNotesDataIterator.java
Original file line number Diff line number Diff line change
@@ -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<Integer> _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();
}
}
33 changes: 33 additions & 0 deletions src/org/labkey/snd/query/EventNotesDataIteratorBuilder.java
Original file line number Diff line number Diff line change
@@ -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);
}
}


16 changes: 3 additions & 13 deletions src/org/labkey/snd/query/EventNotesTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SNDUserSchema>
{
Expand Down Expand Up @@ -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.");
Expand All @@ -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<Integer> 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;
}
Expand Down

0 comments on commit 7c38cde

Please sign in to comment.