Search in sources :

Example 1 with DateFormat

use of jgnash.convert.imports.DateFormat in project jgnash by ccavanaugh.

the class ImportQifAction method importQif.

private static void importQif() {
    final ResourceBundle rb = ResourceUtils.getBundle();
    final Preferences pref = Preferences.userNodeForPackage(ImportQifAction.class);
    final Logger logger = Logger.getLogger("qifimport");
    if (debug) {
        try {
            Handler fh = new FileHandler("%h/jgnash%g.log");
            fh.setFormatter(new SimpleFormatter());
            logger.addHandler(fh);
            logger.setLevel(Level.FINEST);
        } catch (IOException ioe) {
            logger.log(Level.SEVERE, "Could not install file handler", ioe);
        }
    }
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    if (engine.getRootAccount() == null) {
        StaticUIMethods.displayError(rb.getString("Message.Error.CreateBasicAccounts"));
        return;
    }
    final JFileChooser chooser = new JFileChooser(pref.get(QIFDIR, null));
    chooser.setMultiSelectionEnabled(false);
    chooser.addChoosableFileFilter(new FileNameExtensionFilter("Qif Files (*.qif)", "qif"));
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        pref.put(QIFDIR, chooser.getCurrentDirectory().getAbsolutePath());
        boolean fullFile = QifUtils.isFullFile(chooser.getSelectedFile());
        if (fullFile) {
            // prompt for date format
            final DateFormat dateFormat = getQIFDateFormat();
            class ImportFile extends SwingWorker<Void, Void> {

                @Override
                protected Void doInBackground() throws Exception {
                    UIApplication.getFrame().displayWaitMessage(rb.getString("Message.ImportWait"));
                    QifImport imp = new QifImport();
                    try {
                        imp.doFullParse(chooser.getSelectedFile(), dateFormat);
                    } catch (NoAccountException e) {
                        logger.log(Level.SEVERE, "Mistook partial qif file as a full qif file", e);
                    }
                    imp.dumpStats();
                    imp.doFullImport();
                    if (imp.getDuplicateCount() > 0) {
                        String message = imp.getDuplicateCount() + " duplicate transactions were found";
                        logger.info(message);
                    }
                    return null;
                }

                @Override
                protected void done() {
                    UIApplication.getFrame().stopWaitMessage();
                }
            }
            new ImportFile().execute();
        } else {
            final QifImport imp = new QifImport();
            if (!imp.doPartialParse(chooser.getSelectedFile())) {
                StaticUIMethods.displayError(rb.getString("Message.Error.ParseTransactions"));
                return;
            }
            imp.dumpStats();
            if (imp.getParser().accountList.isEmpty()) {
                StaticUIMethods.displayError(rb.getString("Message.Error.ParseTransactions"));
                return;
            }
            PartialDialog dlg = new PartialDialog(imp.getParser());
            DialogUtils.addBoundsListener(dlg);
            dlg.setVisible(true);
            if (dlg.isWizardValid()) {
                imp.doPartialImport(dlg.getAccount());
                if (imp.getDuplicateCount() > 0) {
                    if (YesNoDialog.showYesNoDialog(UIApplication.getFrame(), new MultiLineLabel(TextResource.getString("DupeTransImport.txt")), rb.getString("Title.DuplicateTransactionsFound"), YesNoDialog.WARNING_MESSAGE)) {
                        Transaction[] t = imp.getDuplicates();
                        for (Transaction element : t) {
                            engine.addTransaction(element);
                        }
                    }
                }
            }
        }
    }
}
Also used : SimpleFormatter(java.util.logging.SimpleFormatter) FileHandler(java.util.logging.FileHandler) Handler(java.util.logging.Handler) PartialDialog(jgnash.ui.wizards.imports.qif.PartialDialog) IOException(java.io.IOException) Logger(java.util.logging.Logger) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) FileHandler(java.util.logging.FileHandler) NoAccountException(jgnash.convert.imports.qif.NoAccountException) JFileChooser(javax.swing.JFileChooser) Transaction(jgnash.engine.Transaction) DateFormat(jgnash.convert.imports.DateFormat) QifImport(jgnash.convert.imports.qif.QifImport) SwingWorker(javax.swing.SwingWorker) ResourceBundle(java.util.ResourceBundle) Preferences(java.util.prefs.Preferences) Engine(jgnash.engine.Engine) MultiLineLabel(jgnash.ui.components.MultiLineLabel)

Example 2 with DateFormat

use of jgnash.convert.imports.DateFormat in project jgnash by ccavanaugh.

the class ImportQifAction method getQIFDateFormat.

private static DateFormat getQIFDateFormat() {
    String DATE_FORMAT = "dateFormat";
    DateFormat dateFormat = DateFormat.US;
    ResourceBundle rb = ResourceUtils.getBundle();
    Preferences pref = Preferences.userNodeForPackage(ImportQifAction.class);
    /* Create the combo for date format selection */
    JComboBox<DateFormat> combo = new JComboBox<>(DateFormat.values());
    combo.setSelectedIndex(pref.getInt(DATE_FORMAT, 0));
    Object[] options = { rb.getString("Button.Ok"), rb.getString("Button.Cancel") };
    int result = JOptionPane.showOptionDialog(UIApplication.getFrame(), combo, rb.getString("Title.SelQifDateFormat"), JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]);
    if (result == JOptionPane.YES_OPTION) {
        dateFormat = (DateFormat) combo.getSelectedItem();
    }
    pref.putInt(DATE_FORMAT, combo.getSelectedIndex());
    return dateFormat;
}
Also used : JComboBox(javax.swing.JComboBox) DateFormat(jgnash.convert.imports.DateFormat) ResourceBundle(java.util.ResourceBundle) Preferences(java.util.prefs.Preferences)

Example 3 with DateFormat

use of jgnash.convert.imports.DateFormat in project jgnash by ccavanaugh.

the class QifTransaction method determineDateFormat.

public static DateFormat determineDateFormat(final Collection<QifTransaction> transactions) {
    Objects.requireNonNull(transactions);
    // US date is assumed
    DateFormat dateFormat = DateFormat.US;
    for (final QifTransaction transaction : transactions) {
        int zero;
        int one;
        //int two;
        final String[] chunks = QifTransaction.DATE_DELIMITER_PATTERN.split(transaction.oDate);
        zero = Integer.parseInt(chunks[0].trim());
        one = Integer.parseInt(chunks[1].trim());
        if (zero > 12 && one <= 12) {
            // must have a EU date format
            dateFormat = DateFormat.EU;
            break;
        }
    }
    return dateFormat;
}
Also used : DateFormat(jgnash.convert.imports.DateFormat)

Aggregations

DateFormat (jgnash.convert.imports.DateFormat)3 ResourceBundle (java.util.ResourceBundle)2 Preferences (java.util.prefs.Preferences)2 IOException (java.io.IOException)1 FileHandler (java.util.logging.FileHandler)1 Handler (java.util.logging.Handler)1 Logger (java.util.logging.Logger)1 SimpleFormatter (java.util.logging.SimpleFormatter)1 JComboBox (javax.swing.JComboBox)1 JFileChooser (javax.swing.JFileChooser)1 SwingWorker (javax.swing.SwingWorker)1 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)1 NoAccountException (jgnash.convert.imports.qif.NoAccountException)1 QifImport (jgnash.convert.imports.qif.QifImport)1 Engine (jgnash.engine.Engine)1 Transaction (jgnash.engine.Transaction)1 MultiLineLabel (jgnash.ui.components.MultiLineLabel)1 PartialDialog (jgnash.ui.wizards.imports.qif.PartialDialog)1