Android Open Source - bgBanking Bank Account Operations






From Project

Back to project page bgBanking.

License

The source code is released under:

Apache License

If you think the Android project bgBanking listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright (c) 2012 MASConsult Ltd/*from w  w  w .  j a v  a2 s .  c om*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package eu.masconsult.bgbanking.platform;

import android.accounts.Account;
import android.content.ContentProviderOperation;
import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
import android.text.TextUtils;
import eu.masconsult.bgbanking.provider.BankingContract;

/**
 * Helper class for storing data in the platform content providers.
 */
public class BankAccountOperations {

    private final ContentValues mValues;
    private final BatchOperation mBatchOperation;
    private boolean mIsSyncOperation;
    private long mInternalId;
    private boolean mIsNewContact;

    /**
     * Returns an instance of BankAccountOperations instance for adding new
     * account to the platform banking provider.
     * 
     * @param context the Authenticator Activity context
     * @param account the {@link Account} for the SyncAdapter account
     * @param isSyncOperation are we executing this as part of a sync operation?
     * @return instance of {@link BatchOperation}
     */
    public static BankAccountOperations createNewAccount(Account account, boolean isSyncOperation,
            BatchOperation batchOperation) {
        return new BankAccountOperations(account, isSyncOperation, batchOperation);
    }

    /**
     * Returns an instance of BankAccountOperations for updating existing
     * account in the platform banking provider.
     * 
     * @param context the Authenticator Activity context
     * @param internalId the unique Id of the existing account
     * @param isSyncOperation are we executing this as part of a sync operation?
     * @return instance of {@link BatchOperation}
     */
    public static BankAccountOperations updateExistingContact(long internalId,
            boolean isSyncOperation, BatchOperation batchOperation) {
        return new BankAccountOperations(internalId, isSyncOperation, batchOperation);
    }

    /**
     * initialize common stuff
     * 
     * @param context
     * @param isSyncOperation
     * @param batchOperation
     */
    private BankAccountOperations(boolean isSyncOperation, BatchOperation batchOperation) {
        mValues = new ContentValues();
        mIsSyncOperation = isSyncOperation;
        mBatchOperation = batchOperation;
    }

    /**
     * for new accounts
     * 
     * @param context
     * @param serverId
     * @param account
     * @param isSyncOperation
     * @param batchOperation
     */
    private BankAccountOperations(Account account, boolean isSyncOperation,
            BatchOperation batchOperation) {
        this(isSyncOperation, batchOperation);
        mIsNewContact = true;
        mValues.put(BankingContract.BankAccount.ACCOUNT_NAME, account.name);
        mValues.put(BankingContract.BankAccount.ACCOUNT_TYPE, account.type);
    }

    /**
     * for updating
     * 
     * @param context
     * @param internalId
     * @param isSyncOperation
     * @param batchOperation
     */
    private BankAccountOperations(long internalId, boolean isSyncOperation,
            BatchOperation batchOperation) {
        this(isSyncOperation, batchOperation);
        mIsNewContact = false;
        mInternalId = internalId;
    }

    public void done() {
        if (mValues.size() == 0) {
            return;
        }

        ContentProviderOperation.Builder builder;
        if (mIsNewContact) {
            builder = newInsertCpo(BankingContract.BankAccount.CONTENT_URI, mIsSyncOperation, true);
        } else {
            builder = newUpdateCpo(
                    ContentUris
                            .withAppendedId(BankingContract.BankAccount.CONTENT_URI, mInternalId),
                    mIsSyncOperation, true);
        }
        mBatchOperation.add(builder.withValues(mValues).build());
    }

    public BankAccountOperations setAvailableBalance(float balance) {
        mValues.put(BankingContract.BankAccount.COLUMN_NAME_AVAILABLE_BALANCE, balance);
        return this;
    }

    public BankAccountOperations setBalance(float balance) {
        mValues.put(BankingContract.BankAccount.COLUMN_NAME_BALANCE, balance);
        return this;
    }

    public BankAccountOperations setCurrency(String currency) {
        if (!TextUtils.isEmpty(currency)) {
            mValues.put(BankingContract.BankAccount.COLUMN_NAME_CURRENCY, currency);
        }
        return this;
    }

    public BankAccountOperations setIBAN(String iban) {
        if (!TextUtils.isEmpty(iban)) {
            mValues.put(BankingContract.BankAccount.COLUMN_NAME_IBAN, iban);
        }
        return this;
    }

    public BankAccountOperations setName(String name) {
        if (!TextUtils.isEmpty(name)) {
            mValues.put(BankingContract.BankAccount.COLUMN_NAME_NAME, name);
        }
        return this;
    }

    public BankAccountOperations setServerId(String serverId) {
        if (!TextUtils.isEmpty(serverId)) {
            mValues.put(BankingContract.BankAccount.COLUMN_NAME_SERVER_ID, serverId);
        }
        return this;
    }

    private static ContentProviderOperation.Builder newInsertCpo(Uri uri,
            boolean isSyncOperation, boolean isYieldAllowed) {
        return ContentProviderOperation
                .newInsert(addCallerIsSyncAdapterParameter(uri, isSyncOperation))
                .withYieldAllowed(isYieldAllowed);
    }

    private static ContentProviderOperation.Builder newUpdateCpo(Uri uri,
            boolean isSyncOperation, boolean isYieldAllowed) {
        return ContentProviderOperation
                .newUpdate(addCallerIsSyncAdapterParameter(uri, isSyncOperation))
                .withYieldAllowed(isYieldAllowed);
    }

    private static ContentProviderOperation.Builder newDeleteCpo(Uri uri,
            boolean isSyncOperation, boolean isYieldAllowed) {
        return ContentProviderOperation
                .newDelete(addCallerIsSyncAdapterParameter(uri, isSyncOperation))
                .withYieldAllowed(isYieldAllowed);
    }

    private static Uri addCallerIsSyncAdapterParameter(Uri uri, boolean isSyncOperation) {
        if (isSyncOperation) {
            // If we're in the middle of a real sync-adapter operation, then go
            // ahead and tell the Banking provider that we're the sync adapter.
            // That gives us some special permissions - like the ability to
            // really delete an account, and the ability to clear the dirty
            // flag.
            //
            // If we're not in the middle of a sync operation (for example, we
            // just locally created/edited a new account), then we don't want to
            // use the special permissions, and the system will automagically
            // mark the account as 'dirty' for us!
            return uri
                    .buildUpon()
                    .appendQueryParameter(BankingContract.BankAccount.CALLER_IS_SYNCADAPTER, "true")
                    .build();
        }
        return uri;
    }

    public BankAccountOperations updateAvailableBalance(float existingBalance, float newBalance) {
        if (existingBalance != newBalance) {
            setAvailableBalance(newBalance);
        }
        return this;
    }

    public BankAccountOperations updateBalance(float existingBalance, float newBalance) {
        if (existingBalance != newBalance) {
            setBalance(newBalance);
        }
        return this;
    }

    public BankAccountOperations updateName(String existingName, String newName) {
        if (!TextUtils.isEmpty(newName)) {
            if (!newName.equals(existingName)) {
                setName(newName);
            }
        }
        return this;
    }
}




Java Source Code List

eu.masconsult.bgbanking.BankAdapter.java
eu.masconsult.bgbanking.BankingApplication.java
eu.masconsult.bgbanking.Constants.java
eu.masconsult.bgbanking.accounts.AccountAuthenticator.java
eu.masconsult.bgbanking.accounts.AuthenticationService.java
eu.masconsult.bgbanking.accounts.LoginActivity.java
eu.masconsult.bgbanking.activity.HomeActivity.java
eu.masconsult.bgbanking.activity.fragment.AccountsListFragment.java
eu.masconsult.bgbanking.activity.fragment.ChooseAccountTypeFragment.java
eu.masconsult.bgbanking.banks.BankClient.java
eu.masconsult.bgbanking.banks.Bank.java
eu.masconsult.bgbanking.banks.CaptchaException.java
eu.masconsult.bgbanking.banks.RawBankAccount.java
eu.masconsult.bgbanking.banks.dskbank.AuthenticationService.java
eu.masconsult.bgbanking.banks.dskbank.DskClient.java
eu.masconsult.bgbanking.banks.dskbank.SyncService.java
eu.masconsult.bgbanking.banks.fibank.ebanking.AuthenticationService.java
eu.masconsult.bgbanking.banks.fibank.ebanking.EFIBankClient.java
eu.masconsult.bgbanking.banks.fibank.ebanking.SyncService.java
eu.masconsult.bgbanking.banks.fibank.my.AuthenticationService.java
eu.masconsult.bgbanking.banks.fibank.my.MyFIBankClient.java
eu.masconsult.bgbanking.banks.fibank.my.SyncService.java
eu.masconsult.bgbanking.banks.procreditbank.AuthenticationService.java
eu.masconsult.bgbanking.banks.procreditbank.ProcreditClient.java
eu.masconsult.bgbanking.banks.procreditbank.SyncService.java
eu.masconsult.bgbanking.banks.sgexpress.AuthenticationService.java
eu.masconsult.bgbanking.banks.sgexpress.SGExpressClient.java
eu.masconsult.bgbanking.banks.sgexpress.SyncService.java
eu.masconsult.bgbanking.platform.BankAccountManager.java
eu.masconsult.bgbanking.platform.BankAccountOperations.java
eu.masconsult.bgbanking.platform.BatchOperation.java
eu.masconsult.bgbanking.provider.BankingContract.java
eu.masconsult.bgbanking.provider.BankingProvider.java
eu.masconsult.bgbanking.sync.SyncAdapter.java
eu.masconsult.bgbanking.sync.SyncService.java
eu.masconsult.bgbanking.ui.LightProgressDialog.java
eu.masconsult.bgbanking.utils.Convert.java
eu.masconsult.bgbanking.utils.CookieQuotesFixerResponseInterceptor.java
eu.masconsult.bgbanking.utils.CookieRequestInterceptor.java
eu.masconsult.bgbanking.utils.DumpHeadersRequestInterceptor.java
eu.masconsult.bgbanking.utils.DumpHeadersResponseInterceptor.java
eu.masconsult.bgbanking.utils.SampleCursor.java