Skip to content

Commit

Permalink
Merge pull request #862 from akvo/issue/855-fix-monitoring-forms-disa…
Browse files Browse the repository at this point in the history
…bled

Fix monitoring forms disabled (connect #855)
  • Loading branch information
stellanl authored Sep 6, 2017
2 parents bcf681b + 272d97f commit d002d8e
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 279 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android:
- tools
- build-tools-25.0.1
- android-25
- android-19
- android-21
- extra-android-support
- extra-google-m2repository
- extra-android-m2repository
Expand Down
43 changes: 43 additions & 0 deletions app/src/main/java/org/akvo/flow/data/database/SurveyDbAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,17 @@ public void clearSurveyedLocaleName(long surveyInstanceId) {
new String[] { surveyedLocaleId });
}

public Cursor getDatapointStatus(String recordId) {
Cursor cursor = database.query(Tables.SURVEY_INSTANCE,
new String[] {
SurveyInstanceColumns.STATUS
},
SurveyInstanceColumns.RECORD_ID + "= ?",
new String[] { String.valueOf(recordId) },
null, null, null);
return cursor;
}

/**
* Flag to indicate the type of locale update from a given response
*/
Expand Down Expand Up @@ -1374,6 +1385,38 @@ public Cursor query(String table, String[] columns, String selection, String[] s
return database.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}

public Cursor getDataPointForms(long surveyGroupId, String recordId) {
String table = SurveyDbAdapter.SURVEY_JOIN_SURVEY_INSTANCE;
if (recordId != null) {
// Add record id to the join condition. If put in the where, the left join won't work
table += " AND " + Tables.SURVEY_INSTANCE + "." + SurveyInstanceColumns.RECORD_ID
+ "='" + recordId + "'";
}
return database.query(table,
SurveyQuery.PROJECTION,
SurveyColumns.SURVEY_GROUP_ID + " = ?",
new String[] { String.valueOf(surveyGroupId) },
Tables.SURVEY + "." + SurveyColumns.SURVEY_ID,
null,
SurveyColumns.NAME);
}

public interface SurveyQuery {
String[] PROJECTION = {
Tables.SURVEY + "." + SurveyColumns.SURVEY_ID,
Tables.SURVEY + "." + SurveyColumns.NAME,
Tables.SURVEY + "." + SurveyColumns.VERSION,
Tables.SURVEY + "." + SurveyColumns.DELETED,
Tables.SURVEY_INSTANCE + "." + SurveyInstanceColumns.SUBMITTED_DATE,
};

int SURVEY_ID = 0;
int NAME = 1;
int VERSION = 2;
int DELETED = 3;
int SUBMITTED = 4;
}

// Wrap DB projections and column indexes.
public interface RecordQuery {
String[] PROJECTION = {
Expand Down
115 changes: 115 additions & 0 deletions app/src/main/java/org/akvo/flow/data/loader/FormInfoLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (C) 2010-2017 Stichting Akvo (Akvo Foundation)
*
* This file is part of Akvo Flow.
*
* Akvo Flow is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Akvo Flow is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Akvo Flow. If not, see <http://www.gnu.org/licenses/>.
*
*/

package org.akvo.flow.data.loader;

import android.content.Context;
import android.database.Cursor;
import android.support.annotation.NonNull;

import org.akvo.flow.data.database.SurveyDbAdapter;
import org.akvo.flow.data.database.SurveyInstanceColumns;
import org.akvo.flow.data.database.SurveyInstanceStatus;
import org.akvo.flow.data.loader.base.AsyncLoader;
import org.akvo.flow.data.loader.models.FormInfo;
import org.akvo.flow.domain.SurveyGroup;

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

/**
* Loader to query the database and return the list of surveys. This Loader will
* include the latest submission date for each survey, if exists.
*/
public class FormInfoLoader extends AsyncLoader<List<FormInfo>> {

private final long surveyGroupId;
private final String recordId;
private final SurveyGroup surveyGroup;

public FormInfoLoader(Context context, String recordId, SurveyGroup surveyGroup) {
super(context);
this.surveyGroup = surveyGroup;
this.surveyGroupId = surveyGroup.getId();
this.recordId = recordId;
}

@Override
public List<FormInfo> loadInBackground() {
SurveyDbAdapter database = new SurveyDbAdapter(getContext());
database.open();

boolean submittedDataPoint = isDataPointSubmitted(database);
List<FormInfo> forms = retrieveForms(database, submittedDataPoint);

database.close();
return forms;
}

@NonNull
private List<FormInfo> retrieveForms(SurveyDbAdapter database, boolean submittedDataPoint) {
List<FormInfo> surveys = new ArrayList<>();
Cursor cursor = database.getDataPointForms(surveyGroupId, recordId);
if (cursor.moveToFirst()) {
do {

String id = cursor.getString(SurveyDbAdapter.SurveyQuery.SURVEY_ID);
String name = cursor.getString(SurveyDbAdapter.SurveyQuery.NAME);
String version = String
.valueOf(cursor.getFloat(SurveyDbAdapter.SurveyQuery.VERSION));
boolean registrationSurvey = isRegistrationForm(id);

Long lastSubmission = null;
if (!cursor.isNull(SurveyDbAdapter.SurveyQuery.SUBMITTED)) {
lastSubmission = cursor.getLong(SurveyDbAdapter.SurveyQuery.SUBMITTED);
}
boolean deleted = cursor.getInt(SurveyDbAdapter.SurveyQuery.DELETED) == 1;

FormInfo s = new FormInfo(id, name, version, lastSubmission, deleted,
registrationSurvey, submittedDataPoint);
if (surveyGroup.isMonitored() && registrationSurvey) {
surveys.add(0, s);// Make sure registration survey is at the top
} else {
surveys.add(s);
}
} while (cursor.moveToNext());
}
cursor.close();
return surveys;
}

private boolean isDataPointSubmitted(SurveyDbAdapter database) {
boolean submittedDataPoint = false;
Cursor c = database.getDatapointStatus(recordId);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(SurveyInstanceColumns.STATUS));
submittedDataPoint = status == SurveyInstanceStatus.SUBMITTED
|| status == SurveyInstanceStatus.EXPORTED
|| status == SurveyInstanceStatus.SYNCED
|| status == SurveyInstanceStatus.DOWNLOADED;
}
c.close();
return submittedDataPoint;
}

private boolean isRegistrationForm(String surveyId) {
return surveyId.equals(surveyGroup.getRegisterSurveyId());
}
}
119 changes: 0 additions & 119 deletions app/src/main/java/org/akvo/flow/data/loader/SurveyInfoLoader.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@

package org.akvo.flow.data.loader.models;

public class SurveyInfo {
public class FormInfo {

private final String id;
private final String name;
private final String version;
private final Long lastSubmission;
private final boolean deleted;
private final boolean isRegistrationSurvey;
private final boolean submittedDataPoint;

public SurveyInfo(String id, String name, String version, Long lastSubmission,
boolean deleted, boolean isRegistrationSurvey) {
public FormInfo(String id, String name, String version, Long lastSubmission,
boolean deleted, boolean isRegistrationSurvey, boolean submittedDataPoint) {
this.id = id;
this.name = name;
this.version = version;
this.lastSubmission = lastSubmission;
this.deleted = deleted;
this.isRegistrationSurvey = isRegistrationSurvey;
this.submittedDataPoint = submittedDataPoint;
}

public String getId() {
Expand All @@ -59,7 +61,16 @@ public boolean isDeleted() {
return deleted;
}

public boolean isRegistrationSurvey() {
public boolean isRegistrationForm() {
return isRegistrationSurvey;
}

public boolean isSubmittedDataPoint() {
return submittedDataPoint;
}

public boolean hasBeenSubmitted() {
return lastSubmission != null && lastSubmission != 0L;
}

}
Loading

0 comments on commit d002d8e

Please sign in to comment.