1 19 20 21 package org.apache.cayenne.wocompat; 22 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.math.BigDecimal ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import org.apache.commons.collections.IteratorUtils; 35 import org.apache.cayenne.map.DataMap; 36 import org.apache.cayenne.util.ResourceLocator; 37 import org.apache.cayenne.wocompat.parser.Parser; 38 39 43 public class EOModelHelper { 44 45 private static final ResourceLocator locator = new ResourceLocator(); 46 47 private Parser plistParser = new Parser(); 48 protected URL modelUrl; 49 protected Map entityIndex; 50 protected Map entityClassIndex; 51 protected Map entityQueryIndex; 52 protected Map entityClientClassIndex; 53 protected DataMap dataMap; 54 private Map prototypeValues; 55 56 static { 57 locator.setSkipClasspath(false); 59 locator.setSkipCurrentDirectory(false); 60 locator.setSkipHomeDirectory(true); 61 locator.setSkipAbsolutePath(false); 62 } 63 64 67 public EOModelHelper(String path) throws Exception { 68 69 this.modelUrl = findModelUrl(path); 70 this.dataMap = new DataMap(findModelName(path)); 71 72 List modelIndex = (List ) loadModelIndex().get("entities"); 74 75 entityIndex = new HashMap (); 77 entityClassIndex = new HashMap (); 78 entityClientClassIndex = new HashMap (); 79 entityQueryIndex = new HashMap (); 80 81 Iterator it = modelIndex.iterator(); 82 while (it.hasNext()) { 83 Map info = (Map ) it.next(); 84 String name = (String ) info.get("name"); 85 86 entityIndex.put(name, loadEntityIndex(name)); 87 entityQueryIndex.put(name, loadQueryIndex(name)); 88 entityClassIndex.put(name, info.get("className")); 89 Map entityPlistMap = entityPListMap(name); 90 91 Map internalInfo = (Map ) entityPlistMap.get("internalInfo"); 93 94 if (internalInfo != null) { 95 String clientClassName = (String ) internalInfo 96 .get("_javaClientClassName"); 97 entityClientClassIndex.put(name, clientClassName); 98 } 99 } 100 101 it = modelIndex.iterator(); 102 while (it.hasNext()) { 103 Map info = (Map ) it.next(); 104 String name = (String ) info.get("name"); 105 Map entityPlistMap = entityPListMap(name); 106 List classProperties = (List ) entityPlistMap.get("classProperties"); 107 if(classProperties == null) { 108 classProperties = Collections.EMPTY_LIST; 109 } 110 111 Map internalInfo = (Map ) entityPlistMap.get("internalInfo"); 113 114 List clientClassProperties = (internalInfo != null) ? (List ) internalInfo 115 .get("_clientClassPropertyNames") : null; 116 117 if (clientClassProperties == null) { 119 clientClassProperties = Collections.EMPTY_LIST; 120 } 121 122 clientClassProperties.retainAll(classProperties); 125 126 String parentEntity = (String ) entityPlistMap.get("parent"); 130 while (parentEntity != null) { 131 Map parentEntityPListMap = entityPListMap(parentEntity); 132 List parentClassProps = (List ) parentEntityPListMap 133 .get("classProperties"); 134 classProperties.removeAll(parentClassProps); 135 Map parentInternalInfo = (Map ) parentEntityPListMap.get("internalInfo"); 137 138 if (parentInternalInfo != null) { 139 List parentClientClassProps = (List ) parentInternalInfo 140 .get("_clientClassPropertyNames"); 141 clientClassProperties.removeAll(parentClientClassProps); 142 } 143 144 parentEntity = (String ) parentEntityPListMap.get("parent"); 145 } 146 147 entityPlistMap.put("classProperties", classProperties); 149 entityPlistMap.put("clientClassProperties", clientClassProperties); 151 } 152 } 153 154 161 public String javaTypeForEOModelerType(String valueClassName, String valueType) { 162 if (valueClassName == null) { 163 return null; 164 } 165 166 if (valueClassName.equals("NSString")) { 167 return String .class.getName(); 168 } 169 170 if (valueClassName.equals("NSNumber")) { 171 Class numericClass = numericAttributeClass(valueType); 172 return (numericClass != null) ? numericClass.getName() : Number .class 173 .getName(); 174 } 175 176 if (valueClassName.equals("NSCalendarDate")) 177 return "java.sql.Timestamp"; 178 179 if (valueClassName.equals("NSDecimalNumber")) { 180 Class numericClass = numericAttributeClass(valueType); 181 return (numericClass != null) ? numericClass.getName() : BigDecimal .class 182 .getName(); 183 } 184 185 if (valueClassName.equals("NSData")) 186 return "byte[]"; 187 188 try { 191 return Class.forName(valueClassName).getName(); 192 } 193 catch (ClassNotFoundException aClassNotFoundException) { 194 try { 195 return Class.forName("java.lang." + valueClassName).getName(); 196 } 197 catch (ClassNotFoundException anotherClassNotFoundException) { 198 try { 199 return Class.forName("java.util." + valueClassName).getName(); 200 } 201 catch (ClassNotFoundException yetAnotherClassNotFoundException) { 202 try { 203 return ClassLoader.getSystemClassLoader().loadClass( 204 valueClassName).getName(); 205 } 206 catch (ClassNotFoundException e) { 207 return valueClassName; 209 } 210 } 211 } 212 } 213 } 214 215 218 protected Class numericAttributeClass(String valueType) { 220 if (valueType == null) { 221 return null; 222 } else if ("b".equals(valueType)) { 223 return Byte .class; 224 } else if ("s".equals(valueType)) { 225 return Short .class; 226 } else if ("i".equals(valueType)) { 227 return Integer .class; 228 } else if ("l".equals(valueType)) { 229 return Long .class; 230 } else if ("f".equals(valueType)) { 231 return Float .class; 232 } else if ("d".equals(valueType)) { 233 return Double .class; 234 } else if ("B".equals(valueType)) { 235 return BigDecimal .class; 236 } else if ("c".equals(valueType)) { 237 return Boolean .class; 238 } else { 239 return null; 240 } 241 } 242 243 244 public DataMap getDataMap() { 245 return dataMap; 246 } 247 248 249 public URL getModelUrl() { 250 return modelUrl; 251 } 252 253 256 public Iterator modelNames() { 257 return entityClassIndex.keySet().iterator(); 258 } 259 260 265 public List modelNamesAsList() { 266 return new ArrayList (entityClassIndex.keySet()); 267 } 268 269 public Map getPrototypeAttributeMapFor(String aPrototypeAttributeName) { 270 if (prototypeValues == null) { 271 272 Map eoPrototypesEntityMap = this.entityPListMap("EOPrototypes"); 273 274 if (eoPrototypesEntityMap == null) { 276 prototypeValues = Collections.EMPTY_MAP; 277 } else { 278 List eoPrototypeAttributes = (List ) eoPrototypesEntityMap 279 .get("attributes"); 280 281 prototypeValues = new HashMap (); 282 Iterator it = eoPrototypeAttributes.iterator(); 283 while (it.hasNext()) { 284 Map attrMap = (Map ) it.next(); 285 286 String attrName = (String ) attrMap.get("name"); 287 288 Map prototypeAttrMap = new HashMap (); 290 prototypeValues.put(attrName, prototypeAttrMap); 291 292 prototypeAttrMap.put("name", attrMap.get("name")); 293 prototypeAttrMap.put("prototypeName", attrMap.get("prototypeName")); 294 prototypeAttrMap.put("columnName", attrMap.get("columnName")); 295 prototypeAttrMap.put("valueClassName", attrMap.get("valueClassName")); 296 prototypeAttrMap.put("width", attrMap.get("width")); 297 prototypeAttrMap.put("allowsNull", attrMap.get("allowsNull")); 298 prototypeAttrMap.put("scale", attrMap.get("scale")); 299 prototypeAttrMap.put("valueType", attrMap.get("valueType")); 300 } 301 } 302 } 303 304 Map aMap = (Map ) prototypeValues.get(aPrototypeAttributeName); 305 if (null == aMap) 306 aMap = Collections.EMPTY_MAP; 307 308 return aMap; 309 } 310 311 312 public Map entityPListMap(String entityName) { 313 return (Map ) entityIndex.get(entityName); 314 } 315 316 321 public Iterator queryNames(String entityName) { 322 Map queryPlist = (Map ) entityQueryIndex.get(entityName); 323 if (queryPlist == null || queryPlist.isEmpty()) { 324 return IteratorUtils.EMPTY_ITERATOR; 325 } 326 327 return queryPlist.keySet().iterator(); 328 } 329 330 336 public Map queryPListMap(String entityName, String queryName) { 337 Map queryPlist = (Map ) entityQueryIndex.get(entityName); 338 if (queryPlist == null || queryPlist.isEmpty()) { 339 return null; 340 } 341 342 return (Map ) queryPlist.get(queryName); 343 } 344 345 public String entityClass(String entityName, boolean getClientClass) { 346 if (getClientClass) { 347 return (String ) entityClientClassIndex.get(entityName); 348 } else { 349 return (String ) entityClassIndex.get(entityName); 350 } 351 } 352 353 354 protected Map loadModelIndex() throws Exception { 355 InputStream indexIn = openIndexStream(); 356 try { 357 plistParser.ReInit(indexIn); 358 return (Map ) plistParser.propertyList(); 359 } 360 finally { 361 indexIn.close(); 362 } 363 } 364 365 368 protected Map loadEntityIndex(String entityName) throws Exception { 369 InputStream entIn = openEntityStream(entityName); 370 try { 371 plistParser.ReInit(entIn); 372 return (Map ) plistParser.propertyList(); 373 } 374 finally { 375 entIn.close(); 376 } 377 } 378 379 382 protected Map loadQueryIndex(String entityName) throws Exception { 383 InputStream queryIn = null; 384 385 try { 387 queryIn = openQueryStream(entityName); 388 } 389 catch (IOException ioex) { 390 return Collections.EMPTY_MAP; 391 } 392 393 try { 394 plistParser.ReInit(queryIn); 395 return (Map ) plistParser.propertyList(); 396 } 397 finally { 398 queryIn.close(); 399 } 400 } 401 402 403 protected String findModelName(String path) { 404 if (path.endsWith("/") || path.endsWith("\\")) { 406 path = path.substring(0, path.length() - 1); 407 } 408 409 int i1 = path.lastIndexOf("/"); 411 int i2 = path.lastIndexOf("\\"); 412 int i = (i1 > i2) ? i1 : i2; 413 if (i >= 0) { 414 path = path.substring(i + 1); 415 } 416 417 if (path.endsWith(".eomodeld")) { 419 path = path.substring(0, path.length() - ".eomodeld".length()); 420 } 421 422 return path; 423 } 424 425 428 protected URL findModelUrl(String path) { 429 if (!path.endsWith(".eomodeld")) { 430 path += ".eomodeld"; 431 } 432 433 URL base = locator.findDirectoryResource(path); 434 if (base == null) { 435 throw new IllegalArgumentException ("Can't find EOModel: " + path); 436 } 437 return base; 438 } 439 440 443 protected InputStream openIndexStream() throws Exception { 444 return new URL (modelUrl, "index.eomodeld").openStream(); 445 } 446 447 455 protected InputStream openEntityStream(String entityName) throws Exception { 456 return new URL (modelUrl, entityName + ".plist").openStream(); 457 } 458 459 467 protected InputStream openQueryStream(String entityName) throws Exception { 468 return new URL (modelUrl, entityName + ".fspec").openStream(); 469 } 470 } 471 | Popular Tags |