Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue where certain subpackages were not getting populated in the getEvent response #218

46 changes: 22 additions & 24 deletions src/org/labkey/snd/SNDManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3778,7 +3778,7 @@ public Event getEvent(Container c, User u, int eventId, Set<EventNarrativeOption
public Map<Integer, Event> getBulkEvents(Container c, User u, List<Integer> eventIds, Set<EventNarrativeOption> narrativeOptions,
@Nullable Map<Integer, Map<Integer, SuperPackage>> topLevelSuperPkgs, boolean skipPermissionCheck, BatchValidationException errors,
List<GWTPropertyDescriptor> eventExtraFields, List<GWTPropertyDescriptor> eventDataExtraFields,
boolean includeEmptySubPackges) {
boolean includeEmptySubPackages) {

// Events from query - SELECT * FROM Events WHERE EventId IN {eventIds}
TableSelector eventSelector = getTableSelector(c, u, eventIds, SNDSchema.EVENTS_TABLE_NAME, Event.EVENT_ID, null, null);
Expand All @@ -3796,7 +3796,7 @@ public Map<Integer, Event> getBulkEvents(Container c, User u, List<Integer> even
Map<String, String> projectIdRevs = getBulkProjectIdRevs(c, u, events.stream().map(Event::getParentObjectId).collect(Collectors.toList()));

//EventData grouped by EventId
Map<Integer, List<EventData>> eventData = getBulkEventData(c, topLevelSuperPkgs, rawEventData, eventDataExtensibleFields, eventDataExtraFields, includeEmptySubPackges);
Map<Integer, List<EventData>> eventData = getBulkEventData(c, topLevelSuperPkgs, rawEventData, eventDataExtensibleFields, eventDataExtraFields, includeEmptySubPackages);

// Build events from eventData, eventNotes, and project data and group by EventId
Map<Integer, Event> eventsById = events.stream().collect(Collectors.toMap(Event::getEventId, (Event event) -> {
Expand Down Expand Up @@ -4044,17 +4044,14 @@ private Map<Integer, List<EventData>> getBulkEventData(Container c, Map<Integer,

addExtraFieldsToEventData(eventData, eventDataExtraFields, extraFields);

boolean hasEmptySubpackages =
includeEmptySubPackages
&&
!superPackagesByEventDataId.get(eventData.getEventDataId()).getChildPackages().isEmpty();
boolean hasSubpackages = !superPackagesByEventDataId.get(eventData.getEventDataId()).getChildPackages().isEmpty();

Map<Integer, Map<Integer, SuperPackage>> nextLevelSuperPkgs = getNextLevelEventDataSuperPkgs(eventData, childEventData, currentLevelSuperPkgs, includeEmptySubPackages, hasEmptySubpackages);
Map<Integer, Map<Integer, SuperPackage>> nextLevelSuperPkgs = getNextLevelEventDataSuperPkgs(eventData, childEventData, currentLevelSuperPkgs, includeEmptySubPackages, hasSubpackages);

if (nextLevelSuperPkgs != null && !nextLevelSuperPkgs.isEmpty()) {
if (nextLevelSuperPkgs != null && !nextLevelSuperPkgs.get(eventData.getEventId()).isEmpty()) {
// Recursion for next child level of sub packages
Map<Integer, List<EventData>> subEventData = getBulkEventData(c, nextLevelSuperPkgs, currentEventData, eventDataExtensibleFields, eventDataExtraFields, includeEmptySubPackages);
if (subEventData != null) {
if (subEventData != null && subEventData.containsKey(eventData.getEventId())) {
List<EventData> sorted = subEventData.get(eventData.getEventId()).stream().sorted(Comparator.comparing(
(EventData child) -> nextLevelSuperPkgs.get(child.getEventId()).get(child.getEventDataId()).getTreePath()))
.filter(ed -> ed.getParentEventDataId() == null || ed.getParentEventDataId().equals(eventData.getEventDataId()))
Expand Down Expand Up @@ -4138,9 +4135,9 @@ private AttributeData getAttributeData(GWTPropertyDescriptor propertyDescriptor,
*/
private Map<Integer, Map<Integer, SuperPackage>> getNextLevelEventDataSuperPkgs(EventData eventData, Map<Integer,
List<EventData>> childEventData, Map<Integer, Map<Integer, SuperPackage>> currentLevelSuperPkgs,
boolean includeEmptySubPackages, boolean hasChildPackages) {
boolean includeEmptySubPackages, boolean hasSubpackages) {

if (!childEventData.containsKey(eventData.getEventId()) && !hasChildPackages) {
if ((!includeEmptySubPackages && !childEventData.containsKey(eventData.getEventId())) || (includeEmptySubPackages && !hasSubpackages)) {
return null;
}

Expand All @@ -4157,10 +4154,10 @@ private Map<Integer, Map<Integer, SuperPackage>> getNextLevelEventDataSuperPkgs(
));

// Get superPkg for eventData and group by eventId and then by eventId
Map<Integer, Map<Integer, SuperPackage>> nextLevelEventDataSuperPkgs;
List<Integer> eventDataSuperPkgIds;
Map<Integer, Map<Integer, SuperPackage>> nextLevelEventDataSuperPkgs = new HashMap<>();
nextLevelEventDataSuperPkgs.put(eventData.getEventId(), new HashMap<>());

if (!childEventData.isEmpty()) {
if (!childSuperPkgs.isEmpty() && !childEventData.isEmpty()) {
Map<Integer, SuperPackage> children = childSuperPkgs;
nextLevelEventDataSuperPkgs = childEventData.get(eventData.getEventId())
.stream()
Expand All @@ -4174,24 +4171,25 @@ private Map<Integer, Map<Integer, SuperPackage>> getNextLevelEventDataSuperPkgs(
)
)
);
eventDataSuperPkgIds = nextLevelEventDataSuperPkgs.containsKey(eventData.getEventId())
? nextLevelEventDataSuperPkgs.get(eventData.getEventId()).values().stream().map(SuperPackage::getSuperPkgId).toList()
: new ArrayList<>();
}
else
{
nextLevelEventDataSuperPkgs = new HashMap<>();

if (!nextLevelEventDataSuperPkgs.containsKey(eventData.getEventId())) {
nextLevelEventDataSuperPkgs.put(eventData.getEventId(), new HashMap<>());
eventDataSuperPkgIds = new ArrayList<>();
}

AtomicInteger emptyEventDataId = new AtomicInteger(0);

if (includeEmptySubPackages && nextLevelEventDataSuperPkgs.containsKey(eventData.getEventId())) {
if (includeEmptySubPackages) {

List<Integer> eventDataSuperPkgIds = !nextLevelEventDataSuperPkgs.get(eventData.getEventId()).isEmpty()
? nextLevelEventDataSuperPkgs.get(eventData.getEventId()).values().stream().map(SuperPackage::getSuperPkgId).toList()
: new ArrayList<>();

AtomicInteger emptyEventDataId = new AtomicInteger(0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can i please know if we are using any multithreads in here as i do see AtomicInteger? i checked in the sndManager code and i don't see any threads being used in this file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but in order to iterate an Integer object within a stream it needs to be Atomic

Map<Integer, Map<Integer, SuperPackage>> eventDataSuperPkgs = nextLevelEventDataSuperPkgs;
childSuperPkgs.values().stream()
.filter(superPkg -> !eventDataSuperPkgIds.contains(superPkg.getSuperPkgId()))
.forEach(spkg -> {
nextLevelEventDataSuperPkgs.get(eventData.getEventId()).put(emptyEventDataId.getAndAdd(1), spkg);
eventDataSuperPkgs.get(eventData.getEventId()).put(emptyEventDataId.getAndAdd(1), spkg);
});
}

Expand Down