<commit_before>package edu.columbia.gemma.interceptor;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Date;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.columbia.gemma.common.auditAndSecurity.AuditAction;
import edu.columbia.gemma.common.auditAndSecurity.AuditEvent;
import edu.columbia.gemma.common.auditAndSecurity.AuditTrail;
import edu.columbia.gemma.common.auditAndSecurity.AuditTrailDao;
import edu.columbia.gemma.security.interceptor.PersistAclInterceptorBackend;

/**
 * Add to the audit trail on create, delete, or update of domain objects.
 * <hr>
 * <p>
 * Copyright (c) 2004 - 2005 Columbia University
 * 
 * @author keshav
 * @version $Id$
 */
public class AuditTrailInterceptor implements MethodInterceptor {
    private static Log log = LogFactory.getLog( PersistAclInterceptorBackend.class.getName() );

    private AuditTrailDao auditTrailDao = null;

    /**
     * @return Returns the auditTrailDao.
     */
    public AuditTrailDao getAuditTrailDao() {
        return auditTrailDao;
    }

    /**
     * @param auditTrailDao The auditTrailDao to set.
     */
    public void setAuditTrailDao( AuditTrailDao auditTrailDao ) {
        this.auditTrailDao = auditTrailDao;
    }

    /**
     * 'Around advice' method.
     * 
     * @param invocation
     * @return Object
     * @throws Throwable
     * TODO finish implementation
     */
    public Object invoke( MethodInvocation invocation ) throws Throwable {

        Method m = invocation.getMethod();

        log.debug( "Before: method=[" + m + "]" );

        log.debug( "arguments " + invocation.getArguments() );

        Object retVal = invocation.proceed();

        log.debug( "After" );

        Collection<AuditTrail> col = auditTrailDao.findAllAuditTrails();

        for ( AuditTrail at : col ) {

        }

        AuditTrail auditTrail = AuditTrail.Factory.newInstance();

        AuditEvent auditEvent = AuditEvent.Factory.newInstance();

        auditEvent.setDate( new Date() );

        if ( m.toString().startsWith( "save" ) )
            auditEvent.setAction( AuditAction.CREATE );

        else if ( m.toString().startsWith( "update" ) )
            auditEvent.setAction( AuditAction.DELETE );

        else if ( m.toString().startsWith( "remove" ) ) auditEvent.setAction( AuditAction.DELETE );

        // Object[] methodArgs = invocation.getArguments();
        // Object obj = methodArgs[0];
        //
        // Method method = obj.getClass().getMethod( "setAuditTrail", new Class[] { auditTrail.getClass() } );

        return retVal;
    }

}<commit_msg>auditTrailInterceptor functionality has been added.<commit_after>package edu.columbia.gemma.interceptor;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Date;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.columbia.gemma.common.auditAndSecurity.AuditAction;
import edu.columbia.gemma.common.auditAndSecurity.AuditEvent;
import edu.columbia.gemma.common.auditAndSecurity.AuditTrail;
import edu.columbia.gemma.common.auditAndSecurity.AuditTrailDao;
import edu.columbia.gemma.security.interceptor.PersistAclInterceptorBackend;

/**
 * Add to the audit trail on create, delete, or update of domain objects.
 * <hr>
 * <p>
 * Copyright (c) 2004 - 2005 Columbia University
 * 
 * @author keshav
 * @version $Id$
 * @spring.bean id="auditTrailInterceptor"
 * @spring.property name="auditTrailDao" ref="auditTrailDao"
 */
public class AuditTrailInterceptor implements MethodInterceptor {
    private static Log log = LogFactory.getLog( PersistAclInterceptorBackend.class.getName() );

    private AuditTrailDao auditTrailDao = null;

    /**
     * @return Returns the auditTrailDao.
     */
    public AuditTrailDao getAuditTrailDao() {
        return auditTrailDao;
    }

    /**
     * @param auditTrailDao The auditTrailDao to set.
     */
    public void setAuditTrailDao( AuditTrailDao auditTrailDao ) {
        this.auditTrailDao = auditTrailDao;
    }

    /**
     * 'Around advice' method.
     * 
     * @param invocation
     * @return Object
     * @throws Throwable
     */
    public Object invoke( MethodInvocation invocation ) throws Throwable {

        Method method = invocation.getMethod();
        String mname = method.getName();

        log.debug( "before invocation.proceed(): method=[" + mname + "]" );

        Object retVal = invocation.proceed();

        log.debug( "after invocation.proceed(): retVal= " + retVal );

        Object[] arguments = invocation.getArguments();
        Object argument = arguments[0];

        Method mutator = argument.getClass().getMethod( "getAuditTrail" );

        assert mutator != null : mutator + " does not exist";

        AuditTrail auditTrail = ( AuditTrail ) mutator.invoke( argument, new Object[] {} );

        // Check if an auditTrail has been started for this object.
        if ( auditTrail == null ) {
            auditTrail = AuditTrail.Factory.newInstance();
            auditTrail.start();

        } else {

            // TODO get hook to the performer.
            Collection<AuditEvent> auditEvents = auditTrail.getEvents();
            AuditEvent auditEvent = AuditEvent.Factory.newInstance();
            auditEvent.setDate( new Date() );
            // auditEvent.setNote();
            // auditEvent.setPerformer();

            if ( mname.startsWith( "save" ) )
                auditEvent.setAction( AuditAction.CREATE );
            else if ( mname.toString().startsWith( "update" ) ) auditEvent.setAction( AuditAction.UPDATE );
            // else if ( mname.toString().startsWith( "remove" ) ) auditEvent.setAction( AuditAction.DELETE );

            auditEvents.add( auditEvent );

            auditTrail.setEvents( auditEvents );
        }

        Method accessor = null;
        Method[] methods = argument.getClass().getMethods();

        for ( int i = 0; i < methods.length; i++ ) {
            if ( methods[i].getName().equals( "setAuditTrail" ) ) {
                accessor = methods[i];
                break;
            }
        }

        assert accessor.getName().equals( "setAuditTrail" ) : argument.getClass() + "does not contain setAuditTrail";
        accessor.invoke( argument, new Object[] { auditTrail } );

        return retVal;
    }
}
{'fraction_non_alphanumeric': 0.11138124905029631, 'fraction_numerical': 0.002887099225041787, 'mean_word_length': 2.8717647058823528, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '678800', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1977, 'n_words': 415}