1 19 20 package org.netbeans.modules.schema2beans; 21 22 import java.util.*; 23 24 40 public class BeanComparator { 41 private boolean hasKey = false; 48 49 private boolean useMddKeys = true; 56 57 static boolean useComparatorsMddKeys = true; 59 60 65 public BaseBean compareBean(String beanName, 66 BaseBean curBean, 67 BaseBean newBean) { 68 69 BaseBean ret = curBean; 70 Iterator it = curBean.beanPropsIterator(); 71 boolean useKeys = useMddKeys; 72 73 this.hasKey = false; 74 75 if (!useComparatorsMddKeys) 76 useKeys = false; 77 78 if (curBean.getProperty() != null 79 && curBean.getProperty().isKey()) { 80 81 BaseAttribute[] ba = curBean.listAttributes(); 83 84 if (ba != null) { 85 for(int j=0; j<ba.length; j++) { 86 if (!ba[j].isFixed()) { 87 String attrName = ba[j].getName(); 88 String curValue = curBean.getAttributeValue(attrName); 89 String otherValue = newBean.getAttributeValue(attrName); 90 91 if (curValue != otherValue) { 92 if (curValue == null || otherValue == null || 93 !curValue.equals(otherValue)) { 94 95 return newBean; 97 } 98 } 99 } 100 } 101 } 102 } 103 104 while (it.hasNext()) { 105 BeanProp prop = (BeanProp)it.next(); 107 108 if (prop == null) 109 continue; 110 111 String name = prop.getBeanName(); 112 boolean isArray = Common.isArray(prop.type); 113 boolean isBean = Common.isBean(prop.type); 114 boolean isKey = Common.isKey(prop.type) || !useKeys; 115 Object o1, o2, o3; 116 117 if (!this.hasKey && isKey) 118 this.hasKey = true; 119 120 if (isArray && !isBean && isKey) { 121 int size1 = prop.size(); 126 int size2 = newBean.size(name); 127 128 if (size1 != size2) { 129 ret = newBean; 131 break; 132 } 133 134 for (int i=0; i<size1; i++) { 135 o1 = prop.getValue(i); 136 o2 = newBean.getValue(name, i); 137 o3 = this.compareProperty(name, curBean, o1, i, 138 newBean, o2, i); 139 if (o3 != o1) { 140 ret = newBean; 142 break; 143 } 144 } 145 } 146 else 147 if (!isBean && isKey) { 148 o1 = prop.getValue(0); 149 o2 = newBean.getValue(name); 150 o3 = this.compareProperty(name, curBean, o1, -1, 151 newBean, o2, -1); 152 if (o3 != o1) { 153 ret = newBean; 155 } 156 } 157 } 158 159 if (DDLogFlags.debug) { 160 TraceLogger.put(TraceLogger.DEBUG, 161 TraceLogger.SVC_DD, 162 DDLogFlags.DBG_BLD, 5, 163 DDLogFlags.BEANCOMP, 164 beanName + ": " + 165 ((ret == curBean)? "same":"different")); 166 } 167 168 return ret; 169 } 170 171 175 public Object compareProperty(String propertyName, 176 BaseBean curBean, 177 Object curValue, 178 int curIndex, 179 BaseBean newBean, 180 Object newValue, 181 int newIndex) { 182 Object ret = curValue; 183 BeanProp prop = curBean.beanProp(propertyName); 184 boolean isKey = this.hasKeyDefined(prop); 185 186 if (isKey) { 188 if (curValue == null || !curValue.equals(newValue)) 189 ret = newValue; 190 191 String [] attrs = curBean.getAttributeNames(propertyName); 192 int i1 = 0; 193 int i2 = 0; 194 195 if (curIndex != -1) { 196 i1 = curIndex; 197 i2 = newIndex; 198 } 199 200 for(int j=0; j<attrs.length; j++) { 201 String a = attrs[j]; 202 203 String v1 = curBean.getAttributeValue(propertyName, i1, a); 204 String v2 = newBean.getAttributeValue(propertyName, i2, a); 205 206 if (v1 != null) { 207 if (!v1.equals(v2)) { 208 ret = newValue; 209 break; 210 } 211 } else if (v2 != v1) { 212 ret = newValue; 213 break; 214 } 215 216 } 217 } else { 218 } 219 220 if (DDLogFlags.debug) { 221 TraceLogger.put(TraceLogger.DEBUG, 222 TraceLogger.SVC_DD, 223 DDLogFlags.DBG_BLD, 5, 224 DDLogFlags.PROPCOMP, 225 propertyName + " - " + 226 ((curValue==null)?"<null>":curValue) + 227 ((curIndex==-1)?"":("."+curIndex)) + " / " + 228 ((newValue==null)?"<null>":newValue) + 229 ((newIndex==-1)?"":("."+newIndex)) + " " + 230 ((ret == curValue)? "same":"different") + 231 " (" +((isKey)?"Key":"!Key") + ")"); 232 } 233 234 return ret; 235 } 236 237 protected boolean hasKey() { 242 return this.hasKey; 243 } 244 245 boolean hasKeyDefined(BeanProp prop) { 249 boolean useKeys = useMddKeys; 250 251 if (!useComparatorsMddKeys) 252 useKeys = false; 253 254 this.hasKey = Common.isKey(prop.type) || !useKeys; 255 return this.hasKey; 256 } 257 258 public void enableKey(boolean b) { 259 this.useMddKeys = b; 260 } 261 262 public static void enableComparatorsKey(boolean b) { 263 useComparatorsMddKeys = b; 264 } 265 266 } 267 268 269 270 271 272 273 274 275 276 277 278 279 | Popular Tags |