Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added org.ektrop.audit module #209

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions org.ektorp.audit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
133 changes: 133 additions & 0 deletions org.ektorp.audit/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.ektorp</groupId>
<artifactId>org.ektorp.audit</artifactId>
<packaging>jar</packaging>
<name>Ektorp Audit</name>
<version>1.4.2</version>
<description>Provides Automatic Auditing support to Ektorp</description>
<parent>
<groupId>org.ektorp</groupId>
<artifactId>org.ektorp.parent</artifactId>
<version>1.4.2</version>
</parent>
<developers>
<developer>
<id>eric.benzacar</id>
<name>Eric Benzacar</name>
<email>ebenzacar@gmail.com</email>
<url>https://github.com/benze/Ektorp.git</url>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
<timezone>-5</timezone>
</developer>
</developers>

<dependencies>
<dependency>
<groupId>org.ektorp</groupId>
<artifactId>org.ektorp</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
<!-- Hamcrest -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<weaveDependencies>
<weaveDependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</weaveDependency>
<weaveDependency>
<groupId>org.ektorp</groupId>
<artifactId>org.ektorp</artifactId>
</weaveDependency>
</weaveDependencies>
<Xlint>ignore</Xlint>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>

</build>
</project>
63 changes: 63 additions & 0 deletions org.ektorp.audit/src/main/java/org/ektorp/audit/AuditAspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.ektorp.audit;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Aspect
public class AuditAspect {

// SLF4J logger
private static final Logger logger = LoggerFactory.getLogger(AuditAspect.class);

/**
* The handler to be used during the auditing
*/
private AuditingHandler handler;

/**
* Sets the auditing handler
*/
public void setAuditingHandler( AuditingHandler handler){
this.handler = handler;
}

// Pointcut to the create method
@Pointcut("(execution(* org.ektorp.CouchDbConnector+.create(.., Object)) && args(o) ) || ( execution(* org.ektorp.CouchDbConnector+.create(Object)) && args(o))")
public void create(AuditableBean o) {}

// Pointcut to the update method
@Pointcut("execution(* org.ektorp.CouchDbConnector+.update(Object)) && args(o)")
public void update(AuditableBean o){}

/**
* Sets modification and creation date and auditor on the target object on update events.
*
* @param target
*/
@Before("create(target)")
public void beforeCreate(AuditableBean target){
// get handler
if (handler != null) {
handler.markCreated(target);
}
}

/**
* Sets creation date and auditor on the target object on create events.
*
* @param target
*/
@Before("update(target)")
public void beforeUpdate(AuditableBean target){
// get handler
if (handler != null) {
handler.markModified(target);
}
}


}
71 changes: 71 additions & 0 deletions org.ektorp.audit/src/main/java/org/ektorp/audit/AuditableBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.ektorp.audit;

import org.joda.time.DateTime;


public interface AuditableBean {

/**
* Returns the unique identifier of the user who caused the creation of this
* record. This column is not a foreign key to the User table.
*
* @return The unique identifier of the user who created this record.
*/
public Object getCreatedBy();

/**
* Sets the unique identifier of the user who caused the creation of this
* record.
*
* @param createdBy The unique identifier of the user who created this
* record.
*/
public void setCreatedBy(final Object createdBy);

/**
* Returns the date and time of when this record was created.
*
* @return The date and time this record was created.
*/
public DateTime getCreatedDate();

/**
* Sets the date and time of when this record was created.
*
* @param createdOn The date and time this record was created.
*/
public void setCreatedDate(final DateTime creationDate);

/**
* Returns the unique identifier of the user who most recently caused the
* modification of this record. This column is not a foreign key to the User
* table.
*
* @return The unique identifier of the user who last modified this record.
*/
public Object getLastModifiedBy();

/**
* Sets the unique identifier of the user who most recently caused the
* modification of this record.
*
* @param lastModifiedBy The unique identifier of the user who last modified
* this record.
*/
public void setLastModifiedBy(final Object lastModifiedBy);

/**
* Returns the date and time of when this record was last modified.
*
* @return The date and time this record was last modified.
*/
public DateTime getLastModifiedDate();

/**
* Sets the date and time of when this record was last modified.
*
* @param lastModifiedOn The date and time this record was last modified.
*/
public void setLastModifiedDate(final DateTime lastModifiedOn);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.ektorp.audit;

import javax.validation.constraints.NotNull;

import org.joda.time.DateTime;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Implementation of the AuditableBean interface
*/
public class AuditableBeanImpl implements AuditableBean{

@NotNull
@JsonProperty("createdDate")
private DateTime createdDate;

@NotNull
@JsonProperty("lastModifiedDate")
private DateTime lastModifiedDate;

@JsonProperty("createdBy")
private Object createdBy;

@JsonProperty("lastModifiedBy")
private Object lastModifiedBy;

@Override
public DateTime getCreatedDate(){
return this.createdDate;
}

@Override
public DateTime getLastModifiedDate(){
return this.lastModifiedDate;
}

@Override
public void setCreatedDate(final DateTime createdDate){
this.createdDate = createdDate;
}

@Override
public void setLastModifiedDate(final DateTime lastModifiedDate){
this.lastModifiedDate = lastModifiedDate;
}

@Override
public Object getLastModifiedBy(){
return this.lastModifiedBy;
}

@Override
public Object getCreatedBy(){
return this.createdBy;
}

@Override
public void setCreatedBy(final Object createdBy ){
this.createdBy = createdBy;
}

@Override
public void setLastModifiedBy(final Object modifiedBy){
this.lastModifiedBy = modifiedBy;
}
}
Loading