Skip to content

Commit

Permalink
Added support to import to database from csv files.
Browse files Browse the repository at this point in the history
  • Loading branch information
harshshinde07 committed Nov 1, 2019
1 parent 19a7835 commit 2afa9ef
Show file tree
Hide file tree
Showing 17 changed files with 324 additions and 300 deletions.
4 changes: 2 additions & 2 deletions .idea/assetWizardSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation group: 'com.opencsv', name: 'opencsv', version: '4.1'

// ButterKnife (for sample code brevity)
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
// // ButterKnife (for sample code brevity)
// implementation 'com.jakewharton:butterknife:8.8.1'
// annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.kshitijharsh.dairymanagement.R;
import com.kshitijharsh.dairymanagement.database.DBHelper;
import com.kshitijharsh.dairymanagement.database.DBQuery;
import com.kshitijharsh.dairymanagement.database.DatabaseClass;
import com.kshitijharsh.dairymanagement.utils.FileUtils;
import com.kshitijharsh.dairymanagement.utils.SqliteExporter;
import com.kshitijharsh.dairymanagement.utils.SqliteImporter;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -64,6 +69,7 @@ private void checkForStoragePermission() {
ACCESS_EXTERNAL_STORAGE);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Expand All @@ -76,7 +82,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
public void run() {
finish();
}
},3*1000);
}, 3 * 1000);
} else if (!checkDataBase()) {
Toast.makeText(this, "Database not found! App will exit now...", Toast.LENGTH_LONG).show();
new Handler().postDelayed(new Runnable() {
Expand All @@ -86,21 +92,22 @@ public void run() {
}
}, 3 * 1000);
} else {
FileUtils.createDirIfNotExist(FileUtils.getAppDir() + "/imports");
dbHelper = new DBHelper(this);
db = dbHelper.getReadableDatabase();
Cursor member = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+ TABLE_MEMBER +"'", null);
Cursor rateMaster = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+ TABLE_RATEMASTER +"'", null);
Cursor item = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+ TABLE_ITEM +"'", null);
Cursor rateGrpMaster = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+ TABLE_RATEGRPMASTER +"'", null);
Cursor member = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + TABLE_MEMBER + "'", null);
Cursor rateMaster = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + TABLE_RATEMASTER + "'", null);
Cursor item = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + TABLE_ITEM + "'", null);
Cursor rateGrpMaster = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + TABLE_RATEGRPMASTER + "'", null);

if (member.getCount()<=0 || rateMaster.getCount()<=0 || item.getCount() <=0 || rateGrpMaster.getCount() <=0) {
if (member.getCount() <= 0 || rateMaster.getCount() <= 0 || item.getCount() <= 0 || rateGrpMaster.getCount() <= 0) {
Toast.makeText(this, "Required tables not found in database, app will exit now...", Toast.LENGTH_LONG).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
},3*1000);
}, 3 * 1000);
}
member.close();
rateMaster.close();
Expand All @@ -112,21 +119,24 @@ public void run() {
Log.d(getClass().getSimpleName(), "First Query Result: " + query.getMembercount());
//Toast.makeText(this, DBHelper.DB_PATH, Toast.LENGTH_LONG).show();
query.close();
// db.endTransaction();
db.close();
dbHelper.close();
}
}

public void exitApp(){
if(exit){
public void exitApp() {
if (exit) {
finish();
}else {
} else {
exit = true;
// Toast.makeText(this,"Press back again to exit",Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
},3*1000);
}, 3 * 1000);
}
}

Expand All @@ -152,7 +162,7 @@ public void initiateExport(View view) {
+ File.separator + BACKUP_DIRECTORY, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error: "+ e.toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Error: " + e.toString(), Toast.LENGTH_SHORT).show();
}
}

Expand Down Expand Up @@ -183,4 +193,49 @@ private boolean checkDataBase() {
}
return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_main) {
initiateImport();
return true;
}
return super.onOptionsItemSelected(item);
}

public void initiateImport() {
DBHelper helper = new DBHelper(this);
SQLiteDatabase sqLiteDatabase = helper.getWritableDatabase();
try {

int res = SqliteImporter.importData(sqLiteDatabase);
// Toast.makeText(this, res, Toast.LENGTH_SHORT).show();
switch (res) {
case 0:
Toast.makeText(this, "No CSV files to import", Toast.LENGTH_SHORT).show();
break;
case -1:
Toast.makeText(this, "File names does not match!", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(this, "Successfully Imported.", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(this, "Some problem occurred...", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(this, "Error while importing, try again.", Toast.LENGTH_SHORT).show();
Log.e("Import Exception: ", e.toString());
}
// sqLiteDatabase.endTransaction();
sqLiteDatabase.close();
helper.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import android.widget.TextView;
import android.widget.Toast;

import com.kshitijharsh.dairymanagement.ItemClickListener;
import com.kshitijharsh.dairymanagement.R;
import com.kshitijharsh.dairymanagement.activities.CattleFeedActivity;
import com.kshitijharsh.dairymanagement.database.DatabaseClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import android.widget.TextView;
import android.widget.Toast;

import com.kshitijharsh.dairymanagement.ItemClickListener;
import com.kshitijharsh.dairymanagement.R;
import com.kshitijharsh.dairymanagement.activities.CollectionActivity;
import com.kshitijharsh.dairymanagement.database.DatabaseClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.icu.text.UnicodeSetSpanner;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
Expand All @@ -17,7 +16,6 @@
import android.widget.TextView;
import android.widget.Toast;

import com.kshitijharsh.dairymanagement.ItemClickListener;
import com.kshitijharsh.dairymanagement.R;
import com.kshitijharsh.dairymanagement.activities.MemberActivity;
import com.kshitijharsh.dairymanagement.database.DBQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
import android.widget.TextView;
import android.widget.Toast;

import com.kshitijharsh.dairymanagement.ItemClickListener;
import com.kshitijharsh.dairymanagement.R;
import com.kshitijharsh.dairymanagement.activities.SaleActivity;
import com.kshitijharsh.dairymanagement.database.DatabaseClass;
import com.kshitijharsh.dairymanagement.model.Member;
import com.kshitijharsh.dairymanagement.model.Sale;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Constants {
public static class CONST {
public static final String EXT_DIRECTORY = "/Winsoft";
public static final String BACKUP_DIRECTORY = "backup";
public static final String BACKUP_DIRECTORY = "exports";
public static final String MY_PATH = "/data/data/com.kshitijharsh.dairymanagement/databases/records.db";

public static final String FIELD_NAME = "name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

public class FileUtils {

static String getAppDir() {
public static String getAppDir() {
return Environment.getExternalStorageDirectory() + EXT_DIRECTORY;
}

static File createDirIfNotExist(String path) {
public static File createDirIfNotExist(String path) {
File dir = new File(path);
if( !dir.exists() ){
dir.mkdir();
Expand Down
Loading

0 comments on commit 2afa9ef

Please sign in to comment.