01: package com.technoetic.xplanner.domain;
02:
03: import java.util.*;
04: import java.beans.BeanInfo;
05: import java.beans.Introspector;
06: import java.beans.IntrospectionException;
07: import java.beans.PropertyDescriptor;
08:
09: public abstract class DomainObject implements Identifiable, Nameable {
10: private Date lastUpdateTime;
11: private int id;
12: private Map attributes;
13:
14: public Date getLastUpdateTime() {
15: return lastUpdateTime;
16: }
17:
18: public void setLastUpdateTime(Date lastUpdateTime) {
19: this .lastUpdateTime = lastUpdateTime;
20: }
21:
22: public int getId() {
23: return id;
24: }
25:
26: public void setId(int id) {
27: this .id = id;
28: }
29:
30: public boolean equals(Object o) {
31: if (this == o)
32: return true;
33: if (!(o instanceof DomainObject))
34: return false;
35:
36: final DomainObject object = (DomainObject) o;
37:
38: if (id == 0)
39: return this == o;
40: if (id != object.id)
41: return false;
42:
43: return true;
44: }
45:
46: public int hashCode() {
47: return id;
48: }
49:
50: public String getAttribute(String name) {
51: if (attributes != null) {
52: return (String) attributes.get(name);
53: } else {
54: attributes = getAttributes();
55: if (attributes != null) {
56: return (String) attributes.get(name);
57: } else {
58: return null;
59: }
60: }
61: }
62:
63: public Map getAttributes() {
64: return attributes != null ? Collections
65: .unmodifiableMap(attributes) : null;
66: }
67:
68: protected void setAttributes(Map attributes) {
69: this .attributes = attributes;
70: }
71:
72: protected static String getValidProperty(Class beanClass,
73: String property) {
74: BeanInfo beanInfo;
75: try {
76: beanInfo = Introspector.getBeanInfo(beanClass);
77: } catch (IntrospectionException e) {
78: throw new RuntimeException("could not introspect "
79: + beanClass, e);
80: }
81: PropertyDescriptor[] properties = beanInfo
82: .getPropertyDescriptors();
83: boolean found = false;
84: for (int i = 0; i < properties.length; i++) {
85: if (properties[i].getName().equals(property)) {
86: found = true;
87: break;
88: }
89: }
90: if (!found) {
91: throw new RuntimeException("Could not find property "
92: + property + " in " + beanClass);
93: }
94:
95: return property;
96:
97: }
98: }
|