AuditableAspectConfiguration.java

  1. /*
  2.  * Copyright 2010-2024 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.service.data.impl;

  20. import java.sql.Connection;
  21. import java.util.HashMap;
  22. import java.util.Map;

  23. import javax.persistence.EntityManager;
  24. import javax.persistence.PersistenceContext;

  25. import org.hibernate.engine.spi.SharedSessionContractImplementor;
  26. import org.javers.core.Javers;
  27. import org.javers.core.MappingStyle;
  28. import org.javers.hibernate.integration.HibernateUnproxyObjectAccessHook;
  29. import org.javers.repository.sql.ConnectionProvider;
  30. import org.javers.repository.sql.DialectName;
  31. import org.javers.repository.sql.JaversSqlRepository;
  32. import org.javers.repository.sql.SqlRepositoryBuilder;
  33. import org.javers.spring.auditable.AuthorProvider;
  34. import org.javers.spring.auditable.CommitPropertiesProvider;
  35. import org.javers.spring.auditable.aspect.JaversAuditableAspect;
  36. import org.javers.spring.jpa.TransactionalJpaJaversBuilder;
  37. import org.springframework.context.annotation.Bean;
  38. import org.springframework.context.annotation.Configuration;
  39. import org.springframework.security.core.context.SecurityContext;
  40. import org.springframework.security.core.context.SecurityContextHolder;
  41. import org.springframework.transaction.PlatformTransactionManager;

  42. /**
  43.  * The Class AuditableAspectConfiguration.
  44.  */
  45. @Configuration
  46. public class AuditableAspectConfiguration {

  47.     /** The entity manager. */
  48.     @PersistenceContext
  49.     private EntityManager entityManager;

  50.     /**
  51.      * Instantiates a new auditable aspect configuration.
  52.      */
  53.     public AuditableAspectConfiguration() {
  54.         super();
  55.     }

  56.     /**
  57.      * Gets the javers.
  58.      *
  59.      * @param txManager the tx manager
  60.      * @return the javers
  61.      */
  62.     @Bean
  63.     public Javers getJavers(final PlatformTransactionManager txManager) {
  64.         final JaversSqlRepository sqlRepository = SqlRepositoryBuilder.sqlRepository()
  65.                 .withConnectionProvider(new ConnectionProvider() {

  66.                     @Override
  67.                     public Connection getConnection() {
  68.                         final SharedSessionContractImplementor session = entityManager.unwrap(SharedSessionContractImplementor.class);
  69.                         return session.connection();
  70.                     }
  71.                 }).withDialect(DialectName.POSTGRES).build();

  72.         return TransactionalJpaJaversBuilder.javers().withTxManager(txManager)
  73.                 .withObjectAccessHook(new HibernateUnproxyObjectAccessHook()).registerJaversRepository(sqlRepository)
  74.                 .withMappingStyle(MappingStyle.BEAN).build();
  75.     }

  76.     /**
  77.      * Javers auditable aspect.
  78.      *
  79.      * @param javers                   the javers
  80.      * @param authorProvider           the author provider
  81.      * @param commitPropertiesProvider the commit properties provider
  82.      * @return the javers auditable aspect
  83.      */
  84.     @Bean
  85.     public JaversAuditableAspect javersAuditableAspect(final Javers javers, final AuthorProvider authorProvider,
  86.             final CommitPropertiesProvider commitPropertiesProvider) {
  87.         return new JaversAuditableAspect(javers, authorProvider, commitPropertiesProvider);
  88.     }

  89.     /**
  90.      * Author provider.
  91.      *
  92.      * @return the author provider
  93.      */
  94.     @Bean
  95.     public AuthorProvider authorProvider() {
  96.         return () -> {
  97.             final SecurityContext context = SecurityContextHolder.getContext();
  98.             if (context != null && context.getAuthentication() != null) {
  99.                 return context.getAuthentication().getPrincipal().toString();
  100.             } else {
  101.                 return "system";
  102.             }
  103.         };
  104.     }

  105.     /**
  106.      * Commit properties provider.
  107.      *
  108.      * @return the commit properties provider
  109.      */
  110.     @Bean
  111.     public CommitPropertiesProvider commitPropertiesProvider() {
  112.         return new CommitPropertiesProvider() {
  113.             @Override
  114.             public Map<String, String>  provideForCommittedObject(final Object domainObject) {
  115.                 final Map<String, String> props = new HashMap<>();
  116.                 props.put("key", "ok");
  117.                 return props;
  118.             }
  119.         };
  120.     }
  121. }