2015-05-17 22:20:34 +02:00
|
|
|
package com.sismics.docs.core.util;
|
|
|
|
|
|
|
|
import com.sismics.docs.core.constant.AuditLogType;
|
2018-03-29 17:59:47 +02:00
|
|
|
import com.sismics.docs.core.dao.AuditLogDao;
|
2015-05-17 22:20:34 +02:00
|
|
|
import com.sismics.docs.core.model.jpa.AuditLog;
|
|
|
|
import com.sismics.docs.core.model.jpa.Loggable;
|
|
|
|
import com.sismics.util.context.ThreadLocalContext;
|
|
|
|
|
2023-04-09 21:31:53 +02:00
|
|
|
import jakarta.persistence.EntityManager;
|
2017-11-12 02:06:41 +01:00
|
|
|
|
2015-05-17 22:20:34 +02:00
|
|
|
/**
|
|
|
|
* Audit log utilities.
|
|
|
|
*
|
|
|
|
* @author bgamard
|
|
|
|
*/
|
|
|
|
public class AuditLogUtil {
|
|
|
|
/**
|
|
|
|
* Create an audit log.
|
|
|
|
*
|
2017-11-12 02:06:41 +01:00
|
|
|
* @param loggable Loggable
|
2015-05-17 22:20:34 +02:00
|
|
|
* @param type Audit log type
|
2017-11-12 02:06:41 +01:00
|
|
|
* @param userId User ID
|
2015-05-17 22:20:34 +02:00
|
|
|
*/
|
2016-02-15 22:28:13 +01:00
|
|
|
public static void create(Loggable loggable, AuditLogType type, String userId) {
|
2017-11-17 22:03:54 +01:00
|
|
|
if (userId == null) {
|
|
|
|
userId = "admin";
|
|
|
|
}
|
|
|
|
|
2015-05-17 22:20:34 +02:00
|
|
|
// Get the entity ID
|
|
|
|
EntityManager em = ThreadLocalContext.get().getEntityManager();
|
|
|
|
String entityId = (String) em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(loggable);
|
|
|
|
|
|
|
|
// Create the audit log
|
|
|
|
AuditLogDao auditLogDao = new AuditLogDao();
|
|
|
|
AuditLog auditLog = new AuditLog();
|
2016-02-15 22:28:13 +01:00
|
|
|
auditLog.setUserId(userId);
|
2015-05-17 22:20:34 +02:00
|
|
|
auditLog.setEntityId(entityId);
|
|
|
|
auditLog.setEntityClass(loggable.getClass().getSimpleName());
|
|
|
|
auditLog.setType(type);
|
|
|
|
auditLog.setMessage(loggable.toMessage());
|
|
|
|
auditLogDao.create(auditLog);
|
|
|
|
}
|
|
|
|
}
|