Skip to content

Commit

Permalink
Merge pull request #1095 from PhenoApps/brapi_shared_study_names
Browse files Browse the repository at this point in the history
brapi_shared_study_names
  • Loading branch information
trife authored Dec 11, 2024
2 parents c645b91 + 65b072e commit f936d89
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected Integer doInBackground(Integer... params) {
f.setPrimary_id(primary);
f.setSecondary_id(secondary);

studyId = controller.getDatabase().createField(f, nonEmptyColumns);
studyId = controller.getDatabase().createField(f, nonEmptyColumns, false);

controller.getDatabase().beginTransaction();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ public BrapiControllerResponse saveStudyDetails(BrapiStudyDetails studyDetails,

DataHelper.db.beginTransaction();
// All checks finished, insert our data.
int expId = dataHelper.createField(field, studyDetails.getAttributes());
int expId = dataHelper.createField(field, studyDetails.getAttributes(), true);
field.setExp_id(expId);

boolean fail = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,7 @@ public BrapiControllerResponse saveStudyDetails(BrapiStudyDetails studyDetails,
field.setExp_sort(sortOrder);

// Do a pre-check to see if the field exists so we can show an error
int FieldUniqueStatus = dataHelper.checkFieldNameAndObsLvl(field.getExp_name(), field.getObservation_level());
int FieldUniqueStatus = dataHelper.checkBrapiStudyUnique(field.getObservation_level(), field.getStudy_db_id());
if (FieldUniqueStatus != -1) {
return new BrapiControllerResponse(false, this.notUniqueFieldMessage);
}
Expand All @@ -1586,7 +1586,7 @@ public BrapiControllerResponse saveStudyDetails(BrapiStudyDetails studyDetails,

DataHelper.db.beginTransaction();
// All checks finished, insert our data.
int expId = dataHelper.createField(field, studyDetails.getAttributes());
int expId = dataHelper.createField(field, studyDetails.getAttributes(), true);
field.setExp_id(expId);

boolean fail = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2311,19 +2311,19 @@ public int checkFieldName(String name) {
// return -1;
}

public int checkFieldNameAndObsLvl(String name, String observationLevel) {
public int checkBrapiStudyUnique(String observationLevel, String brapiId) {

open();

return StudyDao.Companion.checkFieldNameAndObsLvl(name, observationLevel);
return StudyDao.Companion.checkBrapiStudyUnique(observationLevel, brapiId);
}

public int createField(FieldObject e, List<String> columns) {
public int createField(FieldObject e, List<String> columns, Boolean fromBrapi) {
// String exp_name, String exp_alias, String unique_id, String primary_id, String secondary_id, String[] columns){

open();

return StudyDao.Companion.createField(e, timeStamp.format(Calendar.getInstance().getTime()), columns);
return StudyDao.Companion.createField(e, timeStamp.format(Calendar.getInstance().getTime()), columns, fromBrapi);

// long exp_id = checkFieldName(e.getExp_name());
// if (exp_id != -1) {
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/java/com/fieldbook/tracker/database/dao/StudyDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,12 @@ class StudyDao {
* This function uses a field object to create a exp/study row in the database.
* Columns are new observation unit attribute names that are inserted as well.
*/
fun createField(e: FieldObject, timestamp: String, columns: List<String>): Int = withDatabase { db ->
fun createField(e: FieldObject, timestamp: String, columns: List<String>, fromBrapi: Boolean): Int = withDatabase { db ->

when (val sid = checkFieldNameAndObsLvl(e.exp_name, e.observation_level)) {
when (val sid = if (fromBrapi) checkBrapiStudyUnique(
e.observation_level,
e.study_db_id
) else checkFieldNameAndObsLvl(e.exp_name, e.observation_level)) {

-1 -> {

Expand Down Expand Up @@ -564,6 +567,16 @@ class StudyDao {

} ?: -1

fun checkBrapiStudyUnique(observationLevel: String?, brapiId: String?): Int = withDatabase { db ->
db.query(
Study.tableName,
arrayOf(Study.PK),
where = "observation_levels = ? AND study_db_id = ?",
whereArgs = arrayOf(observationLevel ?: "", brapiId ?: "")
).toFirst()[Study.PK] as? Int ?: -1

} ?: -1

fun getCount(studyId: Int): Int = withDatabase {

ObservationUnitDao.getAll(studyId).size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ class FieldCreatorDialogFragment(private val activity: ThemedActivity) :
}

val fieldColumns = listOf("Row", "Column", "Plot", "plot_id")
studyDbId = helper.createField(field, fieldColumns)
studyDbId = helper.createField(field, fieldColumns, false)

updateFieldInsertText(rows.toString(), cols.toString())
insertPlotData(
Expand Down

0 comments on commit f936d89

Please sign in to comment.