Search in sources :

Example 1 with RootAccount

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;
}
Also used : RootAccount(jgnash.engine.RootAccount) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with RootAccount

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);
    }
}
Also used : RootAccount(jgnash.engine.RootAccount) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Engine(jgnash.engine.Engine)

Example 3 with RootAccount

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();
}
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 4 with RootAccount

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);
    }
}
Also used : RootAccount(jgnash.engine.RootAccount)

Example 5 with RootAccount

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);
    }
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) RootAccount(jgnash.engine.RootAccount) Account(jgnash.engine.Account) RootAccount(jgnash.engine.RootAccount) ResourceBundle(java.util.ResourceBundle) IOException(java.io.IOException) Engine(jgnash.engine.Engine)

Aggregations

RootAccount (jgnash.engine.RootAccount)12 Account (jgnash.engine.Account)5 Engine (jgnash.engine.Engine)5 IOException (java.io.IOException)2 ResourceBundle (java.util.ResourceBundle)2 ExecutionException (java.util.concurrent.ExecutionException)2 SwingWorker (javax.swing.SwingWorker)2 CurrencyNode (jgnash.engine.CurrencyNode)2 ChartEntity (org.jfree.chart.entity.ChartEntity)2 PieSectionEntity (org.jfree.chart.entity.PieSectionEntity)2 DefaultFormBuilder (com.jgoodies.forms.builder.DefaultFormBuilder)1 FormLayout (com.jgoodies.forms.layout.FormLayout)1 MouseEvent (java.awt.event.MouseEvent)1 File (java.io.File)1 LocalDate (java.time.LocalDate)1 List (java.util.List)1 Set (java.util.Set)1 Preferences (java.util.prefs.Preferences)1 TreeItem (javafx.scene.control.TreeItem)1 JButton (javax.swing.JButton)1