1 56 package org.objectstyle.cayenne.project; 57 58 import java.io.File ; 59 import java.io.FileInputStream ; 60 import java.io.IOException ; 61 import java.util.ArrayList ; 62 import java.util.HashMap ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 import java.util.Map ; 66 67 import org.apache.commons.lang.Validate; 68 import org.objectstyle.cayenne.conf.ConfigLoader; 69 import org.objectstyle.cayenne.conf.ConfigLoaderDelegate; 70 import org.objectstyle.cayenne.conf.ConfigSaverDelegate; 71 import org.objectstyle.cayenne.conf.ConfigStatus; 72 import org.objectstyle.cayenne.conf.DriverDataSourceFactory; 73 import org.objectstyle.cayenne.conf.JNDIDataSourceFactory; 74 75 82 public class PartialProject extends Project { 83 protected String projectVersion; 84 protected Map domains; 85 protected ConfigLoaderDelegate loadDelegate; 86 protected Map dataViewLocations; 87 88 92 public PartialProject(File projectFile) { 93 super(projectFile); 94 } 95 96 99 public void upgrade() throws ProjectException { 100 throw new ProjectException("'PartialProject' does not support upgrades."); 102 } 103 104 109 public void updateNodes(List list) throws ProjectException { 110 Iterator it = list.iterator(); 111 while (it.hasNext()) { 112 DataNodeConfigInfo nodeConfig = (DataNodeConfigInfo) it.next(); 113 String domainName = nodeConfig.getDomain(); 114 if (domainName == null && domains.size() != 1) { 115 throw new IllegalArgumentException ("Node must have domain set explicitly if there is no default domain."); 116 } 117 118 if (domainName == null) { 119 domainName = ((DomainMetaData) domains.values().toArray()[0]).name; 120 } 121 122 NodeMetaData node = findNode(domainName, nodeConfig.getName(), false); 123 if (node == null) { 124 continue; 125 } 126 127 if (nodeConfig.getAdapter() != null) { 128 node.adapter = nodeConfig.getAdapter(); 129 } 130 131 if (nodeConfig.getDataSource() != null) { 132 node.dataSource = nodeConfig.getDataSource(); 133 node.factory = JNDIDataSourceFactory.class.getName(); 134 } 135 else if (nodeConfig.getDriverFile() != null) { 136 node.dataSource = node.name + DataNodeFile.LOCATION_SUFFIX; 137 node.factory = DriverDataSourceFactory.class.getName(); 138 } 139 } 140 } 141 142 protected void prepareSave(List filesToSave, List wrappedObjects) 143 throws ProjectException { 144 filesToSave.addAll(files); 145 } 146 147 protected void postInitialize(File projectFile) { 148 loadDelegate = new LoadDelegate(); 149 domains = new HashMap (); 150 151 try { 152 FileInputStream in = new FileInputStream (projectFile); 153 try { 154 new ConfigLoader(loadDelegate).loadDomains(in); 155 } 156 catch (Exception ex) { 157 throw new ProjectException("Error creating PartialProject.", ex); 158 } 159 finally { 160 in.close(); 161 } 162 } 163 catch (IOException ioex) { 164 throw new ProjectException("Error creating PartialProject.", ioex); 165 } 166 167 super.postInitialize(projectFile); 168 } 169 170 public List getChildren() { 171 return new ArrayList (domains.values()); 172 } 173 174 public void checkForUpgrades() { 175 } 177 178 181 public List buildFileList() { 182 List list = new ArrayList (); 183 list.add(projectFileForObject(this)); 184 return list; 185 } 186 187 190 public ConfigStatus getLoadStatus() { 191 return loadDelegate.getStatus(); 192 } 193 194 public ProjectFile projectFileForObject(Object obj) { 195 if (obj != this) { 196 return null; 197 } 198 199 ApplicationProjectFile projectFile = new ApplicationProjectFile(this); 200 projectFile.setSaveDelegate(new SaveDelegate()); 201 return projectFile; 202 } 203 204 private DomainMetaData findDomain(String domainName) { 205 DomainMetaData domain = (DomainMetaData) domains.get(domainName); 206 if (domain == null) { 207 throw new IllegalArgumentException ("Can't find domain: " + domainName); 208 } 209 210 return domain; 211 } 212 213 private NodeMetaData findNode( 214 String domainName, 215 String nodeName, 216 boolean failIfNotFound) { 217 DomainMetaData domain = findDomain(domainName); 218 NodeMetaData node = (NodeMetaData) domain.nodes.get(nodeName); 219 if (node == null && failIfNotFound) { 220 throw new IllegalArgumentException ( 221 "Can't find node: " + domainName + "." + nodeName); 222 } 223 224 return node; 225 } 226 227 protected class DomainMetaData { 228 protected String name; 229 protected Map nodes = new HashMap (); 230 protected Map maps = new HashMap (); 231 protected Map mapDependencies = new HashMap (); 232 protected Map properties = new HashMap (); 233 234 public DomainMetaData(String name) { 235 this.name = name; 236 } 237 } 238 239 protected class NodeMetaData { 240 protected String name; 241 protected String dataSource; 242 protected String adapter; 243 protected String factory; 244 protected List maps = new ArrayList (); 245 246 public NodeMetaData(String name) { 247 this.name = name; 248 } 249 } 250 251 protected class MapMetaData { 252 protected String name; 253 protected String location; 254 255 public MapMetaData(String name) { 256 this.name = name; 257 } 258 } 259 260 class LoadDelegate implements ConfigLoaderDelegate { 261 protected ConfigStatus status = new ConfigStatus(); 262 263 public void shouldLoadProjectVersion(String version) { 264 PartialProject.this.projectVersion = version; 265 } 266 267 protected DomainMetaData findDomain(String name) { 268 DomainMetaData domain = (DomainMetaData) domains.get(name); 269 if (domain == null) { 270 throw new ProjectException("Can't find domain: " + name); 271 } 272 273 return domain; 274 } 275 276 protected MapMetaData findMap(String domainName, String mapName) { 277 DomainMetaData domain = findDomain(domainName); 278 MapMetaData map = (MapMetaData) domain.maps.get(mapName); 279 if (map == null) { 280 throw new ProjectException("Can't find map: " + mapName); 281 } 282 283 return map; 284 } 285 286 protected NodeMetaData findNode(String domainName, String nodeName) { 287 DomainMetaData domain = findDomain(domainName); 288 NodeMetaData node = (NodeMetaData) domain.nodes.get(nodeName); 289 if (node == null) { 290 throw new ProjectException("Can't find node: " + nodeName); 291 } 292 293 return node; 294 } 295 296 public void startedLoading() { 297 domains.clear(); 298 } 299 300 public void finishedLoading() { 301 } 302 303 public ConfigStatus getStatus() { 304 return status; 305 } 306 307 public boolean loadError(Throwable th) { 308 status.getOtherFailures().add(th.getMessage()); 309 return false; 310 } 311 312 public void shouldLinkDataMap( 313 String domainName, 314 String nodeName, 315 String mapName) { 316 findNode(domainName, nodeName).maps.add(mapName); 317 } 318 319 public void shouldLoadDataDomain(String name) { 320 domains.put(name, new DomainMetaData(name)); 321 } 322 323 public void shouldLoadDataDomainProperties(String domainName, Map properties) { 324 if (properties == null || properties.isEmpty()) { 325 return; 326 } 327 328 DomainMetaData domain = findDomain(domainName); 329 domain.properties.putAll(properties); 330 } 331 332 public void shouldLoadDataMaps(String domainName, Map locations) { 333 if (locations.size() == 0) { 334 return; 335 } 336 337 DomainMetaData domain = findDomain(domainName); 338 339 Iterator it = locations.keySet().iterator(); 341 while (it.hasNext()) { 342 String name = (String ) it.next(); 343 MapMetaData map = new MapMetaData(name); 344 map.location = (String ) locations.get(name); 345 domain.maps.put(name, map); 346 } 347 } 348 349 public void shouldLoadDataNode( 350 String domainName, 351 String nodeName, 352 String dataSource, 353 String adapter, 354 String factory) { 355 356 NodeMetaData node = new NodeMetaData(nodeName); 357 node.adapter = adapter; 358 node.factory = factory; 359 node.dataSource = dataSource; 360 findDomain(domainName).nodes.put(nodeName, node); 361 } 362 363 public void shouldRegisterDataView( 364 String dataViewName, 365 String dataViewLocation) { 366 Validate.notNull(dataViewName); 367 Validate.notNull(dataViewLocation); 368 if (dataViewLocations == null) { 369 dataViewLocations = new HashMap (); 370 } 371 dataViewLocations.put(dataViewName, dataViewLocation); 372 } 373 } 374 375 class SaveDelegate implements ConfigSaverDelegate { 376 379 public String projectVersion() { 380 return projectVersion; 381 } 382 383 public Iterator domainNames() { 384 return domains.keySet().iterator(); 385 } 386 387 public Iterator viewNames() { 388 if (dataViewLocations == null) { 389 dataViewLocations = new HashMap (); 390 } 391 return dataViewLocations.keySet().iterator(); 392 } 393 394 public String viewLocation(String dataViewName) { 395 if (dataViewLocations == null) { 396 dataViewLocations = new HashMap (); 397 } 398 return (String ) dataViewLocations.get(dataViewName); 399 } 400 401 public Iterator propertyNames(String domainName) { 402 return findDomain(domainName).properties.keySet().iterator(); 403 } 404 405 public String propertyValue(String domainName, String propertyName) { 406 return (String ) findDomain(domainName).properties.get(propertyName); 407 } 408 409 public Iterator linkedMapNames(String domainName, String nodeName) { 410 return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)) 411 .maps 412 .iterator(); 413 } 414 415 public String mapLocation(String domainName, String mapName) { 416 return ((MapMetaData) findDomain(domainName).maps.get(mapName)).location; 417 } 418 419 public Iterator mapNames(String domainName) { 420 return findDomain(domainName).maps.keySet().iterator(); 421 } 422 423 public String nodeAdapterName(String domainName, String nodeName) { 424 return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)).adapter; 425 } 426 427 public String nodeDataSourceName(String domainName, String nodeName) { 428 return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)).dataSource; 429 } 430 431 public String nodeFactoryName(String domainName, String nodeName) { 432 return ((NodeMetaData) findDomain(domainName).nodes.get(nodeName)).factory; 433 } 434 435 public Iterator nodeNames(String domainName) { 436 return findDomain(domainName).nodes.keySet().iterator(); 437 } 438 } 439 } 440 | Popular Tags |