public GnucashWritableTransaction importTransaction(final Date date,
final FixedPointNumber value,
final String text) {
try {
GnucashWritableTransaction transaction = getMyAccount()
.getWritableGnucashFile().createWritableTransaction();
transaction.setDescription(text.replace('\n', ' '));
transaction.setDatePosted(date);
transaction.setCurrencyNameSpace(getMyAccount()
.getCurrencyNameSpace());
transaction.setCurrencyID(getMyAccount().getCurrencyID());
// we always need the split on out account's side
GnucashWritableTransactionSplit myAccountSplit = transaction
.createWritingSplit(getMyAccount());
myAccountSplit.setValue(value);
myAccountSplit.setQuantity(value);
myAccountSplit.setUserDefinedAttribute(getPluginName() + ".orig_description",
text);
myAccountSplit.setDescription(text.replace('\n', ' '));
GnucashWritableAccount otherAccount = getDefaultAccount();
String otherAccountText = "automatically added";
boolean scriptMatched = false;
try {
// look if there is a script matching,
int scriptnum = 0;
LOG.log(Level.FINEST, "Testing scripts for a match");
while (getMyProperties().containsKey(
getPluginName() + SETTINGS_PREFIX_IMPORTSCRIPT_REGEXP + scriptnum)) {
String scriptFilenameKey = getPluginName() + SETTINGS_PREFIX_IMPORTSCRIPT + scriptnum;
String scriptAccountKey = getPluginName() + SETTINGS_PREFIX_IMPORTSCRIPT_ACCOUNT + scriptnum;
LOG.log(Level.FINEST, "Testing script number " + scriptnum + " for a match");
String regexp = getMyProperties().getProperty(
getPluginName() + SETTINGS_PREFIX_IMPORTSCRIPT_REGEXP + scriptnum);
if (text.matches(regexp)) {
scriptMatched = true;
// does a full script exist or only an alternate
// accountid?
if (getMyProperties().containsKey(scriptFilenameKey)) {
importViaScript(value, text, myAccountSplit, scriptnum);
} else {
String alternateAccountID = getMyProperties()
.getProperty(scriptAccountKey);
otherAccount = getMyAccount().getWritableGnucashFile()
.getAccountByID(alternateAccountID);
if (otherAccount == null) {
LOG.log(Level.SEVERE, "Error Account " + otherAccount
+ " given in user-" + getPluginName() + "-rule #"
+ scriptnum + " not found");
JOptionPane
.showMessageDialog(
null,
"Error Account "
+ otherAccount
+ " given in user-" + getPluginName() + "-rule #"
+ scriptnum
+ " not found",
"User-supplied target-account not found.",
JOptionPane.ERROR_MESSAGE);
} else {
LOG.log(Level.INFO, "importing transaction using alternate target-account: "
+ alternateAccountID
+ "="
+ otherAccount
.getQualifiedName());
otherAccountText = "";
}
}
break;
}
scriptnum++;
}
} catch (Exception e) {
LOG.log(Level.SEVERE,
"Exception while importing a transaction via a script-handler.",
e);
} catch (java.lang.NoClassDefFoundError e) {
LOG.log(Level.SEVERE,
"NoClassDefFoundError while importing a transaction. Maybe you are using JDK1.5 instead of 1.6+?",
e);
}
if (!scriptMatched) {
Integer newScriptnum = createMissingScript(date, value, text);
if (newScriptnum != null) {
importViaScript(value, text, myAccountSplit, newScriptnum
.intValue());
}
}
// default-case and make sure that all transactions are balanced
if (!transaction.isBalanced()) {
if (transaction.getSplitsCount() > 1) {
JOptionPane.showMessageDialog(null,
"Imported transaction is not balanced."
+ "\ntransaction (value is " + value
+ " but " + transaction.getBalance()
+ " is missing ) \n" + text,
"not balanced", JOptionPane.WARNING_MESSAGE);
}
GnucashWritableTransactionSplit otherSplit = transaction
.createWritingSplit(otherAccount);
FixedPointNumber negatedValue = transaction.getNegatedBalance();
otherSplit.setValue(negatedValue);
otherSplit.setQuantity(negatedValue);
otherSplit.setDescription(otherAccountText);
}
return transaction;