View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      public Dictionary2BeanComparer(String className, DataObjectEntry osDict) {
33          this.className = className;
34          this.osDict = osDict;
35      }
36  
37      public List<String> compare() {
38          if (className == null) {
39              return Arrays.asList(osDict.getFullClassName() + " does not have a corresponding java class");
40          }
41          Class<?> clazz = null;
42          try {
43              clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
44              // had to change the standard Class.forName below to the above so it uses the right class loader
45              // that is defined in KSDictionaryDocMojo.java
46  //            clazz = Class.forName(className);
47          } catch (ClassNotFoundException ex) {
48              return Arrays.asList("Cannot compare the dictionary entry to it's object because the object's class cannot be found");
49          }
50          Stack<DataDictionaryDefinitionBase> parentFields = new Stack<DataDictionaryDefinitionBase>();
51          Stack<Class<?>> parentClasses = new Stack<Class<?>>();
52          DataObjectEntry osBean = new Bean2DictionaryConverter(clazz, parentFields, parentClasses).convert();
53          return compare(osDict, osBean);
54  
55      }
56  
57      private List<String> compare(DataObjectEntry osDict,
58              DataObjectEntry osBean) {
59          List<String> discrepancies = new ArrayList();
60          compareAddDiscrepancy(discrepancies, "Java class name", osDict.getFullClassName(), osBean.getFullClassName());
61          compareAddDiscrepancy(discrepancies, "Entry class", osDict.getEntryClass(), osBean.getEntryClass());
62          for (AttributeDefinition adDict : osDict.getAttributes()) {
63              AttributeDefinition adBean = findAttributeDefinition(adDict.getName(), osBean);
64              if (adBean == null) {
65  //                if (!adDict.isDynamic()) {
66                  discrepancies.add("Field " + adDict.getName() + " does not exist in the corresponding java class");
67  //                }
68                  continue;
69              }
70              compareAddDiscrepancy(discrepancies, adDict.getName() + " dataType", adDict.getDataType(), adBean.getDataType());
71              // TODO: deal with collections
72  //            compareAddDiscrepancy(discrepancies, adDict.getName() + " maxOccurs", adDict.getMaximumNumberOfElements(), adBean.getMaximumNumberOfElements());
73          }
74          for (ComplexAttributeDefinition cadDict : osDict.getComplexAttributes()) {
75              ComplexAttributeDefinition cadBean = findComplexAttributeDefinition(cadDict.getName(), osBean);
76              if (cadBean == null) {
77  //                if (!adDict.isDynamic()) {
78                  discrepancies.add("Field " + cadDict.getName() + " does not exist in the corresponding java class");
79  //                }
80                  continue;
81              }
82               // TODO: deal with collections
83  //            compareAddDiscrepancy(discrepancies, adDict.getName() + " maxOccurs", adDict.getMaximumNumberOfElements(), adBean.getMaximumNumberOfElements());
84          }
85          for (AttributeDefinition fdBean : osBean.getAttributes()) {
86              AttributeDefinition fdDict = findAttributeDefinition(fdBean.getName(), osDict);
87              if (fdDict == null) {
88                  discrepancies.add("Field " + fdBean.getName() + " missing from the dictictionary");
89                  continue;
90              }
91          }
92          for (ComplexAttributeDefinition fdBean : osBean.getComplexAttributes()) {
93              ComplexAttributeDefinition fdDict = findComplexAttributeDefinition(fdBean.getName(), osDict);
94              if (fdDict == null) {
95                  discrepancies.add("Field " + fdBean.getName() + " missing from the dictictionary");
96                  continue;
97              }
98          }
99          return discrepancies;
100     }
101 
102     private AttributeDefinition findAttributeDefinition(String name, DataObjectEntry ode) {
103         for (AttributeDefinition fd : ode.getAttributes()) {
104             if (name.equals(fd.getName())) {
105                 return fd;
106             }
107         }
108         return null;
109     }
110 
111     private ComplexAttributeDefinition findComplexAttributeDefinition(String name, DataObjectEntry ode) {
112         for (ComplexAttributeDefinition cad : ode.getComplexAttributes()) {
113             if (name.equals(cad.getName())) {
114                 return cad;
115             }
116         }
117         return null;
118     }
119 
120     private void compareAddDiscrepancy(List<String> discrepancies, String field, boolean value1,
121             boolean value2) {
122         String discrep = compare(field, value1, value2);
123         if (discrep != null) {
124             discrepancies.add(discrep);
125         }
126     }
127 
128     private void compareAddDiscrepancy(List<String> discrepancies, String field, Object value1,
129             Object value2) {
130         String discrep = compare(field, value1, value2);
131         if (discrep != null) {
132             discrepancies.add(discrep);
133         }
134     }
135 
136     private String compare(String field, boolean value1, boolean value2) {
137         if (value1) {
138             if (value2) {
139                 return null;
140             }
141         }
142         if (!value1) {
143             if (!value2) {
144                 return null;
145             }
146         }
147         return field + " inconsistent: dictionary='" + value1 + "', java class='" + value2 + "'";
148     }
149 
150     private String compare(String field, Object value1, Object value2) {
151         if (value1 == null) {
152             if (value2 == null) {
153                 return null;
154             }
155         } else {
156             if (value1.equals(value2)) {
157                 return null;
158             }
159         }
160         return field + " inconsistent: dictionary='" + value1 + "'], java class='" + value2 + "'";
161     }
162 }