Android Open Source - SMSAlive Account Transaction Crud Service Impl






From Project

Back to project page SMSAlive.

License

The source code is released under:

Apache License

If you think the Android project SMSAlive 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

package com.futuretech.app.smsalive.Domain.crud.impl;
//ww w . j a v a 2 s .  c  o m
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.futuretech.app.smsalive.Domain.models.AccountTransaction;
import com.futuretech.app.smsalive.repository.DBHelper;
import com.futuretech.app.smsalive.Domain.crud.AccountTransactionCrudService;
import com.futuretech.app.smsalive.utils.Constants;
import com.futuretech.app.smsalive.utils.Factory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ironhulk on 2014/11/28.
 */
public class AccountTransactionCrudServiceImpl implements AccountTransactionCrudService {

    private DBHelper dbHelper;
    private Context context;

    public AccountTransactionCrudServiceImpl(Context context){
        this.context = context;
        dbHelper = new DBHelper(context);
    }

    public SQLiteDatabase getDatabase(){
        return dbHelper.getWritableDatabase();
    }

    public void close(){
        dbHelper.close();
    }

    @Override
    public long createEntity(AccountTransaction entity) {
        ContentValues cv = new ContentValues();
        cv.put(Constants.COL_ACCOUNT_NUMBER,entity.getAccountNumber());
        cv.put(Constants.COL_ACCOUNT_TYPE,entity.getAccountType());
        cv.put(Constants.COL_ACCOUNT_BALANCE,entity.getAccountBalance());
        cv.put(Constants.COL_TRANS_AMOUNT,entity.getTransactionAmount());
        cv.put(Constants.COL_TRANS_DATE,entity.getTransactionDate().toString());
        cv.put(Constants.COL_TRANS_PLACE,entity.getTransactionPlace());
        cv.put(Constants.COL_TRANS_TYPE,entity.getTransactionType());

        long id = getDatabase().insert(Constants.TABLE_NAME_TRANS,null,cv);

        close();

        return id;
    }

    @Override
    public void updateEntity(AccountTransaction entity) {
        //Todo: Not Going To Implement
    }

    @Override
    public AccountTransaction findEntityById(long id) {
        String columns[] = {Constants.COL_ACCOUNT_NUMBER,Constants.COL_ACCOUNT_TYPE,Constants.COL_ACCOUNT_BALANCE,Constants.COL_TRANS_AMOUNT,Constants.COL_TRANS_DATE,Constants.COL_TRANS_PLACE,Constants.COL_TRANS_TYPE};
        String selectionArgs[] = {String.valueOf(id)};

        Cursor accountTransaction = getDatabase().query(Constants.TABLE_NAME_TRANS,columns,Constants.COL_TRANS_ID + "=?",selectionArgs,null,null,null,null);

        if(accountTransaction!=null)
            accountTransaction.moveToFirst();

        HashMap<String,String> values = new HashMap<String, String>();
        values.put("_id",String.valueOf(id));
        values.put("accountNumber",accountTransaction.getString(1));
        values.put("accountType",accountTransaction.getString(2));
        values.put("accountBalance",String.valueOf(accountTransaction.getDouble(3)));
        values.put("transactionAmount",String.valueOf(accountTransaction.getDouble(4)));
        values.put("transactionDate",accountTransaction.getString(5));
        values.put("transactionPlace",accountTransaction.getString(6));
        values.put("transactionType",accountTransaction.getString(7));

        AccountTransaction ac = Factory.buildAccountTransaction(values);

        close();

        return ac;
    }

    @Override
    public AccountTransaction findEntity(AccountTransaction entity) {
        return findEntityById(entity.get_id());
    }

    @Override
    public AccountTransaction findEntityByQuery(String query) {
        return null;
    }

    @Override
    public List<AccountTransaction> findAllEntities() {
        String columns[] = {Constants.COL_ACCOUNT_NUMBER,Constants.COL_ACCOUNT_TYPE,Constants.COL_ACCOUNT_BALANCE,Constants.COL_TRANS_AMOUNT,Constants.COL_TRANS_DATE,Constants.COL_TRANS_PLACE,Constants.COL_TRANS_TYPE};

        Cursor accountTransaction = getDatabase().query(Constants.TABLE_NAME_TRANS,columns,null,null,null,null,null,null);

        List<AccountTransaction> all = new ArrayList<AccountTransaction>();

        HashMap<String,String> values = new HashMap<String, String>();

        for(int i = 0;i < accountTransaction.getCount();i++){
            accountTransaction.moveToFirst();

            values.put("_id",Integer.toString(accountTransaction.getInt(0)));
            values.put("accountNumber",accountTransaction.getString(1));
            values.put("accountType",accountTransaction.getString(2));
            values.put("accountBalance",String.valueOf(accountTransaction.getDouble(3)));
            values.put("transactionAmount",String.valueOf(accountTransaction.getDouble(4)));
            values.put("transactionDate",accountTransaction.getString(5));
            values.put("transactionPlace",accountTransaction.getString(6));
            values.put("transactionType",accountTransaction.getString(7));

            AccountTransaction ac = Factory.buildAccountTransaction(values);

            all.add(ac);

            values.clear();
        }

        return all;
    }

    @Override
    public int deleteEntity(AccountTransaction entity) {
        return deleteEntityById(entity.get_id());
    }

    @Override
    public int deleteEntityById(long id) {
        String whereArgs[] = {String.valueOf(id)};
        return getDatabase().delete(Constants.TABLE_NAME_TRANS,Constants.COL_TRANS_ID + "=?",whereArgs);
    }
}




Java Source Code List

com.futuretech.app.smsalive.ApplicationTest.java
com.futuretech.app.smsalive.Test.java
com.futuretech.app.smsalive.Application.Services.ApplicationServices.java
com.futuretech.app.smsalive.Application.Services.TranscactionTypeHandler.java
com.futuretech.app.smsalive.Application.Services.impl.ApplicationServicesImpl.java
com.futuretech.app.smsalive.Application.Services.impl.DepositHandler.java
com.futuretech.app.smsalive.Application.Services.impl.PurchaseHandler.java
com.futuretech.app.smsalive.Application.Services.impl.WithDrawalHandler.java
com.futuretech.app.smsalive.Domain.crud.AccountTransactionCrudService.java
com.futuretech.app.smsalive.Domain.crud.impl.AccountTransactionCrudServiceImpl.java
com.futuretech.app.smsalive.Domain.models.AccountTransaction.java
com.futuretech.app.smsalive.Presentation.activities.InitializingData.java
com.futuretech.app.smsalive.Presentation.fragments.InitialDataGathering.java
com.futuretech.app.smsalive.Presentation.fragments.WelcomeFragment.java
com.futuretech.app.smsalive.Presentation.services.DataGatheringService.java
com.futuretech.app.smsalive.repository.DBHelper.java
com.futuretech.app.smsalive.repository.RepositoryUtil.java
com.futuretech.app.smsalive.utils.Constants.java
com.futuretech.app.smsalive.utils.Factory.java
com.futuretech.app.smsalive.utils.TransactionTypes.java