/**
   * Builds a QIF entry representing this transaction
   *
   * @return String QIF representation of this transaction
   */
  public String toQIF() {
    final String newLine = "\n";

    AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(GnuCashApplication.getAppContext());

    // all transactions are double transactions
    String splitAccountFullName = QifHelper.getImbalanceAccountName(mAmount.getCurrency());
    if (mDoubleEntryAccountUID != null && mDoubleEntryAccountUID.length() > 0) {
      splitAccountFullName = accountsDbAdapter.getFullyQualifiedAccountName(mDoubleEntryAccountUID);
    }

    StringBuffer transactionQifBuffer = new StringBuffer();
    transactionQifBuffer
        .append(QifHelper.DATE_PREFIX)
        .append(QifHelper.formatDate(mTimestamp))
        .append(newLine);
    transactionQifBuffer.append(QifHelper.MEMO_PREFIX).append(mName).append(newLine);

    transactionQifBuffer
        .append(QifHelper.SPLIT_CATEGORY_PREFIX)
        .append(splitAccountFullName)
        .append(newLine);
    if (mDescription != null && mDescription.length() > 0) {
      transactionQifBuffer.append(QifHelper.SPLIT_MEMO_PREFIX).append(mDescription).append(newLine);
    }
    transactionQifBuffer
        .append(QifHelper.SPLIT_AMOUNT_PREFIX)
        .append(mAmount.asString())
        .append(newLine);
    transactionQifBuffer.append(QifHelper.ENTRY_TERMINATOR).append(newLine);

    accountsDbAdapter.close();
    return transactionQifBuffer.toString();
  }
  /**
   * Converts transaction to XML DOM corresponding to OFX Statement transaction and returns the
   * element node for the transaction. The Unique ID of the account is needed in order to properly
   * export double entry transactions
   *
   * @param doc XML document to which transaction should be added
   * @param accountUID Unique Identifier of the account which called the method.
   * @return Element in DOM corresponding to transaction
   */
  public Element toOfx(Document doc, String accountUID) {
    Element transactionNode = doc.createElement("STMTTRN");
    Element type = doc.createElement("TRNTYPE");
    type.appendChild(doc.createTextNode(mType.toString()));
    transactionNode.appendChild(type);

    Element datePosted = doc.createElement("DTPOSTED");
    datePosted.appendChild(doc.createTextNode(OfxExporter.getOfxFormattedTime(mTimestamp)));
    transactionNode.appendChild(datePosted);

    Element dateUser = doc.createElement("DTUSER");
    dateUser.appendChild(doc.createTextNode(OfxExporter.getOfxFormattedTime(mTimestamp)));
    transactionNode.appendChild(dateUser);

    Element amount = doc.createElement("TRNAMT");
    amount.appendChild(doc.createTextNode(mAmount.toPlainString()));
    transactionNode.appendChild(amount);

    Element transID = doc.createElement("FITID");
    transID.appendChild(doc.createTextNode(mTransactionUID));
    transactionNode.appendChild(transID);

    Element name = doc.createElement("NAME");
    name.appendChild(doc.createTextNode(mName));
    transactionNode.appendChild(name);

    if (mDescription != null && mDescription.length() > 0) {
      Element memo = doc.createElement("MEMO");
      memo.appendChild(doc.createTextNode(mDescription));
      transactionNode.appendChild(memo);
    }

    if (mDoubleEntryAccountUID != null && mDoubleEntryAccountUID.length() > 0) {
      Element bankId = doc.createElement("BANKID");
      bankId.appendChild(doc.createTextNode(OfxExporter.APP_ID));

      // select the proper account as the double account
      String doubleAccountUID =
          mDoubleEntryAccountUID.equals(accountUID) ? mAccountUID : mDoubleEntryAccountUID;

      Element acctId = doc.createElement("ACCTID");
      acctId.appendChild(doc.createTextNode(doubleAccountUID));

      Element accttype = doc.createElement("ACCTTYPE");
      AccountsDbAdapter acctDbAdapter = new AccountsDbAdapter(GnuCashApplication.getAppContext());
      OfxAccountType ofxAccountType =
          Account.convertToOfxAccountType(acctDbAdapter.getAccountType(doubleAccountUID));
      accttype.appendChild(doc.createTextNode(ofxAccountType.toString()));
      acctDbAdapter.close();

      Element bankAccountTo = doc.createElement("BANKACCTTO");
      bankAccountTo.appendChild(bankId);
      bankAccountTo.appendChild(acctId);
      bankAccountTo.appendChild(accttype);

      transactionNode.appendChild(bankAccountTo);
    }

    return transactionNode;
  }
  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    mFavoriteAccountMenu = menu.findItem(R.id.menu_favorite_account);
    MenuItem favoriteAccountMenuItem = menu.findItem(R.id.menu_favorite_account);

    if (favoriteAccountMenuItem == null) // when the activity is used to edit a transaction
    return super.onPrepareOptionsMenu(menu);

    AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(this);
    boolean isFavoriteAccount = accountsDbAdapter.isFavoriteAccount(mAccountId);
    accountsDbAdapter.close();

    int favoriteIcon =
        isFavoriteAccount
            ? android.R.drawable.btn_star_big_on
            : android.R.drawable.btn_star_big_off;
    favoriteAccountMenuItem.setIcon(favoriteIcon);
    return super.onPrepareOptionsMenu(menu);
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.i("Gnucash", "Received account creation intent");
    Bundle args = intent.getExtras();

    String uid = args.getString(Intent.EXTRA_UID);

    Account account = new Account(args.getString(Intent.EXTRA_TITLE));
    String currencyCode = args.getString(Account.EXTRA_CURRENCY_CODE);

    if (currencyCode != null) {
      Currency currency = Currency.getInstance(currencyCode);
      account.setCurrency(currency);
    }

    if (uid != null) account.setUID(uid);

    AccountsDbAdapter accountsAdapter = new AccountsDbAdapter(context);
    accountsAdapter.addAccount(account);
    accountsAdapter.close();
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        InputMethodManager imm =
            (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(findViewById(android.R.id.content).getWindowToken(), 0);
        FragmentManager fm = getSupportFragmentManager();
        if (fm.getBackStackEntryCount() > 0) {
          fm.popBackStack();
        } else {
          AccountsActivity.start(this);
          finish();
        }
        return true;

      case R.id.menu_favorite_account:
        AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(this);
        boolean isFavorite = accountsDbAdapter.isFavoriteAccount(mAccountId);
        // toggle favorite preference
        accountsDbAdapter.updateAccount(
            mAccountId, DatabaseHelper.KEY_FAVORITE, isFavorite ? "0" : "1");
        accountsDbAdapter.close();
        supportInvalidateOptionsMenu();
        return true;

      case R.id.menu_edit_account:
        Intent editAccountIntent = new Intent(this, AccountsActivity.class);
        editAccountIntent.setAction(Intent.ACTION_INSERT_OR_EDIT);
        editAccountIntent.putExtra(UxArgument.SELECTED_ACCOUNT_ID, mAccountId);
        startActivityForResult(editAccountIntent, AccountsActivity.REQUEST_EDIT_ACCOUNT);
        return true;

      default:
        return false;
    }
  }
 @Override
 protected void onDestroy() {
   super.onDestroy();
   mAccountsDbAdapter.close();
 }