use of com.axelor.apps.base.db.GlobalTrackingConfigurationLine in project axelor-open-suite by axelor.
the class ExportObserver method onExport.
void onExport(@Observes @Named(RequestEvent.EXPORT) PostRequest event) throws IOException {
List<GlobalTrackingConfigurationLine> gtcLines = Beans.get(AppBaseService.class).getAppBase().getGlobalTrackingConfigurationLines();
if (CollectionUtils.isEmpty(gtcLines)) {
return;
}
MetaModel model = Beans.get(MetaModelRepository.class).all().filter("self.fullName = ?", event.getRequest().getBeanClass().getName()).fetchOne();
if (gtcLines.stream().anyMatch(l -> l.getMetaModel().equals(model) && l.getTrackExport())) {
@SuppressWarnings("unchecked") final Map<String, Object> data = (Map<String, Object>) event.getResponse().getData();
if (data == null || data.get("fileName") == null) {
return;
}
final String fileName = (String) data.get("fileName");
final Path filePath = MetaFiles.findTempFile(fileName);
MetaFile mf = new MetaFile();
mf.setFileName(fileName);
Beans.get(MetaFiles.class).upload(new FileInputStream(filePath.toFile()), mf);
Beans.get(GlobalTrackingLogService.class).createExportLog(model, mf);
}
}
use of com.axelor.apps.base.db.GlobalTrackingConfigurationLine in project axelor-open-suite by axelor.
the class GlobalAuditTracker method onComplete.
/**
* This method should be called from {@link
* AuditInterceptor#beforeTransactionCompletion(Transaction)} method to finish change recording.
*
* @param tx the transaction in which the change tracking is being done
* @param user the session user
*/
public void onComplete(Transaction tx, User user) {
List<GlobalTrackingLog> logList = LOGS.get();
LOGS.remove();
if (CollectionUtils.isEmpty(logList)) {
return;
}
MetaModelRepository modelRepo = Beans.get(MetaModelRepository.class);
MetaFieldRepository fieldRepo = Beans.get(MetaFieldRepository.class);
GlobalTrackingLogRepository logRepo = Beans.get(GlobalTrackingLogRepository.class);
GlobalTrackingConfigurationLineRepository configLineRepo = Beans.get(GlobalTrackingConfigurationLineRepository.class);
GlobalTrackingConfigurationLine configLine;
List<GlobalTrackingConfigurationLine> configLineList;
ScriptBindings bindings;
for (GlobalTrackingLog log : logList) {
configLineList = configLineRepo.all().filter("self.metaModel.name = ?", log.getMetaModelName()).fetch();
if (configLineList.isEmpty()) {
continue;
}
log.setMetaModel(modelRepo.findByName(log.getMetaModelName()));
List<GlobalTrackingLogLine> logLinesToSave = new ArrayList<>();
if ((CollectionUtils.isNotEmpty(log.getGlobalTrackingLogLineList()))) {
try {
bindings = new ScriptBindings(this.getContext(JPA.find((Class<Model>) Class.forName(log.getMetaModel().getFullName()), log.getRelatedId())));
} catch (Exception e) {
continue;
}
for (GlobalTrackingLogLine line : log.getGlobalTrackingLogLineList()) {
configLine = configLineList.stream().filter(l -> l.getMetaField().getName().equals(line.getMetaFieldName())).findFirst().orElse(null);
if (configLine == null || !this.canTrack(configLine, log.getTypeSelect()) || (!Strings.isNullOrEmpty(configLine.getTrackingCondition()) && !Boolean.TRUE.equals(new GroovyScriptHelper(bindings).eval(configLine.getTrackingCondition())))) {
continue;
}
line.setMetaField(fieldRepo.all().filter("self.metaModel.id = ? AND self.name = ?", log.getMetaModel().getId(), line.getMetaFieldName()).fetchOne());
logLinesToSave.add(line);
}
}
if (!logLinesToSave.isEmpty() || (GlobalTrackingLogRepository.TYPE_DELETE == log.getTypeSelect() && configLineList.stream().anyMatch(l -> Boolean.TRUE.equals(l.getTrackDeletion())))) {
log.getGlobalTrackingLogLineList().stream().forEach(l -> l.setGlobalTrackingLog(null));
logLinesToSave.stream().forEach(l -> l.setGlobalTrackingLog(log));
log.setUser(user);
logRepo.save(log);
}
}
}
Aggregations