1 56 57 package org.objectstyle.cayenne.wocompat; 58 59 import java.io.IOException ; 60 import java.io.InputStream ; 61 import java.math.BigDecimal ; 62 import java.net.URL ; 63 import java.util.ArrayList ; 64 import java.util.Collections ; 65 import java.util.HashMap ; 66 import java.util.Iterator ; 67 import java.util.List ; 68 import java.util.Map ; 69 70 import org.apache.commons.collections.IteratorUtils; 71 import org.objectstyle.cayenne.map.DataMap; 72 import org.objectstyle.cayenne.util.ResourceLocator; 73 import org.objectstyle.cayenne.wocompat.parser.Parser; 74 75 79 public class EOModelHelper { 80 81 private static final ResourceLocator locator = new ResourceLocator(); 82 83 private Parser plistParser = new Parser(); 84 protected URL modelUrl; 85 protected Map entityIndex; 86 protected Map entityClassIndex; 87 protected Map entityQueryIndex; 88 protected Map entityClientClassIndex; 89 protected DataMap dataMap; 90 private Map prototypeValues; 91 92 static { 93 locator.setSkipClasspath(false); 95 locator.setSkipCurrentDirectory(false); 96 locator.setSkipHomeDirectory(true); 97 locator.setSkipAbsolutePath(false); 98 } 99 100 103 public EOModelHelper(String path) throws Exception { 104 105 this.modelUrl = findModelUrl(path); 106 this.dataMap = new DataMap(findModelName(path)); 107 108 List modelIndex = (List ) loadModelIndex().get("entities"); 110 111 entityIndex = new HashMap (); 113 entityClassIndex = new HashMap (); 114 entityClientClassIndex = new HashMap (); 115 entityQueryIndex = new HashMap (); 116 117 Iterator it = modelIndex.iterator(); 118 while (it.hasNext()) { 119 Map info = (Map ) it.next(); 120 String name = (String ) info.get("name"); 121 122 entityIndex.put(name, loadEntityIndex(name)); 123 entityQueryIndex.put(name, loadQueryIndex(name)); 124 entityClassIndex.put(name, info.get("className")); 125 Map entityPlistMap = entityPListMap(name); 126 127 Map internalInfo = (Map ) entityPlistMap.get("internalInfo"); 129 130 if (internalInfo != null) { 131 String clientClassName = (String ) internalInfo 132 .get("_javaClientClassName"); 133 entityClientClassIndex.put(name, clientClassName); 134 } 135 } 136 137 it = modelIndex.iterator(); 138 while (it.hasNext()) { 139 Map info = (Map ) it.next(); 140 String name = (String ) info.get("name"); 141 Map entityPlistMap = entityPListMap(name); 142 List classProperties = (List ) entityPlistMap.get("classProperties"); 143 if(classProperties == null) { 144 classProperties = Collections.EMPTY_LIST; 145 } 146 147 Map internalInfo = (Map ) entityPlistMap.get("internalInfo"); 149 150 List clientClassProperties = (internalInfo != null) ? (List ) internalInfo 151 .get("_clientClassPropertyNames") : null; 152 153 if (clientClassProperties == null) { 155 clientClassProperties = Collections.EMPTY_LIST; 156 } 157 158 clientClassProperties.retainAll(classProperties); 161 162 String parentEntity = (String ) entityPlistMap.get("parent"); 166 while (parentEntity != null) { 167 Map parentEntityPListMap = entityPListMap(parentEntity); 168 List parentClassProps = (List ) parentEntityPListMap 169 .get("classProperties"); 170 classProperties.removeAll(parentClassProps); 171 Map parentInternalInfo = (Map ) parentEntityPListMap.get("internalInfo"); 173 174 if (parentInternalInfo != null) { 175 List parentClientClassProps = (List ) parentInternalInfo 176 .get("_clientClassPropertyNames"); 177 clientClassProperties.removeAll(parentClientClassProps); 178 } 179 180 parentEntity = (String ) parentEntityPListMap.get("parent"); 181 } 182 183 entityPlistMap.put("classProperties", classProperties); 185 entityPlistMap.put("clientClassProperties", clientClassProperties); 187 } 188 } 189 190 197 public String javaTypeForEOModelerType(String valueClassName, String valueType) { 198 if (valueClassName == null) { 199 return null; 200 } 201 202 if (valueClassName.equals("NSString")) { 203 return String .class.getName(); 204 } 205 206 if (valueClassName.equals("NSNumber")) { 207 Class numericClass = numericAttributeClass(valueType); 208 return (numericClass != null) ? numericClass.getName() : Number .class 209 .getName(); 210 } 211 212 if (valueClassName.equals("NSCalendarDate")) 213 return "java.sql.Timestamp"; 214 215 if (valueClassName.equals("NSDecimalNumber")) { 216 Class numericClass = numericAttributeClass(valueType); 217 return (numericClass != null) ? numericClass.getName() : BigDecimal .class 218 .getName(); 219 } 220 221 if (valueClassName.equals("NSData")) 222 return "byte[]"; 223 224 try { 227 return Class.forName(valueClassName).getName(); 228 } 229 catch (ClassNotFoundException aClassNotFoundException) { 230 try { 231 return Class.forName("java.lang." + valueClassName).getName(); 232 } 233 catch (ClassNotFoundException anotherClassNotFoundException) { 234 try { 235 return Class.forName("java.util." + valueClassName).getName(); 236 } 237 catch (ClassNotFoundException yetAnotherClassNotFoundException) { 238 try { 239 return ClassLoader.getSystemClassLoader().loadClass( 240 valueClassName).getName(); 241 } 242 catch (ClassNotFoundException e) { 243 return valueClassName; 245 } 246 } 247 } 248 } 249 } 250 251 254 protected Class numericAttributeClass(String valueType) { 256 if (valueType == null) { 257 return null; 258 } else if ("b".equals(valueType)) { 259 return Byte .class; 260 } else if ("s".equals(valueType)) { 261 return Short .class; 262 } else if ("i".equals(valueType)) { 263 return Integer .class; 264 } else if ("l".equals(valueType)) { 265 return Long .class; 266 } else if ("f".equals(valueType)) { 267 return Float .class; 268 } else if ("d".equals(valueType)) { 269 return Double .class; 270 } else if ("B".equals(valueType)) { 271 return BigDecimal .class; 272 } else if ("c".equals(valueType)) { 273 return Boolean .class; 274 } else { 275 return null; 276 } 277 } 278 279 280 public DataMap getDataMap() { 281 return dataMap; 282 } 283 284 285 public URL getModelUrl() { 286 return modelUrl; 287 } 288 289 292 public Iterator modelNames() { 293 return entityClassIndex.keySet().iterator(); 294 } 295 296 301 public List modelNamesAsList() { 302 return new ArrayList (entityClassIndex.keySet()); 303 } 304 305 public Map getPrototypeAttributeMapFor(String aPrototypeAttributeName) { 306 if (prototypeValues == null) { 307 308 Map eoPrototypesEntityMap = this.entityPListMap("EOPrototypes"); 309 310 if (eoPrototypesEntityMap == null) { 312 prototypeValues = Collections.EMPTY_MAP; 313 } else { 314 List eoPrototypeAttributes = (List ) eoPrototypesEntityMap 315 .get("attributes"); 316 317 prototypeValues = new HashMap (); 318 Iterator it = eoPrototypeAttributes.iterator(); 319 while (it.hasNext()) { 320 Map attrMap = (Map ) it.next(); 321 322 String attrName = (String ) attrMap.get("name"); 323 324 Map prototypeAttrMap = new HashMap (); 326 prototypeValues.put(attrName, prototypeAttrMap); 327 328 prototypeAttrMap.put("name", attrMap.get("name")); 329 prototypeAttrMap.put("prototypeName", attrMap.get("prototypeName")); 330 prototypeAttrMap.put("columnName", attrMap.get("columnName")); 331 prototypeAttrMap.put("valueClassName", attrMap.get("valueClassName")); 332 prototypeAttrMap.put("width", attrMap.get("width")); 333 prototypeAttrMap.put("allowsNull", attrMap.get("allowsNull")); 334 prototypeAttrMap.put("scale", attrMap.get("scale")); 335 prototypeAttrMap.put("valueType", attrMap.get("valueType")); 336 } 337 } 338 } 339 340 Map aMap = (Map ) prototypeValues.get(aPrototypeAttributeName); 341 if (null == aMap) 342 aMap = Collections.EMPTY_MAP; 343 344 return aMap; 345 } 346 347 348 public Map entityPListMap(String entityName) { 349 return (Map ) entityIndex.get(entityName); 350 } 351 352 357 public Iterator queryNames(String entityName) { 358 Map queryPlist = (Map ) entityQueryIndex.get(entityName); 359 if (queryPlist == null || queryPlist.isEmpty()) { 360 return IteratorUtils.EMPTY_ITERATOR; 361 } 362 363 return queryPlist.keySet().iterator(); 364 } 365 366 372 public Map queryPListMap(String entityName, String queryName) { 373 Map queryPlist = (Map ) entityQueryIndex.get(entityName); 374 if (queryPlist == null || queryPlist.isEmpty()) { 375 return null; 376 } 377 378 return (Map ) queryPlist.get(queryName); 379 } 380 381 public String entityClass(String entityName, boolean getClientClass) { 382 if (getClientClass) { 383 return (String ) entityClientClassIndex.get(entityName); 384 } else { 385 return (String ) entityClassIndex.get(entityName); 386 } 387 } 388 389 390 protected Map loadModelIndex() throws Exception { 391 InputStream indexIn = openIndexStream(); 392 try { 393 plistParser.ReInit(indexIn); 394 return (Map ) plistParser.propertyList(); 395 } 396 finally { 397 indexIn.close(); 398 } 399 } 400 401 404 protected Map loadEntityIndex(String entityName) throws Exception { 405 InputStream entIn = openEntityStream(entityName); 406 try { 407 plistParser.ReInit(entIn); 408 return (Map ) plistParser.propertyList(); 409 } 410 finally { 411 entIn.close(); 412 } 413 } 414 415 418 protected Map loadQueryIndex(String entityName) throws Exception { 419 InputStream queryIn = null; 420 421 try { 423 queryIn = openQueryStream(entityName); 424 } 425 catch (IOException ioex) { 426 return Collections.EMPTY_MAP; 427 } 428 429 try { 430 plistParser.ReInit(queryIn); 431 return (Map ) plistParser.propertyList(); 432 } 433 finally { 434 queryIn.close(); 435 } 436 } 437 438 439 protected String findModelName(String path) { 440 if (path.endsWith("/") || path.endsWith("\\")) { 442 path = path.substring(0, path.length() - 1); 443 } 444 445 int i1 = path.lastIndexOf("/"); 447 int i2 = path.lastIndexOf("\\"); 448 int i = (i1 > i2) ? i1 : i2; 449 if (i >= 0) { 450 path = path.substring(i + 1); 451 } 452 453 if (path.endsWith(".eomodeld")) { 455 path = path.substring(0, path.length() - ".eomodeld".length()); 456 } 457 458 return path; 459 } 460 461 464 protected URL findModelUrl(String path) { 465 if (!path.endsWith(".eomodeld")) { 466 path += ".eomodeld"; 467 } 468 469 URL base = locator.findDirectoryResource(path); 470 if (base == null) { 471 throw new IllegalArgumentException ("Can't find EOModel: " + path); 472 } 473 return base; 474 } 475 476 479 protected InputStream openIndexStream() throws Exception { 480 return new URL (modelUrl, "index.eomodeld").openStream(); 481 } 482 483 491 protected InputStream openEntityStream(String entityName) throws Exception { 492 return new URL (modelUrl, entityName + ".plist").openStream(); 493 } 494 495 503 protected InputStream openQueryStream(String entityName) throws Exception { 504 return new URL (modelUrl, entityName + ".fspec").openStream(); 505 } 506 } | Popular Tags |