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

[bot] Merge 23.11 to 24.3 #226

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/org/labkey/snd/SNDController.java
Original file line number Diff line number Diff line change
Expand Up @@ -1040,14 +1040,19 @@ public ApiResponse execute(SimpleApiJsonForm form, BindException errors)
}

Event savedEvent = SNDService.get().saveEvent(getContainer(), getUser(), event, validateOnly);
response.put("event", savedEvent.toJSON(getContainer(), getUser()));

if (savedEvent.hasErrors())
{
response.put("success", false);
}
else
{
if (savedEvent != null) {
response.put("event", savedEvent.toJSON(getContainer(), getUser()));
if (savedEvent.hasErrors())
{
response.put("success", false);
}
else
{
response.put("success", true);
}
} else {
response.put("event", null);
response.put("success", true);
}
}
Expand Down
92 changes: 68 additions & 24 deletions src/org/labkey/snd/SNDManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2941,6 +2941,52 @@ private void deleteExpObjects(Container c, User u, int eventId)
}
}

public Event deleteEvent(Container c, User u, Event event) {

if (!event.hasErrors()) {
UserSchema schema = getSndUserSchemaAdminRole(c, u);
TableInfo eventTable = getTableInfo(schema, SNDSchema.EVENTS_TABLE_NAME);
QueryUpdateService eventQus = getQueryUpdateService(eventTable);

BatchValidationException errors = new BatchValidationException();

try (DbScope.Transaction tx = eventTable.getSchema().getScope().ensureTransaction())
{
deleteEventDatas(c, u, event.getEventId());
deleteEventNotes(c, u, event.getEventId());
deleteEventsCache(c, u, event.getEventId());
NarrativeAuditProvider.addAuditEntry(c, u, event.getEventId(), event.getSubjectId(), event.getDate(), "", event.getQcState(), "Delete event");

Map<String, Object> row;
List<Map<String, Object>> rows = new ArrayList<>();
row = new HashMap<>();
row.put("EventId", event.getEventId());
rows.add(row);
eventQus.deleteRows(u, c, rows, null, null);

tx.commit();

} catch (QueryUpdateServiceException | BatchValidationException | SQLException | InvalidKeyException e) {

event.setException(new ValidationException(e.getMessage(), ValidationException.SEVERITY.ERROR));

} finally
{
if (errors.hasErrors())
{
event.addBatchValidationExceptions(errors);
}
// Errors, warnings and info are embedded in the event so don't override
else if (!event.hasErrorsWarningsOrInfo())
{
event = getEvent(c, u, event.getEventId(), null, null, true, errors);
}
}
}

return event;
}

/**
* Called from SNDService.saveEvent to update an existing event.
*/
Expand Down Expand Up @@ -3778,7 +3824,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 +3842,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 +4090,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 +4181,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 +4200,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 +4217,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);
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
12 changes: 11 additions & 1 deletion src/org/labkey/snd/SNDServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,17 @@ public Event saveEvent(Container c, User u, Event event, boolean validateOnly)
{
if (event.getEventId() != null && SNDManager.get().eventExists(c, u, event.getEventId()))
{
event = SNDManager.get().updateEvent(c, u, event, validateOnly);
if ((event.getEventData() == null || event.getEventData().isEmpty()) &&
(!event.getEventNotesRow(c).containsKey("note") ||
event.getEventNotesRow(c).get("note") == null ||
event.getEventNotesRow(c).get("note").toString().trim().length() == 0))
{
event = SNDManager.get().deleteEvent(c, u, event);
}
else
{
event = SNDManager.get().updateEvent(c, u, event, validateOnly);
}
}
else
{
Expand Down
Loading