View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.common.model.bo;
17  
18  import java.beans.PropertyDescriptor;
19  import java.util.Collection;
20  import java.util.Map;
21  
22  import net.sf.oness.common.model.auditing.AbstractAuditableObject;
23  import net.sf.oness.common.model.temporal.DateRange;
24  import net.sf.oness.common.model.util.CollectionUtils;
25  
26  import org.apache.commons.beanutils.PropertyUtils;
27  
28  /***
29   * Basic business object. Some superclass methods are overriden only to set
30   * xdoclet attributes.
31   * 
32   * @author Carlos Sanchez
33   * @version $Revision: 1.6 $
34   */
35  public abstract class AbstractBusinessObject extends AbstractAuditableObject
36          implements AuditableBusinessObject {
37  
38      private String comments;
39  
40      /***
41       * @hibernate.id generator-class="increment"
42       * 
43       * @return
44       */
45      public Long getId() {
46          return super.getId();
47      }
48  
49      /***
50       * @hibernate.property not-null="false"
51       * 
52       * @return Returns the code.
53       */
54      public Long getCode() {
55          return super.getCode();
56      }
57  
58      /***
59       * @hibernate.property type="net.sf.oness.common.model.temporal.hibernate.DateRangeType"
60       * @hibernate.column name="ttstart" not-null="true"
61       * @hibernate.column name="ttend"
62       * 
63       * @see net.sf.oness.common.model.auditing.Auditable#getTransactionTime()
64       */
65      public DateRange getTransactionTime() {
66          return super.getTransactionTime();
67      }
68  
69      /***
70       * @hibernate.property not-null="false"
71       * 
72       * @see net.sf.oness.common.model.auditing.Auditable#getCreatedBy()
73       */
74      public String getCreatedBy() {
75          return super.getCreatedBy();
76      }
77  
78      /***
79       * @hibernate.property not-null="false"
80       * 
81       * @see net.sf.oness.common.model.auditing.Auditable#getDeletedBy()
82       */
83      public String getDeletedBy() {
84          return super.getDeletedBy();
85      }
86  
87      /***
88       * Comments for this object
89       * 
90       * @hibernate.property not-null="false"
91       * 
92       * @return The comments
93       */
94      public String getComments() {
95          return comments;
96      }
97  
98      public void setComments(String comments) {
99          this.comments = comments;
100     }
101 
102     /***
103      * @see java.lang.Object#equals(java.lang.Object)
104      */
105     public boolean equals(Object o) {
106         return CollectionIgnoringEqualsBuilder.reflectionEquals(this, o);
107     }
108 
109     /***
110      * @see java.lang.Object#hashCode()
111      */
112     public int hashCode() {
113         return CollectionIgnoringHashCodeBuilder.reflectionHashCode(this);
114     }
115 
116     /***
117      * @see java.lang.Object#toString()
118      */
119     public String toString() {
120         return new CollectionIgnoringReflectionToStringBuilder(this).toString();
121     }
122 
123     /***
124      * This implementation calls super.clone and then clones collections without
125      * cloning its elements. Returns <code>null</code> on any exception.
126      * 
127      * @see java.lang.Object#clone()
128      */
129     public Object clone() {
130         try {
131             Object o = super.clone();
132             PropertyDescriptor origDescriptors[] = PropertyUtils
133                     .getPropertyDescriptors(this);
134             for (int i = 0; i < origDescriptors.length; i++) {
135                 Class clazz = origDescriptors[i].getPropertyType();
136                 if (CollectionUtils.isCollection(clazz)) {
137                     /* if it's a Collection */
138                     String name = origDescriptors[i].getName();
139                     Collection old = (Collection) PropertyUtils
140                             .getSimpleProperty(this, name);
141                     PropertyUtils.setSimpleProperty(o, name, CollectionUtils
142                             .clone(old));
143                 }
144                 if (CollectionUtils.isMap(clazz)) {
145                     /* if it's a Collection */
146                     String name = origDescriptors[i].getName();
147                     Map old = (Map) PropertyUtils.getSimpleProperty(this, name);
148                     PropertyUtils.setSimpleProperty(o, name, CollectionUtils
149                             .clone(old));
150                 }
151             }
152             return o;
153         } catch (Exception e) {
154             log.error("Exception cloning bean: " + this + "\n"
155                     + "Exception was " + e + ": " + e.getLocalizedMessage());
156             return null;
157         }
158 
159     }
160 }