Skip to content

Commit

Permalink
Merge pull request #124 from opensrp/fix-date-formats
Browse files Browse the repository at this point in the history
Use English Locale for Date Formats When Generating Reporting Daily Tallies
  • Loading branch information
qiarie authored Sep 8, 2022
2 parents 9ca33df + 379b360 commit 029d6d1
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 78 deletions.
4 changes: 1 addition & 3 deletions 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.1.6-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Reporting Library
Expand All @@ -14,5 +14,3 @@ POM_SETTING_DEVELOPER_NAME=OpenSRP Onadev
android.useAndroidX=true
android.enableJetifier=true
org.gradle.jvmargs=-Xmx2048M


Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void generateDailyIndicatorTallies(String lastProcessedDate) {

for (Map.Entry<String, Date> dates : reportEventDates.entrySet()) {
if (dates.getValue().getTime() != timeNow.getTime()) {
lastUpdatedDate = new SimpleDateFormat(eventDateFormat, Locale.getDefault()).format(dates.getValue());
lastUpdatedDate = new SimpleDateFormat(eventDateFormat, Locale.ENGLISH).format(dates.getValue());
}
saveTallies(indicatorQueries, dates, database, executedQueries);
}
Expand Down Expand Up @@ -163,7 +163,7 @@ protected void saveTallies(Map<String, IndicatorQuery> indicatorQueries, Map.Ent
tally.setGrouping(indicatorQuery.getGrouping());

try {
tally.setCreatedAt(new SimpleDateFormat(DAILY_TALLY_DATE_FORMAT, Locale.getDefault()).parse(dates.getKey()));
tally.setCreatedAt(new SimpleDateFormat(DAILY_TALLY_DATE_FORMAT, Locale.ENGLISH).parse(dates.getKey()));
} catch (ParseException e) {
tally.setCreatedAt(new Date());
}
Expand Down Expand Up @@ -197,7 +197,7 @@ protected LinkedHashMap<String, Date> getReportEventDates(@NonNull Date timeNow,
eventDate = formatDate(eventDateString, eventDateFormat);
updateDate = formatDate(val.get(EventClientRepository.event_column.updatedAt.name()), eventDateFormat);

String keyDate = new SimpleDateFormat(DAILY_TALLY_DATE_FORMAT, Locale.getDefault()).format(eventDate);
String keyDate = new SimpleDateFormat(DAILY_TALLY_DATE_FORMAT, Locale.ENGLISH).format(eventDate);

if (reportEventDates.get(keyDate) != null && updateDate != null) {
if (reportEventDates.get(keyDate).getTime() < updateDate.getTime()) {
Expand All @@ -208,7 +208,7 @@ protected LinkedHashMap<String, Date> getReportEventDates(@NonNull Date timeNow,
}
}

String dateToday = new SimpleDateFormat(DAILY_TALLY_DATE_FORMAT, Locale.getDefault()).format(timeNow);
String dateToday = new SimpleDateFormat(DAILY_TALLY_DATE_FORMAT, Locale.ENGLISH).format(timeNow);

if (reportEventDates.get(dateToday) == null) {
reportEventDates.put(dateToday, timeNow);
Expand Down Expand Up @@ -307,7 +307,7 @@ private ArrayList<Object> executeQueryAndReturnMultiResult(@NonNull String query
@Nullable
private Date formatDate(String date, String format) {
try {
return new SimpleDateFormat(format, Locale.getDefault()).parse(date);
return new SimpleDateFormat(format, Locale.ENGLISH).parse(date);
} catch (ParseException pe) {
// Oh no!
Timber.e(pe);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.smartregister.reporting.job;

import android.util.Log;

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

import com.evernote.android.job.Job;
import com.evernote.android.job.JobCreator;

import timber.log.Timber;

public class IndicatorGeneratorJobCreator implements JobCreator {
@Nullable
@Override
Expand All @@ -16,9 +16,8 @@ public Job create(@NonNull String tag) {
case RecurringIndicatorGeneratingJob.TAG:
return new RecurringIndicatorGeneratingJob();
default:
Log.d(IndicatorGeneratorJobCreator.class.getCanonicalName(), "Looks like you tried to create a job " + tag + " that is not declared in the Job Creator");
Timber.d("Looks like you tried to create a job %s that is not declared in the Job Creator", tag);
return null;

}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.smartregister.reporting.job;

import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;

import org.smartregister.AllConstants;
import org.smartregister.job.BaseJob;
import org.smartregister.reporting.service.IndicatorGeneratorIntentService;

import timber.log.Timber;

public class RecurringIndicatorGeneratingJob extends BaseJob {

public static final String TAG = "IndicatorGeneratingJob";
Expand All @@ -18,7 +19,7 @@ public class RecurringIndicatorGeneratingJob extends BaseJob {
protected Result onRunJob(@NonNull Params params) {
Intent intent = new Intent(getApplicationContext(), IndicatorGeneratorIntentService.class);
getApplicationContext().startService(intent);
Log.i(TAG, "IndicatorGeneratorIntentService start service called");
Timber.i("IndicatorGeneratorIntentService start service called");
return params != null && params.getExtras().getBoolean(AllConstants.INTENT_KEY.TO_RESCHEDULE, false) ? Result.RESCHEDULE : Result.SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,25 @@ public void add(@Nullable CompositeIndicatorTally indicatorTally) {
}

SQLiteDatabase database = getWritableDatabase();
database.delete(Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE
, Constants.DailyIndicatorCountRepository.INDICATOR_CODE + " = ? AND "
+ Constants.DailyIndicatorCountRepository.DAY + " = ? "
, new String[]{
database.delete(
Constants.DailyIndicatorCountRepository.INDICATOR_DAILY_TALLY_TABLE,
Constants.DailyIndicatorCountRepository.INDICATOR_CODE + " = ? AND "
+ Constants.DailyIndicatorCountRepository.DAY + " = ? ",
new String[]{
indicatorTally.getIndicatorCode(),
new SimpleDateFormat(
ReportIndicatorDaoImpl.DAILY_TALLY_DATE_FORMAT,
Locale.getDefault()).format(indicatorTally.getCreatedAt()
)
});
new SimpleDateFormat(ReportIndicatorDaoImpl.DAILY_TALLY_DATE_FORMAT, Locale.ENGLISH).format(indicatorTally.getCreatedAt())
}
);
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 Down Expand Up @@ -182,7 +179,6 @@ public List<Map<String, IndicatorTally>> getAllDailyTallies() {
return indicatorTallies;
}


@NonNull
public ArrayList<Date> findDaysWithIndicatorCounts(@NonNull SimpleDateFormat dateFormat, @NonNull Date minDate, @NonNull Date maxDate) {
return findDaysWithIndicatorCounts(dateFormat, minDate, maxDate, null);
Expand All @@ -191,7 +187,7 @@ public ArrayList<Date> findDaysWithIndicatorCounts(@NonNull SimpleDateFormat dat
@NonNull
public ArrayList<Date> findDaysWithIndicatorCounts(@NonNull SimpleDateFormat dateFormat, @NonNull Date minDate, @NonNull Date maxDate, @Nullable String reportGrouping) {
ArrayList<Date> daysWithCounts = new ArrayList<>();
SimpleDateFormat dayFormat = new SimpleDateFormat(ReportIndicatorDaoImpl.DAILY_TALLY_DATE_FORMAT, Locale.getDefault());
SimpleDateFormat dayFormat = new SimpleDateFormat(ReportIndicatorDaoImpl.DAILY_TALLY_DATE_FORMAT, Locale.ENGLISH);

Cursor cursor = null;
try {
Expand Down Expand Up @@ -239,7 +235,7 @@ public ArrayList<IndicatorTally> getIndicatorTalliesForDay(@NonNull Date queryDa

public ArrayList<IndicatorTally> getIndicatorTalliesForDay(@NonNull Date queryDate, @Nullable String reportGrouping) {
ArrayList<IndicatorTally> tallies = new ArrayList<>();
SimpleDateFormat dateFormat = new SimpleDateFormat(ReportIndicatorDaoImpl.DAILY_TALLY_DATE_FORMAT, Locale.getDefault());
SimpleDateFormat dateFormat = new SimpleDateFormat(ReportIndicatorDaoImpl.DAILY_TALLY_DATE_FORMAT, Locale.ENGLISH);
Cursor cursor = null;
try {
Object[] queryArgs = {
Expand Down Expand Up @@ -442,8 +438,8 @@ public Map<String, IndicatorTally> getDailyTallies(@NonNull Date date) {

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 WHERE %s.%s >= ? AND %s.%s < ?"
, queryArgs),
"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 WHERE %s.%s >= ? AND %s.%s < ?"
, queryArgs),
new String[]{String.valueOf(dayStartMillis), String.valueOf(dayEndMillis)});
if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
Expand All @@ -468,7 +464,7 @@ public Map<String, List<IndicatorTally>> findTalliesInMonth(@NonNull Date month)
}

public Map<String, List<IndicatorTally>> findTalliesInMonth(@NonNull Date month, @Nullable String reportGrouping) {
SimpleDateFormat dateFormat = new SimpleDateFormat(ReportIndicatorDaoImpl.DAILY_TALLY_DATE_FORMAT, Locale.getDefault());
SimpleDateFormat dateFormat = new SimpleDateFormat(ReportIndicatorDaoImpl.DAILY_TALLY_DATE_FORMAT, Locale.ENGLISH);
Map<String, List<IndicatorTally>> talliesFromMonth = new HashMap<>();
Cursor cursor = null;
try {
Expand Down Expand Up @@ -512,8 +508,8 @@ public Map<String, List<IndicatorTally>> findTalliesInMonth(@NonNull Date month,
};

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 WHERE %s.%s >= ? AND %s.%s <= ? AND %s.%s %s"
, queryArgs),
"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 WHERE %s.%s >= ? AND %s.%s <= ? AND %s.%s %s"
, queryArgs),
new String[]{dateFormat.format(startDate.getTime()), dateFormat.format(endDate.getTime())});

MultiResultProcessor defaultMultiResultProcessor = ReportingLibrary.getInstance().getDefaultMultiResultProcessor();
Expand Down Expand Up @@ -568,7 +564,7 @@ public Map<String, List<IndicatorTally>> findTalliesInMonth(@NonNull Date month,
}

private CompositeIndicatorTally processCursorRow(@NonNull Cursor cursor) {
SimpleDateFormat dateFormat = new SimpleDateFormat(ReportIndicatorDaoImpl.DAILY_TALLY_DATE_FORMAT, Locale.getDefault());
SimpleDateFormat dateFormat = new SimpleDateFormat(ReportIndicatorDaoImpl.DAILY_TALLY_DATE_FORMAT, Locale.ENGLISH);
CompositeIndicatorTally compositeIndicatorTally = new CompositeIndicatorTally();
compositeIndicatorTally.setId(cursor.getLong(cursor.getColumnIndex(Constants.DailyIndicatorCountRepository.ID)));
compositeIndicatorTally.setIndicatorCode(cursor.getString(cursor.getColumnIndex(Constants.DailyIndicatorCountRepository.INDICATOR_CODE)));
Expand All @@ -584,7 +580,7 @@ private CompositeIndicatorTally processCursorRow(@NonNull Cursor cursor) {
if (cursor.getColumnIndex(Constants.IndicatorQueryRepository.INDICATOR_QUERY_EXPECTED_INDICATORS) != -1) {
compositeIndicatorTally.setExpectedIndicators(
(List<String>) new Gson().fromJson(cursor.getString(cursor.getColumnIndex(
Constants.IndicatorQueryRepository.INDICATOR_QUERY_EXPECTED_INDICATORS))
Constants.IndicatorQueryRepository.INDICATOR_QUERY_EXPECTED_INDICATORS))
, new TypeToken<List<String>>() {
}.getType())
);
Expand All @@ -601,7 +597,7 @@ private CompositeIndicatorTally processCursorRow(@NonNull Cursor cursor) {

public ContentValues createContentValues(@NonNull CompositeIndicatorTally compositeIndicatorTally) {
ContentValues values = new ContentValues();
SimpleDateFormat dateFormat = new SimpleDateFormat(ReportIndicatorDaoImpl.DAILY_TALLY_DATE_FORMAT, Locale.getDefault());
SimpleDateFormat dateFormat = new SimpleDateFormat(ReportIndicatorDaoImpl.DAILY_TALLY_DATE_FORMAT, Locale.ENGLISH);
values.put(Constants.DailyIndicatorCountRepository.INDICATOR_CODE, compositeIndicatorTally.getIndicatorCode());

if (compositeIndicatorTally.isValueSet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public void generateDailyIndicatorTalliesSetsLastProcessedDatePreference() throw
// Test data
String lastProcessedDate = "2019-01-01 00:00";
LinkedHashMap<String, Date> reportEventDates = new LinkedHashMap<>();
reportEventDates.put("20190413", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault()).parse("2019-04-13 12:19:37"));
reportEventDates.put("20190513", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault()).parse("2019-05-13 12:19:37"));
reportEventDates.put("20190413", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH).parse("2019-04-13 12:19:37"));
reportEventDates.put("20190513", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH).parse("2019-05-13 12:19:37"));

Map<String, IndicatorQuery> indicatorQueries = new HashMap<>();
indicatorQueries.put("INDI-100", new IndicatorQuery(1L, "INDI-100", "select count(*) from table", 4, false, null));
Expand Down
Loading

0 comments on commit 029d6d1

Please sign in to comment.