Skip to content

Commit

Permalink
Merge pull request #272 from OpenSRP/issue/35-format-dates-not-unique
Browse files Browse the repository at this point in the history
Added data formatting to client processor
  • Loading branch information
rkodev authored Jul 18, 2019
2 parents 0a35c0f + 9a80789 commit b68338a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package org.smartregister.domain.jsonmapping;

import com.google.gson.annotations.SerializedName;

/**
* Created by keyman on 2/21/2018.
*/

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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.smartregister.domain.jsonmapping;

public interface ColumnType {
String Date = "date";
String String = "string";
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,26 @@
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;
import org.smartregister.repository.DetailsRepository;
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 {
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b68338a

Please sign in to comment.