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