use of jgnash.engine.RootAccount in project jgnash by ccavanaugh.
the class JpaAccountDAO method getRootAccount.
/*
* @see jgnash.engine.AccountDAOInterface#getRootAccount()
*/
@Override
public RootAccount getRootAccount() {
RootAccount root = null;
try {
final Future<RootAccount> future = executorService.submit(() -> {
emLock.lock();
try {
final TypedQuery<RootAccount> q = em.createQuery("select a from RootAccount a", RootAccount.class);
final List<RootAccount> list = q.getResultList();
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
logger.log(Level.SEVERE, "More than one RootAccount was found: " + list.size(), new Exception());
return list.get(0);
}
return null;
} finally {
emLock.unlock();
}
});
// block and return
root = future.get();
} catch (final ExecutionException | InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
return root;
}
use of jgnash.engine.RootAccount in project jgnash by ccavanaugh.
the class AccountTreeModel method loadAccountTree.
private synchronized void loadAccountTree() {
final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
if (engine != null) {
final RootAccount r = engine.getRootAccount();
final DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(r);
setRoot(rootNode);
loadChildren(rootNode);
nodeStructureChanged(rootNode);
} else {
setRoot(null);
}
}
use of jgnash.engine.RootAccount 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.RootAccount in project jgnash by ccavanaugh.
the class NewFileFour method accountListMouseClicked.
private void accountListMouseClicked(final MouseEvent evt) {
int index = accountList.locationToIndex(evt.getPoint());
if (index > -1) {
int[] indices = accountList.getSelectedIndices();
if (Arrays.binarySearch(indices, index) > -1) {
RootAccount root = (RootAccount) accountList.getModel().getElementAt(index);
accountTree.setModel(new AccountModel(root));
expandTree();
return;
}
}
Object o = accountList.getSelectedValue();
if (o != null) {
accountTree.setModel(new AccountModel((RootAccount) o));
expandTree();
} else {
accountTree.setModel(null);
}
}
use of jgnash.engine.RootAccount in project jgnash by ccavanaugh.
the class NewFileUtility method buildNewFile.
public static void buildNewFile(final String fileName, final DataStoreType dataStoreType, final char[] password, final CurrencyNode currencyNode, final Collection<CurrencyNode> currencyNodes, final Collection<RootAccount> rootAccountCollection) throws IOException {
final ResourceBundle resources = ResourceUtils.getBundle();
// have to close the engine first
EngineFactory.closeEngine(EngineFactory.DEFAULT);
// try to delete any existing database
if (Files.exists(Paths.get(fileName))) {
if (!EngineFactory.deleteDatabase(fileName)) {
throw new IOException(ResourceUtils.getString("Message.Error.DeleteExistingFile", fileName));
}
}
// create the directory if needed
Files.createDirectories(Paths.get(fileName).getParent());
final Engine e = EngineFactory.bootLocalEngine(fileName, EngineFactory.DEFAULT, password, dataStoreType);
CurrencyNode defaultCurrency = currencyNode;
// creation of a duplicate currency
if (e.getDefaultCurrency().matches(defaultCurrency)) {
defaultCurrency = e.getDefaultCurrency();
}
// make sure a duplicate default is not added
for (final CurrencyNode node : currencyNodes) {
if (!node.matches(defaultCurrency)) {
e.addCurrency(node);
}
}
if (!defaultCurrency.equals(e.getDefaultCurrency())) {
e.setDefaultCurrency(defaultCurrency);
}
if (!rootAccountCollection.isEmpty()) {
// import account sets
for (final RootAccount root : rootAccountCollection) {
AccountTreeXMLFactory.importAccountTree(e, root);
}
} else {
// none selected, create a very basic account set
final RootAccount root = e.getRootAccount();
final Account bank = new Account(AccountType.BANK, defaultCurrency);
bank.setDescription(resources.getString("Name.BankAccounts"));
bank.setName(resources.getString("Name.BankAccounts"));
e.addAccount(root, bank);
final Account income = new Account(AccountType.INCOME, defaultCurrency);
income.setDescription(resources.getString("Name.IncomeAccounts"));
income.setName(resources.getString("Name.IncomeAccounts"));
e.addAccount(root, income);
final Account expense = new Account(AccountType.EXPENSE, defaultCurrency);
expense.setDescription(resources.getString("Name.ExpenseAccounts"));
expense.setName(resources.getString("Name.ExpenseAccounts"));
e.addAccount(root, expense);
}
}
Aggregations