Search in sources :

Example 1 with DataStoreType

use of jgnash.engine.DataStoreType in project jgnash by ccavanaugh.

the class NewFileDialog method showDialog.

public static void showDialog(final Frame parent) {
    final class Setup extends SwingWorker<Void, Void> {

        NewFileDialog d;

        public Setup(NewFileDialog dialog) {
            d = dialog;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected Void doInBackground() throws Exception {
            final ResourceBundle rb = ResourceUtils.getBundle();
            UIApplication.getFrame().displayWaitMessage(rb.getString("Message.PleaseWait"));
            final String database = (String) d.getSetting(Settings.DATABASE_NAME);
            final Set<CurrencyNode> nodes = (Set<CurrencyNode>) d.getSetting(Settings.CURRENCIES);
            final CurrencyNode defaultCurrency = (CurrencyNode) d.getSetting(Settings.DEFAULT_CURRENCY);
            final DataStoreType type = (DataStoreType) d.getSetting(Settings.TYPE);
            final String password = (String) d.getSetting(Settings.PASSWORD);
            final List<RootAccount> accountList = (List<RootAccount>) d.getSetting(Settings.ACCOUNT_SET);
            try {
                NewFileUtility.buildNewFile(database, type, password.toCharArray(), defaultCurrency, nodes, accountList);
                // force a save and reload of the file
                EngineFactory.closeEngine(EngineFactory.DEFAULT);
                EngineFactory.bootLocalEngine(database, EngineFactory.DEFAULT, password.toCharArray());
            } catch (final IOException e) {
                StaticUIMethods.displayError(e.getMessage());
            }
            return null;
        }

        @Override
        protected void done() {
            UIApplication.getFrame().stopWaitMessage();
        }
    }
    class DisplayDialog extends SwingWorker<Set<CurrencyNode>, Object> {

        @Override
        public Set<CurrencyNode> doInBackground() {
            return DefaultCurrencies.generateCurrencies();
        }

        @Override
        protected void done() {
            try {
                NewFileDialog d = new NewFileDialog(parent);
                d.setSetting(NewFileDialog.Settings.DEFAULT_CURRENCIES, get());
                d.setSetting(NewFileDialog.Settings.DATABASE_NAME, EngineFactory.getDefaultDatabase());
                d.addTaskPage(new NewFileOne());
                d.addTaskPage(new NewFileTwo());
                d.addTaskPage(new NewFileThree());
                d.addTaskPage(new NewFileFour());
                d.addTaskPage(new NewFileSummary());
                d.setLocationRelativeTo(parent);
                d.setVisible(true);
                if (d.isWizardValid()) {
                    new Setup(d).execute();
                }
            } catch (InterruptedException | ExecutionException e) {
                Logger.getLogger(DisplayDialog.class.getName()).log(Level.SEVERE, null, e);
            }
        }
    }
    new DisplayDialog().execute();
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) Set(java.util.Set) IOException(java.io.IOException) DataStoreType(jgnash.engine.DataStoreType) RootAccount(jgnash.engine.RootAccount) SwingWorker(javax.swing.SwingWorker) ResourceBundle(java.util.ResourceBundle) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with DataStoreType

use of jgnash.engine.DataStoreType in project jgnash by ccavanaugh.

the class SaveFileAsAction method saveFileAs.

/**
     * Opens a Save as Dialog. If the extension of the destination file is different than the file currently open, then
     * an attempt is made to identify the new file format and save accordingly. Otherwise, a copy of the file is made.
     */
private static void saveFileAs() {
    final ResourceBundle rb = ResourceUtils.getBundle();
    final Preferences pref = Preferences.userNodeForPackage(SaveFileAsAction.class);
    JFileChooser chooser = new JFileChooser(pref.get(CURRENT_DIR, null));
    chooser.setMultiSelectionEnabled(false);
    chooser.setDialogTitle(rb.getString("Title.SaveAs"));
    final DataStoreType[] types = DataStoreType.values();
    final String[] ext = new String[types.length];
    for (int i = 0; i < types.length; i++) {
        ext[i] = types[i].getDataStore().getFileExt();
    }
    StringBuilder description = new StringBuilder(rb.getString("Label.jGnashFiles") + " (");
    for (int i = 0; i < types.length; i++) {
        description.append("*");
        description.append(types[i].getDataStore().getFileExt());
        if (i < types.length - 1) {
            description.append(", ");
        }
    }
    description.append(')');
    chooser.addChoosableFileFilter(new DataStoreFilter(description.toString(), ext));
    if (chooser.showSaveDialog(UIApplication.getFrame()) == JFileChooser.APPROVE_OPTION) {
        pref.put(CURRENT_DIR, chooser.getCurrentDirectory().getAbsolutePath());
        final class SaveAs extends SwingWorker<Void, Void> {

            @Override
            protected Void doInBackground() throws Exception {
                UIApplication.getFrame().displayWaitMessage(rb.getString("Message.PleaseWait"));
                EngineFactory.saveAs(chooser.getSelectedFile().getAbsolutePath());
                return null;
            }

            @Override
            protected void done() {
                UIApplication.getFrame().stopWaitMessage();
            }
        }
        new SaveAs().execute();
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) SwingWorker(javax.swing.SwingWorker) ResourceBundle(java.util.ResourceBundle) Preferences(java.util.prefs.Preferences) DataStoreType(jgnash.engine.DataStoreType)

Example 3 with DataStoreType

use of jgnash.engine.DataStoreType in project jgnash by ccavanaugh.

the class SqlUtils method useOldPersistenceUnit.

public static boolean useOldPersistenceUnit(final String fileName, final char[] password) {
    // return false only if an error occurs
    boolean result = false;
    try {
        if (Files.exists(Paths.get(fileName))) {
            if (!FileUtils.isFileLocked(fileName)) {
                final DataStoreType dataStoreType = EngineFactory.getDataStoreByType(fileName);
                final Properties properties = JpaConfiguration.getLocalProperties(dataStoreType, fileName, password, false);
                final String url = properties.getProperty(JpaConfiguration.JAVAX_PERSISTENCE_JDBC_URL);
                try (final Connection connection = DriverManager.getConnection(url)) {
                    final DatabaseMetaData metaData = connection.getMetaData();
                    try (final ResultSet resultSet = metaData.getColumns(null, null, "%", "%")) {
                        while (resultSet.next()) {
                            if (resultSet.getString(COLUMN_NAME).equals("SEQUENCE_NEXT_HI_VALUE") && resultSet.getString(TABLE_NAME).equals("HIBERNATE_SEQUENCES")) {
                                result = true;
                            }
                        }
                    }
                    // must issue a shutdown for correct file closure
                    try (final PreparedStatement statement = connection.prepareStatement("SHUTDOWN")) {
                        statement.execute();
                    }
                } catch (final SQLException e) {
                    logger.log(Level.SEVERE, e.getMessage(), e);
                }
            } else {
                logger.severe("File was locked");
            }
        }
    } catch (final IOException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) Properties(java.util.Properties) DatabaseMetaData(java.sql.DatabaseMetaData) DataStoreType(jgnash.engine.DataStoreType)

Example 4 with DataStoreType

use of jgnash.engine.DataStoreType in project jgnash by ccavanaugh.

the class SqlUtils method getTableAndColumnNames.

/**
     * Diagnostic utility method to dump a list of table names and columns to the console.
     * Assumes the file is not password protected.
     *
     * @param fileName name of file to open
     * @return a {@code Set} of strings with the table names and columns, comma separated
     */
public static Set<String> getTableAndColumnNames(final String fileName) {
    final Set<String> tableNames = new TreeSet<>();
    try {
        if (!FileUtils.isFileLocked(fileName)) {
            final DataStoreType dataStoreType = EngineFactory.getDataStoreByType(fileName);
            final Properties properties = JpaConfiguration.getLocalProperties(dataStoreType, fileName, EngineFactory.EMPTY_PASSWORD, true);
            final String url = properties.getProperty(JpaConfiguration.JAVAX_PERSISTENCE_JDBC_URL);
            try (final Connection connection = DriverManager.getConnection(url)) {
                final DatabaseMetaData metaData = connection.getMetaData();
                try (final ResultSet resultSet = metaData.getColumns(null, null, "%", "%")) {
                    while (resultSet.next()) {
                        tableNames.add(resultSet.getString(TABLE_NAME).toUpperCase(Locale.ROOT) + "," + resultSet.getString(COLUMN_NAME).toUpperCase(Locale.ROOT));
                    }
                }
                // must issue a shutdown for correct file closure
                try (final PreparedStatement statement = connection.prepareStatement("SHUTDOWN")) {
                    statement.execute();
                }
            } catch (final SQLException e) {
                logger.log(Level.SEVERE, e.getMessage(), e);
            }
        } else {
            logger.severe("File was locked");
        }
    } catch (final IOException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    }
    return tableNames;
}
Also used : SQLException(java.sql.SQLException) TreeSet(java.util.TreeSet) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) Properties(java.util.Properties) DatabaseMetaData(java.sql.DatabaseMetaData) DataStoreType(jgnash.engine.DataStoreType)

Example 5 with DataStoreType

use of jgnash.engine.DataStoreType in project jgnash by ccavanaugh.

the class NewFileOneController method getSettings.

@Override
public void getSettings(final Map<NewFileWizard.Settings, Object> map) {
    DataStoreType type = (DataStoreType) map.get(NewFileWizard.Settings.TYPE);
    if (type != null) {
        storageTypeComboBox.setValue(type);
    }
    final String fileName = (String) map.get(NewFileWizard.Settings.DATABASE_NAME);
    if (FileUtils.fileHasExtension(fileName)) {
        fileNameField.setText(fileName);
    } else {
        fileNameField.setText(fileName + storageTypeComboBox.getValue().getDataStore().getFileExt());
    }
    updateDescriptor();
}
Also used : DataStoreType(jgnash.engine.DataStoreType)

Aggregations

DataStoreType (jgnash.engine.DataStoreType)12 IOException (java.io.IOException)6 SQLException (java.sql.SQLException)5 Properties (java.util.Properties)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)3 ResourceBundle (java.util.ResourceBundle)3 DatabaseMetaData (java.sql.DatabaseMetaData)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 Preferences (java.util.prefs.Preferences)2 SwingWorker (javax.swing.SwingWorker)2 File (java.io.File)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Statement (java.sql.Statement)1 Set (java.util.Set)1