public void importFile(File file) { try { QifFile qifFile = new QifFile(file, QifDateFormat.DetermineFromFileAndSystem); if (!(account instanceof CurrencyAccount)) { // TODO: process error properly if (QIFPlugin.DEBUG) System.out.println("account is not a currency account"); throw new QifImportException("selected account is not a currency account"); } CurrencyAccount currencyAccount = (CurrencyAccount) account; /* * Create a transaction to be used to import the entries. This allows the entries to * be more efficiently written to the back-end datastore and it also groups * the entire import as a single change for undo/redo purposes. */ TransactionManager transactionManager = new TransactionManager(session.getDataManager()); Session sessionInTransaction = transactionManager.getSession(); CurrencyAccount accountInTransaction = transactionManager.getCopyInTransaction(currencyAccount); /* * Check that this QIF file is of a form that has entries only, no account information. This is the * form that is down-loaded from a banking site. */ if (!qifFile.accountList.isEmpty()) { throw new QifImportException( "QIF file contains information not typical of a bank download. It looks like the file was exported from an accounting program."); } if (!qifFile.invstTransactions.isEmpty()) { throw new QifImportException( "This QIF file has investement transactions. This is not supported for bank imports."); } /* * Import transactions that have no account information. */ if (!qifFile.transactions.isEmpty()) { // TODO: This should come from the account???? Currency currency = sessionInTransaction.getDefaultCurrency(); int transactionCount = 0; MatchingEntryFinder matchFinder = new MatchingEntryFinder() { @Override protected boolean alreadyMatched(Entry entry) { return entry.getPropertyValue(ReconciliationEntryInfo.getUniqueIdAccessor()) != null; } }; // ImportMatcher matcher = new // ImportMatcher(accountInTransaction.getExtension(PatternMatcherAccountInfo.getPropertySet(), true)); Collection<EntryData> importedEntries = new ArrayList<EntryData>(); for (QifTransaction qifTransaction : qifFile.transactions) { if (!qifTransaction.getSplits().isEmpty()) { throw new RuntimeException( "QIF file contains information not typical of a bank download. It looks like the file was exported from an accounting program."); } if (qifTransaction.getCategory() != null) { throw new RuntimeException( "When transactions are listed in the QIF file with no account information (downloaded from bank), there must not be any category information."); } Date date = convertDate(qifTransaction.getDate()); long amount = adjustAmount(qifTransaction.getAmount(), currency); String uniqueId = Long.toString(date.getTime()) + '~' + amount; Entry match = matchFinder.findMatch(currencyAccount, amount, date, qifTransaction.getCheckNumber()); if (match != null) { Entry entryInTrans = transactionManager.getCopyInTransaction(match); entryInTrans.setValuta(date); entryInTrans.setCheck(qifTransaction.getCheckNumber()); entryInTrans.setPropertyValue(ReconciliationEntryInfo.getUniqueIdAccessor(), uniqueId); } else { EntryData entryData = new EntryData(); entryData.amount = amount; entryData.check = qifTransaction.getCheckNumber(); // entryData.valueDate = date; entryData.clearedDate = date; entryData.setMemo(qifTransaction.getMemo()); entryData.setPayee(qifTransaction.getPayee()); // Entry entry = matcher.process(entryData, sessionInTransaction); // entry.setPropertyValue(ReconciliationEntryInfo.getUniqueIdAccessor(), // uniqueId); importedEntries.add(entryData); } /* do just the above. The following is obsolete. Then remove the duplicate autoMatch method. // Create a new transaction Transaction transaction = sessionInTransaction.createTransaction(); // Add the first entry for this transaction and set the account QIFEntry firstEntry = transaction.createEntry().getExtension(QIFEntryInfo.getPropertySet(), true); firstEntry.setAccount(accountInTransaction); transaction.setDate(date); firstEntry.setAmount(amount); firstEntry.setReconcilingState(qifTransaction.getStatus()); firstEntry.setCheck(qifTransaction.getCheckNumber()); firstEntry.setMemo(qifTransaction.getPayee()); // Add the second entry for this transaction Entry secondEntry = transaction.createEntry(); secondEntry.setAmount(-amount); firstEntry.setMemo(qifTransaction.getMemo()); String address = null; for (String line : qifTransaction.getAddressLines()) { if (address == null) { address = line; } else { address = address + '\n' + line; } } firstEntry.setAddress(address); if (secondEntry.getAccount() instanceof IncomeExpenseAccount) { // If this entry is for a multi-currency account, // set the currency to be the same as the currency for this // bank account. if (((IncomeExpenseAccount)secondEntry.getAccount()).isMultiCurrency()) { secondEntry.setIncomeExpenseCurrency(currency); } } */ transactionCount++; } PatternMatcherAccount matcherAccount = account.getExtension(PatternMatcherAccountInfo.getPropertySet(), true); Dialog dialog = new PatternMatchingDialog(window.getShell(), matcherAccount, importedEntries); if (dialog.open() == Dialog.OK) { ImportMatcher matcher = new ImportMatcher( accountInTransaction.getExtension( PatternMatcherAccountInfo.getPropertySet(), true)); for (net.sf.jmoney.importer.matcher.EntryData entryData : importedEntries) { Entry entry = matcher.process(entryData, sessionInTransaction); // entry.setPropertyValue(ReconciliationEntryInfo.getUniqueIdAccessor(), // uniqueId); } } else { return; } /* * All entries have been imported and all the properties * have been set and should be in a valid state, so we * can now commit the imported entries to the datastore. */ String transactionDescription = String.format("Import {0}", file.getName()); transactionManager.commit(transactionDescription); if (transactionCount != 0) { StringBuffer combined = new StringBuffer(); combined.append(file.getName()); combined.append(" was successfully imported. "); combined.append(transactionCount).append(" transactions were imported."); MessageDialog.openInformation( window.getShell(), "QIF file imported", combined.toString()); } else { throw new QifImportException("No transactions were found within the given date range."); } } } catch (IOException e) { MessageDialog.openError( window.getShell(), "Unable to read QIF file", e.getLocalizedMessage()); } catch (InvalidQifFileException e) { MessageDialog.openError( window.getShell(), "Unable to import QIF file", e.getLocalizedMessage()); } catch (QifImportException e) { MessageDialog.openError( window.getShell(), "Unable to import QIF file", e.getLocalizedMessage()); } catch (AmbiguousDateException e) { MessageDialog.openError( window.getShell(), "QIF file has an ambiguous date format that cannot be guessed from your locale.", e.getLocalizedMessage()); } }