use of com.axelor.auth.db.AuditableModel in project axelor-open-suite by axelor.
the class GlobalAuditInterceptor method onSave.
@SuppressWarnings("unchecked")
@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if (!super.onSave(entity, id, state, propertyNames, types) || Arrays.asList(BACKLISTED_CLASSES).contains(entity.getClass()) || !(entity instanceof AuditableModel)) {
return false;
}
GlobalTrackingLog log = globalTracker.get().addLog((AuditableModel) entity, GlobalTrackingLogRepository.TYPE_CREATE);
for (int i = 0; i < propertyNames.length; i++) {
if (state[i] == null || CREATED_ON.equals(propertyNames[i]) || CREATED_BY.equals(propertyNames[i])) {
continue;
}
GlobalTrackingLogLine logLine = new GlobalTrackingLogLine();
logLine.setMetaFieldName(propertyNames[i]);
if (state[i] instanceof AuditableModel) {
logLine.setNewValue(String.valueOf(((AuditableModel) state[i]).getId()));
} else if (state[i] instanceof Collection) {
String newVal = "";
if (CollectionUtils.isNotEmpty((Collection<Object>) state[i])) {
newVal = String.format("[%s]", ((Collection<AuditableModel>) state[i]).stream().map(AuditableModel::getId).map(String::valueOf).collect(Collectors.joining(", ")));
}
logLine.setNewValue(newVal);
} else {
logLine.setNewValue(String.valueOf(Optional.ofNullable(state[i]).orElse("")));
}
log.addGlobalTrackingLogLineListItem(logLine);
}
if (log != null) {
globalTracker.get().addLog(log);
}
return true;
}
use of com.axelor.auth.db.AuditableModel in project axelor-open-suite by axelor.
the class GlobalAuditInterceptor method onFlushDirty.
@SuppressWarnings("unchecked")
@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
if (!super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types) || Arrays.asList(BACKLISTED_CLASSES).contains(entity.getClass()) || !(entity instanceof AuditableModel)) {
return false;
}
if (globalTracker.get() == null) {
globalTracker.set(new GlobalAuditTracker());
globalTracker.get().init();
}
GlobalTrackingLog log = globalTracker.get().addLog((AuditableModel) entity, GlobalTrackingLogRepository.TYPE_UPDATE);
for (int i = 0; i < propertyNames.length; i++) {
if (Objects.equals(currentState[i], previousState[i]) || UPDATED_ON.equals(propertyNames[i]) || UPDATED_BY.equals(propertyNames[i])) {
continue;
}
GlobalTrackingLogLine logLine = new GlobalTrackingLogLine();
logLine.setMetaFieldName(propertyNames[i]);
if (currentState[i] instanceof AuditableModel || previousState[i] instanceof AuditableModel) {
logLine.setNewValue(currentState[i] instanceof AuditableModel ? String.valueOf(((AuditableModel) currentState[i]).getId()) : "");
logLine.setPreviousValue(previousState[i] instanceof AuditableModel ? String.valueOf(((AuditableModel) previousState[i]).getId()) : "");
} else if (currentState[i] instanceof Collection || previousState[i] instanceof Collection) {
String prevVal = "";
String newVal = "";
if (CollectionUtils.isNotEmpty((Collection<Object>) previousState[i])) {
prevVal = String.format("[%s]", ((Collection<AuditableModel>) previousState[i]).stream().map(AuditableModel::getId).map(String::valueOf).collect(Collectors.joining(", ")));
}
if (CollectionUtils.isNotEmpty((Collection<Object>) currentState[i])) {
newVal = String.format("[%s]", ((Collection<AuditableModel>) currentState[i]).stream().map(AuditableModel::getId).map(String::valueOf).collect(Collectors.joining(", ")));
}
logLine.setPreviousValue(prevVal);
logLine.setNewValue(newVal);
} else {
logLine.setNewValue(String.valueOf(Optional.ofNullable(currentState[i]).orElse("")));
logLine.setPreviousValue(String.valueOf(Optional.ofNullable(previousState[i]).orElse("")));
}
log.addGlobalTrackingLogLineListItem(logLine);
}
return true;
}
use of com.axelor.auth.db.AuditableModel in project axelor-open-suite by axelor.
the class GlobalAuditTracker method addCollectionModification.
@SuppressWarnings("unchecked")
protected void addCollectionModification(Object collection, Long id) {
if (collection instanceof AbstractPersistentCollection) {
AbstractPersistentCollection newValues = null;
Collection<AuditableModel> oldValues = null;
if (collection instanceof PersistentSet) {
// MANY-TO-MANY
newValues = (PersistentSet) collection;
oldValues = (Collection<AuditableModel>) ((Map<?, ?>) newValues.getStoredSnapshot()).keySet();
} else if (collection instanceof PersistentBag) {
// ONE-TO-MANY
newValues = (PersistentBag) collection;
oldValues = (Collection<AuditableModel>) newValues.getStoredSnapshot();
}
if (newValues == null) {
return;
}
Object owner = newValues.getOwner();
if (owner == null || Arrays.asList(GlobalAuditInterceptor.BACKLISTED_CLASSES).contains(owner.getClass()) || !(owner instanceof AuditableModel)) {
return;
}
String fieldName = newValues.getRole().replace(owner.getClass().getCanonicalName() + ".", "");
GlobalTrackingLog log = LOGS.get().stream().filter(l -> l.getRelatedId().equals(id) && l.getMetaModelName().equals(owner.getClass().getSimpleName())).findFirst().orElse(addLog((AuditableModel) owner, GlobalTrackingLogRepository.TYPE_UPDATE));
List<Long> previousIdList = new ArrayList<>();
List<Long> newIdList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(oldValues)) {
for (AuditableModel oldValue : oldValues) {
if (oldValue != null) {
previousIdList.add(oldValue.getId());
}
}
}
for (AuditableModel newValue : (Collection<AuditableModel>) newValues) {
if (newValue != null) {
newIdList.add(newValue.getId());
}
}
GlobalTrackingLogLine line = log.getGlobalTrackingLogLineList().stream().filter(l -> l.getMetaFieldName().equals(fieldName)).findFirst().orElse(null);
if (line == null) {
line = new GlobalTrackingLogLine();
line.setMetaFieldName(fieldName);
line.setGlobalTrackingLog(log);
line.setPreviousValue(String.format("[%s]", previousIdList.stream().map(String::valueOf).collect(Collectors.joining(", "))));
log.addGlobalTrackingLogLineListItem(line);
}
line.setNewValue(String.format("[%s]", newIdList.stream().map(String::valueOf).collect(Collectors.joining(", "))));
}
}
use of com.axelor.auth.db.AuditableModel in project axelor-open-suite by axelor.
the class DataBackupCreateService method getSubClassesMap.
protected Map<String, List<String>> getSubClassesMap() {
List<MetaModel> metaModels = getMetaModels();
List<String> subClasses;
Map<String, List<String>> subClassMap = new HashMap<>();
for (MetaModel metaModel : metaModels) {
try {
subClasses = new ArrayList<>();
@SuppressWarnings("unchecked") Class<AuditableModel> superClass = (Class<AuditableModel>) Class.forName(metaModel.getFullName()).getSuperclass();
if (superClass != AuditableModel.class) {
if (!subClassMap.isEmpty() && subClassMap.containsKey(superClass.getName())) {
subClasses = subClassMap.get(superClass.getName());
}
subClasses.add(metaModel.getName());
subClassMap.put(superClass.getName(), subClasses);
}
} catch (ClassNotFoundException e) {
String strError = "Class not found issue: ";
sb.append(strError).append(e.getLocalizedMessage() + "-----------------------------------------\n");
e.printStackTrace();
}
}
return subClassMap;
}
Aggregations