diff --git a/src/com/rjconsultores/ventaboletos/auditoria/AuditControl.java b/src/com/rjconsultores/ventaboletos/auditoria/AuditControl.java new file mode 100644 index 000000000..e9fcea760 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/auditoria/AuditControl.java @@ -0,0 +1,220 @@ +package com.rjconsultores.ventaboletos.auditoria; + +import java.lang.reflect.Field; +import java.lang.reflect.Type; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.rjconsultores.ventaboletos.entidad.AuditService; + +public class AuditControl { + private String currentActionService; + private Boolean checkModuleAudit; + private AuditService currentService; + private static Gson gson; + private static SimpleDateFormat sdf; + + AuditControl(String currentActionService) { + this.currentActionService = currentActionService; + } + + public static String toJson(Object object) throws IllegalArgumentException, IllegalAccessException, ParseException { + SimpleDateFormat sdf = new SimpleDateFormat(); + sdf.applyPattern("dd/MM/yyyy hh:mm:ss"); + + StringBuilder json = new StringBuilder(); + json.append("{"); + + List lsClazz = new ArrayList(); + List lsFieldName = new ArrayList(); + + for (Field field : object.getClass().getDeclaredFields()) { + if ((!field.isAnnotationPresent(Column.class) && !field.isAnnotationPresent(Id.class) && + !field.isAnnotationPresent(ManyToOne.class)) || field.getType().getSimpleName().equalsIgnoreCase("byte[]")) { + continue; + } + + if (!field.isAnnotationPresent(ManyToOne.class)) { + if (json.length() > 1) { + json.append(","); + } + + json.append("\"".concat(field.getName().concat("\":"))); + } else { + lsFieldName.add("\"".concat(field.getName().concat("\":"))); + } + + field.setAccessible(true); + + if (field.getType().getName().contains("String") || field.getType().getName().contains("Date")) { + json.append("\""); + + Date date = null; + + if (field.getType().getName().equals("java.util.Date")) { + date = (java.util.Date) field.get(object); + json.append(date == null ? "null" : sdf.format(date)); + } else if (field.getType().getName().equals("java.sql.Date")) { + date = (Date) field.get(object); + json.append(date == null ? "null" : sdf.format(date)); + } else if (field.getType().getName().contains("String")) { + json.append((String) field.get(object)); + } + + json.append("\""); + + continue; + } + + if (field.isAnnotationPresent(ManyToOne.class)) { + lsClazz.add(field.get(object)); + continue; + } + + json.append(field.get(object) == null ? "null" : field.get(object).toString()); + } + + for (int i = 0; i < lsClazz.size(); i++) { + json.append(","); + + json.append(lsFieldName.get(i)); + json.append(AuditControl.toJson(lsClazz.get(i))); + } + + json.append("}"); + + return json.toString(); + } + + public static Gson getGson() { + if (AuditControl.gson == null) { + GsonBuilder gsonBuilder = new GsonBuilder(); + gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer() { + DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"); + + public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) + throws JsonParseException { + try { + return df.parse(json.getAsString()); + } catch (ParseException e) { + return null; + } + } + }); + gson = gsonBuilder.create(); + } + + return gson; + } + + public static String formatJson(Object clazz, boolean mainClass) throws IllegalArgumentException, IllegalAccessException { + String init = "{"; + String fim = "}"; + String tab = "\t"; + String ql = "\r"; + + String ret = init.concat(ql).concat(tab); + + List lsClazz = new ArrayList(); + List lsNameFd = new ArrayList(); + + for (Field field : clazz.getClass().getDeclaredFields()) { + if (!field.isAnnotationPresent(Column.class) && !field.isAnnotationPresent(Id.class) && + !field.isAnnotationPresent(ManyToOne.class)) { + continue; + } + + field.setAccessible(true); + + if (field.isAnnotationPresent(ManyToOne.class) && field.get(clazz) != null) { + lsNameFd.add(field.getName()); + lsClazz.add(field.get(clazz)); + continue; + } + + + if (!mainClass) { + ret += tab; + } + + ret += field.getName().concat(":"); + + String fieldValue = null; + + if (field.getType().getSimpleName().equalsIgnoreCase("date") && field.get(clazz) != null) { + fieldValue = AuditControl.convertDateObjectToString(field.get(clazz)); + } else { + fieldValue = (field.get(clazz) == null ? "null" : field.get(clazz)).toString(); + } + + ret += fieldValue; + ret += ql.concat(tab); + } + + for (int i = 0; i < lsClazz.size(); i++) { + ret += lsNameFd.get(i).concat(tab.concat(":").concat(tab)); + ret += formatJson(lsClazz.get(i), false); + } + + if (mainClass) { + ret += ql.concat(fim); + } else { + ret += ql.concat(tab.concat(fim).concat(",").concat(ql).concat(tab)); + } + + return ret; + } + + public static String convertDateObjectToString(Object dateObject) throws RuntimeException { + if (sdf == null) { + sdf = new SimpleDateFormat(); + sdf.applyPattern("dd/MM/yyyy hh:mm:ss"); + } + + if (dateObject.getClass().getName().contains("util")) { + Date date = (Date) dateObject; + return sdf.format(date); + } + + if (dateObject.getClass().getName().contains("sql")) { + java.sql.Date date = (java.sql.Date) dateObject; + return sdf.format(new Date(date.getTime())); + } + + throw new RuntimeException("Não foi possível realizar a conversão de data.\rClass: " + AuditControl.class.getName()); + } + + public String getCurrentActionService() { + return currentActionService; + } + + public AuditService getCurrentService() { + return currentService; + } + + public void setCurrentService(AuditService currentService) { + this.currentService = currentService; + } + + public Boolean getCheckModuleAudit() { + return checkModuleAudit; + } + + public void setCheckModuleAudit(Boolean checkModuleAudit) { + this.checkModuleAudit = checkModuleAudit; + } +} diff --git a/src/com/rjconsultores/ventaboletos/auditoria/AuditManager.java b/src/com/rjconsultores/ventaboletos/auditoria/AuditManager.java new file mode 100644 index 000000000..9dec4163e --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/auditoria/AuditManager.java @@ -0,0 +1,14 @@ +package com.rjconsultores.ventaboletos.auditoria; + +public class AuditManager { + private static AuditControl INSTANCE; + + public static AuditControl getINSTANCE(String currentActionService) { + INSTANCE = new AuditControl(currentActionService); + return INSTANCE; + } + + public static AuditControl getINSTANCE() { + return INSTANCE; + } +} diff --git a/src/com/rjconsultores/ventaboletos/auditoria/interceptor/AuditInterceptor.java b/src/com/rjconsultores/ventaboletos/auditoria/interceptor/AuditInterceptor.java new file mode 100644 index 000000000..2dd40ec14 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/auditoria/interceptor/AuditInterceptor.java @@ -0,0 +1,148 @@ +package com.rjconsultores.ventaboletos.auditoria.interceptor; + +import java.io.Serializable; +import java.sql.Date; + +import javax.enterprise.context.SessionScoped; + +import org.apache.log4j.Logger; +import org.hibernate.Criteria; +import org.hibernate.EmptyInterceptor; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.criterion.Restrictions; +import org.hibernate.type.Type; +import org.jfree.util.Log; + +import com.rjconsultores.ventaboletos.auditoria.AuditControl; +import com.rjconsultores.ventaboletos.auditoria.AuditManager; +import com.rjconsultores.ventaboletos.dao.util.DBUtil; +import com.rjconsultores.ventaboletos.entidad.AuditLog; +import com.rjconsultores.ventaboletos.entidad.AuditService; +import com.rjconsultores.ventaboletos.entidad.FuncionSistema; +import com.rjconsultores.ventaboletos.entidad.Sistema; +import com.rjconsultores.ventaboletos.enums.auditoria.EnumAuditAction; +import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado; + +@SessionScoped +public class AuditInterceptor extends EmptyInterceptor { + + private static Logger log = Logger.getLogger(AuditInterceptor.class); + private static final long serialVersionUID = 1L; + + // Considera que o ADM sempre tenha o ID igual a 1 + private Integer SISTEMA_ID = 1; + + @Override + public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { + try { + genAudit(entity, EnumAuditAction.CRIACAO); + } catch (Exception exception) { + log.error(exception.getStackTrace()); + } + + return super.onSave(entity, id, state, propertyNames, types); + } + + @Override + public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { + try { + genAudit(entity, EnumAuditAction.ALTERACAO); + } catch (Exception exception) { + log.error(exception.getStackTrace()); + } + + return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types); + } + + @Override + public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { + try { + genAudit(entity, EnumAuditAction.EXCLUSAO); + } catch (Exception exception) { + log.error(exception.getStackTrace()); + } + + super.onDelete(entity, id, state, propertyNames, types); + } + + private boolean getAuditModuleService() { + + if (!AuditManager.getINSTANCE().getCheckModuleAudit()) { + return AuditManager.getINSTANCE().getCurrentService() != null ? true : false; + } + + AuditService auditService = null; + + SessionFactory sessionFactory = DBUtil.getInstance().getSessionFactory(); + Session 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; + } + + private void genAudit(Object entity, EnumAuditAction action) { + if (!getAuditModuleService()) { + return; + } + + try { + /** + * Ignorado caso a entidade for de auditoria e se não for encontrado o módulo no banco + */ + if (!(entity instanceof AuditLog)) { + + AuditService service = AuditManager.getINSTANCE().getCurrentService(); + + if (!(entity instanceof AuditLog) && service != null && service.getModule() != null) { + /** + * A auditoria não será realizada nos seguintes cenários: 1) Não for encontrado o serviço no banco de dados 2) O serviço for encontrado, porém não está habilitado para auditoria + */ + if (service.getModule().getIndAuditable()) { + AuditLog audit = new AuditLog(); + audit.setCreatedDate(new Date(System.currentTimeMillis())); + audit.setEntityName(entity.getClass().getSimpleName()); + audit.setService(service); + audit.setEntityDetail(AuditControl.toJson(entity)); + + Sistema sistema = new Sistema(); + sistema.setSistemaId(SISTEMA_ID); + audit.setSistema(sistema); + + audit.setAction(action.getId()); + audit.setUsuario(UsuarioLogado.getUsuarioLogado()); + + SessionFactory sessionFactory = DBUtil.getInstance().getSessionFactory(); + Session session = sessionFactory.openSession(); + + session.persist(audit); + session.flush(); + } + } + } + + } catch (Exception exception) { + log.error(exception.getStackTrace()); + } + } +} diff --git a/src/com/rjconsultores/ventaboletos/dao/AuditLogDAO.java b/src/com/rjconsultores/ventaboletos/dao/AuditLogDAO.java new file mode 100644 index 000000000..4164f302c --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/AuditLogDAO.java @@ -0,0 +1,12 @@ +package com.rjconsultores.ventaboletos.dao; + +import java.util.List; + +import com.rjconsultores.ventaboletos.entidad.AuditLog; +import com.rjconsultores.ventaboletos.entidad.AuditModule; + +public interface AuditLogDAO extends GenericDAO { + public List carregarLog(AuditModule modulo); + + public List filtrarLog(AuditLog log); +} diff --git a/src/com/rjconsultores/ventaboletos/dao/AuditModuleDAO.java b/src/com/rjconsultores/ventaboletos/dao/AuditModuleDAO.java new file mode 100644 index 000000000..beb08ea69 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/AuditModuleDAO.java @@ -0,0 +1,7 @@ +package com.rjconsultores.ventaboletos.dao; + +import com.rjconsultores.ventaboletos.entidad.AuditModule; + +public interface AuditModuleDAO extends GenericDAO{ + +} diff --git a/src/com/rjconsultores/ventaboletos/dao/AuditServiceDAO.java b/src/com/rjconsultores/ventaboletos/dao/AuditServiceDAO.java new file mode 100644 index 000000000..e7e249c6f --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/AuditServiceDAO.java @@ -0,0 +1,7 @@ +package com.rjconsultores.ventaboletos.dao; + +import com.rjconsultores.ventaboletos.entidad.AuditService; + +public interface AuditServiceDAO extends GenericDAO { + public AuditService carregarService(AuditService auditService); +} diff --git a/src/com/rjconsultores/ventaboletos/dao/hibernate/AuditLogHibernateDAO.java b/src/com/rjconsultores/ventaboletos/dao/hibernate/AuditLogHibernateDAO.java new file mode 100644 index 000000000..26db63f9a --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/hibernate/AuditLogHibernateDAO.java @@ -0,0 +1,84 @@ +package com.rjconsultores.ventaboletos.dao.hibernate; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.Criteria; +import org.hibernate.SessionFactory; +import org.hibernate.criterion.MatchMode; +import org.hibernate.criterion.Restrictions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Repository; + +import com.rjconsultores.ventaboletos.dao.AuditLogDAO; +import com.rjconsultores.ventaboletos.entidad.AuditLog; +import com.rjconsultores.ventaboletos.entidad.AuditModule; +import com.rjconsultores.ventaboletos.entidad.AuditService; + +@Repository("auditLogDAO") +public class AuditLogHibernateDAO extends GenericHibernateDAO + implements AuditLogDAO { + + @Autowired + public AuditLogHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) { + setSessionFactory(factory); + } + + @Override + public List carregarLog(AuditModule modulo) { + + List services = recuperarServices(modulo); + + List ids = new ArrayList(); + + for (AuditService service : services) { + ids.add(service.getAuditServiceId()); + } + + Criteria c = getSession().createCriteria(getPersistentClass()); + c.add(Restrictions.in("service.auditServiceId", ids)); + + return c.list(); + } + + private List recuperarServices(AuditModule module) { + Criteria c = getSession().createCriteria(AuditService.class); + c.add(Restrictions.eq("module", module)); + + return c.list(); + } + + @Override + public List filtrarLog(AuditLog log) { + Criteria c = getSession().createCriteria(getPersistentClass()); + + if (log.getSistema() != null) { + c.add(Restrictions.eq("sistema", log.getSistema())); + } + + if (log.getAction() != null) { + c.add(Restrictions.eq("action", log.getAction())); + } + + if (log.getEntityDetail() != null) { + c.add(Restrictions.like("entityDetail", log.getEntityDetail(), MatchMode.ANYWHERE)); + } + + if (log.getUsuario() != null) { + c.add(Restrictions.eq("usuario", log.getUsuario())); + } + + if (log.getService() != null && log.getService().getModule() != null) { + c.add(Restrictions.eq("service.module", log.getService().getModule())); + } + + if (log.getDataInicio() != null && log.getDataFim() != null) { + c.add(Restrictions.between("createDate", log.getDataInicio(), log.getDataFim())); + } + + return c.list(); + } + + +} diff --git a/src/com/rjconsultores/ventaboletos/dao/hibernate/AuditModuleHibernateDAO.java b/src/com/rjconsultores/ventaboletos/dao/hibernate/AuditModuleHibernateDAO.java new file mode 100644 index 000000000..969df2441 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/hibernate/AuditModuleHibernateDAO.java @@ -0,0 +1,28 @@ +package com.rjconsultores.ventaboletos.dao.hibernate; + +import java.util.List; + +import org.hibernate.Criteria; +import org.hibernate.SessionFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Repository; + +import com.rjconsultores.ventaboletos.dao.AuditModuleDAO; +import com.rjconsultores.ventaboletos.entidad.AuditModule; + +@Repository("auditModuleDAO") +public class AuditModuleHibernateDAO extends GenericHibernateDAO + implements AuditModuleDAO { + + @Autowired + public AuditModuleHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) { + setSessionFactory(factory); + } + + @Override + public List obtenerTodos() { + Criteria c = getSession().createCriteria(getPersistentClass()); + return c.list(); + } +} \ No newline at end of file diff --git a/src/com/rjconsultores/ventaboletos/dao/hibernate/AuditServiceHibernateDAO.java b/src/com/rjconsultores/ventaboletos/dao/hibernate/AuditServiceHibernateDAO.java new file mode 100644 index 000000000..8d3a59c41 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/dao/hibernate/AuditServiceHibernateDAO.java @@ -0,0 +1,35 @@ +package com.rjconsultores.ventaboletos.dao.hibernate; + +import org.hibernate.Criteria; +import org.hibernate.SessionFactory; +import org.hibernate.criterion.Restrictions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Repository; + +import com.rjconsultores.ventaboletos.dao.AuditServiceDAO; +import com.rjconsultores.ventaboletos.entidad.AuditService; + +@Repository("auditServiceDAO") +public class AuditServiceHibernateDAO extends GenericHibernateDAO + implements AuditServiceDAO { + + @Autowired + public AuditServiceHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) { + setSessionFactory(factory); + } + + @Override + public AuditService carregarService(AuditService auditService) { + AuditService service = null; + + Criteria criteriaService = getSession().createCriteria(AuditService.class); + + if (auditService.getFuncionSistema() != null && auditService.getFuncionSistema().getDescruta() != null) { + criteriaService.add(Restrictions.eq("funcionSistema", auditService.getFuncionSistema().getDescruta())); + service = (AuditService) criteriaService.uniqueResult(); + } + + return service; + } +} diff --git a/src/com/rjconsultores/ventaboletos/entidad/AuditLog.java b/src/com/rjconsultores/ventaboletos/entidad/AuditLog.java new file mode 100644 index 000000000..a40962b34 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/entidad/AuditLog.java @@ -0,0 +1,143 @@ +package com.rjconsultores.ventaboletos.entidad; + +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.SequenceGenerator; +import javax.persistence.Table; +import javax.persistence.Transient; + +@Entity +@SequenceGenerator(name = "AUDITLOG_SEQ", sequenceName = "AUDIT_LOG_SEQ", allocationSize = 1) +@Table(name = "AUDIT_LOG") +public class AuditLog implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @Column(name = "AUDITLOG_ID") + @GeneratedValue(strategy = GenerationType.AUTO, generator = "AUDITLOG_SEQ") + private Long auditLogId; + + @Column(name = "AUDITACTION_ID") + private Integer action; + + @Column(name = "ENTITY_DETAIL") + private String entityDetail; + + @Column(name = "ENTITY_NAME") + private String entityName; + + @Column(name = "CREATED_DATE") + private Date createdDate; + + @JoinColumn(name = "SISTEMA_ID", referencedColumnName = "SISTEMA_ID") + @ManyToOne + private Sistema sistema; + + @JoinColumn(name = "USUARIO_ID", referencedColumnName = "USUARIO_ID") + @ManyToOne + private Usuario usuario; + + @JoinColumn(name = "AUDITSERVICE_ID", referencedColumnName = "AUDITSERVICE_ID") + @ManyToOne + private AuditService service; + + @Transient + private Date dataInicio; + + @Transient + private Date dataFim; + + public Long getAuditLogId() { + return auditLogId; + } + + public void setAuditLogId(Long auditLogId) { + this.auditLogId = auditLogId; + } + + public Integer getAction() { + return action; + } + + public void setAction(Integer action) { + this.action = action; + } + + public Sistema getSistema() { + return sistema; + } + + public void setSistema(Sistema sistema) { + this.sistema = sistema; + } + + public String getEntityDetail() { + return entityDetail; + } + + public void setEntityDetail(String entityDetail) { + this.entityDetail = entityDetail; + } + + public String getEntityName() { + return entityName; + } + + public void setEntityName(String entityName) { + this.entityName = entityName; + } + + public Date getCreatedDate() { + return createdDate; + } + + public void setCreatedDate(Date createdDate) { + this.createdDate = createdDate; + } + + public Usuario getUsuario() { + return usuario; + } + + public void setUsuario(Usuario usuario) { + this.usuario = usuario; + } + + public AuditService getService() { + return service; + } + + public void setService(AuditService service) { + this.service = service; + } + + @Override + public String toString() { + return createdDate.toString() + sistema.toString(); + } + + public Date getDataFim() { + return dataFim; + } + + public void setDataFim(Date dataFim) { + this.dataFim = dataFim; + } + + public Date getDataInicio() { + return dataInicio; + } + + public void setDataInicio(Date dataInicio) { + this.dataInicio = dataInicio; + } +} diff --git a/src/com/rjconsultores/ventaboletos/entidad/AuditModule.java b/src/com/rjconsultores/ventaboletos/entidad/AuditModule.java new file mode 100644 index 000000000..9ae1d3189 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/entidad/AuditModule.java @@ -0,0 +1,54 @@ +package com.rjconsultores.ventaboletos.entidad; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "AUDIT_MODULE") +public class AuditModule implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @Column(name = "AUDITMODULE_ID") + private Long auditModuleId; + + @Column(name = "NAMEMODULE") + private String nameModule; + + @Column(name = "INDAUDITABLE") + private Boolean indAuditable; + + public Long getAuditModuleId() { + return auditModuleId; + } + + public void setAuditModuleId(Long auditModuleId) { + this.auditModuleId = auditModuleId; + } + + public Boolean getIndAuditable() { + return indAuditable; + } + + public String getNameModule() { + return nameModule; + } + + public void setNameModule(String nameModule) { + this.nameModule = nameModule; + } + + public void setIndAuditable(Boolean indAuditable) { + this.indAuditable = indAuditable; + } + + @Override + public String toString() { + return nameModule; + } +} diff --git a/src/com/rjconsultores/ventaboletos/entidad/AuditService.java b/src/com/rjconsultores/ventaboletos/entidad/AuditService.java new file mode 100644 index 000000000..62eb1099a --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/entidad/AuditService.java @@ -0,0 +1,64 @@ +package com.rjconsultores.ventaboletos.entidad; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "AUDIT_SERVICE") +public class AuditService implements Serializable{ + + private static final long serialVersionUID = 1L; + + @Id + @Column(name = "AUDITSERVICE_ID") + private Long auditServiceId; + + @Column(name = "NAMESERVICE") + private String nameService; + + @JoinColumn(name = "FUNCIONSISTEMA_ID", referencedColumnName = "FUNCIONSISTEMA_ID") + @ManyToOne + private FuncionSistema funcionSistema; + + @JoinColumn(name = "AUDITMODULE_ID", referencedColumnName = "AUDITMODULE_ID") + @ManyToOne + private AuditModule module; + + public Long getAuditServiceId() { + return auditServiceId; + } + + public void setAuditServiceId(Long auditServiceId) { + this.auditServiceId = auditServiceId; + } + + public String getNameService() { + return nameService; + } + + public void setNameService(String nameService) { + this.nameService = nameService; + } + + public AuditModule getModule() { + return module; + } + + public void setModule(AuditModule module) { + this.module = module; + } + + public FuncionSistema getFuncionSistema() { + return funcionSistema; + } + + public void setFuncionSistema(FuncionSistema funcionSistema) { + this.funcionSistema = funcionSistema; + } +} diff --git a/src/com/rjconsultores/ventaboletos/enums/auditoria/EnumAuditAction.java b/src/com/rjconsultores/ventaboletos/enums/auditoria/EnumAuditAction.java new file mode 100644 index 000000000..cfed2b721 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/enums/auditoria/EnumAuditAction.java @@ -0,0 +1,44 @@ +package com.rjconsultores.ventaboletos.enums.auditoria; + +public enum EnumAuditAction { + TODOS(-1, "Todos"), CRIACAO(1,"Criação de Registro"), ALTERACAO(2, "Alteração de registro"), EXCLUSAO(3, "Exclusão de Registro"); + + private String descricao; + private Integer id; + + private EnumAuditAction(Integer id, String descricao) { + this.descricao = descricao; + this.id = id; + } + + public String getDescricao() { + return descricao; + } + + public void setDescricao(String descricao) { + this.descricao = descricao; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public static String findAction(Integer id) { + for (EnumAuditAction action : EnumAuditAction.values()) { + if (action.getId() == id) { + return action.getDescricao(); + } + } + + return null; + } + + @Override + public String toString() { + return descricao; + } +} diff --git a/src/com/rjconsultores/ventaboletos/service/AuditLogService.java b/src/com/rjconsultores/ventaboletos/service/AuditLogService.java new file mode 100644 index 000000000..de5336425 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/service/AuditLogService.java @@ -0,0 +1,12 @@ +package com.rjconsultores.ventaboletos.service; + +import java.util.List; + +import com.rjconsultores.ventaboletos.entidad.AuditLog; +import com.rjconsultores.ventaboletos.entidad.AuditModule; + +public interface AuditLogService extends GenericService{ + public List carregarLog(AuditModule modulo); + + public List filtrarLog(AuditLog log); +} diff --git a/src/com/rjconsultores/ventaboletos/service/AuditModuleService.java b/src/com/rjconsultores/ventaboletos/service/AuditModuleService.java new file mode 100644 index 000000000..f38c24731 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/service/AuditModuleService.java @@ -0,0 +1,8 @@ +package com.rjconsultores.ventaboletos.service; + +import com.rjconsultores.ventaboletos.entidad.AuditModule; + +public interface AuditModuleService extends GenericService { + + +} diff --git a/src/com/rjconsultores/ventaboletos/service/AuditServiceService.java b/src/com/rjconsultores/ventaboletos/service/AuditServiceService.java new file mode 100644 index 000000000..5abfaa462 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/service/AuditServiceService.java @@ -0,0 +1,7 @@ +package com.rjconsultores.ventaboletos.service; + +import com.rjconsultores.ventaboletos.entidad.AuditService; + +public interface AuditServiceService extends GenericService { + public AuditService carregarService(AuditService auditService); +} diff --git a/src/com/rjconsultores/ventaboletos/service/impl/AuditLogServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/AuditLogServiceImpl.java new file mode 100644 index 000000000..5f08d94e3 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/service/impl/AuditLogServiceImpl.java @@ -0,0 +1,53 @@ +package com.rjconsultores.ventaboletos.service.impl; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.rjconsultores.ventaboletos.dao.AuditLogDAO; +import com.rjconsultores.ventaboletos.entidad.AuditLog; +import com.rjconsultores.ventaboletos.entidad.AuditModule; +import com.rjconsultores.ventaboletos.service.AuditLogService; + +@Service("auditLogService") +public class AuditLogServiceImpl implements AuditLogService { + + @Autowired + private AuditLogDAO auditLogDAO; + + @Override + public List obtenerTodos() { + return auditLogDAO.obtenerTodos(); + } + + @Override + public AuditLog obtenerID(Long id) { + return auditLogDAO.obtenerID(id); + } + + @Override + public AuditLog suscribir(AuditLog auditLog) { + return auditLogDAO.suscribir(auditLog); + } + + @Override + public AuditLog actualizacion(AuditLog auditLog) { + return auditLogDAO.actualizacion(auditLog); + } + + @Override + public void borrar(AuditLog auditLog) { + auditLogDAO.borrar(auditLog); + } + + @Override + public List carregarLog(AuditModule modulo) { + return auditLogDAO.carregarLog(modulo); + } + + @Override + public List filtrarLog(AuditLog auditLog) { + return auditLogDAO.filtrarLog(auditLog); + } +} diff --git a/src/com/rjconsultores/ventaboletos/service/impl/AuditModuleServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/AuditModuleServiceImpl.java new file mode 100644 index 000000000..f53474177 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/service/impl/AuditModuleServiceImpl.java @@ -0,0 +1,46 @@ +package com.rjconsultores.ventaboletos.service.impl; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.rjconsultores.ventaboletos.dao.AuditModuleDAO; +import com.rjconsultores.ventaboletos.entidad.AuditModule; +import com.rjconsultores.ventaboletos.service.AuditModuleService; + +@Service("auditModuleService") +public class AuditModuleServiceImpl implements AuditModuleService { + + @Autowired + private AuditModuleDAO auditModuleDAO; + + @Override + public List obtenerTodos() { + return auditModuleDAO.obtenerTodos(); + } + + @Override + public AuditModule obtenerID(Long id) { + // TODO Auto-generated method stub + return null; + } + + @Override + public AuditModule suscribir(AuditModule entidad) { + // TODO Auto-generated method stub + return null; + } + + @Override + public AuditModule actualizacion(AuditModule entidad) { + // TODO Auto-generated method stub + return null; + } + + @Override + public void borrar(AuditModule entidad) { + // TODO Auto-generated method stub + + } +} diff --git a/src/com/rjconsultores/ventaboletos/service/impl/AuditServiceServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/AuditServiceServiceImpl.java new file mode 100644 index 000000000..0960b4204 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/service/impl/AuditServiceServiceImpl.java @@ -0,0 +1,47 @@ +package com.rjconsultores.ventaboletos.service.impl; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.rjconsultores.ventaboletos.dao.AuditServiceDAO; +import com.rjconsultores.ventaboletos.entidad.AuditService; +import com.rjconsultores.ventaboletos.service.AuditServiceService; + +@Service("auditServiceService") +public class AuditServiceServiceImpl implements AuditServiceService { + + @Autowired + private AuditServiceDAO auditServiceDAO; + + @Override + public List obtenerTodos() { + return auditServiceDAO.obtenerTodos(); + } + + @Override + public AuditService obtenerID(Long id) { + return auditServiceDAO.obtenerID(id); + } + + @Override + public AuditService suscribir(AuditService entidad) { + return auditServiceDAO.suscribir(entidad); + } + + @Override + public AuditService actualizacion(AuditService entidad) { + return auditServiceDAO.actualizacion(entidad); + } + + @Override + public void borrar(AuditService entidad) { + auditServiceDAO.borrar(entidad); + } + + @Override + public AuditService carregarService(AuditService auditService) { + return auditServiceDAO.carregarService(auditService); + } +} diff --git a/src/com/rjconsultores/ventaboletos/utilerias/spring/MyAnnotationSessionFactoryBean.java b/src/com/rjconsultores/ventaboletos/utilerias/spring/MyAnnotationSessionFactoryBean.java index 549c0cd07..e97526a24 100644 --- a/src/com/rjconsultores/ventaboletos/utilerias/spring/MyAnnotationSessionFactoryBean.java +++ b/src/com/rjconsultores/ventaboletos/utilerias/spring/MyAnnotationSessionFactoryBean.java @@ -4,19 +4,30 @@ import org.hibernate.HibernateException; import org.hibernate.cfg.AnnotationConfiguration; import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean; +import com.rjconsultores.ventaboletos.auditoria.interceptor.AuditInterceptor; import com.rjconsultores.ventaboletos.dao.hibernate.sqlfunction.FnArredondamentoTarifa; +import com.rjconsultores.ventaboletos.dao.util.DBUtil; @SuppressWarnings("deprecation") public class MyAnnotationSessionFactoryBean extends AnnotationSessionFactoryBean{ - + + public MyAnnotationSessionFactoryBean() { - super(); + super(); + super.setEntityInterceptor(new AuditInterceptor()); } - @Override protected void postProcessAnnotationConfiguration(AnnotationConfiguration config) throws HibernateException { config.addSqlFunction("FN_ARREDONDAMENTO_TARIFA", new FnArredondamentoTarifa()); } + + @Override + protected void afterSessionFactoryCreation() throws Exception { + DBUtil.getInstance().setSessionFactory(this.getSessionFactory()); + super.afterSessionFactoryCreation(); + } } + +