1 56 57 package org.objectstyle.cayenne.modeler.action; 58 59 import java.awt.Component ; 60 import java.awt.HeadlessException ; 61 import java.awt.event.ActionEvent ; 62 import java.io.File ; 63 import java.util.ArrayList ; 64 import java.util.Collection ; 65 import java.util.Iterator ; 66 import java.util.Map ; 67 68 import javax.swing.JDialog ; 69 import javax.swing.JFileChooser ; 70 import javax.swing.filechooser.FileFilter ; 71 72 import org.apache.commons.collections.CollectionUtils; 73 import org.apache.log4j.Level; 74 import org.apache.log4j.Logger; 75 import org.objectstyle.cayenne.access.DataDomain; 76 import org.objectstyle.cayenne.access.DataNode; 77 import org.objectstyle.cayenne.conf.DriverDataSourceFactory; 78 import org.objectstyle.cayenne.conf.JNDIDataSourceFactory; 79 import org.objectstyle.cayenne.conn.DataSourceInfo; 80 import org.objectstyle.cayenne.dba.DbAdapter; 81 import org.objectstyle.cayenne.map.DataMap; 82 import org.objectstyle.cayenne.map.Entity; 83 import org.objectstyle.cayenne.map.event.DataNodeEvent; 84 import org.objectstyle.cayenne.map.event.EntityEvent; 85 import org.objectstyle.cayenne.query.Query; 86 import org.objectstyle.cayenne.map.event.QueryEvent; 87 import org.objectstyle.cayenne.modeler.AdapterMapping; 88 import org.objectstyle.cayenne.modeler.Application; 89 import org.objectstyle.cayenne.modeler.ProjectController; 90 import org.objectstyle.cayenne.modeler.dialog.ErrorDebugDialog; 91 import org.objectstyle.cayenne.modeler.event.DataMapDisplayEvent; 92 import org.objectstyle.cayenne.modeler.event.DataNodeDisplayEvent; 93 import org.objectstyle.cayenne.modeler.pref.FSPath; 94 import org.objectstyle.cayenne.modeler.util.CayenneAction; 95 import org.objectstyle.cayenne.modeler.util.FileFilters; 96 import org.objectstyle.cayenne.project.NamedObjectFactory; 97 import org.objectstyle.cayenne.project.ProjectDataSource; 98 import org.objectstyle.cayenne.project.ProjectPath; 99 import org.objectstyle.cayenne.wocompat.EOModelProcessor; 100 101 106 public class ImportEOModelAction extends CayenneAction { 107 108 private static final Logger logObj = Logger.getLogger(ImportEOModelAction.class); 109 110 public static String getActionName() { 111 return "Import EOModel"; 112 } 113 114 protected JFileChooser eoModelChooser; 115 116 public ImportEOModelAction(Application application) { 117 super(getActionName(), application); 118 } 119 120 public String getIconName() { 121 return "icon-eomodel.gif"; 122 } 123 124 public void performAction(ActionEvent event) { 125 importEOModel(); 126 } 127 128 131 protected void importEOModel() { 132 JFileChooser fileChooser = getEOModelChooser(); 133 int status = fileChooser.showOpenDialog(Application.getFrame()); 134 135 if (status == JFileChooser.APPROVE_OPTION) { 136 137 FSPath lastDir = getApplication() 139 .getFrameController() 140 .getLastEOModelDirectory(); 141 lastDir.updateFromChooser(fileChooser); 142 143 File file = fileChooser.getSelectedFile(); 144 if (file.isFile()) { 145 file = file.getParentFile(); 146 } 147 148 DataMap currentMap = getProjectController().getCurrentDataMap(); 149 150 try { 151 String path = file.getCanonicalPath(); 152 153 EOModelProcessor processor = new EOModelProcessor(); 154 155 if (currentMap == null) { 157 loadDataNode(processor.loadModeIndex(path)); 158 } 159 160 DataMap map = processor.loadEOModel(path); 162 addDataMap(map, currentMap); 163 164 } 165 catch (Exception ex) { 166 logObj.log(Level.INFO, "EOModel Loading Exception", ex); 167 ErrorDebugDialog.guiException(ex); 168 } 169 170 } 171 } 172 173 protected void loadDataNode(Map eomodelIndex) { 174 178 String adapter = (String ) eomodelIndex.get("adaptorName"); 179 Map connection = (Map ) eomodelIndex.get("connectionDictionary"); 180 181 if (adapter != null && connection != null) { 182 CreateNodeAction nodeBuilder = (CreateNodeAction) getApplication().getAction( 183 CreateNodeAction.getActionName()); 184 185 DataNode node = nodeBuilder.buildDataNode(); 188 189 if ("JNDI".equalsIgnoreCase(adapter)) { 191 node.setDataSourceFactory(JNDIDataSourceFactory.class.getName()); 192 node.setDataSourceLocation((String ) connection.get("serverUrl")); 193 } 194 else { 195 AdapterMapping adapterDefaults = getApplication().getAdapterMapping(); 197 String cayenneAdapter = adapterDefaults.adapterForEOFPluginOrDriver( 198 (String ) connection.get("plugin"), 199 (String ) connection.get("driver")); 200 if (cayenneAdapter != null) { 201 try { 202 Class adapterClass = getApplication() 203 .getClassLoadingService() 204 .loadClass(cayenneAdapter); 205 node.setAdapter((DbAdapter) adapterClass.newInstance()); 206 } 207 catch (Throwable ex) { 208 } 210 } 211 212 node.setDataSourceFactory(DriverDataSourceFactory.class.getName()); 213 214 DataSourceInfo dsi = ((ProjectDataSource) node.getDataSource()) 215 .getDataSourceInfo(); 216 217 218 219 dsi.setDataSourceUrl(keyAsString(connection, "URL")); 220 dsi.setJdbcDriver(keyAsString(connection, "driver")); 221 dsi.setPassword(keyAsString(connection, "password")); 222 dsi.setUserName(keyAsString(connection, "username")); 223 } 224 225 getProjectController().fireDataNodeEvent( 227 new DataNodeEvent(this, node, DataNodeEvent.ADD)); 228 getProjectController().fireDataNodeDisplayEvent( 229 new DataNodeDisplayEvent(this, getProjectController() 230 .getCurrentDataDomain(), node)); 231 } 232 } 233 234 private String keyAsString(Map map, String key) { 237 Object value = map.get(key); 238 return (value != null) ? value.toString() : null; 239 } 240 241 244 public boolean enableForPath(ProjectPath path) { 245 if (path == null) { 246 return false; 247 } 248 249 return path.firstInstanceOf(DataDomain.class) != null; 250 } 251 252 255 protected void addDataMap(DataMap map, DataMap currentMap) { 256 257 ProjectController mediator = getProjectController(); 258 259 if (currentMap != null) { 260 263 Collection originalOE = new ArrayList (currentMap.getObjEntities()); 264 Collection originalDE = new ArrayList (currentMap.getDbEntities()); 265 Collection originalQueries = new ArrayList (currentMap.getQueries()); 266 267 currentMap.mergeWithDataMap(map); 268 map = currentMap; 269 270 Collection newOE = new ArrayList (currentMap.getObjEntities()); 272 Collection newDE = new ArrayList (currentMap.getDbEntities()); 273 Collection newQueries = new ArrayList (currentMap.getQueries()); 274 275 EntityEvent entityEvent = new EntityEvent(Application.getFrame(), null); 276 QueryEvent queryEvent = new QueryEvent(Application.getFrame(), null); 277 278 Collection addedOE = CollectionUtils.subtract(newOE, originalOE); 279 Iterator it = addedOE.iterator(); 280 while (it.hasNext()) { 281 Entity e = (Entity) it.next(); 282 entityEvent.setEntity(e); 283 entityEvent.setId(EntityEvent.ADD); 284 mediator.fireObjEntityEvent(entityEvent); 285 } 286 287 Collection removedOE = CollectionUtils.subtract(originalOE, newOE); 288 it = removedOE.iterator(); 289 while (it.hasNext()) { 290 Entity e = (Entity) it.next(); 291 entityEvent.setEntity(e); 292 entityEvent.setId(EntityEvent.REMOVE); 293 mediator.fireObjEntityEvent(entityEvent); 294 } 295 296 Collection addedDE = CollectionUtils.subtract(newDE, originalDE); 297 it = addedDE.iterator(); 298 while (it.hasNext()) { 299 Entity e = (Entity) it.next(); 300 entityEvent.setEntity(e); 301 entityEvent.setId(EntityEvent.ADD); 302 mediator.fireDbEntityEvent(entityEvent); 303 } 304 305 Collection removedDE = CollectionUtils.subtract(originalDE, newDE); 306 it = removedDE.iterator(); 307 while (it.hasNext()) { 308 Entity e = (Entity) it.next(); 309 entityEvent.setEntity(e); 310 entityEvent.setId(EntityEvent.REMOVE); 311 mediator.fireDbEntityEvent(entityEvent); 312 } 313 314 Collection addedQueries = CollectionUtils.subtract(newQueries, originalQueries); 316 it = addedQueries.iterator(); 317 while (it.hasNext()) { 318 Query q = (Query) it.next(); 319 queryEvent.setQuery(q); 320 queryEvent.setId(QueryEvent.ADD); 321 mediator.fireQueryEvent(queryEvent); 322 } 323 324 Collection removedQueries = CollectionUtils.subtract(originalQueries, newQueries); 325 it = removedQueries.iterator(); 326 while (it.hasNext()) { 327 Query q = (Query) it.next(); 328 queryEvent.setQuery(q); 329 queryEvent.setId(QueryEvent.REMOVE); 330 mediator.fireQueryEvent(queryEvent); 331 } 332 333 mediator.fireDataMapDisplayEvent(new DataMapDisplayEvent(Application 334 .getFrame(), map, mediator.getCurrentDataDomain(), mediator 335 .getCurrentDataNode())); 336 } 337 else { 338 DataDomain domain = mediator.getCurrentDataDomain(); 340 map.setName(NamedObjectFactory.createName(DataMap.class, domain, map 341 .getName())); 342 343 mediator.addDataMap(Application.getFrame(), map); 346 } 347 } 348 349 352 public JFileChooser getEOModelChooser() { 353 354 if (eoModelChooser == null) { 355 eoModelChooser = new EOModelChooser("Select EOModel"); 356 } 357 358 FSPath lastDir = getApplication().getFrameController().getLastEOModelDirectory(); 359 lastDir.updateChooser(eoModelChooser); 360 361 return eoModelChooser; 362 } 363 364 367 class EOModelChooser extends JFileChooser { 368 369 protected FileFilter selectFilter; 370 protected JDialog cachedDialog; 371 372 public EOModelChooser(String title) { 373 super.setFileFilter(FileFilters.getEOModelFilter()); 374 super.setDialogTitle(title); 375 super.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 376 377 this.selectFilter = FileFilters.getEOModelSelectFilter(); 378 } 379 380 public int showOpenDialog(Component parent) { 381 int status = super.showOpenDialog(parent); 382 if (status != JFileChooser.APPROVE_OPTION) { 383 cachedDialog = null; 384 return status; 385 } 386 387 File file = this.getSelectedFile(); 389 if (selectFilter.accept(file)) { 390 cachedDialog = null; 391 return JFileChooser.APPROVE_OPTION; 392 } 393 else { 394 if (file.isDirectory()) { 395 this.setCurrentDirectory(file); 396 } 397 398 return this.showOpenDialog(parent); 399 } 400 } 401 402 protected JDialog createDialog(Component parent) throws HeadlessException { 403 404 if (cachedDialog == null) { 405 cachedDialog = super.createDialog(parent); 406 } 407 return cachedDialog; 408 } 409 } 410 } | Popular Tags |