use of jgnash.convert.importat.ImportTransaction in project jgnash by ccavanaugh.
the class OfxImport method importTransactions.
public static void importTransactions(final OfxBank ofxBank, final Account baseAccount) {
Objects.requireNonNull(ofxBank.getTransactions());
Objects.requireNonNull(baseAccount);
final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
Objects.requireNonNull(engine);
for (final ImportTransaction tran : ofxBank.getTransactions()) {
// do not import matched transactions
if (tran.getState() == ImportState.NEW || tran.getState() == ImportState.NOT_EQUAL) {
Transaction transaction = null;
if (tran.isInvestmentTransaction()) {
if (baseAccount.getAccountType().getAccountGroup() == AccountGroup.INVEST) {
transaction = importInvestmentTransaction(ofxBank, tran, baseAccount);
if (transaction != null) {
// check and add the security node to the account if not present
if (!baseAccount.containsSecurity(((InvestmentTransaction) transaction).getSecurityNode())) {
engine.addAccountSecurity(((InvestmentTransaction) transaction).getInvestmentAccount(), ((InvestmentTransaction) transaction).getSecurityNode());
}
}
} else {
// Signal an error
System.out.println("Base account was not an investment account type");
}
} else {
if (baseAccount.equals(tran.getAccount())) {
// single entry oTran
transaction = TransactionFactory.generateSingleEntryTransaction(baseAccount, tran.getAmount(), tran.getDatePosted(), tran.getMemo(), tran.getPayee(), tran.getCheckNumber());
} else {
// double entry
if (tran.getAmount().signum() >= 0) {
transaction = TransactionFactory.generateDoubleEntryTransaction(baseAccount, tran.getAccount(), tran.getAmount().abs(), tran.getDatePosted(), tran.getMemo(), tran.getPayee(), tran.getCheckNumber());
} else {
transaction = TransactionFactory.generateDoubleEntryTransaction(tran.getAccount(), baseAccount, tran.getAmount().abs(), tran.getDatePosted(), tran.getMemo(), tran.getPayee(), tran.getCheckNumber());
}
}
}
// add the new transaction
if (transaction != null) {
transaction.setFitid(tran.getFITID());
engine.addTransaction(transaction);
}
}
}
}
use of jgnash.convert.importat.ImportTransaction in project jgnash by ccavanaugh.
the class OfxV2Parser method parseInvestmentTransaction.
private void parseInvestmentTransaction(final XMLStreamReader reader) throws XMLStreamException {
logger.entering(OfxV2Parser.class.getName(), "parseInvestmentTransaction");
final QName parsingElement = reader.getName();
final ImportTransaction tran = new ImportTransaction();
// set the descriptive transaction type text as well
tran.setTransactionTypeDescription(parsingElement.toString());
// extract the investment transaction type from the element name
switch(parsingElement.toString()) {
case BUYMF:
case BUYOTHER:
case BUYSTOCK:
tran.setTransactionType(TransactionType.BUYSHARE);
break;
case SELLMF:
case SELLOTHER:
case SELLSTOCK:
tran.setTransactionType(TransactionType.SELLSHARE);
break;
case // dividend
INCOME:
tran.setTransactionType(TransactionType.DIVIDEND);
break;
case REINVEST:
tran.setTransactionType(TransactionType.REINVESTDIV);
break;
default:
logger.log(Level.WARNING, "Unknown investment transaction type: {0}", parsingElement.toString());
break;
}
parse: while (reader.hasNext()) {
final int event = reader.next();
switch(event) {
case XMLStreamConstants.START_ELEMENT:
switch(reader.getLocalName()) {
case DTSETTLE:
tran.setDatePosted(parseDate(reader.getElementText()));
break;
case DTTRADE:
tran.setDateUser(parseDate(reader.getElementText()));
break;
case // total of the investment transaction
TOTAL:
tran.setAmount(parseAmount(reader.getElementText()));
break;
case FITID:
tran.setFITID(reader.getElementText());
break;
case // the security for the transaction
UNIQUEID:
tran.setSecurityId(reader.getElementText());
break;
case // the type of security for the transaction
UNIQUEIDTYPE:
tran.setSecurityType(reader.getElementText());
break;
case UNITS:
tran.setUnits(parseAmount(reader.getElementText()));
break;
case UNITPRICE:
tran.setUnitPrice(parseAmount(reader.getElementText()));
break;
case // investment fees
FEES:
tran.setFees(parseAmount(reader.getElementText()));
break;
case // investment commission
COMMISSION:
tran.setCommission(parseAmount(reader.getElementText()));
break;
case INCOMETYPE:
tran.setIncomeType(reader.getElementText());
break;
case SUBACCTSEC:
case SUBACCTFROM:
case SUBACCTTO:
case SUBACCTFUND:
tran.setSubAccount(reader.getElementText());
break;
case CHECKNUM:
tran.setCheckNumber(reader.getElementText());
break;
case NAME:
case // either PAYEE or NAME will be used
PAYEE:
tran.setPayee(removeExtraWhiteSpace(reader.getElementText()));
break;
case MEMO:
tran.setMemo(removeExtraWhiteSpace(reader.getElementText()));
break;
case // Chase bank mucking up the OFX standard
CATEGORY:
break;
case SIC:
tran.setSIC(reader.getElementText());
break;
case REFNUM:
tran.setRefNum(reader.getElementText());
break;
case PAYEEID:
tran.setPayeeId(removeExtraWhiteSpace(reader.getElementText()));
break;
case // currency used for the transaction
CURRENCY:
// tran.currency = reader.getElementText();
consumeElement(reader);
break;
case ORIGCURRENCY:
tran.setCurrency(reader.getElementText());
break;
case TAXEXEMPT:
tran.setTaxExempt(parseBoolean(reader.getElementText()));
break;
case BUYTYPE:
// consume
case INVBUY:
case INVTRAN:
case SECID:
break;
// consume 401k loan information
case LOANID:
case LOANPRINCIPAL:
case LOANINTEREST:
break;
// consume 401k information
case INV401KSOURCE:
case DTPAYROLL:
case PRIORYEARCONTRIB:
break;
default:
logger.log(Level.WARNING, "Unknown investment transaction element: {0}", reader.getLocalName());
break;
}
break;
case XMLStreamConstants.END_ELEMENT:
if (reader.getName().equals(parsingElement)) {
// logger.fine("Found the end of the investment transaction");
break parse;
}
default:
}
}
bank.addTransaction(tran);
logger.exiting(OfxV2Parser.class.getName(), "parseInvestmentTransaction");
}
use of jgnash.convert.importat.ImportTransaction in project jgnash by ccavanaugh.
the class OfxBank method toString.
@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append("statusCode: ").append(statusCode).append('\n');
b.append("statusSeverity: ").append(statusSeverity).append('\n');
b.append("statusMessage: ").append(statusMessage).append('\n');
b.append("currency: ").append(currency).append('\n');
b.append("bankId: ").append(bankId).append('\n');
b.append("branchId: ").append(branchId).append('\n');
b.append("accountId: ").append(accountId).append('\n');
b.append("accountType: ").append(accountType).append('\n');
b.append("dateStart: ").append(dateStart).append('\n');
b.append("dateEnd: ").append(dateEnd).append('\n');
b.append("ledgerBalance: ").append(ledgerBalance).append('\n');
b.append("ledgerBalanceDate: ").append(ledgerBalanceDate).append('\n');
if (availBalance != null) {
b.append("availBalance: ").append(availBalance).append('\n');
}
if (availBalanceDate != null) {
b.append("availBalanceDate: ").append(availBalanceDate).append('\n');
}
for (final ImportTransaction t : getTransactions()) {
b.append(t).append('\n');
}
for (final ImportSecurity importSecurity : securityList) {
b.append(importSecurity.toString()).append('\n');
}
return b.toString();
}
use of jgnash.convert.importat.ImportTransaction in project jgnash by ccavanaugh.
the class Mt940Test method testMt940Rabobank.
/**
* Test parsing an (anonimized) mt940 file as produced by the Rabobank online
* bank
*
* @throws IOException thrown if an IO exception occurs while reading the file
*/
@Test
void testMt940Rabobank() throws IOException {
int nTransactions = 6;
InputStream inputStream = this.getClass().getResourceAsStream("/rabobank.swi");
final Charset charset = FileMagic.detectCharset(inputStream);
// reopen the input stream, FileMagic closed it
inputStream = this.getClass().getResourceAsStream("/rabobank.swi");
try (final LineNumberReader reader = new LineNumberReader(new InputStreamReader(inputStream, charset))) {
Mt940File file = Mt940Parser.parse(reader);
assertEquals(nTransactions, file.getEntries().size());
ImportBank<ImportTransaction> bank = Mt940Exporter.convert(file);
assertEquals(nTransactions, bank.getTransactions().size());
}
}
use of jgnash.convert.importat.ImportTransaction in project jgnash by ccavanaugh.
the class Mt940Test method testMt940.
/**
* Rudimentary test for mt940 importing: creates a parser, parses a given mt940
* file and checks that indeed, 18 transactions have been generated.
*
* Then, converts the parsed file to an ImportBank and verifies that there's
* still 18 transactions.
*
* @throws Exception throws assert exception
*/
@Test
void testMt940() throws Exception {
int nTransactions = 18;
InputStream inputStream = this.getClass().getResourceAsStream("/bank1.STA");
final Charset charset = FileMagic.detectCharset(inputStream);
// reopen the input stream, FileMagic closed it
inputStream = this.getClass().getResourceAsStream("/bank1.STA");
try (LineNumberReader reader = new LineNumberReader(new InputStreamReader(inputStream, charset))) {
Mt940File file = Mt940Parser.parse(reader);
assertEquals(nTransactions, file.getEntries().size());
ImportBank<ImportTransaction> bank = Mt940Exporter.convert(file);
assertEquals(nTransactions, bank.getTransactions().size());
}
}
Aggregations