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

Add get table view layout and latest tallies #127

Open
wants to merge 15 commits 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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=0.1.5-SNAPSHOT
VERSION_NAME=0.2.0-alpha-SNAPSHOT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to update the snapshot version for tagging and snapshot release

VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Reporting Library
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ interface Model {

List<Map<String, IndicatorTally>> getIndicatorsDailyTallies();

List<Map<String, IndicatorTally>> getLatestIndicatorTallies();

}

interface IndicatorView {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public List<Map<String, IndicatorTally>> getIndicatorsDailyTallies() {
return dailyIndicatorCountRepository.getAllDailyTallies();
}

public List<Map<String, IndicatorTally>> getLatestIndicatorTallies() {
return dailyIndicatorCountRepository.getLatestIndicatorTallies();
}

@Override
public void generateDailyIndicatorTallies(String lastProcessedDate) {
SQLiteDatabase database = getReportingLibrary().getRepository().getWritableDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ public List<Map<String, IndicatorTally>> getIndicatorsDailyTallies() {
dao.setDailyIndicatorCountRepository(reportingLibrary.dailyIndicatorCountRepository());
return dao.getIndicatorsDailyTallies();
}

@Override
public List<Map<String, IndicatorTally>> getLatestIndicatorTallies() {
dao.setDailyIndicatorCountRepository(reportingLibrary.dailyIndicatorCountRepository());
return dao.getLatestIndicatorTallies();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.smartregister.reporting.domain;

import java.util.Date;
import java.util.Map;

public class MultiValueIndicatorTally extends IndicatorTally {

private Map<String, String> multiValuesMap;

public MultiValueIndicatorTally(Long id, int count, String indicatorCode, Date createdAt, Map<String, String> multiResults) {
super(id, count, indicatorCode, createdAt);
this.multiValuesMap = multiResults;
}

public MultiValueIndicatorTally() {
}

public Map<String, String> getMultiValuesMap() {
return multiValuesMap;
}

public void setMultiValuesMap(Map<String, String> multiValuesMap) {
this.multiValuesMap = multiValuesMap;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.smartregister.reporting.domain;

import java.util.List;

public class TabularVisualization extends ReportingIndicatorVisualization {

private int titleLabelStringResource;
private List<String> tableHeaderList;
private List<String> tableDataList;
private boolean tableHeaderHidden;

public TabularVisualization(int titleLabelStringResource, List<String> tableHeaderList, List<String> tableDataList, boolean tableHeaderHidden) {
this.titleLabelStringResource = titleLabelStringResource;
this.tableHeaderList = tableHeaderList;
this.tableDataList = tableDataList;
this.tableHeaderHidden = tableHeaderHidden;
}

public int getTitleLabelStringResource() {
return titleLabelStringResource;
}

public void setTitleLabelStringResource(int titleLabelStringResource) {
this.titleLabelStringResource = titleLabelStringResource;
}

public List<String> getTableHeaderList() {
return tableHeaderList;
}

public void setTableHeaderList(List<String> tableHeaderList) {
this.tableHeaderList = tableHeaderList;
}

public List<String> getTableDataList() {
return tableDataList;
}

public void setTableDataList(List<String> tableDataList) {
this.tableDataList = tableDataList;
}

public boolean isTableHeaderHidden() {
return tableHeaderHidden;
}

public void setTableHeaderHidden(boolean tableHeaderHidden) {
this.tableHeaderHidden = tableHeaderHidden;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.smartregister.reporting.factory;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import org.smartregister.reporting.R;
import org.smartregister.reporting.domain.ReportingIndicatorVisualization;
import org.smartregister.reporting.domain.TabularVisualization;
import org.smartregister.reporting.view.TableView;

import java.util.ArrayList;

public class TableDisplayFactory implements IndicatorVisualisationFactory {

@Override
public View getIndicatorView(ReportingIndicatorVisualization visualization, Context context) {
LinearLayout rootLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.table_view_layout, null);
TextView tableTitle = rootLayout.findViewById(R.id.tableViewTitle);
TableView tableView = rootLayout.findViewById(R.id.tableView);

TabularVisualization tabularVisualization = (TabularVisualization) visualization;

tableTitle.setText(tabularVisualization.getTitleLabelStringResource());
tableView.setTableData(tabularVisualization.getTableHeaderList(), tabularVisualization.getTableDataList(), new ArrayList<>(), null);
tableView.setHeaderTextStyle(Typeface.BOLD);
tableView.setRowBorderHidden(false);
tableView.setBorderColor(ContextCompat.getColor(context, R.color.light_grey)); // Default
tableView.setTableHeaderHidden(tabularVisualization.isTableHeaderHidden());
return rootLayout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,26 @@ public class DailyIndicatorCountRepository extends BaseRepository {
+ Constants.DailyIndicatorCountRepository.INDICATOR_VALUE_SET_FLAG + " BOOLEAN NOT NULL default 0, "
+ Constants.DailyIndicatorCountRepository.DAY + " DATETIME NOT NULL DEFAULT (DATETIME('now')))";

private static String CREATE_UNIQUE_CONSTRAINT = "CREATE UNIQUE INDEX indicator_daily_tally_ix ON " +
private static final String CREATE_UNIQUE_CONSTRAINT = "CREATE UNIQUE INDEX indicator_daily_tally_ix ON " +
Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE + " ( " +
Constants.DailyIndicatorCountRepository.INDICATOR_CODE + " , "
+ Constants.DailyIndicatorCountRepository.DAY + " ) ";

private static final Object[] dailyIndicatorQueryArgs = {
Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.ID
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.INDICATOR_CODE
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.INDICATOR_VALUE
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.INDICATOR_VALUE_SET
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.INDICATOR_GROUPING
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.INDICATOR_VALUE_SET_FLAG
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.DAY
, Constants.IndicatorQueryRepository.INDICATOR_QUERY_TABLE, Constants.IndicatorQueryRepository.INDICATOR_QUERY_EXPECTED_INDICATORS
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE
, Constants.IndicatorQueryRepository.INDICATOR_QUERY_TABLE
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.INDICATOR_CODE
, Constants.IndicatorQueryRepository.INDICATOR_QUERY_TABLE, Constants.IndicatorQueryRepository.INDICATOR_CODE
};

public static void performMigrations(@NonNull SQLiteDatabase database) {
// Perform migrations
if (ReportingUtils.isTableExists(database, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE)
Expand Down Expand Up @@ -109,15 +124,13 @@ public void add(@Nullable CompositeIndicatorTally indicatorTally) {
database.insert(Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, null, createContentValues(indicatorTally));
}

public void updateIndicatorValue(String id, String Value)
{
public void updateIndicatorValue(String id, String Value) {
SQLiteDatabase database = getWritableDatabase();
Cursor cursor = null;
try{
cursor = database.rawQuery("UPDATE "+Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE+" SET "+ Constants.DailyIndicatorCountRepository.INDICATOR_VALUE+" = ? WHERE "+ Constants.DailyIndicatorCountRepository.ID+" = ?",new Object[]{Value,id});
try {
cursor = database.rawQuery("UPDATE " + Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE + " SET " + Constants.DailyIndicatorCountRepository.INDICATOR_VALUE + " = ? WHERE " + Constants.DailyIndicatorCountRepository.ID + " = ?", new Object[]{Value, id});
cursor.moveToFirst();
}
catch (Exception ex) {
} catch (Exception ex) {
Timber.e(ex.toString());
} finally {
if (cursor != null) {
Expand All @@ -129,49 +142,33 @@ public void updateIndicatorValue(String id, String Value)

public List<Map<String, IndicatorTally>> getAllDailyTallies() {
List<Map<String, IndicatorTally>> indicatorTallies = new ArrayList<>();
Map<String, IndicatorTally> tallyMap;

SQLiteDatabase database = getReadableDatabase();
Object[] queryArgs = {
Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.ID
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.INDICATOR_CODE
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.INDICATOR_VALUE
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.INDICATOR_VALUE_SET
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.INDICATOR_GROUPING
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.INDICATOR_VALUE_SET_FLAG
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.DAY
, Constants.IndicatorQueryRepository.INDICATOR_QUERY_TABLE, Constants.IndicatorQueryRepository.INDICATOR_QUERY_EXPECTED_INDICATORS
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE
, Constants.IndicatorQueryRepository.INDICATOR_QUERY_TABLE
, Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE, Constants.DailyIndicatorCountRepository.INDICATOR_CODE
, Constants.IndicatorQueryRepository.INDICATOR_QUERY_TABLE, Constants.IndicatorQueryRepository.INDICATOR_CODE
};

Cursor cursor = null;
try {
cursor = database.rawQuery(String.format("SELECT %s.%s, %s.%s, %s.%s, %s.%s, %s.%s, %s.%s, %s.%s, %s.%s FROM %s INNER JOIN %s ON %s.%s = %s.%s", queryArgs)
cursor = database.rawQuery(String.format("SELECT %s.%s, %s.%s, %s.%s, %s.%s, %s.%s, %s.%s, %s.%s, %s.%s FROM %s INNER JOIN %s ON %s.%s = %s.%s", dailyIndicatorQueryArgs)
, null);
if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
MultiResultProcessor defaultMultiResultProcessor = ReportingLibrary.getInstance().getDefaultMultiResultProcessor();
ArrayList<MultiResultProcessor> multiResultProcessors = ReportingLibrary.getInstance().getMultiResultProcessors();

while (!cursor.isAfterLast()) {
tallyMap = new HashMap<>();
CompositeIndicatorTally compositeIndicatorTally = processCursorRow(cursor);

if (compositeIndicatorTally.isValueSet()) {
extractIndicatorTalliesFromMultiResult(tallyMap, defaultMultiResultProcessor, multiResultProcessors, compositeIndicatorTally);
} else {
tallyMap.put(compositeIndicatorTally.getIndicatorCode(), compositeIndicatorTally);
}
indicatorTallies = processDailyTalliesQueryResults(cursor);
} catch (Exception ex) {
Timber.e(ex.toString());
} finally {
if (cursor != null) {
cursor.close();
}
}
return indicatorTallies;
}

if (tallyMap.size() > 0) {
indicatorTallies.add(tallyMap);
}
public List<Map<String, IndicatorTally>> getLatestIndicatorTallies() {
List<Map<String, IndicatorTally>> indicatorTallies = new ArrayList<>();
SQLiteDatabase database = getReadableDatabase();

cursor.moveToNext();
}
}
Cursor cursor = null;
try {
cursor = database.rawQuery(String.format("SELECT %s.%s, %s.%s, %s.%s, %s.%s, %s.%s, %s.%s, %s.%s, %s.%s, max(" + Constants.DailyIndicatorCountRepository.DAY + ") as newest_date FROM %s INNER JOIN %s ON %s.%s = %s.%s " +
"GROUP BY " + Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE + "." + Constants.DailyIndicatorCountRepository.INDICATOR_CODE, dailyIndicatorQueryArgs)
, null);
indicatorTallies = processDailyTalliesQueryResults(cursor);
} catch (Exception ex) {
Timber.e(ex.toString());
} finally {
Expand All @@ -182,6 +179,32 @@ public List<Map<String, IndicatorTally>> getAllDailyTallies() {
return indicatorTallies;
}

private List<Map<String, IndicatorTally>> processDailyTalliesQueryResults(Cursor cursor) {
List<Map<String, IndicatorTally>> indicatorTallies = new ArrayList<>();
Map<String, IndicatorTally> tallyMap;
if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
MultiResultProcessor defaultMultiResultProcessor = ReportingLibrary.getInstance().getDefaultMultiResultProcessor();
ArrayList<MultiResultProcessor> multiResultProcessors = ReportingLibrary.getInstance().getMultiResultProcessors();

while (!cursor.isAfterLast()) {
tallyMap = new HashMap<>();
CompositeIndicatorTally compositeIndicatorTally = processCursorRow(cursor);

if (compositeIndicatorTally.isValueSet()) {
extractIndicatorTalliesFromMultiResult(tallyMap, defaultMultiResultProcessor, multiResultProcessors, compositeIndicatorTally);
} else {
tallyMap.put(compositeIndicatorTally.getIndicatorCode(), compositeIndicatorTally);
}

if (tallyMap.size() > 0) {
indicatorTallies.add(tallyMap);
}

cursor.moveToNext();
}
}
return indicatorTallies;
}

@NonNull
public ArrayList<Date> findDaysWithIndicatorCounts(@NonNull SimpleDateFormat dateFormat, @NonNull Date minDate, @NonNull Date maxDate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public class AggregationUtil {
public static float getTotalIndicatorCount(List<Map<String, IndicatorTally>> indicatorTallies,
String indicatorKey) {
String indicatorKey) {
float count = 0;
if (indicatorTallies != null && !indicatorTallies.isEmpty()) {
for (Map<String, IndicatorTally> indicatorTallyMap : indicatorTallies) {
Expand All @@ -27,7 +27,7 @@ public static float getTotalIndicatorCount(List<Map<String, IndicatorTally>> ind
}

public static float getLatestIndicatorCount(List<Map<String, IndicatorTally>> indicatorTallies,
String indicatorKey) {
String indicatorKey) {
float count = 0;
//Back date
Date currentDate = new Date(Long.MIN_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public static PieChartDisplayModel getPieChartDisplayModel(List<PieChartSlice> p

public static PieChartSlice getPieChartSlice(CountType countType, String indicatorCode, String label, int color,
List<Map<String, IndicatorTally>> indicatorTallies, String key) {
return new PieChartSlice((float) getCount(countType, indicatorCode, indicatorTallies), label, color, key);
return new PieChartSlice(getCount(countType, indicatorCode, indicatorTallies), label, color, key);
}

public static PieChartSlice getPieChartSlice(CountType countType, String indicatorCode, String label, int color,
List<Map<String, IndicatorTally>> indicatorTallies) {
return new PieChartSlice((float) getCount(countType, indicatorCode, indicatorTallies), label, color, indicatorCode);
return new PieChartSlice(getCount(countType, indicatorCode, indicatorTallies), label, color, indicatorCode);
}

public static List<PieChartSlice> addPieChartSlices(PieChartSlice... chartSlices) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.smartregister.reporting.view;

import static org.smartregister.reporting.util.ReportingUtil.getIndicatorView;

import android.content.Context;
import android.view.View;

import androidx.annotation.Nullable;

import org.smartregister.reporting.contract.ReportContract;
import org.smartregister.reporting.domain.TabularVisualization;
import org.smartregister.reporting.factory.TableDisplayFactory;

public class ReportingTableView implements ReportContract.IndicatorView{

private Context context;
private TableDisplayFactory tableDisplayFactory;
private TabularVisualization tabularVisualization;

public ReportingTableView(Context context, TabularVisualization visualization) {
this.context = context;
this.tabularVisualization = visualization;
tableDisplayFactory = new TableDisplayFactory();
}

@Nullable
@Override
public View createView() {
return getIndicatorView(this.tabularVisualization, tableDisplayFactory, context);

}
}
Loading