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();
}
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();
}
}
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;
}
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;
}
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();
}
Aggregations