2 Replies Latest reply on Jul 15, 2009 4:50 AM by Muhammed Patel

    @RevisionEntity

    Muhammed Patel Newbie

      Hi there,


      I am having a problem trying to get my tables to include a field called logged in.
      Basically i have envers auditing now and i am trying to use a RevisionListener to add more detail to my logging. such as the user that made the modification.

      I have added all the fields that are needed such as ID and timestamp but i would like to add an extra on that is logged in(see bottom of code)

      I am using the update
      propety on my machine and i have the following code below.

      package za.co.aforbes.fpc.db.model.user;
      
      import java.util.Collection;
      import java.util.Date;
      
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.OneToMany;
      import javax.persistence.Table;
      import javax.persistence.Transient;
      
      import org.hibernate.annotations.Cascade;
      import org.hibernate.annotations.GenericGenerator;
      import org.hibernate.annotations.LazyCollection;
      import org.hibernate.annotations.LazyCollectionOption;
      import org.hibernate.envers.Audited;
      import org.hibernate.envers.RevisionEntity;
      import org.hibernate.envers.RevisionNumber;
      import org.hibernate.envers.RevisionTimestamp;
      
      import za.co.aforbes.fpc.db.model.BaseLookupModel;
      import za.co.aforbes.fpc.db.model.ListenerClass;
      import za.co.aforbes.fpc.db.model.SelectableModel;
      import za.co.aforbes.fpc.db.model.util.UUIDGenerator;
      
      @Entity
      @Table(name = "SystemUser")
      @RevisionEntity(ListenerClass.class)
      public final class User extends BaseLookupModel<String> implements SelectableModel {
      
       /**
       * {@link Enum} representation of common accounts.
       *
       * @author Justin Rundle
       */
       public static enum Common {
       SystemAccount("00000000-0000-0000-0000-000000000000"), IndividualAdviceCentre(
       "00000000-0000-0000-0000-000000000003");
      
       private String primaryId = null;
      
       private Common(String primaryId) {
       this.primaryId = primaryId;
       }
      
       public String getPrimaryId() {
       return primaryId;
       }
      
       } // end Common
      
       @Id
       @GeneratedValue(generator = "UUIDGenerator")
       @GenericGenerator(name = "UUIDGenerator", strategy = UUIDGenerator.STRATEGY)
       @Column(name = "SystemUserGUID", updatable = false)
       @Audited
       private String primaryId;
      
       @Column(name = "ActiveDirectoryGUID")
       private String activeDirectoryId;
      
       @Column(name = "FirstName")
       @Audited
       private String firstName;
      
       @Column(name = "LastName")
       @Audited
       private String lastName;
      
       @Column(name = "InternalEMailAddress")
       @Audited
       private String internalEMailAddress;
      
       @Column(name = "DomainName")
       @Audited
       private String domainName;
      
       @Column(name = "EmployeeNo")
       private String employeeNo;
      
       @ManyToOne
       @JoinColumn(name = "Designation")
       @Audited
       private UserDesignation userDesignation;
      
       @ManyToOne
       @JoinColumn(name = "Department")
       @Audited
       private Department department;
      
       @ManyToOne
       @JoinColumn(name = "Remunerationtype")
       @Audited
       private RemunerationType remunerationType;
      
       @Column(name = "Available")
       @Audited
       private boolean available;
      
       @Column(name = "Enabled")
       @Audited
       private boolean enabled;
      
       @Column(name = "Revised")
       private boolean revised;
      
       @Column(name = "CreatedBy")
       private String createdById;
      
       @Column(name = "DateCreated")
       private Date dateCreated;
      
       @Column(name = "ModifiedBy")
       private String modifiedById;
      
       @Column(name = "DateModified")
       private Date dateModified;
      
       @OneToMany(mappedBy = "user")
       @Cascade( {org.hibernate.annotations.CascadeType.SAVE_UPDATE,
       org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
       @LazyCollection(LazyCollectionOption.FALSE)
       private Collection<UserRole> userRoles;
      
       @Transient
       private boolean selected = false;
      
       /**
       * Constructs a new User object.
       */
       public User() {}
      
       /*
       * (non-Javadoc)
       * @see za.co.aforbes.fpc.db.model.SelectableModel#isSelected()
       */
       @Override
       public final boolean isSelected() {
       return selected;
       }
      
       /*
       * (non-Javadoc)
       * @see za.co.aforbes.fpc.db.model.SelectableModel#setSelected(boolean)
       */
       @Override
       public final void setSelected(boolean selected) {
       this.selected = selected;
       }
      
       /**
       * Returns a {@link Boolean} representation of whether the user is the specific role.
       *
       * @param role the role to check
       * @return true if the user is the specifid role
       */
       public final boolean isRole(Role.Type role) {
       if(role == null) {
       return false;
       }
      
       for(UserRole userRole : getUserRoles()) {
       if(userRole.getRole().isRole(Role.Type.SystemAdministrator)) {
       return true;
       } else if(userRole.getRole().isRole(role)) {
       return true;
       }
       }
       return false;
       }
      
       /**
       * Returns true if this user is equal to the specified user.
       *
       * @param user to compare.
       * @return true if this user is equal.
       */
       public boolean isUser(Common user) {
       return this.getPrimaryId().equalsIgnoreCase(user.getPrimaryId());
       }
      
       /*
       * (non-Javadoc)
       * @see za.co.aforbes.fpc.db.model.BaseModel#getPrimaryId()
       */
       @Override
       public final String getPrimaryId() {
       return primaryId;
       }
      
       /*
       * (non-Javadoc)
       * @see za.co.aforbes.fpc.db.model.BaseModel#setPrimaryId(java.lang.Object)
       */
       @Override
       public final void setPrimaryId(String primaryId) {
       this.primaryId = primaryId;
       }
      
       @Override
       public String getDescription() {
       return getFirstName() + " " + getLastName();
       }
      
       @Override
       public void setDescription(String description) {
       // Do nothing.
       }
      
       public String getFirstName() {
       return firstName;
       }
      
       public void setFirstName(String firstName) {
       this.firstName = firstName;
       }
      
       public final String getLastName() {
       return lastName;
       }
      
       public final void setLastName(String lastName) {
       this.lastName = lastName;
       }
      
       public String getInternalEMailAddress() {
       return internalEMailAddress;
       }
      
       public void setInternalEMailAddress(String internalEMailAddress) {
       this.internalEMailAddress = internalEMailAddress;
       }
      
       public UserDesignation getUserDesignation() {
       if(userDesignation == null) {
       userDesignation = new UserDesignation();
       }
       return userDesignation;
       }
      
       public void setUserDesignation(UserDesignation userDesignation) {
       this.userDesignation = userDesignation;
       }
      
       public boolean isEnabled() {
       return enabled;
       }
      
       public void setEnabled(boolean enabled) {
       this.enabled = enabled;
       }
      
       public boolean isAvailable() {
       return available;
       }
      
       public void setAvailable(boolean available) {
       this.available = available;
       }
      
       public String getEmployeeNo() {
       return employeeNo;
       }
      
       public void setEmployeeNo(String employeeNo) {
       this.employeeNo = employeeNo;
       }
      
       public Department getDepartment() {
       if(department == null) {
       department = new Department();
       }
       return department;
       }
      
       public void setDepartment(Department department) {
       this.department = department;
       }
      
       public final Collection<UserRole> getUserRoles() {
       return userRoles;
       }
      
       public final void setUserRoles(Collection<UserRole> userRoles) {
       this.userRoles = userRoles;
       }
      
       public RemunerationType getRemunerationType() {
       if(remunerationType == null) {
       remunerationType = new RemunerationType();
       }
       return remunerationType;
       }
      
       public void setRemunerationType(RemunerationType remunerationType) {
       this.remunerationType = remunerationType;
       }
      
       public final boolean isRevised() {
       return revised;
       }
      
       public final void setRevised(boolean revised) {
       this.revised = revised;
       }
      
       public final String getActiveDirectoryId() {
       return activeDirectoryId;
       }
      
       public final void setActiveDirectoryId(String activeDirectoryId) {
       this.activeDirectoryId = activeDirectoryId;
       }
      
       public final String getDomainName() {
       return domainName;
       }
      
       public final void setDomainName(String domainName) {
       this.domainName = domainName;
       }
      
       public final String getCreatedById() {
       return createdById;
       }
      
       public final void setCreatedById(String createdById) {
       this.createdById = createdById;
       }
      
       public final Date getDateCreated() {
       return dateCreated;
       }
      
       public final void setDateCreated(Date dateCreated) {
       this.dateCreated = dateCreated;
       }
      
       public final String getModifiedById() {
       return modifiedById;
       }
      
       public final void setModifiedById(String modifiedById) {
       this.modifiedById = modifiedById;
       }
      
       public final Date getDateModified() {
       return dateModified;
       }
      
       public final void setDateModified(Date dateModified) {
       this.dateModified = dateModified;
       }
      
       @Id
       @GeneratedValue
       @RevisionNumber
       private int revisionId;
      
       @RevisionTimestamp
       private Long timeStamp = 0L;
      
      
       private String loggedIn;
      
       public int getRevisionId() {
       return revisionId;
       }
      
       public void setRevisionId(int revisionId) {
       this.revisionId = revisionId;
       }
      
       public Long getTimeStamp() {
       return timeStamp;
       }
      
       public void setTimeStamp(Long timeStamp) {
       this.timeStamp = timeStamp;
       }
      
       public String getLoggedIn() {
       return loggedIn;
       }
      
       public void setLoggedIn(String loggedIn) {
       this.loggedIn = loggedIn;
       }
      
      
      
      
      
      
      
      }
      


      here is my application context.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       ">
       <!-- START - IFA DATASOURCE AND SESSION FACTORY-->
       <!--
       Below are connection values to the database. These values are used in
       the hibernate.properties file and are populated at runtime
       -->
      
      
      
      <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       <property name="sessionFactory" ref="ifaSessionFactory"/>
       </bean>
      
      
       <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
       <property name="jndiName">
       <value>java:comp/env/jdbc/ifaDB</value>
       </property>
       </bean>
      
       <bean id="ifaSessionFactory"
       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      
       <property name="dataSource">
       <ref bean="dataSource" />
       </property>
      
       <property name="configLocation">
       <value>WEB-INF/hibernate/hibernate-ifa.cfg.xml</value>
       </property>
       <property name="configurationClass">
       <value>org.hibernate.cfg.AnnotationConfiguration</value>
       </property>
       <property name="hibernateProperties">
       <props>
       <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
       <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
       <prop key="hibernate.show_sql">true</prop>
      
       <prop key="hibernate.transaction.auto_close_session">true</prop>
      
       <prop key="hibernate.hbm2ddl.auto">update</prop>
      
      <!-- </prop>-->
       <prop key="org.hibernate.envers.auditTableSuffix">Log</prop>
       <prop key="hibernate.format_sql">true</prop>
       <prop key="hibernate.connection.release_mode">after_statement</prop>
       <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
       </props>
       </property>
       <property name="eventListeners">
       <map>
       <entry key="post-insert">
       <list>
       <ref bean="ejbInsert" />
       <ref bean="audit" />
       </list>
       </entry>
       <entry key="post-update">
       <list>
      
       <ref bean="ejbUpdate" />
       <ref bean="audit" />
       </list>
       </entry>
       <entry key="post-delete">
       <list>
       <ref bean="ejbDelete" />
       <ref bean="audit" />
       </list>
       </entry>
       <entry key="pre-collection-update">
       <ref bean="audit" />
       </entry>
       <entry key="pre-collection-remove">
       <ref bean="audit" />
       </entry>
       <entry key="post-collection-recreate">
       <ref bean="audit" />
       </entry>
       </map>
       </property>
      
       </bean>
      
      
       <!-- END - IFA DATASOURCE AND SESSION FACTORY-->
      
      
      
       <bean name="callBack" class="org.hibernate.ejb.event.EntityCallbackHandler" />
      
       <bean name="audit" class="org.hibernate.envers.event.AuditEventListener" />
       <bean name="ejbInsert" class="org.hibernate.ejb.event.EJB3PostInsertEventListener">
       <property name="callbackHandler">
       <ref bean="callBack" />
       </property>
       </bean>
       <bean name="ejbUpdate" class="org.hibernate.ejb.event.EJB3PostUpdateEventListener">
       <property name="callbackHandler">
       <ref bean="callBack" />
       </property>
       </bean>
      <bean name="ejbDelete" class="org.hibernate.ejb.event.EJB3PostDeleteEventListener" >
      <property name="callbackHandler">
       <ref bean="callBack" />
       </property>
       </bean>
      
      <bean id="hibernateDao"
       class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
       <property name="transactionManager" ref="transactionManager" />
       <property name="transactionAttributes">
       <props>
       <prop key="*">PROPAGATION_REQUIRED</prop>
       </props>
       </property>
       </bean>
      </beans
      


      I just need the REVINFO table to include my custom field which is the loggedInUser

      Thanks,
      Muhammed Patel