Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package beans
- import br.com.gridnet.AuditService
- import br.com.gridnet.audit.Audit
- import br.com.gridnet.audit.AuditOrigin
- import br.com.gridnet.data.enumeration.AuditType
- import grails.util.Holders
- import org.hibernate.event.spi.*
- import org.springframework.context.ApplicationContext
- class AuditHibernateEventListeners implements PreUpdateEventListener,
- PostInsertEventListener,PostUpdateEventListener,PostDeleteEventListener {
- // No audit clazz
- private static String[] notAuditedClasses = ['DBFile','IntegrationClientLog','AsynchronousMailMessage',
- 'AsynchronousMailAttachment', 'Stock',
- 'TicketDocument','Warehouse','Submenu', 'Menu', 'MenuXMenuItem',
- 'MenuItem', 'UserPasswordHistory','IntegrationService','DocumentReference',
- 'Movement', 'Receiving','Addressing', 'Separation', 'Inventory', 'Dispatch',
- 'Operation', 'StockMirror', 'StockMirror','StockMirrorClassification',
- 'StockMirrorFeature', 'StockMirrorGroup', 'StockMirrorOwner', 'StockMirrorProduct',
- 'StockMirrorProductSerial', 'StockMirrorSubgroup', 'StockReserve', 'StockDivergenceErp',
- 'SlowMoving', 'BillingAmount','BillingOperation','BillingPalletOperation',
- 'BillingPosition','BillingServices','BillingStay','BillingWindow','BillingWindowStay',
- 'AutomaticPrintDocument','SystemAlert','Audit','AuditOrigin','SeparationCheckingDiv',
- 'SalesOrderGWDispatchPrograming', 'SalesOrderGWDocuments']
- private static String[] notAuditedFiels = ['stockEndDate','availableAmountInStock']
- private static ApplicationContext applicationContext
- private static saveLog = Boolean.TRUE
- private static ApplicationContext getContext() {
- if(!applicationContext)
- applicationContext = Holders.applicationContext
- return applicationContext
- }
- // Generic way to get service
- private <T> T getService(Class<T> clazz) {
- if(!clazz) return
- try{
- return context.getBean(clazz.simpleName.replaceFirst(/^[A-Z]/){it.toLowerCase()})
- }catch(Exception e){
- print(e.stackTrace)
- }
- }
- @Override
- boolean onPreUpdate(PreUpdateEvent event) {
- event.entity.updatedProperties += event.entity.dirtyPropertyNames ?: []
- return false
- }
- @Override
- void onPostDelete(PostDeleteEvent event) {
- if (saveLog && !(event.entity.class.simpleName in notAuditedClasses)){
- def entity = event.entity
- def fieldChange = Boolean.FALSE
- def information = '<table class="tableLog subtable"><thead><th>Campo</th><th>Valor</th></thead>'
- event.persister.propertyNames?.each {
- try {
- def _value = getValue(entity[it])
- def _isValidField = !it.equals('warehouse') && !it.equals('orderTimestamp')
- if (_value && _isValidField){
- fieldChange = Boolean.TRUE
- def propertyLabel = context.getMessage("${entity.class.simpleName?.toLowerCase()}.${it}.label".toString(), [] as String[], it, Locale.getDefault())
- information <<= "<tr><td><strong>${propertyLabel}</strong></td> <td>${_value}</td></tr>"
- }
- }catch(e) { }
- }
- information <<= "</table>"
- if(fieldChange) Audit.withNewSession {
- getService(AuditService)?.addAudit(AuditType.DE,entity.class.simpleName,entity.id,"${information ?: entity}")
- }
- }
- }
- @Override
- void onPostInsert(PostInsertEvent event) {
- if (saveLog && !(event.entity.class.simpleName in notAuditedClasses)){
- def entity = event.entity
- def fieldChange = Boolean.FALSE
- switch(entity.class) {
- case Audit :
- case AuditOrigin :
- return
- }
- def information = '<table class="tableLog subtable"><thead><th>Campo</th><th>Valor</th></thead>'
- event.persister.propertyNames?.each {
- try {
- def _value = getValue(entity[it])
- def _isValidField = !it.equals('warehouse') && !it.equals('orderTimestamp')
- if (_value && _isValidField){
- fieldChange = Boolean.TRUE
- def propertyLabel = context.getMessage("${entity.class.simpleName?.toLowerCase()}.${it}.label".toString(), [] as String[], it, Locale.getDefault())
- information <<= "<tr><td><strong>${propertyLabel}</strong></td> <td>${_value}</td></tr>"
- }
- }catch(e) { }
- }
- information <<= "</table>"
- if(fieldChange) Audit.withNewSession {
- getService(AuditService)?.addAudit(AuditType.IN,entity.class.simpleName,entity.id,information.toString())
- }
- }
- }
- @Override
- void onPostUpdate(PostUpdateEvent event) {
- if (saveLog && !(event.entity.class.simpleName in notAuditedClasses)){
- def String information = '<table class="tableLog subtable"><thead><th>Campo</th><th>Antes</th><th>Depois</th><thead>'
- def entity = event.entity
- def nameList = event.persister.propertyNames
- if(entity.updatedProperties?.size() != event.dirtyProperties?.size()) return
- def propertiesChange = 0
- event.dirtyProperties?.each {
- def _property = nameList[it]
- if (!(_property in notAuditedFiels)) {
- propertiesChange++
- def propertyLabel = context.getMessage("${entity.class.simpleName?.toLowerCase()}.${_property}.label".toString(), [] as String[], _property, Locale.getDefault())
- def oldValue = getValue(event.oldState[it] ?: ' ')
- def newValue = getValue(event.state[it] ?: ' ')
- information <<= "<tr><td><strong>${propertyLabel}</strong></td> <td>${oldValue}</td><td>${newValue}</td></tr>"
- }
- }
- if (propertiesChange == 0) return
- /*
- Put the Id information
- */
- def propertyLabel = "ID"
- def _value = getValue(entity['id']?:' ')
- information <<= "<tr><td><strong>${propertyLabel}</strong></td> <td colspan='2'>${_value}</td></tr>"
- def hasCode = event.persister.propertyNames?.find {'code'.equals(it)}
- if (hasCode){
- propertyLabel = context.getMessage("${entity.class.simpleName?.toLowerCase()}.code.label".toString(), [] as String[], 'Codigo', Locale.getDefault())
- _value = getValue(entity['code']?:' ')
- information <<= "<tr><td><strong>${propertyLabel}</strong></td> <td colspan='2'>${_value}</td></tr>"
- }
- information <<= "</table>"
- if(information) Audit.withNewSession {
- getService(AuditService)?.addAudit(AuditType.UP,entity.class.simpleName,entity.id,information.toString())
- }
- }
- }
- public boolean requiresPostCommitHanding(org.hibernate.persister.entity.EntityPersister entityPersister){
- return false
- }
- private Object getValue(Object obj){
- if (obj && obj?.toString()?.contains('label.') || obj?.toString()?.contains('.label')){
- return context.getMessage(obj?.toString(), [] as String[], obj?.toString(), Locale.getDefault())
- }
- else return obj
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement