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

Create drop-down for Resurvey Validation key-value pairs and include checkbox #2538

Open
wants to merge 1 commit into
base: master
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
@@ -0,0 +1,115 @@
package de.blau.android.validation;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.app.Instrumentation;
import android.content.Context;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;

import de.blau.android.Main;
import de.blau.android.R;
import de.blau.android.prefs.PrefEditorFragment;
import de.blau.android.prefs.Preferences;
import de.blau.android.validation.ValidatorRulesUI;
import de.blau.android.LayerUtils;
import de.blau.android.TestUtils;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ValidatorRulesUITest {

Context context = null;
Instrumentation.ActivityMonitor monitor = null;
Instrumentation instrumentation = null;
UiDevice device = null;
Main main = null;
ValidatorRulesUI validatorRulesUI;

@Rule
public ActivityTestRule<Main> mActivityRule = new ActivityTestRule<>(Main.class);

/**
* Pre-test setup
*/
@Before
public void setup() {
instrumentation = InstrumentationRegistry.getInstrumentation();
device = UiDevice.getInstance(instrumentation);
context = instrumentation.getTargetContext();
monitor = instrumentation.addMonitor(ValidatorRulesUI.class.getName(), null, false);
main = mActivityRule.getActivity();
Preferences prefs = new Preferences(context);
LayerUtils.removeImageryLayers(context);

TestUtils.grantPermissons(device);
TestUtils.dismissStartUpDialogs(device, main);
TestUtils.stopEasyEdit(main);

}

// @After
// public void teardown(){ instrumentation.removeMonitor(monitor);}

@Test
public void addRuleset(){
// ValidatorRulesUI Vri = new ValidatorRulesUI();
// Vri.manageRulesetContents(context);
// TestUtils.clickText(device, false, "shop", true);
}
}
// @Test
// public void testToggleHeaderCheckbox() {
// // Open the resurveyList and check the initial state
// Cursor resurveyCursor = ValidatorRulesDatabase.queryResurveyByName(writableDb, ValidatorRulesDatabase.DEFAULT_RULESET_NAME);
// ResurveyAdapter resurveyAdapter = new ResurveyAdapter(writableDb, context, resurveyCursor);
// resurveyList.setAdapter(resurveyAdapter);
// runUiThreadTasksIncludingDelayedTasks();
//
//
// // Click the header checkbox
// headerEnabled.performClick();
// headerEnabled.performClick();
// runUiThreadTasksIncludingDelayedTasks();
//
// // Verify that all child checkboxes are checked
// assertFalse(headerEnabled.isChecked());
// for (int i = 0; i < resurveyList.getChildCount(); i++) {
// View childView = resurveyList.getChildAt(i);
// CheckBox childCheckBox = childView.findViewById(R.id.resurvey_enabled);
// assertFalse(childCheckBox.isChecked());
// }
//
// // Click the "Done" button to save the changes
// // Assuming you have a method to get the "Done" button and click it
// clickDoneButton();
//
// // Re-open the resurveyList
// resurveyCursor = ValidatorRulesDatabase.queryResurveyByName(writableDb, ValidatorRulesDatabase.DEFAULT_RULESET_NAME);
// resurveyAdapter = new ResurveyAdapter(writableDb, context, resurveyCursor);
// resurveyList.setAdapter(resurveyAdapter);
// runUiThreadTasksIncludingDelayedTasks();
//
// // Verify that the changes are persisted
// assertFalse(headerEnabled.isChecked());
// for (int i = 0; i < resurveyList.getChildCount(); i++) {
// View childView = resurveyList.getChildAt(i);
// CheckBox childCheckBox = childView.findViewById(R.id.resurvey_enabled);
// assertFalse(childCheckBox.isChecked());
// }
// }
//
//// Helper method to click the "Done" button
//private void clickDoneButton() {
// // Implement the logic to find and click the "Done" button
// // based on your application's UI structure
// }
//}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package de.blau.android.validation;

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

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import de.blau.android.bookmarks.BookmarkStorage;
import de.blau.android.util.collections.MultiHashMap;

/**
Expand All @@ -19,10 +25,10 @@
public final class ValidatorRulesDatabase {
/**
* Table: rulesets (id INTEGER, name TEXT) Table: resurvey (ruleset INTEGER, key TEXT, value TEXT DEFAULT NULL, days
* INTEGER DEFAULT 365, FOREIGN KEY(ruleset) REFERENCES rulesets(id)) Table: check (ruleset INTEGER, key TEXT,
* INTEGER DEFAULT 365, enabled INTEGER DEFAULT 1, FOREIGN KEY(ruleset) REFERENCES rulesets(id)) Table: check (ruleset INTEGER, key TEXT,
* optional INTEGER DEFAULT 0, FOREIGN KEY(ruleset) REFERENCES rulesets(id))
*/

private static final String DEBUG_TAG = ValidatorRulesDatabase.class.getSimpleName().substring(0, Math.min(23, ValidatorRulesDatabase.class.getSimpleName().length()));
static final String DEFAULT_RULESET_NAME = "Default";
static final int DEFAULT_RULESET = 0;
private static final String RULESET_TABLE = "rulesets";
Expand All @@ -36,11 +42,13 @@ public final class ValidatorRulesDatabase {
static final String RULESET_FIELD = "ruleset";
private static final String CHECK_TABLE = "checktags";
static final String OPTIONAL_FIELD = "optional";
static final String ENABLED_FIELD = "enabled";

static final String QUERY_RESURVEY_DEFAULT = "SELECT resurveytags.rowid as _id, key, value, is_regexp, days FROM resurveytags WHERE ruleset = "
static final String QUERY_RESURVEY_DEFAULT = "SELECT resurveytags.rowid as _id, key, value, is_regexp, days, enabled FROM resurveytags WHERE ruleset = "
+ DEFAULT_RULESET + " ORDER BY key, value";
static final String QUERY_RESURVEY_BY_ROWID = "SELECT key, value, is_regexp, days FROM resurveytags WHERE rowid=?";
static final String QUERY_RESURVEY_BY_NAME = "SELECT resurveytags.rowid as _id, key, value, is_regexp, days FROM resurveytags, rulesets WHERE ruleset = rulesets.id and rulesets.name = ? ORDER BY key, value";
static final String QUERY_RESURVEY_ALL_ID = "SELECT resurveytags.rowid as _id FROM resurveytags";
static final String QUERY_RESURVEY_BY_ROWID = "SELECT key, value, is_regexp, days, enabled FROM resurveytags WHERE rowid=?";
static final String QUERY_RESURVEY_BY_NAME = "SELECT resurveytags.rowid as _id, key, value, is_regexp, days, enabled FROM resurveytags, rulesets WHERE ruleset = rulesets.id and rulesets.name = ? ORDER BY key, value";

static final String QUERY_CHECK_DEFAULT = "SELECT checktags.rowid as _id, key, optional FROM checktags WHERE ruleset = " + DEFAULT_RULESET
+ " ORDER BY key";
Expand Down Expand Up @@ -77,19 +85,21 @@ public static void addRuleset(SQLiteDatabase db, int id, String name) {
@Nullable
public static MultiHashMap<String, PatternAndAge> getDefaultResurvey(@NonNull SQLiteDatabase database) {
MultiHashMap<String, PatternAndAge> result = null;
Cursor dbresult = database.query(RESURVEY_TABLE, new String[] { KEY_FIELD, VALUE_FIELD, ISREGEXP_FIELD, DAYS_FIELD },
Cursor dbresult = database.query(RESURVEY_TABLE, new String[] { KEY_FIELD, VALUE_FIELD, ISREGEXP_FIELD, DAYS_FIELD, ENABLED_FIELD },
RULESET_FIELD + " = " + DEFAULT_RULESET, null, null, null, KEY_FIELD + "," + VALUE_FIELD);

if (dbresult.getCount() >= 1) {
result = new MultiHashMap<>();
boolean haveEntry = dbresult.moveToFirst();
while (haveEntry) {
PatternAndAge v = new PatternAndAge();
v.setValue(dbresult.getString(1));
v.setIsRegexp(dbresult.getInt(2) == 1);
v.setAge(dbresult.getLong(3) * 24 * 3600); // days -> secs
result.add(dbresult.getString(0), v);
haveEntry = dbresult.moveToNext();
if (dbresult.getInt(dbresult.getColumnIndexOrThrow(ValidatorRulesDatabase.ENABLED_FIELD)) == 1) {
PatternAndAge v = new PatternAndAge();
v.setValue(dbresult.getString(1));
v.setIsRegexp(dbresult.getInt(2) == 1);
v.setAge(dbresult.getLong(3) * 24 * 3600); // days -> secs
result.add(dbresult.getString(0), v);
haveEntry = dbresult.moveToNext();
}
}
}
dbresult.close();
Expand All @@ -110,6 +120,29 @@ public static Cursor queryResurveyByName(@NonNull SQLiteDatabase database, @Null
return database.rawQuery(ValidatorRulesDatabase.QUERY_RESURVEY_BY_NAME, new String[] { name });
}
}
/**
* Return all the resurvey ids at the time a new instance is created
*
* @param database readable database
* @return a List of resurvey ids
*/
@Nullable
public static List<Integer> getAllResurveyIds(@NonNull SQLiteDatabase database) {
List<Integer> result = new ArrayList<>();
Cursor dbresult = database.rawQuery(ValidatorRulesDatabase.QUERY_RESURVEY_ALL_ID, null);
int counter = 0;
if (dbresult.getCount() >= 1) {
boolean haveEntry = dbresult.moveToFirst();
while (haveEntry) {
result.add(dbresult.getInt(dbresult.getColumnIndexOrThrow("_id")));
haveEntry = dbresult.moveToNext();
counter++;
}
}
dbresult.close();
Log.d(DEBUG_TAG, "Retrieved" + counter +"ids from validator database");
return result;
}

/**
* Add an entry to the resurvey table
Expand All @@ -121,13 +154,14 @@ public static Cursor queryResurveyByName(@NonNull SQLiteDatabase database, @Null
* @param isRegexp if true the value is a regexp
* @param days how man days old the object should max be
*/
public static void addResurvey(@NonNull SQLiteDatabase db, int ruleSetId, @NonNull String key, @Nullable String value, boolean isRegexp, int days) {
public static void addResurvey(@NonNull SQLiteDatabase db, int ruleSetId, @NonNull String key, @Nullable String value, boolean isRegexp, int days, boolean enabled) {
ContentValues values = new ContentValues();
values.put(RULESET_FIELD, ruleSetId);
values.put(KEY_FIELD, key);
values.put(VALUE_FIELD, value);
values.put(ISREGEXP_FIELD, isRegexp ? 1 : 0);
values.put(DAYS_FIELD, days);
values.put(ENABLED_FIELD, enabled ? 1 : 0);
db.insert(RESURVEY_TABLE, null, values);
}

Expand Down Expand Up @@ -160,6 +194,33 @@ static void deleteResurvey(final SQLiteDatabase db, final int id) {
db.delete(RESURVEY_TABLE, "rowid=?", new String[] { Integer.toString(id) });
}

/**
* Change the enabled row of the resurvey table.
*
* @param db writable database
* @param idIsEnabledMap rowid and their checkbox state
*/
public static void enableResurvey(SQLiteDatabase db, Map<Integer, Boolean> idIsEnabledMap) {
List<ContentValues> contentValuesList = new ArrayList<>();
for (Map.Entry<Integer, Boolean> entry : idIsEnabledMap.entrySet()) {
ContentValues contentValues = new ContentValues();
contentValues.put(ENABLED_FIELD, entry.getValue() ? 1 : 0);
contentValuesList.add(contentValues);
}
StringBuilder selectionBuilder = new StringBuilder("rowid IN (");
List<String> selectionArgs = new ArrayList<>();
for (int key : idIsEnabledMap.keySet()) {
selectionBuilder.append("?,");
selectionArgs.add(String.valueOf(key));
}
selectionBuilder.replace(selectionBuilder.length() - 1, selectionBuilder.length(), ")");
String selection = selectionBuilder.toString();

int rowsUpdated = db.update(RESURVEY_TABLE, contentValuesList.get(0), selection, selectionArgs.toArray(new String[0]));
Log.d(DEBUG_TAG, "Rulesets updated: " + rowsUpdated);

}

/**
* Return the default resurvey entries if any
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ValidatorRulesDatabaseHelper extends SQLiteOpenHelper {
private static final String DEBUG_TAG = ValidatorRulesDatabaseHelper.class.getSimpleName().substring(0, Math.min(23, ValidatorRulesDatabaseHelper.class.getSimpleName().length()));

private static final String DATABASE_NAME = "validator_rules";
private static final int DATABASE_VERSION = 3;
private static final int DATABASE_VERSION = 4;
static final int ONE_YEAR = 365;

/**
Expand All @@ -31,18 +31,18 @@ public void onCreate(SQLiteDatabase db) {
ValidatorRulesDatabase.addRuleset(db, ValidatorRulesDatabase.DEFAULT_RULESET, ValidatorRulesDatabase.DEFAULT_RULESET_NAME);

db.execSQL(
"CREATE TABLE resurveytags (ruleset INTEGER, key TEXT, value TEXT DEFAULT NULL, is_regexp INTEGER DEFAULT 0, days INTEGER DEFAULT 365, FOREIGN KEY(ruleset) REFERENCES rulesets(id))");
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_SHOP, null, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_RESTAURANT, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_FAST_FOOD, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_CAFE, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_PUB, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_BAR, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_TOILETS, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_LANDUSE, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_HIGHWAY, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_RAILWAY, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_BUILDING, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR);
"CREATE TABLE resurveytags (ruleset INTEGER, key TEXT, value TEXT DEFAULT NULL, is_regexp INTEGER DEFAULT 0, days INTEGER DEFAULT 365, enabled INTEGER DEFAULT 1, FOREIGN KEY(ruleset) REFERENCES rulesets(id))");
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_SHOP, null, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_RESTAURANT, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_FAST_FOOD, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_CAFE, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_PUB, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_BAR, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_TOILETS, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_LANDUSE, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_HIGHWAY, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_RAILWAY, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_BUILDING, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR, true);

db.execSQL("CREATE TABLE checktags (ruleset INTEGER, key TEXT, optional INTEGER DEFAULT 0, FOREIGN KEY(ruleset) REFERENCES rulesets(id))");
ValidatorRulesDatabase.addCheck(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_OPENING_HOURS, false);
Expand All @@ -62,5 +62,8 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion <= 2 && newVersion >= 3) {
db.execSQL("UPDATE checktags SET key='name|ref' WHERE key='name'");
}
if (oldVersion <= 3 && newVersion >= 4) {
db.execSQL("ALTER TABLE resurveytags ADD COLUMN enabled INTEGER DEFAULT 1");
}
}
}
Loading
Loading