Search in sources :

Example 1 with DBConnectionInfo

use of org.apache.cayenne.modeler.pref.DBConnectionInfo in project cayenne by apache.

the class DataSourceDuplicator method createDataSource.

protected DBConnectionInfo createDataSource() {
    if (canceled) {
        return null;
    }
    DBConnectionInfo prototype = (DBConnectionInfo) dataSources.get(prototypeKey);
    DBConnectionInfo dataSource = (DBConnectionInfo) getApplication().getCayenneProjectPreferences().getDetailObject(DBConnectionInfo.class).create(getName());
    prototype.copyTo(dataSource);
    return dataSource;
}
Also used : DBConnectionInfo(org.apache.cayenne.modeler.pref.DBConnectionInfo)

Example 2 with DBConnectionInfo

use of org.apache.cayenne.modeler.pref.DBConnectionInfo in project cayenne by apache.

the class DataSourcePreferences method duplicateDataSourceAction.

/**
 * Shows a dialog to duplicate an existing local DataSource configuration.
 */
public void duplicateDataSourceAction() {
    Object selected = view.getDataSources().getSelectedItem();
    if (selected != null) {
        DataSourceDuplicator wizard = new DataSourceDuplicator(this, selected.toString());
        DBConnectionInfo dataSource = wizard.startupAction();
        if (dataSource != null) {
            dataSourcePreferences.create(wizard.getName(), dataSource);
            dataSources = dataSourcePreferences.getChildrenPreferences();
            Object[] keys = dataSources.keySet().toArray();
            Arrays.sort(keys);
            view.getDataSources().setModel(new DefaultComboBoxModel(keys));
            view.getDataSources().setSelectedItem(wizard.getName());
            editDataSourceAction();
            fireEvent(wizard.getName(), MapEvent.ADD);
        }
    }
}
Also used : DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) DBConnectionInfo(org.apache.cayenne.modeler.pref.DBConnectionInfo)

Example 3 with DBConnectionInfo

use of org.apache.cayenne.modeler.pref.DBConnectionInfo in project cayenne by apache.

the class DataSourcePreferences method testDataSourceAction.

/**
 * Tries to establish a DB connection, reporting the status of this
 * operation.
 */
public void testDataSourceAction() {
    DBConnectionInfo currentDataSource = getConnectionInfo();
    if (currentDataSource == null) {
        return;
    }
    if (currentDataSource.getJdbcDriver() == null) {
        JOptionPane.showMessageDialog(null, "No JDBC Driver specified", "Warning", JOptionPane.WARNING_MESSAGE);
        return;
    }
    if (currentDataSource.getUrl() == null) {
        JOptionPane.showMessageDialog(null, "No Database URL specified", "Warning", JOptionPane.WARNING_MESSAGE);
        return;
    }
    try {
        FileClassLoadingService classLoader = new FileClassLoadingService();
        List<File> oldPathFiles = ((FileClassLoadingService) getApplication().getClassLoadingService()).getPathFiles();
        Collection<String> details = new ArrayList<>();
        for (File oldPathFile : oldPathFiles) {
            details.add(oldPathFile.getAbsolutePath());
        }
        Preferences classPathPreferences = getApplication().getPreferencesNode(ClasspathPreferences.class, "");
        if (editor.getChangedPreferences().containsKey(classPathPreferences)) {
            Map<String, String> map = editor.getChangedPreferences().get(classPathPreferences);
            for (Map.Entry<String, String> en : map.entrySet()) {
                String key = en.getKey();
                if (!details.contains(key)) {
                    details.add(key);
                }
            }
        }
        if (editor.getRemovedPreferences().containsKey(classPathPreferences)) {
            Map<String, String> map = editor.getRemovedPreferences().get(classPathPreferences);
            for (Map.Entry<String, String> en : map.entrySet()) {
                String key = en.getKey();
                if (details.contains(key)) {
                    details.remove(key);
                }
            }
        }
        if (details.size() > 0) {
            classLoader.setPathFiles(details.stream().map(File::new).collect(Collectors.toList()));
        }
        Class<Driver> driverClass = classLoader.loadClass(Driver.class, currentDataSource.getJdbcDriver());
        Driver driver = driverClass.newInstance();
        // connect via Cayenne DriverDataSource - it addresses some driver
        // issues...
        // can't use try with resource here as we can loose meaningful exception
        Connection c = new DriverDataSource(driver, currentDataSource.getUrl(), currentDataSource.getUserName(), currentDataSource.getPassword()).getConnection();
        try {
            c.close();
        } catch (SQLException ignored) {
        // i guess we can ignore this...
        }
        JOptionPane.showMessageDialog(null, "Connected Successfully", "Success", JOptionPane.INFORMATION_MESSAGE);
    } catch (Throwable th) {
        th = Util.unwindException(th);
        String message = "Error connecting to DB: " + th.getLocalizedMessage();
        StringTokenizer st = new StringTokenizer(message);
        StringBuilder sbMessage = new StringBuilder();
        int len = 0;
        String tempString;
        while (st.hasMoreTokens()) {
            tempString = st.nextElement().toString();
            if (len < 110) {
                len = len + tempString.length() + 1;
            } else {
                sbMessage.append("\n");
                len = 0;
            }
            sbMessage.append(tempString).append(" ");
        }
        JOptionPane.showMessageDialog(null, sbMessage.toString(), "Warning", JOptionPane.WARNING_MESSAGE);
    }
}
Also used : DriverDataSource(org.apache.cayenne.datasource.DriverDataSource) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) Driver(java.sql.Driver) DBConnectionInfo(org.apache.cayenne.modeler.pref.DBConnectionInfo) StringTokenizer(java.util.StringTokenizer) Preferences(java.util.prefs.Preferences) File(java.io.File) Map(java.util.Map) FileClassLoadingService(org.apache.cayenne.modeler.FileClassLoadingService)

Example 4 with DBConnectionInfo

use of org.apache.cayenne.modeler.pref.DBConnectionInfo in project cayenne by apache.

the class DataSourceWizard method okAction.

/**
 * Tests that the entered information is valid and can be used to open a
 * conneciton. Does not store the open connection.
 */
public void okAction() {
    DBConnectionInfo info = getConnectionInfo();
    ClassLoadingService classLoader = getApplication().getClassLoadingService();
    // doing connection testing...
    try {
        this.adapter = info.makeAdapter(classLoader);
        this.dataSource = info.makeDataSource(classLoader);
        try (Connection connection = dataSource.getConnection()) {
        } catch (SQLException ignore) {
        }
    } catch (Throwable th) {
        reportError("Connection Error", th);
        return;
    }
    onClose(false);
}
Also used : ClassLoadingService(org.apache.cayenne.modeler.ClassLoadingService) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DBConnectionInfo(org.apache.cayenne.modeler.pref.DBConnectionInfo)

Example 5 with DBConnectionInfo

use of org.apache.cayenne.modeler.pref.DBConnectionInfo in project cayenne by apache.

the class JDBCDataSourceEditor method syncDataSourceAction.

/**
 * This action is called whenever the password location is changed
 * in the GUI pulldown.  It changes labels and editability of the
 * password fields depending on the option that was selected.
 */
public void syncDataSourceAction() {
    CayenneModelerController mainController = getApplication().getFrameController();
    if (getNode() == null || getNode().getDataSourceDescriptor() == null) {
        return;
    }
    DataSourceInfo projectDSI = getNode().getDataSourceDescriptor();
    ProjectController parent = (ProjectController) getParent();
    String key = parent.getDataNodePreferences().getLocalDataSource();
    if (key == null) {
        mainController.updateStatus("No Local DataSource selected for node...");
        return;
    }
    DBConnectionInfo dataSource = (DBConnectionInfo) getApplication().getCayenneProjectPreferences().getDetailObject(DBConnectionInfo.class).getObject(key);
    if (dataSource != null) {
        if (dataSource.copyTo(projectDSI)) {
            refreshView();
            super.nodeChangeProcessor.modelUpdated(null, null, null);
            mainController.updateStatus(null);
        } else {
            mainController.updateStatus("DataNode is up to date...");
        }
    } else {
        mainController.updateStatus("Invalid Local DataSource selected for node...");
    }
}
Also used : DataSourceInfo(org.apache.cayenne.conn.DataSourceInfo) CayenneModelerController(org.apache.cayenne.modeler.CayenneModelerController) ProjectController(org.apache.cayenne.modeler.ProjectController) DBConnectionInfo(org.apache.cayenne.modeler.pref.DBConnectionInfo)

Aggregations

DBConnectionInfo (org.apache.cayenne.modeler.pref.DBConnectionInfo)10 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 DefaultComboBoxModel (javax.swing.DefaultComboBoxModel)2 File (java.io.File)1 Driver (java.sql.Driver)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 Preferences (java.util.prefs.Preferences)1 DataSourceInfo (org.apache.cayenne.conn.DataSourceInfo)1 DriverDataSource (org.apache.cayenne.datasource.DriverDataSource)1 Catalog (org.apache.cayenne.dbsync.reverse.dbimport.Catalog)1 DbImportConfiguration (org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfiguration)1 ExcludeTable (org.apache.cayenne.dbsync.reverse.dbimport.ExcludeTable)1 IncludeProcedure (org.apache.cayenne.dbsync.reverse.dbimport.IncludeProcedure)1 IncludeTable (org.apache.cayenne.dbsync.reverse.dbimport.IncludeTable)1 ReverseEngineering (org.apache.cayenne.dbsync.reverse.dbimport.ReverseEngineering)1 Schema (org.apache.cayenne.dbsync.reverse.dbimport.Schema)1 DbLoaderDelegate (org.apache.cayenne.dbsync.reverse.dbload.DbLoaderDelegate)1