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