Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anc issue fix 906 for the human readable values not updating in events when the translation is on #892

Merged
merged 14 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) : "";
}
Comment on lines +401 to +408
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@junaidwarsivd What does the user id do? Why are we concatenating it with the username?

Copy link
Contributor Author

@junaidwarsivd junaidwarsivd Oct 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dubdabasoduba these changes are from the https://github.com/opensrp/opensrp-client-core/pull/887/files merge as it has some resolved codacy issues as well as these changes also needs to be merged in the targeted branch issue ref# opensrp/opensrp-client-anc#878

}

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);
}
}
Comment on lines +852 to +857
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what we are using the user ID for

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
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));
Comment on lines +356 to +363
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please share a sample JSON for the popupJson

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dubdabasoduba
{"key":"hiv_test_status","openmrs_entity":"concept","openmrs_entity_id":"165383AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","value":"done_today","text":"Done today","options":[{"key":"done_today","text":"Done today","type":"done_today","openmrs_entity_parent":"","openmrs_entity":"concept","openmrs_entity_id":"165383AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"}],"type":"extended_radio_button","openmrs_entity_parent":""}

in options only the selected key is added to avoid the extra looping when filling up the human readable values

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);

}
}