|
| 1 | +/* |
| 2 | + * SORMAS® - Surveillance Outbreak Response Management & Analysis System |
| 3 | + * Copyright © 2016-2026 SORMAS Foundation gGmbH |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * You should have received a copy of the GNU General Public License |
| 13 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 14 | + */ |
| 15 | + |
| 16 | +package de.symeda.sormas.backend.audit; |
| 17 | + |
| 18 | +import javax.ejb.Stateless; |
| 19 | +import javax.enterprise.inject.Instance; |
| 20 | +import javax.enterprise.inject.spi.CDI; |
| 21 | +import javax.inject.Inject; |
| 22 | +import javax.persistence.PrePersist; |
| 23 | +import javax.persistence.PreUpdate; |
| 24 | + |
| 25 | +import de.symeda.sormas.backend.common.AbstractDomainObject; |
| 26 | +import de.symeda.sormas.backend.user.CurrentUserContext; |
| 27 | +import de.symeda.sormas.backend.user.User; |
| 28 | + |
| 29 | +/** |
| 30 | + * Stateless JPA entity listener for auditing AbstractDomainObject lifecycle events. |
| 31 | + * |
| 32 | + * This listener automatically updates audit information on AbstractDomainObject entities |
| 33 | + * during persist and update operations. It captures the current authenticated user |
| 34 | + * and sets them as the change user for tracking purposes. |
| 35 | + * |
| 36 | + * <p> |
| 37 | + * The listener: |
| 38 | + * <ul> |
| 39 | + * <li>Responds to {@link PrePersist} and {@link PreUpdate} events</li> |
| 40 | + * <li>Only processes entities that extend {@link AbstractDomainObject}</li> |
| 41 | + * <li>Sets the current user as the change user for audit tracking</li> |
| 42 | + * <li>Uses defensive CDI lookup for dependency resolution</li> |
| 43 | + * </ul> |
| 44 | + * |
| 45 | + * <p> |
| 46 | + * This listener should be registered with JPA entities that extend |
| 47 | + * AbstractDomainObject to enable automatic audit user tracking. |
| 48 | + */ |
| 49 | +@Stateless |
| 50 | +public class AdoAuditUpdateListener { |
| 51 | + |
| 52 | + private CurrentUserContext currentUserContext; |
| 53 | + |
| 54 | + /** |
| 55 | + * Sets the current user context for audit operations. |
| 56 | + * This method is called by the CDI container to inject the current user context. |
| 57 | + * |
| 58 | + * @param currentUserContext |
| 59 | + * the current user context to set |
| 60 | + */ |
| 61 | + @Inject |
| 62 | + public void setCurrentUserContext(CurrentUserContext currentUserContext) { |
| 63 | + this.currentUserContext = currentUserContext; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Retrieves the current user context, with fallback to CDI lookup if not injected. |
| 68 | + * This method provides a defensive mechanism to obtain the current user context |
| 69 | + * even if dependency injection fails or is unavailable. |
| 70 | + * |
| 71 | + * @return the current user context, or null if unavailable |
| 72 | + */ |
| 73 | + public CurrentUserContext getCurrentUserContext() { |
| 74 | + if (currentUserContext != null) { |
| 75 | + return currentUserContext; |
| 76 | + } |
| 77 | + final Instance<CurrentUserContext> instance = CDI.current().select(CurrentUserContext.class); |
| 78 | + return instance.isUnsatisfied() ? null : instance.get(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * JPA lifecycle callback that updates audit information before entity persistence. |
| 83 | + * |
| 84 | + * This method is automatically invoked by the JPA provider when an AbstractDomainObject |
| 85 | + * entity is about to be persisted or updated. It sets the current authenticated user |
| 86 | + * as the change user for audit tracking purposes. |
| 87 | + * |
| 88 | + * <p> |
| 89 | + * The method: |
| 90 | + * <ul> |
| 91 | + * <li>Only processes entities that are instances of {@link AbstractDomainObject}</li> |
| 92 | + * <li>Retrieves the current user from the user context</li> |
| 93 | + * <li>Sets the change user on the entity if a current user is available</li> |
| 94 | + * <li>Skips the update if no current user is authenticated</li> |
| 95 | + * </ul> |
| 96 | + * |
| 97 | + * @param entity |
| 98 | + * the entity being persisted or updated |
| 99 | + */ |
| 100 | + @PrePersist |
| 101 | + @PreUpdate |
| 102 | + public void updateADOAuditRecord(Object entity) { |
| 103 | + if (!(entity instanceof AbstractDomainObject)) { |
| 104 | + return; |
| 105 | + } |
| 106 | + final AbstractDomainObject ado = (AbstractDomainObject) entity; |
| 107 | + final User changeUser = getCurrentUserContext().getUserEntityReference(); |
| 108 | + if (changeUser != null) { |
| 109 | + ado.setChangeUser(changeUser); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments