diff --git a/src/org/labkey/snd/SNDController.java b/src/org/labkey/snd/SNDController.java index 1c7fc01..a38af14 100644 --- a/src/org/labkey/snd/SNDController.java +++ b/src/org/labkey/snd/SNDController.java @@ -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); } } diff --git a/src/org/labkey/snd/SNDManager.java b/src/org/labkey/snd/SNDManager.java index 4392d6c..93338a9 100644 --- a/src/org/labkey/snd/SNDManager.java +++ b/src/org/labkey/snd/SNDManager.java @@ -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 row; + List> 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. */ @@ -3778,7 +3824,7 @@ public Event getEvent(Container c, User u, int eventId, Set getBulkEvents(Container c, User u, List eventIds, Set narrativeOptions, @Nullable Map> topLevelSuperPkgs, boolean skipPermissionCheck, BatchValidationException errors, List eventExtraFields, List 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); @@ -3796,7 +3842,7 @@ public Map getBulkEvents(Container c, User u, List even Map projectIdRevs = getBulkProjectIdRevs(c, u, events.stream().map(Event::getParentObjectId).collect(Collectors.toList())); //EventData grouped by EventId - Map> eventData = getBulkEventData(c, topLevelSuperPkgs, rawEventData, eventDataExtensibleFields, eventDataExtraFields, includeEmptySubPackges); + Map> eventData = getBulkEventData(c, topLevelSuperPkgs, rawEventData, eventDataExtensibleFields, eventDataExtraFields, includeEmptySubPackages); // Build events from eventData, eventNotes, and project data and group by EventId Map eventsById = events.stream().collect(Collectors.toMap(Event::getEventId, (Event event) -> { @@ -4044,17 +4090,14 @@ private Map> getBulkEventData(Container c, Map> nextLevelSuperPkgs = getNextLevelEventDataSuperPkgs(eventData, childEventData, currentLevelSuperPkgs, includeEmptySubPackages, hasEmptySubpackages); + Map> 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> subEventData = getBulkEventData(c, nextLevelSuperPkgs, currentEventData, eventDataExtensibleFields, eventDataExtraFields, includeEmptySubPackages); - if (subEventData != null) { + if (subEventData != null && subEventData.containsKey(eventData.getEventId())) { List 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())) @@ -4138,9 +4181,9 @@ private AttributeData getAttributeData(GWTPropertyDescriptor propertyDescriptor, */ private Map> getNextLevelEventDataSuperPkgs(EventData eventData, Map> childEventData, Map> 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; } @@ -4157,10 +4200,10 @@ private Map> getNextLevelEventDataSuperPkgs( )); // Get superPkg for eventData and group by eventId and then by eventId - Map> nextLevelEventDataSuperPkgs; - List eventDataSuperPkgIds; + Map> nextLevelEventDataSuperPkgs = new HashMap<>(); + nextLevelEventDataSuperPkgs.put(eventData.getEventId(), new HashMap<>()); - if (!childEventData.isEmpty()) { + if (!childSuperPkgs.isEmpty() && !childEventData.isEmpty()) { Map children = childSuperPkgs; nextLevelEventDataSuperPkgs = childEventData.get(eventData.getEventId()) .stream() @@ -4174,24 +4217,25 @@ private Map> 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 eventDataSuperPkgIds = !nextLevelEventDataSuperPkgs.get(eventData.getEventId()).isEmpty() + ? nextLevelEventDataSuperPkgs.get(eventData.getEventId()).values().stream().map(SuperPackage::getSuperPkgId).toList() + : new ArrayList<>(); + + AtomicInteger emptyEventDataId = new AtomicInteger(0); + Map> 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); }); } diff --git a/src/org/labkey/snd/SNDServiceImpl.java b/src/org/labkey/snd/SNDServiceImpl.java index 0761acb..21cfab0 100644 --- a/src/org/labkey/snd/SNDServiceImpl.java +++ b/src/org/labkey/snd/SNDServiceImpl.java @@ -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 {