P280 - Auditoria alterações (issue 8366)
git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@66876 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
7281704506
commit
6af3cce27f
|
@ -32,10 +32,17 @@ public class AuditInterceptor extends EmptyInterceptor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
|
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
|
||||||
|
final SessionFactory sessionFactory = DBUtil.getInstance().getSessionFactory();
|
||||||
|
final Session session = sessionFactory.openSession();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
genAudit(entity, EnumAuditAction.CRIACAO);
|
genAudit(session, entity, EnumAuditAction.CRIACAO);
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
log.error(exception.getStackTrace());
|
log.error(exception.getMessage(), exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (session != null) {
|
||||||
|
session.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.onSave(entity, id, state, propertyNames, types);
|
return super.onSave(entity, id, state, propertyNames, types);
|
||||||
|
@ -43,10 +50,17 @@ public class AuditInterceptor extends EmptyInterceptor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
|
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
|
||||||
|
final SessionFactory sessionFactory = DBUtil.getInstance().getSessionFactory();
|
||||||
|
final Session session = sessionFactory.openSession();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
genAudit(entity, EnumAuditAction.ALTERACAO);
|
genAudit(session, entity, EnumAuditAction.ALTERACAO);
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
log.error(exception.getStackTrace());
|
log.error(exception.getMessage(), exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (session != null) {
|
||||||
|
session.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types);
|
return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types);
|
||||||
|
@ -54,70 +68,32 @@ public class AuditInterceptor extends EmptyInterceptor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
|
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
|
||||||
|
final SessionFactory sessionFactory = DBUtil.getInstance().getSessionFactory();
|
||||||
|
final Session session = sessionFactory.openSession();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
genAudit(entity, EnumAuditAction.EXCLUSAO);
|
genAudit(session, entity, EnumAuditAction.EXCLUSAO);
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
log.error(exception.getStackTrace());
|
log.error(exception.getMessage(), exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (session != null) {
|
||||||
|
session.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
super.onDelete(entity, id, state, propertyNames, types);
|
super.onDelete(entity, id, state, propertyNames, types);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean getAuditModuleService() {
|
private void genAudit(final Session session, Object entity, EnumAuditAction action) {
|
||||||
|
if (!getAuditModuleService(session)) {
|
||||||
Session session = null;
|
|
||||||
try {
|
|
||||||
if (!AuditManager.getINSTANCE().getCheckModuleAudit()) {
|
|
||||||
return AuditManager.getINSTANCE().getCurrentService() != null ? true : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
AuditService auditService = null;
|
|
||||||
|
|
||||||
SessionFactory sessionFactory = DBUtil.getInstance().getSessionFactory();
|
|
||||||
session = sessionFactory.openSession();
|
|
||||||
|
|
||||||
Criteria criteriaFuncionSistema = session.createCriteria(FuncionSistema.class);
|
|
||||||
criteriaFuncionSistema.add(Restrictions.eq("descruta", AuditManager.getINSTANCE().getCurrentActionService()));
|
|
||||||
FuncionSistema funcionSistema = (FuncionSistema) criteriaFuncionSistema.uniqueResult();
|
|
||||||
|
|
||||||
if (funcionSistema == null) {
|
|
||||||
Log.info("Função sistema " + funcionSistema + " não encontrada na base de dados.");
|
|
||||||
AuditManager.getINSTANCE().setCurrentService(auditService);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Criteria criteriaService = session.createCriteria(AuditService.class);
|
|
||||||
criteriaService.add(Restrictions.eq("funcionSistema", funcionSistema));
|
|
||||||
auditService = (AuditService) criteriaService.uniqueResult();
|
|
||||||
|
|
||||||
if (auditService == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
AuditManager.getINSTANCE().setCurrentService(auditService);
|
|
||||||
AuditManager.getINSTANCE().setCheckModuleAudit(Boolean.FALSE);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
|
|
||||||
} finally {
|
|
||||||
if (session != null)
|
|
||||||
session.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void genAudit(Object entity, EnumAuditAction action) {
|
|
||||||
if (!getAuditModuleService()) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Session session = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
/**
|
/**
|
||||||
* Ignorado caso a entidade for de auditoria e se não for encontrado o módulo no banco
|
* Ignorado caso a entidade for de auditoria e se não for encontrado o módulo no banco
|
||||||
*/
|
*/
|
||||||
if (!(entity instanceof AuditLog)) {
|
if (!(entity instanceof AuditLog)) {
|
||||||
|
|
||||||
AuditService service = AuditManager.getINSTANCE().getCurrentService();
|
AuditService service = AuditManager.getINSTANCE().getCurrentService();
|
||||||
|
|
||||||
if (!(entity instanceof AuditLog) && service != null && service.getModule() != null) {
|
if (!(entity instanceof AuditLog) && service != null && service.getModule() != null) {
|
||||||
|
@ -138,9 +114,6 @@ public class AuditInterceptor extends EmptyInterceptor {
|
||||||
audit.setAction(action.getId());
|
audit.setAction(action.getId());
|
||||||
audit.setUsuario(UsuarioLogado.getUsuarioLogado());
|
audit.setUsuario(UsuarioLogado.getUsuarioLogado());
|
||||||
|
|
||||||
SessionFactory sessionFactory = DBUtil.getInstance().getSessionFactory();
|
|
||||||
session = sessionFactory.openSession();
|
|
||||||
|
|
||||||
session.persist(audit);
|
session.persist(audit);
|
||||||
session.flush();
|
session.flush();
|
||||||
}
|
}
|
||||||
|
@ -148,10 +121,40 @@ public class AuditInterceptor extends EmptyInterceptor {
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
log.error(exception.getStackTrace());
|
log.error(exception.getMessage(), exception);
|
||||||
} finally {
|
|
||||||
if (session != null)
|
|
||||||
session.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean getAuditModuleService(final Session session) {
|
||||||
|
if (!AuditManager.getINSTANCE().getCheckModuleAudit()) {
|
||||||
|
return AuditManager.getINSTANCE().getCurrentService() != null ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
AuditService auditService = null;
|
||||||
|
|
||||||
|
Criteria criteriaFuncionSistema = session.createCriteria(FuncionSistema.class);
|
||||||
|
criteriaFuncionSistema.add(Restrictions.eq("descruta", AuditManager.getINSTANCE().getCurrentActionService()));
|
||||||
|
FuncionSistema funcionSistema = (FuncionSistema) criteriaFuncionSistema.uniqueResult();
|
||||||
|
|
||||||
|
if (funcionSistema == null) {
|
||||||
|
Log.info("Função sistema " + funcionSistema + " não encontrada na base de dados.");
|
||||||
|
AuditManager.getINSTANCE().setCurrentService(auditService);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Criteria criteriaService = session.createCriteria(AuditService.class);
|
||||||
|
criteriaService.add(Restrictions.eq("funcionSistema", funcionSistema));
|
||||||
|
auditService = (AuditService) criteriaService.uniqueResult();
|
||||||
|
|
||||||
|
if (auditService == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
AuditManager.getINSTANCE().setCurrentService(auditService);
|
||||||
|
AuditManager.getINSTANCE().setCheckModuleAudit(Boolean.FALSE);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue