bug#20776
dev:wilian qua: git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/Outros/Auditador/trunk@104185 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
739bde4ac6
commit
97e0cd98d2
|
@ -0,0 +1,32 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>br.com.rjconsultores</groupId>
|
||||
<artifactId>Auditador</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src/main/java</sourceDirectory>
|
||||
<testSourceDirectory>src/test/java</testSourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.3</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>2.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,15 @@
|
|||
package br.com.rjconsultores.auditador.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AuditarAtributo {
|
||||
|
||||
String nome() default "";
|
||||
String pattern() default "";
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package br.com.rjconsultores.auditador.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AuditarClasse {
|
||||
|
||||
String nome();
|
||||
String campoEmpresa() default "";
|
||||
String tela();
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package br.com.rjconsultores.auditador.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AuditarEntidade {
|
||||
|
||||
String nome() default "";
|
||||
boolean auditarCampos() default false;
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package br.com.rjconsultores.auditador.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AuditarID {
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package br.com.rjconsultores.auditador.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AuditarLista {
|
||||
|
||||
String nome() default "";
|
||||
boolean auditarEntidades() default false;
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package br.com.rjconsultores.auditador.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface NaoAuditar {
|
||||
|
||||
}
|
|
@ -0,0 +1,384 @@
|
|||
package br.com.rjconsultores.auditador.auditadores;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import br.com.rjconsultores.auditador.annotations.AuditarAtributo;
|
||||
import br.com.rjconsultores.auditador.annotations.AuditarClasse;
|
||||
import br.com.rjconsultores.auditador.annotations.AuditarEntidade;
|
||||
import br.com.rjconsultores.auditador.annotations.AuditarID;
|
||||
import br.com.rjconsultores.auditador.annotations.AuditarLista;
|
||||
import br.com.rjconsultores.auditador.annotations.NaoAuditar;
|
||||
import br.com.rjconsultores.auditador.enums.AuditadorTipoAlteracao;
|
||||
import br.com.rjconsultores.auditador.exceptions.AuditadorException;
|
||||
import br.com.rjconsultores.auditador.interfaces.Auditavel;
|
||||
import br.com.rjconsultores.auditador.interfaces.AuditavelTelaAlternativa;
|
||||
import br.com.rjconsultores.auditador.model.AuditadorObjects;
|
||||
|
||||
public class Auditador {
|
||||
|
||||
AuditadorAtributo auditadorAtributo;
|
||||
AuditadorEntidade auditadorEntidade;
|
||||
AuditadorList auditadorList;
|
||||
ExtrairAnotacao extrairAnotacao;
|
||||
|
||||
private AuditadorObjects padrao;
|
||||
|
||||
private Auditador() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static Auditador getInstance( ) {
|
||||
Auditador auditar = new Auditador();
|
||||
auditar.auditadorAtributo = new AuditadorAtributo(auditar);
|
||||
auditar.auditadorEntidade = new AuditadorEntidade(auditar);
|
||||
auditar.auditadorList = new AuditadorList(auditar);
|
||||
auditar.extrairAnotacao = new ExtrairAnotacao();
|
||||
return auditar;
|
||||
}
|
||||
|
||||
public List<AuditadorObjects> auditar(Object original, Object novo) {
|
||||
List<AuditadorObjects> lsRetorno = new ArrayList<AuditadorObjects>();
|
||||
try {
|
||||
|
||||
if(isObjetosNulos(original, novo) || !iniciarObjetoPadrao(novo != null ? novo : original, original == null)) {
|
||||
return lsRetorno;
|
||||
}
|
||||
|
||||
AuditadorObjects objectNovo = auditarObjetoNovo(null, original, novo);
|
||||
if(objectNovo != null) {
|
||||
lsRetorno.add(objectNovo);
|
||||
return lsRetorno;
|
||||
}
|
||||
|
||||
lsRetorno.addAll(auditarCampos(original, novo, padrao.getClasseAlterada()));
|
||||
return lsRetorno;
|
||||
} catch (Exception e) {
|
||||
throw new AuditadorException(e.getMessage(), e);
|
||||
} catch (Throwable e) {
|
||||
throw new AuditadorException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public List<AuditadorObjects> auditarList(Object objeto, String nomeCampoListAuditar, List<Object> original, List<Object> novo) {
|
||||
List<AuditadorObjects> lsRetorno = new ArrayList<AuditadorObjects>();
|
||||
try {
|
||||
if(!isObjetosNulos(original, novo)) {
|
||||
Class<?> classe = original != null ? original.getClass() : novo.getClass();
|
||||
Field[] campos = classe.getDeclaredFields();
|
||||
for (Field campo : campos) {
|
||||
campo.setAccessible(true);
|
||||
if(!campo.getName().equals(nomeCampoListAuditar)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
NaoAuditar naoAuditar = extrairAnotacao.extrairNaoAuditar(campo);
|
||||
if(naoAuditar != null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
AuditarLista auditarLista = extrairAnotacao.extrairAuditarLista(campo);
|
||||
lsRetorno.addAll(auditadorList.auditar(campo, original, novo, auditarLista.auditarEntidades()));
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new AuditadorException(e.getMessage(), e);
|
||||
} catch (Throwable e) {
|
||||
throw new AuditadorException(e.getMessage(), e);
|
||||
}
|
||||
return lsRetorno;
|
||||
}
|
||||
|
||||
public List<AuditadorObjects> auditarExclusao(Object objeto) {
|
||||
try {
|
||||
List<AuditadorObjects> lsRetorno = new ArrayList<AuditadorObjects>();
|
||||
|
||||
if(isObjetosNulos(null, objeto) || !iniciarObjetoPadrao(objeto != null ? objeto : null, false)) {
|
||||
return lsRetorno;
|
||||
}
|
||||
|
||||
AuditadorObjects objetoExcluido = auditarObjetoExcluido(objeto);
|
||||
if(objetoExcluido != null) {
|
||||
lsRetorno.add(objetoExcluido);
|
||||
}
|
||||
|
||||
return lsRetorno;
|
||||
} catch (Exception e) {
|
||||
throw new AuditadorException(e.getMessage(), e);
|
||||
} catch (Throwable e) {
|
||||
throw new AuditadorException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
protected AuditadorObjects auditarObjetoNovo(Field campo, Object original, Object novo) throws CloneNotSupportedException {
|
||||
if(original == null && novo != null) {
|
||||
AuditadorObjects auditado = padrao.clonar();
|
||||
auditado.setValorNovo(String.format("Registro Criado [%s]", extrairTextoInclusaoExclusao(novo)));
|
||||
auditado.setCampoAlterado(extrairNomeCampo(campo));
|
||||
return auditado;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected AuditadorObjects auditarObjetoExcluido(Object objeto) throws CloneNotSupportedException {
|
||||
if(objeto != null) {
|
||||
AuditadorObjects auditado = padrao.clonar();
|
||||
auditado.setTipoAlteracao(AuditadorTipoAlteracao.EXCLUSAO);
|
||||
auditado.setValorNovo(String.format("Registro excluido [%s]", extrairTextoInclusaoExclusao(objeto)));
|
||||
return auditado;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected AuditadorObjects auditarCampo(Field campo, Object valorOriginal, Object valorNovo, String nomeClasse) throws CloneNotSupportedException {
|
||||
if(isDiferente(valorOriginal, valorNovo)) {
|
||||
String nomeCampo = extrairNomeCampo(campo);
|
||||
AuditadorObjects auditado = padrao.clonar();
|
||||
auditado.setTipoAlteracao(AuditadorTipoAlteracao.ALTERACAO);
|
||||
auditado.setCampoAlterado(nomeCampo);
|
||||
auditado.setValorAnterior(valorOriginal != null ? valorOriginal.toString() : null);
|
||||
auditado.setValorNovo(valorNovo != null ? valorNovo.toString() : null);
|
||||
auditado.setClasseAlterada(nomeClasse);
|
||||
return auditado;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected String extrairNomeClasse(Object original, Object novo) {
|
||||
if(original != null) {
|
||||
return original.getClass().getSimpleName();
|
||||
}
|
||||
if(novo != null) {
|
||||
return novo.getClass().getSimpleName();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected String extrairNomeCampo(Field campo) {
|
||||
String nomeCampo = campo != null ? campo.getName() : null;
|
||||
AuditarAtributo auditarAtributo = extrairAnotacao.extrairAuditarAtributo(campo);
|
||||
if(auditarAtributo != null && StringUtils.isNotBlank(auditarAtributo.nome())) {
|
||||
nomeCampo = auditarAtributo.nome();
|
||||
}
|
||||
AuditarEntidade auditarEntidade = extrairAnotacao.extrairAuditarEntidade(campo);
|
||||
if(auditarEntidade != null && StringUtils.isNotBlank(auditarEntidade.nome())) {
|
||||
nomeCampo = auditarEntidade.nome();
|
||||
}
|
||||
AuditarLista auditarLista = extrairAnotacao.extrairAuditarLista(campo);
|
||||
if(auditarLista != null && StringUtils.isNotBlank(auditarLista.nome())) {
|
||||
nomeCampo = auditarLista.nome();
|
||||
}
|
||||
return nomeCampo;
|
||||
}
|
||||
|
||||
protected boolean isDiferente(Object valorOriginal, Object valorNovo) {
|
||||
if(valorOriginal == null && valorNovo == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(valorOriginal != null && valorNovo == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if(valorOriginal == null && valorNovo != null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return !valorOriginal.equals(valorNovo);
|
||||
}
|
||||
|
||||
protected List<AuditadorObjects> auditarCampos(Object original, Object novo, String nomeClasse) throws IllegalArgumentException, IllegalAccessException {
|
||||
List<AuditadorObjects> lsRetorno = new ArrayList<AuditadorObjects>();
|
||||
if(!isObjetosNulos(original, novo)) {
|
||||
Class<?> classe = original != null ? original.getClass() : novo.getClass();
|
||||
Field[] campos = classe.getDeclaredFields();
|
||||
for (Field campo : campos) {
|
||||
campo.setAccessible(true);
|
||||
|
||||
NaoAuditar naoAuditar = extrairAnotacao.extrairNaoAuditar(campo);
|
||||
if(naoAuditar != null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
AuditarEntidade auditarEntidade = extrairAnotacao.extrairAuditarEntidade(campo);
|
||||
AuditarLista auditarLista = extrairAnotacao.extrairAuditarLista(campo);
|
||||
|
||||
if(auditarEntidade != null && auditarEntidade.auditarCampos()) {
|
||||
lsRetorno.addAll(auditadorEntidade.auditar(campo, original, novo, null));
|
||||
} else if(auditarLista != null) {
|
||||
lsRetorno.addAll(auditadorList.auditar(campo, original, novo, auditarLista.auditarEntidades()));
|
||||
} else {
|
||||
lsRetorno.addAll(auditadorAtributo.auditar(campo, original, novo, nomeClasse));
|
||||
}
|
||||
}
|
||||
}
|
||||
return lsRetorno;
|
||||
|
||||
}
|
||||
|
||||
protected Object getValor(Field field, Object object) throws IllegalArgumentException, IllegalAccessException, ParseException {
|
||||
return getValor(field, object, null);
|
||||
}
|
||||
|
||||
protected Object getValor(Field field, Object object, String pattern) throws IllegalArgumentException, IllegalAccessException, ParseException {
|
||||
if(field == null || object == null) {
|
||||
return null;
|
||||
}
|
||||
Object retorno = field.get(object);
|
||||
|
||||
Object atributo = null;
|
||||
if (retorno instanceof Date) {
|
||||
Date aux = (Date) retorno;
|
||||
|
||||
atributo = getStringDate(aux, pattern);
|
||||
} else if (retorno instanceof Integer || retorno instanceof Long ) {
|
||||
atributo = retorno.toString();
|
||||
} else if (retorno instanceof Number) {
|
||||
atributo = getValorFormatado(retorno.toString());
|
||||
} else if(retorno instanceof String) {
|
||||
atributo = StringUtils.isNotBlank(retorno.toString()) ? retorno.toString() : "nulo";
|
||||
} else {
|
||||
return retorno;
|
||||
}
|
||||
return atributo;
|
||||
|
||||
}
|
||||
|
||||
protected String getValorFormatado(String value) throws ParseException {
|
||||
|
||||
if(value != null && StringUtils.isNotBlank(value)) {
|
||||
NumberFormat nf = NumberFormat.getInstance(new Locale("pt", "br"));
|
||||
BigDecimal valor = new BigDecimal(nf.parse(value.replace(".", ",")).toString());
|
||||
|
||||
return nf.format(valor.doubleValue());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected String getStringDate(java.util.Date d, String pattern) {
|
||||
if(StringUtils.isBlank(pattern)) {
|
||||
pattern = "dd/MM/yyyy HH:mm";
|
||||
}
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
||||
if (d != null) {
|
||||
return sdf.format(d);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected List<Object> getValorList(Field field, Object object) throws IllegalArgumentException, IllegalAccessException {
|
||||
if(field == null || object == null) {
|
||||
return new ArrayList<Object>();
|
||||
}
|
||||
Object aux = field.get(object);
|
||||
|
||||
List<Object> retorno = new ArrayList<Object>();
|
||||
if(aux != null && aux instanceof Collection) {
|
||||
retorno.addAll((Collection<Object>) aux);
|
||||
}
|
||||
|
||||
return retorno;
|
||||
}
|
||||
|
||||
protected boolean isObjetosNulos(Object original, Object novo) {
|
||||
return novo == null && original == null;
|
||||
}
|
||||
|
||||
protected boolean isObjetoNovo(Object original, Object novo) {
|
||||
return novo != null && original == null;
|
||||
}
|
||||
|
||||
protected boolean iniciarObjetoPadrao(Object objeto, boolean isObjetoNovo) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException {
|
||||
padrao = null;
|
||||
Class<?> classe = objeto.getClass();
|
||||
AuditarClasse auditarClasse = extrairAnotacao.extrair(classe);
|
||||
if(auditarClasse != null) {
|
||||
padrao = new AuditadorObjects();
|
||||
padrao.setTipoAlteracao(isObjetoNovo ? AuditadorTipoAlteracao.INCLUSAO : AuditadorTipoAlteracao.EXCLUSAO);
|
||||
padrao.setTela(getNomeTela(auditarClasse, objeto));
|
||||
padrao.setEmpresaId(getEmpresaId(objeto, auditarClasse.campoEmpresa()));
|
||||
padrao.setIdAuditado(getIdEntidade(objeto));
|
||||
padrao.setClassePrincipal(StringUtils.isNotBlank(auditarClasse.nome()) ? auditarClasse.nome() : classe.getSimpleName());
|
||||
}
|
||||
return padrao != null;
|
||||
}
|
||||
|
||||
protected String getNomeTela(AuditarClasse auditarClasse, Object objeto) {
|
||||
if(objeto instanceof AuditavelTelaAlternativa) {
|
||||
AuditavelTelaAlternativa aux = (AuditavelTelaAlternativa) objeto;
|
||||
if(aux != null && StringUtils.isNotBlank(aux.getTelaAlternativa())) {
|
||||
return aux.getTelaAlternativa();
|
||||
}
|
||||
}
|
||||
return auditarClasse.tela();
|
||||
}
|
||||
|
||||
protected Integer getEmpresaId(Object objeto, String campoEmpresa) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
|
||||
if(StringUtils.isBlank(campoEmpresa)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Class<?> classe = objeto.getClass();
|
||||
Field field = classe.getDeclaredField(campoEmpresa);
|
||||
if(field != null) {
|
||||
field.setAccessible(true);
|
||||
Object valor = field.get(objeto);
|
||||
if(valor instanceof Number) {
|
||||
return Integer.valueOf(valor.toString());
|
||||
}
|
||||
Long idEntidade = getIdEntidade(valor);
|
||||
if(idEntidade != null) {
|
||||
return Integer.valueOf(idEntidade.toString());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected Long getIdEntidade(Object objeto) throws IllegalArgumentException, IllegalAccessException {
|
||||
Class<?> classe = objeto.getClass();
|
||||
Field[] campos = classe.getDeclaredFields();
|
||||
for (Field field : campos) {
|
||||
field.setAccessible(true);
|
||||
AuditarID auditarID = extrairAnotacao.extrairAuditarID(field);
|
||||
if(auditarID != null) {
|
||||
Object valor = field.get(objeto);
|
||||
if(valor instanceof Number) {
|
||||
return Long.valueOf(valor.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected AuditadorObjects getPadrao() {
|
||||
try {
|
||||
return padrao.clonar();
|
||||
} catch (Exception e) {
|
||||
throw new AuditadorException("Não foi possível clonar o objeto padrão", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected String extrairTextoInclusaoExclusao(Object objeto) {
|
||||
if(objeto instanceof Auditavel) {
|
||||
Auditavel<?> auditavelAux = (Auditavel<?>) objeto;
|
||||
return auditavelAux.getTextoInclusaoExclusao();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package br.com.rjconsultores.auditador.auditadores;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import br.com.rjconsultores.auditador.annotations.AuditarAtributo;
|
||||
import br.com.rjconsultores.auditador.exceptions.AuditadorException;
|
||||
import br.com.rjconsultores.auditador.model.AuditadorObjects;
|
||||
|
||||
class AuditadorAtributo {
|
||||
|
||||
protected Auditador auditador;
|
||||
|
||||
public AuditadorAtributo(Auditador auditar) {
|
||||
super();
|
||||
this.auditador = auditar;
|
||||
}
|
||||
|
||||
public List<AuditadorObjects> auditar(Field campo, Object original, Object novo, String nomeClasse) {
|
||||
List<AuditadorObjects> lsRetorno = new ArrayList<AuditadorObjects>();
|
||||
try {
|
||||
|
||||
if(!auditador.isObjetosNulos(original, novo)) {
|
||||
AuditarAtributo auditarAtributo = auditador.extrairAnotacao.extrairAuditarAtributo(campo);
|
||||
String pattern = auditarAtributo != null && StringUtils.isNotBlank(auditarAtributo.pattern()) ? auditarAtributo.pattern() : null;
|
||||
|
||||
Object valorOriginal = auditador.getValor(campo, original, pattern);
|
||||
Object valorNovo = auditador.getValor(campo, novo, pattern);
|
||||
AuditadorObjects auditado = auditador.auditarCampo(campo, valorOriginal, valorNovo, nomeClasse);
|
||||
if(auditado != null) {
|
||||
lsRetorno.add(auditado);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new AuditadorException(e.getMessage(), e);
|
||||
} catch (Throwable e) {
|
||||
throw new AuditadorException(e.getMessage(), e);
|
||||
}
|
||||
return lsRetorno;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package br.com.rjconsultores.auditador.auditadores;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import br.com.rjconsultores.auditador.annotations.AuditarAtributo;
|
||||
import br.com.rjconsultores.auditador.annotations.NaoAuditar;
|
||||
import br.com.rjconsultores.auditador.exceptions.AuditadorException;
|
||||
import br.com.rjconsultores.auditador.model.AuditadorObjects;
|
||||
|
||||
class AuditadorEntidade {
|
||||
|
||||
protected Auditador auditador;
|
||||
|
||||
public AuditadorEntidade(Auditador auditar) {
|
||||
super();
|
||||
this.auditador = auditar;
|
||||
}
|
||||
|
||||
public List<AuditadorObjects> auditar(Field campo, Object original, Object novo, String nomeClasse) {
|
||||
List<AuditadorObjects> lsRetorno = new ArrayList<AuditadorObjects>();
|
||||
try {
|
||||
Object valorOriginal = campo != null && original != null ? campo.get(original) : original;
|
||||
Object valorNovo = campo != null && novo != null ? campo.get(novo) : novo;
|
||||
|
||||
String localNomeClasse = nomeClasse != null ? nomeClasse : auditador.extrairNomeClasse(valorOriginal, valorNovo);
|
||||
|
||||
AuditadorObjects objectNovo = auditador.auditarObjetoNovo(campo, valorOriginal, valorNovo);
|
||||
if(objectNovo != null) {
|
||||
lsRetorno.add(objectNovo);
|
||||
return lsRetorno;
|
||||
}
|
||||
|
||||
if(!auditador.isObjetosNulos(valorOriginal, valorNovo)) {
|
||||
Class<?> classe = valorOriginal != null ? valorOriginal.getClass() : valorNovo.getClass();
|
||||
Field[] campos = classe.getDeclaredFields();
|
||||
for (Field campoEntidade : campos) {
|
||||
campoEntidade.setAccessible(true);
|
||||
|
||||
NaoAuditar naoAuditar = auditador.extrairAnotacao.extrairNaoAuditar(campoEntidade);
|
||||
if(naoAuditar != null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
AuditarAtributo auditarAtributo = auditador.extrairAnotacao.extrairAuditarAtributo(campoEntidade);
|
||||
String pattern = auditarAtributo != null && StringUtils.isNotBlank(auditarAtributo.pattern()) ? auditarAtributo.pattern() : null;
|
||||
|
||||
Object valorOriginalEntidade = auditador.getValor(campoEntidade, valorOriginal, pattern);
|
||||
Object valorNovoEntidade = auditador.getValor(campoEntidade, valorNovo, pattern);
|
||||
|
||||
AuditadorObjects auditado = auditador.auditarCampo(campoEntidade, valorOriginalEntidade, valorNovoEntidade, localNomeClasse);
|
||||
if(auditado != null) {
|
||||
lsRetorno.add(auditado);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new AuditadorException(e.getMessage(), e);
|
||||
} catch (Throwable e) {
|
||||
throw new AuditadorException(e.getMessage(), e);
|
||||
}
|
||||
return lsRetorno;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
package br.com.rjconsultores.auditador.auditadores;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import br.com.rjconsultores.auditador.enums.AuditadorTipoAlteracao;
|
||||
import br.com.rjconsultores.auditador.exceptions.AuditadorException;
|
||||
import br.com.rjconsultores.auditador.model.AuditadorObjects;
|
||||
|
||||
class AuditadorList {
|
||||
|
||||
protected Auditador auditador;
|
||||
|
||||
public AuditadorList(Auditador auditar) {
|
||||
super();
|
||||
this.auditador = auditar;
|
||||
}
|
||||
|
||||
public List<AuditadorObjects> auditar(Field campo, Object original, Object novo, boolean auditarEntidades) {
|
||||
List<AuditadorObjects> lsRetorno = new ArrayList<AuditadorObjects>();
|
||||
try {
|
||||
|
||||
List<Object> lsOriginal = auditador.getValorList(campo, original);
|
||||
List<Object> lsNovo = auditador.getValorList(campo, novo);
|
||||
|
||||
lsRetorno.addAll(auditarList(campo, auditarEntidades, lsOriginal, lsNovo));
|
||||
} catch (Exception e) {
|
||||
throw new AuditadorException(e.getMessage(), e);
|
||||
} catch (Throwable e) {
|
||||
throw new AuditadorException(e.getMessage(), e);
|
||||
}
|
||||
return lsRetorno;
|
||||
}
|
||||
|
||||
public List<AuditadorObjects> auditarList(Field campo, boolean auditarEntidades,
|
||||
List<Object> lsOriginal, List<Object> lsNovo) throws CloneNotSupportedException {
|
||||
List<AuditadorObjects> lsRetorno = new ArrayList<AuditadorObjects>();
|
||||
|
||||
if(isObjetosList(lsOriginal, lsNovo)) {
|
||||
|
||||
List<Object> lsObjetosInseridos = new ArrayList<Object>(lsNovo);
|
||||
lsObjetosInseridos.removeAll(lsOriginal);
|
||||
lsRetorno.addAll(auditarRegistros(campo, lsObjetosInseridos, true));
|
||||
|
||||
List<Object> lsObjetosRemovidos = new ArrayList<Object>(lsOriginal);
|
||||
lsObjetosRemovidos.removeAll(lsNovo);
|
||||
lsRetorno.addAll(auditarRegistros(campo, lsObjetosRemovidos, false));
|
||||
|
||||
if(auditarEntidades) {
|
||||
List<Object> lsObjetosMantidos = new ArrayList<Object>(lsNovo);
|
||||
lsObjetosMantidos.retainAll(lsOriginal);
|
||||
for (Object object : lsObjetosMantidos) {
|
||||
Object originalEntidade = lsOriginal.get(lsOriginal.indexOf(object));
|
||||
Object novoEntidade = lsNovo.get(lsNovo.indexOf(object));
|
||||
String nomeClasse = auditador.extrairNomeClasse(originalEntidade, novoEntidade);
|
||||
|
||||
lsRetorno.addAll(auditador.auditadorEntidade.auditar(null, originalEntidade, novoEntidade, nomeClasse));
|
||||
}
|
||||
}
|
||||
}
|
||||
return lsRetorno;
|
||||
}
|
||||
|
||||
protected List<AuditadorObjects> auditarRegistros(Field campo, List<Object> lsObjetosRemovidos, boolean inserido)
|
||||
throws CloneNotSupportedException {
|
||||
List<AuditadorObjects> lsRetorno = new ArrayList<AuditadorObjects>();
|
||||
String nomeCampo = auditador.extrairNomeCampo(campo);
|
||||
for (Object removido : lsObjetosRemovidos) {
|
||||
AuditadorObjects objectInserido = null;
|
||||
|
||||
if(inserido) {
|
||||
objectInserido = auditarObjetoInserido(nomeCampo, removido);
|
||||
} else {
|
||||
objectInserido = auditarObjetoRemovido(nomeCampo, removido);
|
||||
}
|
||||
|
||||
if(objectInserido != null) {
|
||||
lsRetorno.add(objectInserido);
|
||||
}
|
||||
}
|
||||
return lsRetorno;
|
||||
}
|
||||
|
||||
private boolean isObjetosList(Object original, Object novo) {
|
||||
if(auditador.isObjetosNulos(original, novo)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return original instanceof Collection && novo instanceof Collection;
|
||||
}
|
||||
|
||||
protected AuditadorObjects auditarObjetoInserido(String nomeCampo, Object objeto) throws CloneNotSupportedException {
|
||||
if(objeto != null) {
|
||||
AuditadorObjects auditado = criarObjetoInseridoRemovido(nomeCampo, objeto);
|
||||
auditado.setValorNovo(String.format("Registro inserido [%s]", auditador.extrairTextoInclusaoExclusao(objeto)));
|
||||
return auditado;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private AuditadorObjects criarObjetoInseridoRemovido(String nomeCampo, Object objeto) throws CloneNotSupportedException {
|
||||
AuditadorObjects auditado = auditador.getPadrao().clonar();
|
||||
auditado.setTipoAlteracao(AuditadorTipoAlteracao.ALTERACAO);
|
||||
auditado.setCampoAlterado(nomeCampo);
|
||||
auditado.setClasseAlterada(objeto.getClass().getSimpleName());
|
||||
return auditado;
|
||||
}
|
||||
|
||||
protected AuditadorObjects auditarObjetoRemovido(String nomeCampo, Object objeto) throws CloneNotSupportedException {
|
||||
if(objeto != null) {
|
||||
AuditadorObjects auditado = criarObjetoInseridoRemovido(nomeCampo, objeto);
|
||||
auditado.setValorNovo(String.format("Registro removido [%s]", auditador.extrairTextoInclusaoExclusao(objeto)));
|
||||
return auditado;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package br.com.rjconsultores.auditador.auditadores;
|
||||
|
||||
import java.lang.reflect.AccessibleObject;
|
||||
|
||||
import br.com.rjconsultores.auditador.annotations.AuditarAtributo;
|
||||
import br.com.rjconsultores.auditador.annotations.AuditarClasse;
|
||||
import br.com.rjconsultores.auditador.annotations.AuditarEntidade;
|
||||
import br.com.rjconsultores.auditador.annotations.AuditarID;
|
||||
import br.com.rjconsultores.auditador.annotations.AuditarLista;
|
||||
import br.com.rjconsultores.auditador.annotations.NaoAuditar;
|
||||
|
||||
class ExtrairAnotacao {
|
||||
|
||||
protected AuditarClasse extrair(Class<?> classe) {
|
||||
if(classe == null) {
|
||||
return null;
|
||||
}
|
||||
return classe.getAnnotation(AuditarClasse.class);
|
||||
}
|
||||
|
||||
protected AuditarAtributo extrairAuditarAtributo(AccessibleObject campo) {
|
||||
if(campo == null) {
|
||||
return null;
|
||||
}
|
||||
campo.setAccessible(true);
|
||||
return campo.getAnnotation(AuditarAtributo.class);
|
||||
}
|
||||
|
||||
protected AuditarEntidade extrairAuditarEntidade(AccessibleObject campo) {
|
||||
if(campo == null) {
|
||||
return null;
|
||||
}
|
||||
campo.setAccessible(true);
|
||||
return campo.getAnnotation(AuditarEntidade.class);
|
||||
}
|
||||
|
||||
protected AuditarID extrairAuditarID(AccessibleObject campo) {
|
||||
if(campo == null) {
|
||||
return null;
|
||||
}
|
||||
campo.setAccessible(true);
|
||||
return campo.getAnnotation(AuditarID.class);
|
||||
}
|
||||
|
||||
protected AuditarLista extrairAuditarLista(AccessibleObject campo) {
|
||||
if(campo == null) {
|
||||
return null;
|
||||
}
|
||||
campo.setAccessible(true);
|
||||
return campo.getAnnotation(AuditarLista.class);
|
||||
}
|
||||
|
||||
protected NaoAuditar extrairNaoAuditar(AccessibleObject campo) {
|
||||
if(campo == null) {
|
||||
return null;
|
||||
}
|
||||
campo.setAccessible(true);
|
||||
return campo.getAnnotation(NaoAuditar.class);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package br.com.rjconsultores.auditador.enums;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public enum AuditadorTipoAlteracao {
|
||||
|
||||
INCLUSAO,
|
||||
ALTERACAO,
|
||||
EXCLUSAO;
|
||||
|
||||
public static List<AuditadorTipoAlteracao> getList() {
|
||||
return Arrays.asList(values());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package br.com.rjconsultores.auditador.exceptions;
|
||||
|
||||
public class AuditadorException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public AuditadorException(String message, Throwable throwable) {
|
||||
super(message, throwable);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package br.com.rjconsultores.auditador.interfaces;
|
||||
|
||||
public interface Auditavel<T> extends Cloneable {
|
||||
|
||||
public void clonar() throws CloneNotSupportedException;
|
||||
|
||||
public T getCloneObject() throws CloneNotSupportedException;
|
||||
|
||||
public String getTextoInclusaoExclusao();
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package br.com.rjconsultores.auditador.interfaces;
|
||||
|
||||
public interface AuditavelTelaAlternativa {
|
||||
|
||||
public String getTelaAlternativa();
|
||||
|
||||
}
|
|
@ -0,0 +1,106 @@
|
|||
package br.com.rjconsultores.auditador.model;
|
||||
|
||||
import br.com.rjconsultores.auditador.enums.AuditadorTipoAlteracao;
|
||||
|
||||
public class AuditadorObjects implements Cloneable {
|
||||
|
||||
private String valorAnterior;
|
||||
private String valorNovo;
|
||||
private String campoAlterado;
|
||||
private String classeAlterada;
|
||||
private String classePrincipal;
|
||||
private String tela;
|
||||
private Integer empresaId;
|
||||
private AuditadorTipoAlteracao tipoAlteracao;
|
||||
private Long idAuditado;
|
||||
|
||||
public String getValorAnterior() {
|
||||
return valorAnterior;
|
||||
}
|
||||
|
||||
public void setValorAnterior(String valorAnterior) {
|
||||
this.valorAnterior = valorAnterior;
|
||||
}
|
||||
|
||||
public String getValorNovo() {
|
||||
return valorNovo;
|
||||
}
|
||||
|
||||
public void setValorNovo(String valorNovo) {
|
||||
this.valorNovo = valorNovo;
|
||||
}
|
||||
|
||||
public String getCampoAlterado() {
|
||||
return campoAlterado;
|
||||
}
|
||||
|
||||
public void setCampoAlterado(String campoAlterado) {
|
||||
this.campoAlterado = campoAlterado;
|
||||
}
|
||||
|
||||
public String getClasseAlterada() {
|
||||
return classeAlterada;
|
||||
}
|
||||
|
||||
public void setClasseAlterada(String classeAlterada) {
|
||||
this.classeAlterada = classeAlterada;
|
||||
}
|
||||
|
||||
public String getClassePrincipal() {
|
||||
return classePrincipal;
|
||||
}
|
||||
|
||||
public void setClassePrincipal(String classePrincipal) {
|
||||
this.classePrincipal = classePrincipal;
|
||||
}
|
||||
|
||||
public String getTela() {
|
||||
return tela;
|
||||
}
|
||||
|
||||
public void setTela(String tela) {
|
||||
this.tela = tela;
|
||||
}
|
||||
|
||||
public Integer getEmpresaId() {
|
||||
return empresaId;
|
||||
}
|
||||
|
||||
public void setEmpresaId(Integer empresaId) {
|
||||
this.empresaId = empresaId;
|
||||
}
|
||||
|
||||
public AuditadorTipoAlteracao getTipoAlteracao() {
|
||||
return tipoAlteracao;
|
||||
}
|
||||
|
||||
public void setTipoAlteracao(AuditadorTipoAlteracao tipoAlteracao) {
|
||||
this.tipoAlteracao = tipoAlteracao;
|
||||
}
|
||||
|
||||
public Long getIdAuditado() {
|
||||
return idAuditado;
|
||||
}
|
||||
|
||||
public void setIdAuditado(Long idAuditado) {
|
||||
this.idAuditado = idAuditado;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
public AuditadorObjects clonar() throws CloneNotSupportedException {
|
||||
return (AuditadorObjects) this.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AuditadorObjects [valorAnterior=" + valorAnterior + ", valorNovo=" + valorNovo + ", campoAlterado="
|
||||
+ campoAlterado + ", classeAlterada=" + classeAlterada + ", classePrincipal=" + classePrincipal
|
||||
+ ", tela=" + tela + ", empresaId=" + empresaId + ", tipoAlteracao=" + tipoAlteracao + ", idAuditado="
|
||||
+ idAuditado + "]";
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue