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