Skip to content

Commit

Permalink
Merge pull request #892 from opensrp/anc-issue-fix-906
Browse files Browse the repository at this point in the history
Anc issue fix 906 for the human readable values not updating in events when the translation is on
  • Loading branch information
dubdabasoduba authored Oct 18, 2022
2 parents 2974f25 + bedfc97 commit 5fdb3dd
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 17 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=6.0.0-BETA2-SNAPSHOT
VERSION_NAME=6.0.0-BETA3-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
Expand Up @@ -46,6 +46,7 @@ public class AllConstants {
public static final String DEFAULT_TEAM_PREFIX = "dfltTeam-";
public static final String DEFAULT_TEAM_ID_PREFIX = "dfltTeamId-";
public static final String USER_LOCALITY_ID_PREFIX = "userLoc-";
public static final String USER_ID_PREFIX = "userId-";
public static final String PIONEER_USER = "pioneerUser";
public static final String LANGUAGE_PREFERENCE_KEY = "locale";
public static final String CURRENT_LOCALITY = "current_locality";
Expand Down Expand Up @@ -135,6 +136,7 @@ public class AllConstants {
public static final String OPENMRS_ATTRIBUTES = "openmrs_attributes";
public static final String VALUE_OPENMRS_ATTRIBUTES = "value_openmrs_attributes";
public static final String SECONDARY_VALUE = "secondary_value";
public static final String VALUE = "value";
public static final String EXPANSION_PANEL = "expansion_panel";
public static final String SPINNER = "spinner";
public static final String ROWID = "rowid";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,5 +398,13 @@ public void updateLastAuthenticationHttpStatus(int httpStatus) {
public int getLastAuthenticationHttpStatus() {
return preferences.getInt(LAST_AUTHENTICATION_HTTP_STATUS, 0);
}

public void saveUserId(String username, String userId) {
preferences.edit().putString(AllConstants.USER_ID_PREFIX + username, userId).apply();
}

public String getUserId(String userName) {
return StringUtils.isNotBlank(userName) ? preferences.getString(AllConstants.USER_ID_PREFIX + userName, null) : "";
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package org.smartregister.service;

import static org.smartregister.AllConstants.ENGLISH_LANGUAGE;
import static org.smartregister.AllConstants.ENGLISH_LOCALE;
import static org.smartregister.AllConstants.JURISDICTION_IDS;
import static org.smartregister.AllConstants.KANNADA_LANGUAGE;
import static org.smartregister.AllConstants.KANNADA_LOCALE;
import static org.smartregister.AllConstants.OPENSRP_AUTH_USER_URL_PATH;
import static org.smartregister.AllConstants.OPENSRP_LOCATION_URL_PATH;
import static org.smartregister.AllConstants.OPERATIONAL_AREAS;
import static org.smartregister.AllConstants.ORGANIZATION_IDS;
import static org.smartregister.event.Event.ON_LOGOUT;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -63,17 +74,6 @@

import timber.log.Timber;

import static org.smartregister.AllConstants.ENGLISH_LANGUAGE;
import static org.smartregister.AllConstants.ENGLISH_LOCALE;
import static org.smartregister.AllConstants.JURISDICTION_IDS;
import static org.smartregister.AllConstants.KANNADA_LANGUAGE;
import static org.smartregister.AllConstants.KANNADA_LOCALE;
import static org.smartregister.AllConstants.OPENSRP_AUTH_USER_URL_PATH;
import static org.smartregister.AllConstants.OPENSRP_LOCATION_URL_PATH;
import static org.smartregister.AllConstants.OPERATIONAL_AREAS;
import static org.smartregister.AllConstants.ORGANIZATION_IDS;
import static org.smartregister.event.Event.ON_LOGOUT;

public class UserService {
private static final String KEYSTORE = "AndroidKeyStore";
private static final String CIPHER = "RSA/ECB/PKCS1Padding";
Expand Down Expand Up @@ -404,6 +404,7 @@ public void processLoginResponseDataForUser(String userName, LoginResponseData u
saveJurisdictions(userInfo.jurisdictions);
saveJurisdictionIds(userInfo.jurisdictionIds);
saveOrganizations(getUserTeam(userInfo));
saveUserId(userName, userInfo.user.getBaseEntityId());
if (loginSuccessful &&
(StringUtils.isBlank(getUserDefaultLocationId(userInfo)) ||
StringUtils.isNotBlank(allSharedPreferences.fetchDefaultLocalityId(username))) &&
Expand Down Expand Up @@ -848,4 +849,10 @@ public byte[] getGroupId(String userName, KeyStore.PrivateKeyEntry privateKeyEnt
}
return null;
}

public void saveUserId(String userName, String baseEntityId) {
if (userName != null) {
allSharedPreferences.saveUserId(userName, baseEntityId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,14 @@ private static void createObsFromPopUpValues(Event event, JSONObject jsonObject,
popupJson.put(KEY, secondaryValueKey);
popupJson.put(OPENMRS_ENTITY, CONCEPT);
popupJson.put(OPENMRS_ENTITY_ID, parentOpenMRSAttributes.getString(OPENMRS_ENTITY_ID));
popupJson.put(VALUE, valueOpenMRSAttribute.getString(OPENMRS_ENTITY_ID));
if(valueOpenMRSAttribute.has(VALUE))
popupJson.put(VALUE, valueOpenMRSAttribute.getString(VALUE));
else
popupJson.put(VALUE, valueOpenMRSAttribute.getString(OPENMRS_ENTITY_ID));
if(valueOpenMRSAttribute.has(TEXT))
popupJson.put(TEXT,valueOpenMRSAttribute.getString(TEXT));
if(valueOpenMRSAttribute.has(OPTIONS_FIELD_NAME))
popupJson.put(OPTIONS_FIELD_NAME,valueOpenMRSAttribute.getJSONArray(OPTIONS_FIELD_NAME));
popupJson.put(AllConstants.TYPE, secondaryValueType);

if (AllConstants.NATIVE_RADIO.equals(secondaryValueType) ||
Expand Down Expand Up @@ -534,7 +541,7 @@ private static void createObservation(Event e, JSONObject jsonObject, String val
List<Object> vall = new ArrayList<>();

String formSubmissionField = getString(jsonObject, KEY);
String obsValue = value;
String obsValue = Utils.extractTranslatableValue(value);

String dataType = getString(jsonObject, OPENMRS_DATA_TYPE);
if (StringUtils.isBlank(dataType)) {
Expand Down
15 changes: 15 additions & 0 deletions opensrp-core/src/main/java/org/smartregister/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.Years;
import org.json.JSONObject;
import org.smartregister.AllConstants;
import org.smartregister.CoreLibrary;
import org.smartregister.R;
Expand Down Expand Up @@ -1023,4 +1024,18 @@ public static String toStringNullable(@Nullable Object value) {
public String getName() {
return getPrefferedName();
}

public static String extractTranslatableValue(String value) {
if (value.startsWith("{") && value.endsWith("}")) {
try {
JSONObject valueObject = new JSONObject(value);
return valueObject.getString(AllConstants.VALUE);
}
catch(Exception e)
{
Timber.e(e);
}
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.reflect.Whitebox;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.BaseUnitTest;
Expand Down Expand Up @@ -188,7 +187,7 @@ public void shouldRegisterANewUser() {
@Test
public void shouldDeleteDataAndSettingsWhenLogoutHappens() throws Exception {
SyncConfiguration syncConfiguration = mock(SyncConfiguration.class);
Mockito.doReturn(false).when(syncConfiguration).clearDataOnNewTeamLogin();
doReturn(false).when(syncConfiguration).clearDataOnNewTeamLogin();
ReflectionHelpers.setField(CoreLibrary.getInstance(), "syncConfiguration", syncConfiguration);
Whitebox.setInternalState(drishtiApplication, "password", password);

Expand Down Expand Up @@ -373,5 +372,11 @@ public void testIsUserInPioneerGroupShouldReturnFalseForOthers() throws Exceptio
assertFalse(userService.isUserInPioneerGroup("john"));
}


@Test
public void saveProviderBaseEntityIdOnSharedPref() {
String providerBaseEntityId = "00ab-88dc-ea11-8471-ad90";
String providerName = "provider";
userService.saveUserId(providerName, providerBaseEntityId);
verify(userService).saveUserId(providerName, providerBaseEntityId);
}
}
20 changes: 20 additions & 0 deletions opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.Years;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.AllConstants;
import org.smartregister.BaseRobolectricUnitTest;
import org.smartregister.CoreLibrary;
import org.smartregister.SyncFilter;
Expand Down Expand Up @@ -733,5 +736,22 @@ public void testComposeApiCallParamsStringWithMultipleParamValues() {
apiParams.add(Pair.create("serverVersion", "21"));
assertEquals("&identifier=global_configs&serverVersion=21", Utils.composeApiCallParamsString(apiParams));
}

@Test
public void testExtractTrabslatableValue() throws JSONException
{
JSONObject object = new JSONObject();
String value = "testValue";
object.put(AllConstants.VALUE,value);
object.put (AllConstants.TEXT , AllConstants.TEXT);

// testing with Json Object
String result = Utils.extractTranslatableValue(object.toString());
assertEquals(result,value);
// testing backward compatibility.
String result2 = Utils.extractTranslatableValue(value);
assertEquals(result2,value);

}
}

0 comments on commit 5fdb3dd

Please sign in to comment.