AbstractEntity.java

  1. /*
  2.  * This software was designed and created by Jason Carroll.
  3.  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
  4.  * The author can be reached at jcarroll@cowsultants.com
  5.  * ITracker website: http://www.cowsultants.com
  6.  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it only under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  */

  18. package org.itracker.model;

  19. import org.apache.commons.lang.builder.CompareToBuilder;
  20. import org.apache.commons.lang.builder.EqualsBuilder;
  21. import org.apache.commons.lang.builder.HashCodeBuilder;

  22. import java.io.Serializable;
  23. import java.util.Comparator;
  24. import java.util.Date;

  25. /**
  26.  * This is a POJO Business Domain Object. Hibernate Bean.
  27.  * <p/>
  28.  * <p>
  29.  * All entities are Java Beans and should inherit this class to make sure they
  30.  * are Serializable and Cloneable and have the following fields : an id, a
  31.  * creation date and a last modifiation date.
  32.  * </p>
  33.  *
  34.  * @author ready
  35.  */
  36. public abstract class AbstractEntity implements Entity {

  37.     /**
  38.      *
  39.      */
  40.     private static final long serialVersionUID = 1L;

  41.     public static final Comparator<Entity> ID_COMPARATOR = new IdComparator();

  42.     public static final Comparator<AbstractEntity> CREATE_DATE_COMPARATOR = new CreateDateComparator();

  43.     public static final Comparator<AbstractEntity> LAST_MODIFIED_DATE_COMPARATOR = new LastModifiedDateComparator();

  44.     /**
  45.      * System ID
  46.      */
  47.     private Integer id;

  48.     /**
  49.      * Creation date and time.
  50.      */
  51.     private Date createDate = new Date();

  52.     /**
  53.      * Last modification date and time.
  54.      */
  55.     private Date lastModifiedDate = new Date();

  56.     /**
  57.      * Default constructor (required by Hibernate).
  58.      */
  59.     public AbstractEntity() {
  60.     }

  61.     public Integer getId() {
  62.         return id;
  63.     }

  64.     public void setId(Integer id) {
  65.         this.id = id;
  66.     }

  67.     /**
  68.      * @return creation time stamp or <tt>null</tt> for transient entities
  69.      */
  70.     public Date getCreateDate() {
  71.         if (null == createDate)
  72.             createDate = new Date();
  73.         return new Date(createDate.getTime());
  74.     }

  75.     /**
  76.      * Sets the creation date and time.
  77.      * <p/>
  78.      * <p>
  79.      * The persistence framework automatically sets this property when a new
  80.      * entity is persisted. <br>
  81.      * Note that the value is managed by the persistence framework and may be
  82.      * generated by the database in the future.
  83.      * </p>
  84.      * <p/>
  85.      * <p>
  86.      * The creation time stamp should never change once initialized.
  87.      * </p>
  88.      *
  89.      * @param dateTime creation time stamp
  90.      */
  91.     public void setCreateDate(Date dateTime) {
  92.         if (null == dateTime)
  93.             return;
  94.         this.createDate = new Date(dateTime.getTime());
  95.     }

  96.     /**
  97.      * @return last modification time stamp or <tt>null</tt> for transient
  98.      *         entities
  99.      */
  100.     public Date getLastModifiedDate() {
  101.         if (null == this.lastModifiedDate)
  102.             this.lastModifiedDate = new Date();
  103.         return new Date(lastModifiedDate.getTime());
  104.     }

  105.     /**
  106.      * Sets the last modification date and time.
  107.      * <p/>
  108.      * <p>
  109.      * The persistence framework automatically sets this property to the same
  110.      * value as the createDate property when persisting a new entity and
  111.      * automatically updates it when saving an existing one. <br>
  112.      * Note that the value is managed by the persistence framework and may be
  113.      * generated by the database in the future.
  114.      * </p>
  115.      *
  116.      * @param dateTime last modification time stamp
  117.      */
  118.     public void setLastModifiedDate(Date dateTime) {
  119.         if (null == dateTime)
  120.             return;
  121.         this.lastModifiedDate = new Date(dateTime.getTime());
  122.     }

  123.     /**
  124.      * Returns whether this instance represents a new transient instance.
  125.      *
  126.      * @return <tt>true</tt> if <code>id</code> is <tt>null</tt>
  127.      */
  128.     public boolean isNew() {
  129.         return (this.getId() == null);
  130.     }

  131.     @Override
  132.     public Object clone() throws CloneNotSupportedException {
  133.         return super.clone();
  134.     }

  135.     /**
  136.      * Compares 2 instances by ID.
  137.      */
  138.     protected static class IdComparator implements Comparator<Entity>, Serializable {

  139.         /**
  140.          *
  141.          */
  142.         private static final long serialVersionUID = 1L;

  143.         public int compare(Entity a, Entity b) {
  144.             return new CompareToBuilder().append(a.getId(), b.getId())
  145.                     .toComparison();
  146.         }

  147.     }

  148.     protected static class CreateDateComparator implements
  149.             Comparator<AbstractEntity>, Serializable {

  150.         /**
  151.          *
  152.          */
  153.         private static final long serialVersionUID = 1L;

  154.         public int compare(AbstractEntity a, AbstractEntity b) {
  155.             return new CompareToBuilder().append(a.getCreateDate(),
  156.                     b.getCreateDate()).toComparison();
  157.         }

  158.     }

  159.     /**
  160.      * Compares 2 instances by last modified date.
  161.      */
  162.     protected static class LastModifiedDateComparator implements
  163.             Comparator<AbstractEntity>, Serializable {

  164.         /**
  165.          *
  166.          */
  167.         private static final long serialVersionUID = 1L;

  168.         public int compare(AbstractEntity a, AbstractEntity b) {
  169.             return new CompareToBuilder().append(a.getLastModifiedDate(),
  170.                     b.getLastModifiedDate()).toComparison();
  171.         }

  172.     }

  173.     @Override
  174.     public final boolean equals(Object obj) {

  175.         if (this == obj) {
  176.             return true;
  177.         }
  178.         if (isNew() || null == obj) {
  179.             return false;
  180.         }

  181.         if (getClass().equals(obj.getClass())) {
  182.             Entity o = (Entity) obj;
  183.             return new EqualsBuilder()
  184.                     .append(getId(), o.getId()).isEquals();

  185.         }

  186.         return false;

  187.     }

  188.     public final int compareTo(Entity o) {
  189.         if (this.equals(o)) {
  190.             return 0;
  191.         }
  192.         return new CompareToBuilder().append(getClass(), o.getClass(), AbstractEntity.CLASS_COMPARATOR).append(
  193.                 getId(), o.getId()).toComparison();
  194.     }

  195.     @Override
  196.     public final int hashCode() {
  197.         return new HashCodeBuilder().append(getClass()).append(getId()).toHashCode();

  198.     }

  199.     private static final Comparator<Class<?>> CLASS_COMPARATOR = new Comparator<Class<?>>() {
  200.         public int compare(Class<?> o1, Class<?> o2) {
  201.             return new CompareToBuilder().append(o1.getSimpleName(), o2.getSimpleName()).append(o1.hashCode(), hashCode()).toComparison();
  202.         }
  203.     };

  204. }