/**
 * Copyright (C) 2014 Esup Portail http://www.esup-portail.org
 * @Author (C) 2012 Julien Gribonvald <julien.gribonvald@recia.fr>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *                 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.esupportail.publisher.repository;

import org.esupportail.publisher.config.audit.AuditEventConverter;
import org.esupportail.publisher.domain.PersistentAuditEvent;
import org.joda.time.LocalDateTime;
import org.springframework.boot.actuate.audit.AuditEvent;
import org.springframework.boot.actuate.audit.AuditEventRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.Date;
import java.util.List;

/**
 * Wraps an implementation of Spring Boot's AuditEventRepository.
 */
@Repository
public class CustomAuditEventRepository {

    @Inject
    private PersistenceAuditEventRepository persistenceAuditEventRepository;

    @Bean
    public AuditEventRepository auditEventRepository() {
        return new AuditEventRepository() {

            @Inject
            private AuditEventConverter auditEventConverter;

            @Override
            public List<AuditEvent> find(String principal, Date after) {
                Iterable<PersistentAuditEvent> persistentAuditEvents;
                if (principal == null && after == null) {
                    persistentAuditEvents = persistenceAuditEventRepository.findAll();
                } else if (after == null) {
                    persistentAuditEvents = persistenceAuditEventRepository.findByPrincipal(principal);
                } else {
                    persistentAuditEvents =
                            persistenceAuditEventRepository.findByPrincipalAndAuditEventDateAfter(principal, new LocalDateTime(after));
                }
                return auditEventConverter.convertToAuditEvent(persistentAuditEvents);
            }

            @Override
            @Transactional(propagation = Propagation.REQUIRES_NEW)
            public void add(AuditEvent event) {
                PersistentAuditEvent persistentAuditEvent = new PersistentAuditEvent();
                persistentAuditEvent.setPrincipal(event.getPrincipal());
                persistentAuditEvent.setAuditEventType(event.getType());
                persistentAuditEvent.setAuditEventDate(new LocalDateTime(event.getTimestamp()));
                persistentAuditEvent.setData(auditEventConverter.convertDataToStrings(event.getData()));

                persistenceAuditEventRepository.save(persistentAuditEvent);
            }
        };
    }
}

@@ -1669,24 +1669,189 @@
 sitory() %7B%0A%0A
+            private static final String AUTHORIZATION_FAILURE = %22AUTHORIZATION_FAILURE%22;%0A%0A            private static final String ANONYMOUS_USER = %22anonymousUser%22;%0A%0A
             
@@ -2836,24 +2836,176 @@
 nt event) %7B%0A
+                if(!AUTHORIZATION_FAILURE.equals(event.getType()) &&%0A                    !ANONYMOUS_USER.equals(event.getPrincipal().toString())) %7B%0A    
             
@@ -3088,32 +3088,36 @@
                 
+   
+ 
 persistentAuditE
@@ -3153,24 +3153,28 @@
 incipal());%0A
+    
             
@@ -3242,32 +3242,36 @@
                 
+ 
+   
 persistentAuditE
@@ -3343,32 +3343,36 @@
                 
+   
+ 
 persistentAuditE
@@ -3453,32 +3453,36 @@
                 
+    
 persistenceAudit
@@ -3525,16 +3525,34 @@
 Event);%0A
+                %7D%0A
         

remove audit event saved on database for anonymous_user and Authorization_Failure as nothing is relevant