KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > action > DBWizardAction


1 package org.objectstyle.cayenne.modeler.action;
2
3 import org.objectstyle.cayenne.access.DataNode;
4 import org.objectstyle.cayenne.conf.DriverDataSourceFactory;
5 import org.objectstyle.cayenne.map.DataMap;
6 import org.objectstyle.cayenne.modeler.Application;
7 import org.objectstyle.cayenne.modeler.ProjectController;
8 import org.objectstyle.cayenne.modeler.pref.DBConnectionInfo;
9 import org.objectstyle.cayenne.modeler.pref.DataNodeDefaults;
10 import org.objectstyle.cayenne.modeler.util.CayenneAction;
11 import org.objectstyle.cayenne.project.ProjectDataSource;
12
13 /**
14  * @author Andrei Adamchik
15  */

16 public abstract class DBWizardAction extends CayenneAction {
17
18     public DBWizardAction(String JavaDoc name, Application application) {
19         super(name, application);
20     }
21
22     // ==== Guessing user preferences... *****
23

24     protected DataNode getPreferredNode() {
25         ProjectController projectController = getProjectController();
26         DataNode node = projectController.getCurrentDataNode();
27
28         // try a node that belongs to the current DataMap ...
29
if (node == null) {
30             DataMap map = projectController.getCurrentDataMap();
31             if (map != null) {
32                 node = projectController.getCurrentDataDomain().lookupDataNode(map);
33             }
34         }
35
36         return node;
37     }
38
39     protected String JavaDoc preferredDataSourceLabel(DBConnectionInfo nodeInfo) {
40         if (nodeInfo == null || nodeInfo.getDomainPreference() == null) {
41
42             // only driver nodes have meaningful connection info set
43
DataNode node = getPreferredNode();
44             return (node != null && DriverDataSourceFactory.class.getName().equals(
45                     node.getDataSourceFactory())) ? "DataNode Connection Info" : null;
46         }
47
48         return nodeInfo.getKey();
49     }
50
51     /**
52      * Determines the most reasonable default DataSource choice.
53      */

54     protected DBConnectionInfo preferredDataSource() {
55         DataNode node = getPreferredNode();
56
57         // no current node...
58
if (node == null) {
59             return null;
60         }
61
62         // if node has local DS set, use it
63
DataNodeDefaults nodeDefaults = (DataNodeDefaults) getProjectController()
64                 .getPreferenceDomainForDataDomain()
65                 .getDetail(node.getName(), DataNodeDefaults.class, false);
66
67         String JavaDoc key = (nodeDefaults != null) ? nodeDefaults.getLocalDataSource() : null;
68         if (key != null) {
69             DBConnectionInfo info = (DBConnectionInfo) getApplication()
70                     .getPreferenceDomain()
71                     .getDetail(key, DBConnectionInfo.class, false);
72             if (info != null) {
73                 return info;
74             }
75         }
76
77         // extract data from the node
78
if (!DriverDataSourceFactory.class.getName().equals(node.getDataSourceFactory())) {
79             return null;
80         }
81
82         // create transient object..
83
DBConnectionInfo nodeInfo = new DBConnectionInfo();
84
85         nodeInfo.copyFrom(((ProjectDataSource) node.getDataSource()).getDataSourceInfo());
86         if (node.getAdapter() != null) {
87             nodeInfo.setDbAdapter(node.getAdapter().getClass().getName());
88         }
89
90         return nodeInfo;
91     }
92
93 }
Popular Tags