From 763d944e72141712055783e00e399b7ad73db5d5 Mon Sep 17 00:00:00 2001 From: Allan O Date: Tue, 15 Sep 2020 13:50:38 +0300 Subject: [PATCH 1/5] :bug: Format date for calculation If display format is set, format the date into format expected for calculations --- .../vijay/jsonwizard/rules/RulesEngineDateUtil.java | 6 +++--- .../main/java/com/vijay/jsonwizard/utils/Utils.java | 10 +++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java index ed24fc0bc..858c0d806 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java @@ -78,7 +78,7 @@ public String getWeeksAndDaysFromDays(Integer days) { } public String formatDate(String dateString, String duration) { - LocalDate date = new LocalDate(Utils.reverseDateString(dateString, "-")); + LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString), "-")); int result = 0; String cleanDuration = duration.trim().toLowerCase(); @@ -128,7 +128,7 @@ public String addDuration(String durationString) { * @return String with date */ public String addDuration(String dateString, String durationString) { - LocalDate date = new LocalDate(Utils.reverseDateString(dateString, "-")); + LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString), "-")); String[] durationArr = getDurationArray(durationString); for (String duration : durationArr) { @@ -181,7 +181,7 @@ public String subtractDuration(String durationString) { */ public String subtractDuration(String dateString, String durationString) { - LocalDate date = new LocalDate(Utils.reverseDateString(dateString, "-")); + LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString), "-")); String[] durationArr = getDurationArray(durationString); diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java index e6fe5f9fc..720205622 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java @@ -107,6 +107,14 @@ public static String reverseDateString(String str, String delimiter) { return strr[2] + "-" + strr[1] + "-" + strr[0]; } + public static String getDateFormattedForCalculation(String date) { + String datePickerDisplayFormat = Form.getDatePickerDisplayFormat(); + if (StringUtils.isNotBlank(datePickerDisplayFormat)) { + return formatDateToPattern(date, datePickerDisplayFormat, DatePickerFactory.DATE_FORMAT.toPattern()); + } else + return date; + } + public static String getDuration(String date) { return getDuration(date, null); } @@ -818,7 +826,7 @@ public static int getResourceId(Context context, String name, ResourceType resou return -1; } } - + public static boolean isEmptyJsonArray(JSONArray jsonArray) { return jsonArray == null || jsonArray.length() == 0; } From c92b39cb5a19cc2899849d57127f1e8823335aab Mon Sep 17 00:00:00 2001 From: Allan O Date: Tue, 15 Sep 2020 13:51:58 +0300 Subject: [PATCH 2/5] :white_check_mark: Add test for calculation date format --- .../java/com/vijay/jsonwizard/utils/UtilsTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java index bbb60327a..f81af97d4 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java @@ -13,6 +13,7 @@ import com.vijay.jsonwizard.BaseTest; import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.constants.JsonFormConstants; +import com.vijay.jsonwizard.domain.Form; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.JsonApi; import com.vijay.jsonwizard.rules.RuleConstant; @@ -330,6 +331,16 @@ public void testReverseDateStringShouldReturnReverseDateString() { Assert.assertEquals("1997-12-20", Utils.reverseDateString(date, "-")); } + @Test + public void getDateForCalculationShouldReturnCorrectDateString() { + String date = "20-12-2021"; + Assert.assertEquals("20-12-2021", Utils.getDateFormattedForCalculation(date)); + Form form = new Form(); + form.setDatePickerDisplayFormat("dd MMM yyyy"); + date = "20 DEC 2021"; + Assert.assertEquals("20-12-2021", Utils.getDateFormattedForCalculation(date)); + } + @Test public void testGetStringValueShouldReturnStringValue() throws Exception { JSONObject jsonObject = new JSONObject(); From 02622d0f0d7ad07ddc93393c1fc4b2a6cd7f9f1d Mon Sep 17 00:00:00 2001 From: Allan O Date: Tue, 15 Sep 2020 13:52:36 +0300 Subject: [PATCH 3/5] :arrow_up: Update Native Forms Version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 1d2ee72b0..0d1a873e0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=1.14.3.0-SNAPSHOT +VERSION_NAME=1.14.3.1-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard From 329d3c59cc5b5adbd0bce1b6445f64c032eac5c4 Mon Sep 17 00:00:00 2001 From: Allan O Date: Tue, 15 Sep 2020 18:20:18 +0300 Subject: [PATCH 4/5] :white_check_mark: Pass dateFormat in get date for calculation --- .../vijay/jsonwizard/rules/RulesEngineDateUtil.java | 7 ++++--- .../main/java/com/vijay/jsonwizard/utils/Utils.java | 3 +-- .../java/com/vijay/jsonwizard/utils/UtilsTest.java | 11 ++++++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java index 858c0d806..18530581b 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java @@ -3,6 +3,7 @@ import android.support.annotation.NonNull; import android.text.TextUtils; +import com.vijay.jsonwizard.domain.Form; import com.vijay.jsonwizard.utils.FormUtils; import com.vijay.jsonwizard.utils.Utils; @@ -78,7 +79,7 @@ public String getWeeksAndDaysFromDays(Integer days) { } public String formatDate(String dateString, String duration) { - LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString), "-")); + LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString, Form.getDatePickerDisplayFormat()), "-")); int result = 0; String cleanDuration = duration.trim().toLowerCase(); @@ -128,7 +129,7 @@ public String addDuration(String durationString) { * @return String with date */ public String addDuration(String dateString, String durationString) { - LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString), "-")); + LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString, Form.getDatePickerDisplayFormat()), "-")); String[] durationArr = getDurationArray(durationString); for (String duration : durationArr) { @@ -181,7 +182,7 @@ public String subtractDuration(String durationString) { */ public String subtractDuration(String dateString, String durationString) { - LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString), "-")); + LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString, Form.getDatePickerDisplayFormat()), "-")); String[] durationArr = getDurationArray(durationString); diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java index 720205622..9ddae8dd1 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java @@ -107,8 +107,7 @@ public static String reverseDateString(String str, String delimiter) { return strr[2] + "-" + strr[1] + "-" + strr[0]; } - public static String getDateFormattedForCalculation(String date) { - String datePickerDisplayFormat = Form.getDatePickerDisplayFormat(); + public static String getDateFormattedForCalculation(String date, String datePickerDisplayFormat) { if (StringUtils.isNotBlank(datePickerDisplayFormat)) { return formatDateToPattern(date, datePickerDisplayFormat, DatePickerFactory.DATE_FORMAT.toPattern()); } else diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java index f81af97d4..6a7ac37e7 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java @@ -334,11 +334,16 @@ public void testReverseDateStringShouldReturnReverseDateString() { @Test public void getDateForCalculationShouldReturnCorrectDateString() { String date = "20-12-2021"; - Assert.assertEquals("20-12-2021", Utils.getDateFormattedForCalculation(date)); + Assert.assertEquals("20-12-2021", Utils.getDateFormattedForCalculation(date, null)); + } + + @Test + public void getDateForCalculationReturnsDateStringWhenDisplayFormatIsSet() { Form form = new Form(); form.setDatePickerDisplayFormat("dd MMM yyyy"); - date = "20 DEC 2021"; - Assert.assertEquals("20-12-2021", Utils.getDateFormattedForCalculation(date)); + String date = "20 DEC 2021"; + Assert.assertEquals("20-12-2021", Utils.getDateFormattedForCalculation(date, Form.getDatePickerDisplayFormat())); + form.setDatePickerDisplayFormat(null); // To not pollute other tests } @Test From e779d52dd258036eeb0b775ace7a3669dbc3983c Mon Sep 17 00:00:00 2001 From: Allan O Date: Tue, 15 Sep 2020 18:25:14 +0300 Subject: [PATCH 5/5] :white_check_mark: Remove static imports --- .../java/com/vijay/jsonwizard/utils/UtilsTest.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java index 6a7ac37e7..4d46b8df5 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java @@ -46,9 +46,6 @@ import static com.vijay.jsonwizard.utils.Utils.formatDateToPattern; import static com.vijay.jsonwizard.utils.Utils.isEmptyJsonArray; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; public class UtilsTest extends BaseTest { @@ -373,15 +370,15 @@ public void testFormatDateToPattern() { String inputFormat = "dd/MM/yyyy"; String outputFormat = "dd MMM yyyy"; String formattedDate = formatDateToPattern(date, inputFormat, outputFormat); - assertEquals("05 May 2022", formattedDate); + Assert.assertEquals("05 May 2022", formattedDate); } @Test public void testIsEmptyJsonArrayShouldReturnCorrectStatus() { - assertTrue(isEmptyJsonArray(null)); + Assert.assertTrue(isEmptyJsonArray(null)); JSONArray jsonArray = new JSONArray(); - assertTrue(isEmptyJsonArray(jsonArray)); + Assert.assertTrue(isEmptyJsonArray(jsonArray)); jsonArray.put("value"); - assertFalse(isEmptyJsonArray(jsonArray)); + Assert.assertFalse(isEmptyJsonArray(jsonArray)); } } \ No newline at end of file