1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.framework.persistence.jpa; |
17 | |
|
18 | |
import org.apache.commons.lang.builder.HashCodeBuilder; |
19 | |
|
20 | |
import javax.persistence.Id; |
21 | |
import java.io.Serializable; |
22 | |
import java.lang.reflect.Field; |
23 | |
import java.lang.reflect.InvocationTargetException; |
24 | |
import java.lang.reflect.Method; |
25 | |
import java.util.Arrays; |
26 | |
import java.util.Comparator; |
27 | |
|
28 | 0 | public abstract class CompositePrimaryKeyBase implements Serializable { |
29 | |
|
30 | |
public boolean equals(Object o) { |
31 | 0 | Class thisClass = (this.getClass()); |
32 | 0 | Class otherClass = thisClass.cast(o).getClass(); |
33 | |
|
34 | 0 | if (o == this) return true; |
35 | 0 | if (!(thisClass.isInstance(o))) return false; |
36 | 0 | if (o == null) return false; |
37 | |
|
38 | 0 | Method[] methods = thisClass.getMethods(); |
39 | 0 | for (Method method : methods) { |
40 | 0 | if (isGetter(method)) { |
41 | |
try { |
42 | 0 | if (method.invoke(this) != null) { |
43 | 0 | if (method.getReturnType().isInstance(Object.class) |
44 | |
&& !method.invoke(this).equals(method.invoke(o))) { |
45 | 0 | return false; |
46 | 0 | } else if (method.invoke(this) != method.invoke(o)) { |
47 | 0 | return false; |
48 | |
} |
49 | |
} else { |
50 | 0 | return false; |
51 | |
} |
52 | |
} |
53 | 0 | catch (IllegalArgumentException ex) { |
54 | 0 | ex.printStackTrace(); |
55 | 0 | return false; |
56 | |
} |
57 | 0 | catch (SecurityException ex) { |
58 | 0 | ex.printStackTrace(); |
59 | 0 | return false; |
60 | |
} |
61 | 0 | catch (IllegalAccessException ex) { |
62 | 0 | ex.printStackTrace(); |
63 | 0 | return false; |
64 | |
} |
65 | 0 | catch (InvocationTargetException ex) { |
66 | 0 | ex.printStackTrace(); |
67 | 0 | return false; |
68 | 0 | } |
69 | |
} |
70 | |
} |
71 | 0 | return true; |
72 | |
} |
73 | |
|
74 | |
public int hashCode() { |
75 | 0 | HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(); |
76 | 0 | Class thisClass = (this.getClass()); |
77 | |
|
78 | 0 | Method[] methods = thisClass.getMethods(); |
79 | |
|
80 | |
|
81 | 0 | Arrays.sort(methods, new Comparator() { |
82 | |
public int compare(Object a, Object b) { |
83 | 0 | return ((Method)a).getName().compareTo(((Method)b).getName()); |
84 | |
} |
85 | |
}); |
86 | 0 | for (Method method : methods) { |
87 | 0 | if (isGetter(method)) { |
88 | |
try { |
89 | 0 | hashCodeBuilder.append(method.invoke(this)); |
90 | |
} |
91 | 0 | catch (IllegalArgumentException ex) { |
92 | 0 | ex.printStackTrace(); |
93 | |
} |
94 | 0 | catch (IllegalAccessException ex) { |
95 | 0 | ex.printStackTrace(); |
96 | |
} |
97 | 0 | catch (InvocationTargetException ex) { |
98 | 0 | ex.printStackTrace(); |
99 | 0 | } |
100 | |
} |
101 | |
} |
102 | |
|
103 | 0 | return hashCodeBuilder.toHashCode(); |
104 | |
} |
105 | |
|
106 | |
protected static boolean isGetter(Method method){ |
107 | 0 | if(!(method.getName().startsWith("get") |
108 | |
|| method.getName().startsWith("is"))) { |
109 | 0 | return false; |
110 | |
} |
111 | 0 | if(method.getParameterTypes().length != 0) { |
112 | 0 | return false; |
113 | |
} |
114 | 0 | if(void.class.equals(method.getReturnType())) { |
115 | 0 | return false; |
116 | |
} |
117 | |
|
118 | 0 | return true; |
119 | |
} |
120 | |
|
121 | |
public String toString() { |
122 | 0 | StringBuilder s = new StringBuilder(); |
123 | |
|
124 | |
try { |
125 | 0 | for (Field field : this.getClass().getDeclaredFields()) { |
126 | 0 | field.setAccessible(true); |
127 | |
|
128 | 0 | if (field.isAnnotationPresent(Id.class)) { |
129 | 0 | s.append(field.getName()); |
130 | 0 | s.append(": "); |
131 | 0 | s.append(field.get(this)); |
132 | 0 | s.append(", "); |
133 | |
} |
134 | |
} |
135 | |
|
136 | 0 | return s.substring(0, s.length() - 2).toString(); |
137 | 0 | } catch (Exception e) { |
138 | 0 | return s.toString(); |
139 | |
} |
140 | |
} |
141 | |
|
142 | |
} |