Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field import feedback #1114

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.fieldbook.tracker.async;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.text.Html;
import android.util.Log;

import androidx.preference.PreferenceManager;

Expand All @@ -15,6 +18,7 @@
import com.fieldbook.tracker.objects.FieldFileObject;
import com.fieldbook.tracker.objects.FieldObject;
import com.fieldbook.tracker.preferences.GeneralKeys;
import com.fieldbook.tracker.utilities.StringUtil;
import com.fieldbook.tracker.utilities.Utils;

import java.lang.ref.WeakReference;
Expand All @@ -33,8 +37,10 @@ public class ImportRunnableTask extends AsyncTask<Integer, Integer, Integer> {

int lineFail = -1;
boolean fail;
private CharSequence failMessage;
boolean uniqueFail;
boolean containsDuplicates = false;
private static final String TAG = "ImportRunnableTask";

public ImportRunnableTask(Context context, FieldFileObject.FieldFileBase fieldFile,
int idColPosition, String unique, String primary, String secondary) {
Expand Down Expand Up @@ -82,9 +88,11 @@ protected Integer doInBackground(Integer... params) {
}

if (mFieldFile.hasSpecialCharacters()) {
Log.d(TAG, "doInBackground: Special characters found in file column names");
return 0;
}


mFieldFile.open();
String[] data;
String[] columns = mFieldFile.readNext();
Expand All @@ -107,7 +115,7 @@ protected Integer doInBackground(Integer... params) {
//populate an array of indices that have a non empty column
//later we will only add data rows with the non empty columns
//also find the unique/primary/secondary indices
//later we will skip the rows if these are not present
//later we will return an error if these are not present
if (!columns[i].isEmpty()) {

if (!nonEmptyColumns.contains(columns[i])) {
Expand Down Expand Up @@ -136,9 +144,7 @@ protected Integer doInBackground(Integer... params) {

//start iterating over all the rows of the csv file only if we found the u/p/s indices
if (uniqueIndex > -1 && primaryIndex > -1 && secondaryIndex > -1) {

int line = 0;

try {
while (true) {
data = mFieldFile.readNext();
Expand All @@ -164,15 +170,45 @@ protected Integer doInBackground(Integer... params) {

controller.getDatabase().createFieldData(studyId, nonEmptyColumns, nonEmptyData);

} else {
fail = true;

String fixFileMessage = mContext.get().getString(R.string.import_runnable_create_field_fix_file);
String missingIdMessageTemplate = mContext.get().getString(R.string.import_runnable_create_field_missing_identifier);

String missingField = null;
String fieldValue = null;

if (data[uniqueIndex].isEmpty()) {
missingField = mContext.get().getString(R.string.import_dialog_unique).toLowerCase();
fieldValue = unique;
} else if (data[primaryIndex].isEmpty()) {
missingField = mContext.get().getString(R.string.import_dialog_primary).toLowerCase();
fieldValue = primary;
} else if (data[secondaryIndex].isEmpty()) {
missingField = mContext.get().getString(R.string.import_dialog_secondary).toLowerCase();
fieldValue = secondary;
}

if (missingField != null) {
String missingIdMessage = String.format(missingIdMessageTemplate, missingField, fieldValue, line + 1);
failMessage = StringUtil.INSTANCE.applyBoldStyleToString(
String.format("%s\n\n%s", missingIdMessage, fixFileMessage),
fieldValue,
String.valueOf(line + 1)
);
}
}
}

line++;
}

controller.getDatabase().setTransactionSuccessfull();
Log.d(TAG, "doInBackground: Field data created successfully for study ID: " + studyId);

} catch (Exception e) {
Log.e(TAG, "doInBackground: Exception at line " + line + ", Error: " + e.getMessage(), e);

lineFail = line;

Expand All @@ -185,6 +221,8 @@ protected Integer doInBackground(Integer... params) {
controller.getDatabase().endTransaction();

}
} else {
Log.d(TAG, "doInBackground: Required indices not found. UniqueIndex: " + uniqueIndex + ", PrimaryIndex: " + primaryIndex + ", SecondaryIndex: " + secondaryIndex);
}


Expand All @@ -198,7 +236,7 @@ protected Integer doInBackground(Integer... params) {
} catch (Exception e) {
e.printStackTrace();
fail = true;

failMessage = mContext.get().getString(R.string.import_runnable_create_field_data_failed);
controller.getDatabase().close();
controller.getDatabase().open();
}
Expand All @@ -215,28 +253,27 @@ protected void onPostExecute(Integer result) {
if (dialog.isShowing())
dialog.dismiss();

if (fail | uniqueFail | mFieldFile.hasSpecialCharacters()) {
// Display user feedback in an alert dialog
if (context != null && (uniqueFail || mFieldFile.hasSpecialCharacters())) {
CharSequence errorMessage = mFieldFile.getLastError();
showAlertDialog(context, "Unable to Import", errorMessage);
} else if (context != null && fail ) {
showAlertDialog(context, "Unable to Import", failMessage);
} else if (containsDuplicates) {
showAlertDialog(context, "Import Warning", context.getString(R.string.import_runnable_duplicates_skipped));
}

if (fail || uniqueFail || mFieldFile.hasSpecialCharacters()) {
controller.getDatabase().deleteField(result);
SharedPreferences.Editor ed = preferences.edit();
ed.putString(GeneralKeys.FIELD_FILE, null);
ed.putBoolean(GeneralKeys.IMPORT_FIELD_FINISHED, false);
ed.apply();
}
if (containsDuplicates) {
Utils.makeToast(context, context.getString(R.string.import_runnable_duplicates_skipped));
}
if (fail) {
Utils.makeToast(context, context.getString(R.string.import_runnable_create_field_data_failed, lineFail));
//makeToast(getString(R.string.import_error_general));
} else if (uniqueFail && context != null) {
Utils.makeToast(context,context.getString(R.string.import_error_unique));
} else if (mFieldFile.hasSpecialCharacters()) {
Utils.makeToast(context,context.getString(R.string.import_error_unique_characters_illegal));
} else {
SharedPreferences.Editor ed = preferences.edit();
Log.d(TAG, "onPostExecute: Import successful. Field setup for ID: " + result);

SharedPreferences.Editor ed = preferences.edit();
CollectActivity.reloadData = true;

controller.queryAndLoadFields();

try {
Expand All @@ -262,11 +299,26 @@ protected void onPostExecute(Integer result) {
}

private boolean verifyUniqueColumn(FieldFileObject.FieldFileBase fieldFile) {
HashMap<String, String> check = fieldFile.getColumnSet(idColPosition);
if (check.isEmpty()) {
HashMap<String, String> result = fieldFile.getColumnSet(unique, idColPosition);
if (result == null) {
return false;
} else {
return controller.getDatabase().checkUnique(check);
return controller.getDatabase().checkUnique(result);
}
}

private void showAlertDialog(Context context, String title, CharSequence message) {
new AlertDialog.Builder(context, R.style.AppAlertDialog)
.setTitle(title)
.setMessage(message)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setCancelable(false)
.show();
}

}
Loading