Commit 6d12d82d authored by Matija Obreza's avatar Matija Obreza

Use entity listener for @CreatedBy, @CreatedDate, ...

parent f069c91b
...@@ -342,6 +342,16 @@ ...@@ -342,6 +342,16 @@
<artifactId>spring-security-oauth2</artifactId> <artifactId>spring-security-oauth2</artifactId>
<version>${spring.security.oauth2.version}</version> <version>${spring.security.oauth2.version}</version>
</dependency> </dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -18,17 +18,37 @@ package org.genesys2.server.model; ...@@ -18,17 +18,37 @@ package org.genesys2.server.model;
import java.util.Date; import java.util.Date;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass; import javax.persistence.MappedSuperclass;
import org.genesys2.server.model.impl.User; import org.genesys2.server.model.impl.User;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
@MappedSuperclass @MappedSuperclass
public abstract class AuditedModel extends OwnedModel { public abstract class AuditedModel extends BusinessModel {
private static final long serialVersionUID = -5927214471682331909L; private static final long serialVersionUID = -5927214471682331909L;
@CreatedBy
@ManyToOne(optional = true)
@JoinColumn(name = "createdBy")
private User createdBy; private User createdBy;
@CreatedDate
@Column(name = "createdDate")
private Date createdDate; private Date createdDate;
@LastModifiedBy
@ManyToOne(optional = true)
@JoinColumn(name = "lastModifiedBy")
private User lastModifiedBy; private User lastModifiedBy;
@LastModifiedDate
@Column(name = "lastModifiedDate")
private Date lastModifiedDate; private Date lastModifiedDate;
public User getCreatedBy() { public User getCreatedBy() {
......
...@@ -29,7 +29,7 @@ import javax.persistence.Table; ...@@ -29,7 +29,7 @@ import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.BusinessModel; import org.genesys2.server.model.AuditedModel;
import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store; import org.hibernate.search.annotations.Store;
...@@ -40,7 +40,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -40,7 +40,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@Entity @Entity
@Table(name = "crop") @Table(name = "crop")
@Indexed @Indexed
public class Crop extends BusinessModel { public class Crop extends AuditedModel {
private static final long serialVersionUID = -2686341831839109257L; private static final long serialVersionUID = -2686341831839109257L;
/** /**
......
...@@ -14,26 +14,31 @@ ...@@ -14,26 +14,31 @@
* limitations under the License. * limitations under the License.
**/ **/
package org.genesys2.server.model; package org.genesys2.server.service.audit;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.impl.User; import org.genesys2.server.model.impl.User;
import org.genesys2.server.security.AuthUserDetails;
@MappedSuperclass import org.springframework.data.domain.AuditorAware;
public abstract class OwnedModel extends BusinessModel { import org.springframework.security.core.Authentication;
private static final long serialVersionUID = -6176890712426050571L; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
@ManyToOne(cascade = {}, optional = false)
private User owner; @Component("auditorAware")
public class SpringSecurityAuditorAware implements AuditorAware<User> {
public User getOwner() { public static final Log LOG = LogFactory.getLog(SpringSecurityAuditorAware.class);
return owner;
@Override
public User getCurrentAuditor() {
LOG.info("Getting current auditor..");
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
LOG.info("Got " + authentication);
if (authentication == null || !authentication.isAuthenticated()) {
LOG.info("Not authenticated");
return null;
}
return ((AuthUserDetails) authentication.getPrincipal()).getUser();
} }
}
public void setOwner(User owner) { \ No newline at end of file
this.owner = owner;
}
}
<!--
Copyright 2013 Global Crop Diversity Trust
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.
-->
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" version="2.0">
<persistence-unit-metadata>
<persistence-unit-defaults>
<entity-listeners>
<entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />
</entity-listeners>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
\ No newline at end of file
...@@ -43,7 +43,6 @@ ...@@ -43,7 +43,6 @@
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="dataSource" ref="dataSource" /> <property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="spring-jpa" /> <property name="persistenceUnitName" value="spring-jpa" />
<property name="jpaVendorAdapter"> <property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="${db.showSql}" /> <property name="showSql" value="${db.showSql}" />
...@@ -65,6 +64,8 @@ ...@@ -65,6 +64,8 @@
</array> </array>
</property> </property>
</bean> </bean>
<jpa:auditing auditor-aware-ref="auditorAware" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource" /> <property name="dataSource" ref="dataSource" />
...@@ -77,5 +78,4 @@ ...@@ -77,5 +78,4 @@
<jpa:repositories base-package="org.genesys2.server.persistence.acl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager" repository-impl-postfix="CustomImpl" /> <jpa:repositories base-package="org.genesys2.server.persistence.acl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager" repository-impl-postfix="CustomImpl" />
</beans> </beans>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment