1 package org.kuali.student.lum.course.service.impl; 2 3 import java.beans.IntrospectionException; 4 import java.util.ArrayList; 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 Dictionary2DictionaryComparer 11 { 12 13 private ObjectStructureDefinition os1; 14 private ObjectStructureDefinition os2; 15 16 public Dictionary2DictionaryComparer (ObjectStructureDefinition os1, 17 ObjectStructureDefinition os2) 18 { 19 this.os1 = os1; 20 this.os2 = os2; 21 } 22 23 public List<String> compare () 24 { 25 return compare (os1, os2); 26 27 } 28 29 private List<String> compare (ObjectStructureDefinition os1, 30 ObjectStructureDefinition os2) 31 { 32 List<String> discrepancies = new ArrayList (); 33 compare (discrepancies, "Java Object Name", os2.getName (), 34 os1.getName ()); 35 compare (discrepancies, "hasMetaData", os2.isHasMetaData (), 36 os1.isHasMetaData ()); 37 compare (discrepancies, "BusinessObjectClass", 38 os2.getBusinessObjectClass (), 39 os1.getBusinessObjectClass ()); 40 for (FieldDefinition fdDict : os1.getAttributes ()) 41 { 42 FieldDefinition fdBean = findField (fdDict.getName (), os2); 43 if (fdBean == null) 44 { 45 discrepancies.add ("Field " + fdDict.getName () 46 + " is missing from the 1st structure"); 47 } 48 compare (discrepancies, fdDict.getName () + " dataType", 49 fdDict.getDataType (), fdBean.getDataType ()); 50 compare (discrepancies, fdDict.getName () + " minOccurs", 51 fdDict.getMinOccurs (), fdBean.getMinOccurs ()); 52 compare (discrepancies, fdDict.getName () + " maxOccurs", 53 fdDict.getMaxOccurs (), fdBean.getMaxOccurs ()); 54 } 55 for (FieldDefinition fdBean : os2.getAttributes ()) 56 { 57 FieldDefinition fdDict = findField (fdBean.getName (), os1); 58 if (fdDict == null) 59 { 60 discrepancies.add ("Field " + fdBean.getName () 61 + " missing from the 2nd structure"); 62 continue; 63 } 64 } 65 return discrepancies; 66 } 67 68 private FieldDefinition findField (String name, ObjectStructureDefinition os) 69 { 70 for (FieldDefinition fd : os.getAttributes ()) 71 { 72 if (name.equals (fd.getName ())) 73 { 74 return fd; 75 } 76 } 77 return null; 78 } 79 80 private void compare (List<String> discrepancies, 81 String field, 82 Object value1, 83 Object value2) 84 { 85 String discrep = compare (field, value1, value2); 86 if (discrep != null) 87 { 88 discrepancies.add (discrep); 89 } 90 } 91 92 private String compare (String field, Object value1, Object value2) 93 { 94 if (value1 == null) 95 { 96 if (value2 == null) 97 { 98 return null; 99 } 100 } 101 else 102 { 103 if (value1.equals (value2)) 104 { 105 return null; 106 } 107 } 108 return field + " inconsistent: os1=[" + value1 + "], os2=[" + value2 + "]"; 109 } 110 }