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

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

parent f069c91b
......@@ -342,6 +342,16 @@
<artifactId>spring-security-oauth2</artifactId>
<version>${spring.security.oauth2.version}</version>
</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>
<build>
......
......@@ -18,17 +18,37 @@ package org.genesys2.server.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
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
public abstract class AuditedModel extends OwnedModel {
public abstract class AuditedModel extends BusinessModel {
private static final long serialVersionUID = -5927214471682331909L;
@CreatedBy
@ManyToOne(optional = true)
@JoinColumn(name = "createdBy")
private User createdBy;
@CreatedDate
@Column(name = "createdDate")
private Date createdDate;
@LastModifiedBy
@ManyToOne(optional = true)
@JoinColumn(name = "lastModifiedBy")
private User lastModifiedBy;
@LastModifiedDate
@Column(name = "lastModifiedDate")
private Date lastModifiedDate;
public User getCreatedBy() {
......
......@@ -29,7 +29,7 @@ import javax.persistence.Table;
import javax.persistence.Transient;
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.Indexed;
import org.hibernate.search.annotations.Store;
......@@ -40,7 +40,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@Entity
@Table(name = "crop")
@Indexed
public class Crop extends BusinessModel {
public class Crop extends AuditedModel {
private static final long serialVersionUID = -2686341831839109257L;
/**
......
......@@ -14,26 +14,31 @@
* limitations under the License.
**/
package org.genesys2.server.model;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
package org.genesys2.server.service.audit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.impl.User;
@MappedSuperclass
public abstract class OwnedModel extends BusinessModel {
private static final long serialVersionUID = -6176890712426050571L;
@ManyToOne(cascade = {}, optional = false)
private User owner;
public User getOwner() {
return owner;
import org.genesys2.server.security.AuthUserDetails;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
@Component("auditorAware")
public class SpringSecurityAuditorAware implements AuditorAware<User> {
public static final Log LOG = LogFactory.getLog(SpringSecurityAuditorAware.class);
@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;
}
public void setOwner(User owner) {
this.owner = owner;
return ((AuthUserDetails) authentication.getPrincipal()).getUser();
}
}
\ No newline at end of file
<!--
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 @@
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="spring-jpa" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="${db.showSql}" />
......@@ -66,6 +65,8 @@
</property>
</bean>
<jpa:auditing auditor-aware-ref="auditorAware" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="entityManagerFactory" ref="entityManagerFactory" />
......@@ -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" />
</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