| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.datadictionary.util; |
| 17 | |
|
| 18 | |
import java.util.ArrayList; |
| 19 | |
import java.util.Arrays; |
| 20 | |
import java.util.List; |
| 21 | |
import java.util.Stack; |
| 22 | |
import org.kuali.rice.krad.datadictionary.AttributeDefinition; |
| 23 | |
import org.kuali.rice.krad.datadictionary.ComplexAttributeDefinition; |
| 24 | |
import org.kuali.rice.krad.datadictionary.DataDictionaryDefinitionBase; |
| 25 | |
import org.kuali.rice.krad.datadictionary.DataObjectEntry; |
| 26 | |
|
| 27 | |
public class Dictionary2BeanComparer { |
| 28 | |
|
| 29 | |
private String className; |
| 30 | |
private DataObjectEntry osDict; |
| 31 | |
|
| 32 | 0 | public Dictionary2BeanComparer(String className, DataObjectEntry osDict) { |
| 33 | 0 | this.className = className; |
| 34 | 0 | this.osDict = osDict; |
| 35 | 0 | } |
| 36 | |
|
| 37 | |
public List<String> compare() { |
| 38 | 0 | if (className == null) { |
| 39 | 0 | return Arrays.asList(osDict.getFullClassName() + " does not have a corresponding java class"); |
| 40 | |
} |
| 41 | 0 | Class<?> clazz = null; |
| 42 | |
try { |
| 43 | 0 | clazz = Thread.currentThread().getContextClassLoader().loadClass(className); |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 0 | } catch (ClassNotFoundException ex) { |
| 48 | 0 | return Arrays.asList("Cannot compare the dictionary entry to it's object because the object's class cannot be found"); |
| 49 | 0 | } |
| 50 | 0 | Stack<DataDictionaryDefinitionBase> parentFields = new Stack<DataDictionaryDefinitionBase>(); |
| 51 | 0 | Stack<Class<?>> parentClasses = new Stack<Class<?>>(); |
| 52 | 0 | DataObjectEntry osBean = new Bean2DictionaryConverter(clazz, parentFields, parentClasses).convert(); |
| 53 | 0 | return compare(osDict, osBean); |
| 54 | |
|
| 55 | |
} |
| 56 | |
|
| 57 | |
private List<String> compare(DataObjectEntry osDict, |
| 58 | |
DataObjectEntry osBean) { |
| 59 | 0 | List<String> discrepancies = new ArrayList(); |
| 60 | 0 | compareAddDiscrepancy(discrepancies, "Java class name", osDict.getFullClassName(), osBean.getFullClassName()); |
| 61 | 0 | compareAddDiscrepancy(discrepancies, "Entry class", osDict.getEntryClass(), osBean.getEntryClass()); |
| 62 | 0 | for (AttributeDefinition adDict : osDict.getAttributes()) { |
| 63 | 0 | AttributeDefinition adBean = findAttributeDefinition(adDict.getName(), osBean); |
| 64 | 0 | if (adBean == null) { |
| 65 | |
|
| 66 | 0 | discrepancies.add("Field " + adDict.getName() + " does not exist in the corresponding java class"); |
| 67 | |
|
| 68 | 0 | continue; |
| 69 | |
} |
| 70 | 0 | compareAddDiscrepancy(discrepancies, adDict.getName() + " dataType", adDict.getDataType(), adBean.getDataType()); |
| 71 | |
|
| 72 | |
|
| 73 | 0 | } |
| 74 | 0 | for (ComplexAttributeDefinition cadDict : osDict.getComplexAttributes()) { |
| 75 | 0 | ComplexAttributeDefinition cadBean = findComplexAttributeDefinition(cadDict.getName(), osBean); |
| 76 | 0 | if (cadBean == null) { |
| 77 | |
|
| 78 | 0 | discrepancies.add("Field " + cadDict.getName() + " does not exist in the corresponding java class"); |
| 79 | |
|
| 80 | 0 | continue; |
| 81 | |
} |
| 82 | |
|
| 83 | |
|
| 84 | 0 | } |
| 85 | 0 | for (AttributeDefinition fdBean : osBean.getAttributes()) { |
| 86 | 0 | AttributeDefinition fdDict = findAttributeDefinition(fdBean.getName(), osDict); |
| 87 | 0 | if (fdDict == null) { |
| 88 | 0 | discrepancies.add("Field " + fdBean.getName() + " missing from the dictictionary"); |
| 89 | 0 | continue; |
| 90 | |
} |
| 91 | 0 | } |
| 92 | 0 | for (ComplexAttributeDefinition fdBean : osBean.getComplexAttributes()) { |
| 93 | 0 | ComplexAttributeDefinition fdDict = findComplexAttributeDefinition(fdBean.getName(), osDict); |
| 94 | 0 | if (fdDict == null) { |
| 95 | 0 | discrepancies.add("Field " + fdBean.getName() + " missing from the dictictionary"); |
| 96 | 0 | continue; |
| 97 | |
} |
| 98 | 0 | } |
| 99 | 0 | return discrepancies; |
| 100 | |
} |
| 101 | |
|
| 102 | |
private AttributeDefinition findAttributeDefinition(String name, DataObjectEntry ode) { |
| 103 | 0 | for (AttributeDefinition fd : ode.getAttributes()) { |
| 104 | 0 | if (name.equals(fd.getName())) { |
| 105 | 0 | return fd; |
| 106 | |
} |
| 107 | |
} |
| 108 | 0 | return null; |
| 109 | |
} |
| 110 | |
|
| 111 | |
private ComplexAttributeDefinition findComplexAttributeDefinition(String name, DataObjectEntry ode) { |
| 112 | 0 | for (ComplexAttributeDefinition cad : ode.getComplexAttributes()) { |
| 113 | 0 | if (name.equals(cad.getName())) { |
| 114 | 0 | return cad; |
| 115 | |
} |
| 116 | |
} |
| 117 | 0 | return null; |
| 118 | |
} |
| 119 | |
|
| 120 | |
private void compareAddDiscrepancy(List<String> discrepancies, String field, boolean value1, |
| 121 | |
boolean value2) { |
| 122 | 0 | String discrep = compare(field, value1, value2); |
| 123 | 0 | if (discrep != null) { |
| 124 | 0 | discrepancies.add(discrep); |
| 125 | |
} |
| 126 | 0 | } |
| 127 | |
|
| 128 | |
private void compareAddDiscrepancy(List<String> discrepancies, String field, Object value1, |
| 129 | |
Object value2) { |
| 130 | 0 | String discrep = compare(field, value1, value2); |
| 131 | 0 | if (discrep != null) { |
| 132 | 0 | discrepancies.add(discrep); |
| 133 | |
} |
| 134 | 0 | } |
| 135 | |
|
| 136 | |
private String compare(String field, boolean value1, boolean value2) { |
| 137 | 0 | if (value1) { |
| 138 | 0 | if (value2) { |
| 139 | 0 | return null; |
| 140 | |
} |
| 141 | |
} |
| 142 | 0 | if (!value1) { |
| 143 | 0 | if (!value2) { |
| 144 | 0 | return null; |
| 145 | |
} |
| 146 | |
} |
| 147 | 0 | return field + " inconsistent: dictionary='" + value1 + "', java class='" + value2 + "'"; |
| 148 | |
} |
| 149 | |
|
| 150 | |
private String compare(String field, Object value1, Object value2) { |
| 151 | 0 | if (value1 == null) { |
| 152 | 0 | if (value2 == null) { |
| 153 | 0 | return null; |
| 154 | |
} |
| 155 | |
} else { |
| 156 | 0 | if (value1.equals(value2)) { |
| 157 | 0 | return null; |
| 158 | |
} |
| 159 | |
} |
| 160 | 0 | return field + " inconsistent: dictionary='" + value1 + "'], java class='" + value2 + "'"; |
| 161 | |
} |
| 162 | |
} |