diff --git a/gradle.properties b/gradle.properties index 403d131da..4bee1e814 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=1.6.46-SNAPSHOT +VERSION_NAME=1.6.47-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Core Application diff --git a/opensrp-app/src/main/java/org/smartregister/domain/jsonmapping/Column.java b/opensrp-app/src/main/java/org/smartregister/domain/jsonmapping/Column.java index e67d14b17..63c983c82 100644 --- a/opensrp-app/src/main/java/org/smartregister/domain/jsonmapping/Column.java +++ b/opensrp-app/src/main/java/org/smartregister/domain/jsonmapping/Column.java @@ -1,5 +1,7 @@ package org.smartregister.domain.jsonmapping; +import com.google.gson.annotations.SerializedName; + /** * Created by keyman on 2/21/2018. */ @@ -7,5 +9,11 @@ public class Column { public String column_name; public String type; + @SerializedName("data_type") + public String dataType; + @SerializedName("source_format") + public String sourceFormat; + @SerializedName("save_format") + public String saveFormat; public JsonMapping json_mapping; } diff --git a/opensrp-app/src/main/java/org/smartregister/domain/jsonmapping/ColumnType.java b/opensrp-app/src/main/java/org/smartregister/domain/jsonmapping/ColumnType.java new file mode 100644 index 000000000..3692b91e9 --- /dev/null +++ b/opensrp-app/src/main/java/org/smartregister/domain/jsonmapping/ColumnType.java @@ -0,0 +1,6 @@ +package org.smartregister.domain.jsonmapping; + +public interface ColumnType { + String Date = "date"; + String String = "string"; +} diff --git a/opensrp-app/src/main/java/org/smartregister/sync/ClientProcessorForJava.java b/opensrp-app/src/main/java/org/smartregister/sync/ClientProcessorForJava.java index d6851fcf4..0cda2b6f4 100644 --- a/opensrp-app/src/main/java/org/smartregister/sync/ClientProcessorForJava.java +++ b/opensrp-app/src/main/java/org/smartregister/sync/ClientProcessorForJava.java @@ -18,6 +18,7 @@ import org.smartregister.domain.jsonmapping.ClientClassification; import org.smartregister.domain.jsonmapping.ClientField; import org.smartregister.domain.jsonmapping.Column; +import org.smartregister.domain.jsonmapping.ColumnType; import org.smartregister.domain.jsonmapping.JsonMapping; import org.smartregister.domain.jsonmapping.Rule; import org.smartregister.domain.jsonmapping.Table; @@ -25,14 +26,18 @@ import org.smartregister.util.AssetHandler; import java.lang.reflect.Field; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; +import timber.log.Timber; + import static org.smartregister.event.Event.FORM_SUBMITTED; public class ClientProcessorForJava { @@ -417,7 +422,8 @@ public void processCaseModel(Event event, Client client, Column column, ContentV // in Content value if (columnValue != null) { columnValue = getHumanReadableConceptResponse(columnValue, segment); - contentValues.put(columnName, columnValue); + String formattedValue = getFormattedValue(column, columnValue); + contentValues.put(columnName, formattedValue); } } @@ -449,8 +455,41 @@ public void processCaseModel(Event event, Client client, Column column, ContentV } } } catch (Exception e) { - Log.e(TAG, e.toString(), e); + Timber.e(e); + } + } + + /** + * Reformat the data to be persisted in the database. + * This function will reformat dates with supplied types for storage in the DB + * @param column + * @param columnValue + * @return + */ + protected String getFormattedValue(Column column, String columnValue) { + // covert the column if its a formatted column with both + + String dataType = StringUtils.isNotBlank(column.dataType) ? column.dataType : ""; + switch (dataType) { + case ColumnType.Date: + if (StringUtils.isNotBlank(column.saveFormat) && StringUtils.isNotBlank(column.sourceFormat)) { + try { + Date sourceDate = new SimpleDateFormat(column.sourceFormat, Locale.getDefault()).parse(columnValue); + return new SimpleDateFormat(column.saveFormat, Locale.getDefault()).format(sourceDate); + } catch (Exception e) { + Timber.e(e); + } + } + case ColumnType.String: + if (StringUtils.isNotBlank(column.saveFormat)) { + return String.format(column.saveFormat, columnValue); + } + break; + default: + return columnValue; } + + return columnValue; } /** diff --git a/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorForJavaTest.java b/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorForJavaTest.java index 4bcc2f76d..e26a46488 100644 --- a/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorForJavaTest.java +++ b/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorForJavaTest.java @@ -7,14 +7,48 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.powermock.reflect.Whitebox; import org.smartregister.BaseUnitTest; import org.smartregister.domain.db.Event; +import org.smartregister.domain.jsonmapping.Column; +import org.smartregister.domain.jsonmapping.ColumnType; + +import static org.junit.Assert.assertEquals; + public class ClientProcessorForJavaTest extends BaseUnitTest { @Mock private Context context; + @Test + public void testGetFormattedValueDate() throws Exception { + ClientProcessorForJava clientProcessor = new ClientProcessorForJava(context); + + Column column = new Column(); + column.dataType = ColumnType.Date; + column.saveFormat = "yyyy-MM-dd"; + column.sourceFormat = "dd-MM-yyyy"; + String columnValue = "16-04-2019"; + + String res = Whitebox.invokeMethod(clientProcessor, "getFormattedValue", column, columnValue); + + assertEquals(res, "2019-04-16"); + } + + @Test + public void testGetFormattedValueString() throws Exception { + ClientProcessorForJava clientProcessor = new ClientProcessorForJava(context); + + Column column = new Column(); + column.dataType = ColumnType.String; + column.saveFormat = "Sheila is %s"; + String columnValue = "smart"; + + String res = Whitebox.invokeMethod(clientProcessor, "getFormattedValue", column, columnValue); + + assertEquals(res, "Sheila is smart"); + } @Before public void setUp() throws Exception {