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 = null;
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 ();
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 continue;
91 }
92 }
93 }
94 return discrepancies;
95 }
96
97 private FieldDefinition findField (String name, ObjectStructureDefinition os)
98 {
99 for (FieldDefinition fd : os.getAttributes ())
100 {
101 if (name.equals (fd.getName ()))
102 {
103 return fd;
104 }
105 }
106 return null;
107 }
108
109 private void compareAddDiscrepancy (List<String> discrepancies, String field, boolean value1,
110 boolean value2)
111 {
112 String discrep = compare (field, value1, value2);
113 if (discrep != null)
114 {
115 discrepancies.add (discrep);
116 }
117 }
118
119 private void compareAddDiscrepancy (List<String> discrepancies, String field, Object value1,
120 Object value2)
121 {
122 String discrep = compare (field, value1, value2);
123 if (discrep != null)
124 {
125 discrepancies.add (discrep);
126 }
127 }
128
129 private String compare (String field, boolean value1, boolean value2)
130 {
131 if (value1)
132 {
133 if (value2)
134 {
135 return null;
136 }
137 }
138 if ( ! value1)
139 {
140 if ( ! value2)
141 {
142 return null;
143 }
144 }
145 return field + " inconsistent: dictionary='" + value1 + "', java class='" + value2 + "'";
146 }
147
148 private String compare (String field, Object value1, Object value2)
149 {
150 if (value1 == null)
151 {
152 if (value2 == null)
153 {
154 return null;
155 }
156 }
157 else
158 {
159 if (value1.equals (value2))
160 {
161 return null;
162 }
163 }
164 return field + " inconsistent: dictionary='" + value1 + "'], java class='" + value2 + "'";
165 }
166 }