1 | |
package org.kuali.student.core.dictionary.service.impl; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.Arrays; |
5 | |
import java.util.List; |
6 | |
|
7 | |
import org.kuali.student.core.dictionary.dto.FieldDefinition; |
8 | |
import org.kuali.student.core.dictionary.dto.ObjectStructureDefinition; |
9 | |
|
10 | |
public class Dictionary2BeanComparer |
11 | |
{ |
12 | |
|
13 | |
|
14 | |
private String className; |
15 | |
private ObjectStructureDefinition osDict; |
16 | |
|
17 | |
public Dictionary2BeanComparer (String className, ObjectStructureDefinition osDict) |
18 | 0 | { |
19 | 0 | this.className = className; |
20 | 0 | this.osDict = osDict; |
21 | 0 | } |
22 | |
|
23 | |
|
24 | |
public List<String> compare () |
25 | |
{ |
26 | 0 | if (className == null) |
27 | |
{ |
28 | 0 | return Arrays.asList (osDict.getName () + " does not have a corresponding java class"); |
29 | |
} |
30 | 0 | Class<?> clazz = null; |
31 | |
try |
32 | |
{ |
33 | 0 | clazz = Class.forName (className); |
34 | |
} |
35 | 0 | catch (ClassNotFoundException ex) |
36 | |
{ |
37 | 0 | return Arrays.asList (className + " does not have a corresponding java class"); |
38 | 0 | } |
39 | 0 | ObjectStructureDefinition osBean = new Bean2DictionaryConverter (clazz).convert (); |
40 | 0 | return compare (osDict, osBean); |
41 | |
|
42 | |
} |
43 | |
|
44 | |
private List<String> compare (ObjectStructureDefinition osDict, |
45 | |
ObjectStructureDefinition osBean) |
46 | |
{ |
47 | 0 | List<String> discrepancies = new ArrayList (); |
48 | 0 | compareAddDiscrepancy (discrepancies, "Java class name", osDict.getName (), osBean.getName ()); |
49 | 0 | compareAddDiscrepancy (discrepancies, "Has meta data?", osDict.isHasMetaData (), osBean.isHasMetaData ()); |
50 | 0 | compareAddDiscrepancy (discrepancies, "Business object class", osDict.getBusinessObjectClass (), osBean.getBusinessObjectClass ()); |
51 | 0 | for (FieldDefinition fdDict : osDict.getAttributes ()) |
52 | |
{ |
53 | 0 | FieldDefinition fdBean = findField (fdDict.getName (), osBean); |
54 | 0 | if (fdBean == null) |
55 | |
{ |
56 | 0 | if ( ! fdDict.isDynamic ()) |
57 | |
{ |
58 | 0 | discrepancies.add ("Field " + fdDict.getName () + " does not exist in the corresponding java class"); |
59 | |
} |
60 | |
continue; |
61 | |
} |
62 | 0 | compareAddDiscrepancy (discrepancies, fdDict.getName () + " dataType", fdDict.getDataType (), fdBean.getDataType ()); |
63 | 0 | compareAddDiscrepancy (discrepancies, fdDict.getName () + " maxOccurs", fdDict.getMaxOccurs (), fdBean.getMaxOccurs ()); |
64 | 0 | } |
65 | 0 | for (FieldDefinition fdBean : osBean.getAttributes ()) |
66 | |
{ |
67 | 0 | FieldDefinition fdDict = findField (fdBean.getName (), osDict); |
68 | 0 | if (fdDict == null) |
69 | |
{ |
70 | 0 | discrepancies.add ("Field " + fdBean.getName () + " missing from the dictictionary"); |
71 | 0 | continue; |
72 | |
} |
73 | 0 | } |
74 | 0 | return discrepancies; |
75 | |
} |
76 | |
|
77 | |
private FieldDefinition findField (String name, ObjectStructureDefinition os) |
78 | |
{ |
79 | 0 | for (FieldDefinition fd : os.getAttributes ()) |
80 | |
{ |
81 | 0 | if (name.equals (fd.getName ())) |
82 | |
{ |
83 | 0 | return fd; |
84 | |
} |
85 | |
} |
86 | 0 | return null; |
87 | |
} |
88 | |
|
89 | |
private void compareAddDiscrepancy (List<String> discrepancies, String field, boolean value1, |
90 | |
boolean value2) |
91 | |
{ |
92 | 0 | String discrep = compare (field, value1, value2); |
93 | 0 | if (discrep != null) |
94 | |
{ |
95 | 0 | discrepancies.add (discrep); |
96 | |
} |
97 | 0 | } |
98 | |
|
99 | |
private void compareAddDiscrepancy (List<String> discrepancies, String field, Object value1, |
100 | |
Object value2) |
101 | |
{ |
102 | 0 | String discrep = compare (field, value1, value2); |
103 | 0 | if (discrep != null) |
104 | |
{ |
105 | 0 | discrepancies.add (discrep); |
106 | |
} |
107 | 0 | } |
108 | |
|
109 | |
private String compare (String field, boolean value1, boolean value2) |
110 | |
{ |
111 | 0 | if (value1) |
112 | |
{ |
113 | 0 | if (value2) |
114 | |
{ |
115 | 0 | return null; |
116 | |
} |
117 | |
} |
118 | 0 | if ( ! value1) |
119 | |
{ |
120 | 0 | if ( ! value2) |
121 | |
{ |
122 | 0 | return null; |
123 | |
} |
124 | |
} |
125 | 0 | return field + " inconsistent: dictionary='" + value1 + "', java class='" + value2 + "'"; |
126 | |
} |
127 | |
|
128 | |
private String compare (String field, Object value1, Object value2) |
129 | |
{ |
130 | 0 | if (value1 == null) |
131 | |
{ |
132 | 0 | if (value2 == null) |
133 | |
{ |
134 | 0 | return null; |
135 | |
} |
136 | |
} |
137 | |
else |
138 | |
{ |
139 | 0 | if (value1.equals (value2)) |
140 | |
{ |
141 | 0 | return null; |
142 | |
} |
143 | |
} |
144 | 0 | return field + " inconsistent: dictionary='" + value1 + "'], java class='" + value2 + "'"; |
145 | |
} |
146 | |
} |