Skip to content

Commit

Permalink
Replace account number by UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
my-flow committed Jan 4, 2020
1 parent 9a34d1e commit d1fc7fe
Show file tree
Hide file tree
Showing 36 changed files with 342 additions and 329 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project does *not* adhere to Semantic Versioning.
- Replace [FindBugs](http://findbugs.sourceforge.net) by [SpotBugs](https://spotbugs.github.io).

### Fixed
- Crash in Moneydance 2019 when importing into new account.
- Remember designated account ([#13]).


Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.PHONY: test

test:
./gradlew clean check
./gradlew assemble sign <<< ''
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
com.moneydance.modules.features.paypalimporter.bootstrap.FactoryModule.class,
com.moneydance.modules.features.paypalimporter.controller.FactoryModule.class,
com.moneydance.modules.features.paypalimporter.domain.FactoryModule.class,
com.moneydance.modules.features.paypalimporter.model.FactoryModule.class,
com.moneydance.modules.features.paypalimporter.service.FactoryModule.class,
com.moneydance.modules.features.paypalimporter.util.FactoryModule.class,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class AccountDelegateListener implements AccountListener {

@Override
public void accountModified(final Account account) {
this.viewController.refreshAccounts(-1);
this.viewController.refreshAccounts(null);
}

@Override
Expand All @@ -33,13 +33,13 @@ public void accountBalanceChanged(final Account account) {
public void accountDeleted(
final Account parentAccount,
final Account account) {
this.viewController.refreshAccounts(-1);
this.viewController.refreshAccounts(null);
}

@Override
public void accountAdded(
final Account parentAccount,
final Account account) {
this.viewController.refreshAccounts(-1);
this.viewController.refreshAccounts(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ final class CheckCurrencyRequestHandler
extends AbstractRequestHandler<CurrencyCodeType> {

private final IAccountBook accountBook;
private final int accountNum;
private final String accountId;

CheckCurrencyRequestHandler(
final ViewController argViewController,
final IAccountBook argAccountBook,
final int argAccountNum,
final String argAccountId,
final Localizable argLocalizable) {
super(argViewController,
argLocalizable);
this.accountBook = argAccountBook;
this.accountNum = argAccountNum;
this.accountId = argAccountId;
}


Expand All @@ -42,7 +42,7 @@ public void serviceCallSucceeded(
final ServiceResult<CurrencyCodeType> serviceResult) {

final List<CurrencyCodeType> currencyCodes = serviceResult.getResults().orElseThrow(AssertionError::new);
Account useAccount = this.accountBook.getAccountByNum(this.accountNum);
Account useAccount = this.accountBook.getAccountById(this.accountId);

// 1. determine the currency of the Moneydance account
CurrencyType currencyType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.moneydance.apps.md.controller.FeatureModuleContext;
import com.moneydance.modules.features.paypalimporter.domain.DateConverter;
import com.moneydance.modules.features.paypalimporter.model.AccountFilter;
import com.moneydance.modules.features.paypalimporter.model.IAccountBook;
import com.moneydance.modules.features.paypalimporter.service.ServiceProvider;
import com.moneydance.modules.features.paypalimporter.util.Localizable;
import com.moneydance.modules.features.paypalimporter.util.Preferences;
Expand All @@ -27,13 +29,17 @@ ViewController provideViewController(
final FeatureModuleContext context,
final ServiceProvider serviceProvider,
final DateConverter dateConverter,
final IAccountBook accountBook,
final AccountFilter accountFilter,
final Settings settings,
final Preferences prefs,
final Localizable localizable) {
return new ViewControllerImpl(
context,
serviceProvider,
dateConverter,
accountBook,
accountFilter,
settings,
prefs,
localizable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ final class TransactionSearchIterator implements ViewController {
this.requestHandler = new TransactionSearchRequestHandler(
this,
this.accountBook,
argInputData.getAccountId().orElseThrow(AssertionError::new),
argDateFormat,
argLocalizable);
this.resultSet = new LinkedHashSet<>();
Expand All @@ -111,7 +112,7 @@ void callTransactionSearchService() {
public void transactionsImported(
final List<OnlineTxn> argOnlineTxns,
final Date argStartDate,
@Nullable final Account argAccount,
final Account argAccount,
@Nullable final String errorCode) {

this.resultSet.addAll(argOnlineTxns); // aggregate all results
Expand All @@ -133,7 +134,7 @@ public void transactionsImported(

account = findOrCreateAccount(
this.accountBook,
this.inputData.getAccountId(),
this.inputData.getAccountId().orElseThrow(AssertionError::new),
this.currencyType);
final OnlineTxnList txnList = account.getDownloadedTxns();

Expand Down Expand Up @@ -184,10 +185,10 @@ private void callTransactionSearchService(final Date endDate) {

private Account findOrCreateAccount(
final IAccountBook argAccountBook,
final int argAccountId,
final String argAccountId,
final CurrencyType argCurrencyType) {

Account account = argAccountBook.getAccountByNum(argAccountId);
Account account = argAccountBook.getAccountById(argAccountId);
if (account == null) {
// lazy creation of a Moneydance account if none has been given
LOG.info("Creating new account");
Expand Down Expand Up @@ -238,7 +239,7 @@ public void showHelp() throws MalformedURLException {
}

@Override
public void refreshAccounts(final int accountId) {
public void refreshAccounts(final String accountId) {
this.viewController.refreshAccounts(accountId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.moneydance.modules.features.paypalimporter.controller;

import com.infinitekind.moneydance.model.Account;
import com.infinitekind.moneydance.model.OnlineTxn;
import com.infinitekind.moneydance.model.OnlineTxnList;
import com.moneydance.modules.features.paypalimporter.model.IAccountBook;
Expand Down Expand Up @@ -40,17 +41,20 @@ final class TransactionSearchRequestHandler

private static final BigDecimal MULTIPLIER = BigDecimal.valueOf(100);

private final Account account;
private final OnlineTxnList txnList;
private final DateFormat dateFormat;

TransactionSearchRequestHandler(
final ViewController argViewController,
final IAccountBook accountBook,
final String argAccountId,
final DateFormat argDateFormat,
final Localizable argLocalizable) {

super(argViewController,
argLocalizable);
this.account = accountBook.getAccountById(argAccountId);
this.txnList = accountBook.getRootAccount().getDownloadedTxns();
this.dateFormat = argDateFormat;
}
Expand Down Expand Up @@ -86,7 +90,7 @@ public void serviceCallSucceeded(
this.getViewController().transactionsImported(
resultList,
new Date(startDateLong),
null,
this.account,
serviceResult.getErrorCode().orElse(null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ void transactionsImported(
*
* @param selectedAccountId Preselected account in the combobox.
*/
void refreshAccounts(int selectedAccountId);
void refreshAccounts(String selectedAccountId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
import com.moneydance.apps.md.controller.FeatureModuleContext;
import com.moneydance.apps.md.controller.Main;
import com.moneydance.apps.md.controller.Util;
import com.moneydance.apps.md.view.gui.AccountListModel;
import com.moneydance.apps.md.view.gui.MainFrame;
import com.moneydance.apps.md.view.gui.MoneydanceGUI;
import com.moneydance.apps.md.view.gui.OnlineManager;
import com.moneydance.modules.features.paypalimporter.domain.DateConverter;
import com.moneydance.modules.features.paypalimporter.model.AccountBookFactoryImpl;
import com.moneydance.modules.features.paypalimporter.model.AccountFilter;
import com.moneydance.modules.features.paypalimporter.model.IAccountBook;
import com.moneydance.modules.features.paypalimporter.model.IAccountBookFactory;
import com.moneydance.modules.features.paypalimporter.model.InputData;
import com.moneydance.modules.features.paypalimporter.model.InputDataValidator;
import com.moneydance.modules.features.paypalimporter.presentation.WizardHandler;
Expand All @@ -36,6 +34,7 @@
import java.util.Date;
import java.util.List;
import java.util.Observable;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -71,7 +70,8 @@ public final class ViewControllerImpl implements ViewController {
private final FeatureModuleContext context;
private final ServiceProvider serviceProvider;
private final DateConverter dateConverter;
private final IAccountBookFactory accountBookFactory;
private final IAccountBook accountBook;
private final AccountFilter accountFilter;
private final DateFormat dateFormat;
@Nullable private WizardHandler wizard;
@Nullable private InputData inputData;
Expand All @@ -80,6 +80,8 @@ public ViewControllerImpl(
final FeatureModuleContext argContext,
final ServiceProvider argServiceProvider,
final DateConverter argDateConverter,
final IAccountBook argAccountBook,
final AccountFilter argAccountFilter,
final Settings argSettings,
final Preferences argPrefs,
final Localizable argLocalizable) {
Expand All @@ -89,7 +91,8 @@ public ViewControllerImpl(
this.context = argContext;
this.serviceProvider = argServiceProvider;
this.dateConverter = argDateConverter;
this.accountBookFactory = AccountBookFactoryImpl.INSTANCE;
this.accountBook = argAccountBook;
this.accountFilter = argAccountFilter;
this.dateFormat = settings.getDateFormat();
}

Expand Down Expand Up @@ -149,21 +152,21 @@ private void initAndShowWizard() {
this.settings);
this.wizard.addComponentListener(
new ComponentDelegateListener(
this.accountBookFactory.createAccountBook(this.context).orElseThrow(AssertionError::new),
this.accountBook,
this));
} else if (this.wizard.isVisible()) {
this.wizard.setVisible(true);
return;
}
this.wizard.pack();

int accountId = this.prefs.getAccountId();
if (accountId < 0) {
String accountId = this.prefs.getAccountId();
if (this.accountBook.getAccountById(accountId) == null) {
final MainFrame mainFrame = (MainFrame) mdGUI.getTopLevelFrame();
if (mainFrame != null) {
final Account selectedAccount = mainFrame.getSelectedAccount();
Account selectedAccount = mainFrame.getSelectedAccount();
if (selectedAccount != null) {
accountId = selectedAccount.getAccountNum();
accountId = selectedAccount.getUUID();
}
}
}
Expand All @@ -175,7 +178,7 @@ private void initAndShowWizard() {
accountId);

this.wizard.setInputData(newUserData);
this.refreshAccounts(newUserData.getAccountId());
this.refreshAccounts(newUserData.getAccountId().orElse(null));
this.wizard.setLocationRelativeTo(null);
this.wizard.setVisible(true);
}
Expand All @@ -194,7 +197,8 @@ public void cancel() {
@Override
public void proceed(final InputData argInputData) {
LOG.config(argInputData.toString());
Validator<InputData> inputValidator = new InputDataValidator(this.localizable);
Validator<InputData> inputValidator = new InputDataValidator(
this.localizable);
ValidationResult result = inputValidator.validate(argInputData);
if (result.hasErrors()) {
this.unlock(
Expand All @@ -204,21 +208,20 @@ public void proceed(final InputData argInputData) {
}

this.inputData = argInputData;
final IAccountBook accountBook = this.accountBookFactory.createAccountBook(this.context)
.orElseThrow(AssertionError::new);

final String username = argInputData.getUsername().orElseThrow(AssertionError::new);
final char[] password = argInputData.getPassword(false).orElseThrow(AssertionError::new);
final String signature = argInputData.getSignature().orElseThrow(AssertionError::new);
final String accountId = argInputData.getAccountId().orElse(null);

this.serviceProvider.callCheckCurrencyService(
username,
password,
signature,
new CheckCurrencyRequestHandler(
this,
accountBook,
argInputData.getAccountId(),
this.accountBook,
accountId,
this.localizable));
}

Expand All @@ -243,9 +246,12 @@ public void currencyChecked(
final CurrencyCodeType currencyCode,
final List<CurrencyCodeType> currencyCodes) {

if (currencyCodes.size() > 1 && !this.prefs.hasUsedCombination(
this.inputData.getAccountId(),
this.inputData.getUsername().orElseThrow(AssertionError::new))) {
final Optional<String> accountId = this.inputData.getAccountId();
if (currencyCodes.size() > 1
&& accountId.isPresent()
&& !this.prefs.hasUsedCombination(
accountId.get(),
this.inputData.getUsername().orElseThrow(AssertionError::new))) {

final String message =
this.localizable.getQuestionMessageMultipleCurrencies(
Expand Down Expand Up @@ -292,7 +298,7 @@ private void importTransactions(

TransactionSearchIterator iter = new TransactionSearchIterator(
this,
this.accountBookFactory.createAccountBook(this.context).orElseThrow(AssertionError::new),
this.accountBook,
this.serviceProvider,
this.inputData,
currencyType,
Expand All @@ -307,7 +313,7 @@ private void importTransactions(
public void transactionsImported(
final List<OnlineTxn> onlineTxns,
final Date currentStartDate,
@Nullable final Account account,
final Account account,
@Nullable final String errorCode) {

final InputData input = this.inputData;
Expand All @@ -328,7 +334,7 @@ public void transactionsImported(
this.wizard.setBoundedRangeModel(model);
}
if (errorCode == null) {
final int accountId = account.getAccountNum();
final String accountId = account.getUUID();
this.prefs.setUsername(accountId, input.getUsername().orElseThrow(AssertionError::new));
this.prefs.setPassword(accountId, input.getPassword(true).orElseThrow(AssertionError::new));
this.prefs.setSignature(accountId, input.getSignature().orElseThrow(AssertionError::new));
Expand All @@ -354,28 +360,14 @@ public void showHelp() throws MalformedURLException {
}

@Override
public void refreshAccounts(final int accountId) {
public void refreshAccounts(final String accountId) {
LOG.config("Refreshing accounts");
final IAccountBook accountBook = this.accountBookFactory.createAccountBook(this.context)
.orElseThrow(AssertionError::new);
final AccountListModel accountModel = new AccountListModel(
accountBook.getRootAccount());
accountModel.setShowBankAccounts(true);
accountModel.setShowCreditCardAccounts(true);
accountModel.setShowInvestAccounts(true);
accountModel.setShowAssetAccounts(true);
accountModel.setShowLiabilityAccounts(true);
accountModel.setShowLoanAccounts(true);
final Account selectedAccount = accountBook.getAccountByNum(accountId);
if (selectedAccount != null
&& accountModel.isAccountViewable(selectedAccount)) {
accountModel.setSelectedAccount(selectedAccount);
}
final WizardHandler wizardHandler = this.wizard;
assert wizardHandler != null : "@AssumeAssertion(nullness)";
wizardHandler.setAccounts(accountModel);
wizardHandler.invalidate();
wizardHandler.validate();
Account account = this.accountBook.getAccountById(accountId);
this.accountFilter.setSelectedAccount(account);
assert this.wizard != null : "@AssumeAssertion(nullness)";
this.wizard.setAccounts(this.accountFilter.getComboBoxModel());
this.wizard.invalidate();
this.wizard.validate();
}

void setInputData(@Nonnull final InputData argInputData) {
Expand Down
Loading

0 comments on commit d1fc7fe

Please sign in to comment.