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

Add user-practitioner-role in AllSharedPreferences #756

Open
wants to merge 9 commits into
base: add-reconfigurable-registers-and-profiles
Choose a base branch
from
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=4.2.13-gs.1-SNAPSHOT
VERSION_NAME=4.2.13-gs.2-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
2 changes: 1 addition & 1 deletion opensrp-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ dependencies {
transitive = true
}

implementation 'org.smartregister:opensrp-plan-evaluator:1.3.1-SNAPSHOT'
implementation 'org.smartregister:opensrp-plan-evaluator:1.4.0-SNAPSHOT'

implementation 'xerces:xercesImpl:2.12.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.smartregister.account.AccountHelper;
import org.smartregister.account.AccountResponse;
import org.smartregister.domain.LoginResponse;
import org.smartregister.domain.Practitioner;
import org.smartregister.domain.PractitionerRole;
import org.smartregister.domain.jsonmapping.User;
import org.smartregister.event.Listener;
import org.smartregister.sync.helper.SyncSettingsServiceHelper;
Expand Down Expand Up @@ -140,6 +142,11 @@ protected LoginResponse doInBackground(Void... params) {
}

}

// Save the registered ANM
getOpenSRPContext().allSharedPreferences().updateANMUserName(mUsername);

fetchUserRole();
} else {
if (response.getAccountError() != null && response.getAccountError().getError() != null) {
return LoginResponse.valueOf(response.getAccountError().getError().toUpperCase(Locale.ENGLISH));
Expand All @@ -162,6 +169,36 @@ protected LoginResponse doInBackground(Void... params) {
return loginResponse;
}

protected void fetchUserRole() {
Practitioner[] practitioners = getOpenSRPContext().httpAgent().fetchPractitioners();
PractitionerRole[] practitionerRoles = getOpenSRPContext().httpAgent().fetchPractitionerRoles();

if (practitioners == null) {
return;
}

Practitioner loggedInPractitioner = null;
for (Practitioner practitioner: practitioners) {
if (practitioner != null && mUsername.equals(practitioner.getUsername())) {
loggedInPractitioner = practitioner;
}
}

if (loggedInPractitioner != null && practitionerRoles != null) {

PractitionerRole loggedInPractitionerRole = null;
for (PractitionerRole practitionerRole: practitionerRoles) {
if (loggedInPractitioner.getIdentifier().equals(practitionerRole.getPractitionerIdentifier())) {
loggedInPractitionerRole = practitionerRole;
}
}

if (loggedInPractitionerRole != null) {
getOpenSRPContext().allSharedPreferences().setUserPractitionerRole(loggedInPractitionerRole.getCode().getText());
}
}
}

@Override
protected void onProgressUpdate(Integer... messageIdentifier) {
mLoginView.updateProgressMessage(getOpenSRPContext().applicationContext().getString(messageIdentifier[0]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class AllSharedPreferences {
public static final String FORMS_VERSION = "FORMS_VERSION";
private static final String ENCRYPTED_PASSPHRASE_KEY = "ENCRYPTED_PASSPHRASE_KEY";
private static final String DB_ENCRYPTION_VERSION = "DB_ENCRYPTION_VERSION";
private static final String USER_PRACTITIONER_ROLE = "USER_PRACTITIONER_ROLE";
private SharedPreferences preferences;

public AllSharedPreferences(SharedPreferences preferences) {
Expand Down Expand Up @@ -385,5 +386,14 @@ public int getDBEncryptionVersion() {
public void setDBEncryptionVersion(int encryptionVersion) {
preferences.edit().putInt(DB_ENCRYPTION_VERSION, encryptionVersion).commit();
}

@Nullable
public String getUserPractitionerRole() {
return preferences.getString(USER_PRACTITIONER_ROLE, null);
}

public void setUserPractitionerRole(String practitionerRole) {
preferences.edit().putString(USER_PRACTITIONER_ROLE, practitionerRole).commit();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ public int getUnsyncedCreatedTasksAndTaskStatusCount() {
return unsyncedRecordsCount;
}


@NonNull
public Set<Task> getTasksByJurisdictionAndPlan(@NonNull String jurisdictionId, String planIdentifier) {
String query = "SELECT * FROM " + TASK_TABLE + " WHERE " + GROUP_ID + " = ? AND " + PLAN_ID + " = ?";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public org.smartregister.domain.Task updateTask(org.smartregister.domain.Task ta

@Override
public List<Task> findTasksByJurisdiction(String jurisdiction,String planIdentifier) {
return getTasksByJurisdictionAndPlan(jurisdiction,planIdentifier)
return getTasksByJurisdictionAndPlan(jurisdiction, planIdentifier)
.stream()
.map(TaskConverter::convertTasktoFihrResource)
.collect(Collectors.toList());
Expand Down
89 changes: 89 additions & 0 deletions opensrp-app/src/main/java/org/smartregister/service/HTTPAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.smartregister.compression.GZIPCompression;
import org.smartregister.domain.DownloadStatus;
import org.smartregister.domain.LoginResponse;
import org.smartregister.domain.Practitioner;
import org.smartregister.domain.PractitionerRole;
import org.smartregister.domain.ProfileImage;
import org.smartregister.domain.Response;
import org.smartregister.domain.ResponseErrorStatus;
Expand All @@ -36,6 +38,7 @@
import org.smartregister.security.SecurityHelper;
import org.smartregister.ssl.OpensrpSSLHelper;
import org.smartregister.util.Utils;
import org.smartregister.view.contract.IView;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
Expand All @@ -61,7 +64,9 @@
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.net.ssl.HttpsURLConnection;

Expand Down Expand Up @@ -107,6 +112,8 @@ public class HTTPAgent {
private Gson gson;

private static final String DETAILS_URL = "/user-details?anm-id=";
private static final String PRACTITIONER_ROLE_URL = "/rest/practitionerRole";
private static final String PRACTITIONER_URL = "/rest/practitioner";


public HTTPAgent(Context context, AllSharedPreferences
Expand Down Expand Up @@ -1022,6 +1029,88 @@ public AccountConfiguration fetchOAuthConfiguration() {
return null;
}

private void initializeAdapter(Set<? extends IView> iviews) {
Set<IView> mySet = new HashSet<>();

initializeAdapter(mySet);
}

public Practitioner[] fetchPractitioners() {

String baseUrl = configuration.dristhiBaseURL();

if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
}

baseUrl = baseUrl + PRACTITIONER_URL;

HttpURLConnection urlConnection = null;

InputStream inputStream = null;
try {

urlConnection = initializeHttp(baseUrl, true);

int statusCode = urlConnection.getResponseCode();
if (statusCode == HttpStatus.SC_OK) {

inputStream = urlConnection.getInputStream();

String responseString = IOUtils.toString(inputStream);

return gson.fromJson(responseString, Practitioner[].class);
}

} catch (IOException | URISyntaxException e) {
Timber.e(e);
} finally {

closeConnection(urlConnection);
closeIOStream(inputStream);

}
return null;
}

public PractitionerRole[] fetchPractitionerRoles() {

String baseUrl = configuration.dristhiBaseURL();

if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
}

baseUrl = baseUrl + PRACTITIONER_ROLE_URL;

HttpURLConnection urlConnection = null;

InputStream inputStream = null;
try {

urlConnection = initializeHttp(baseUrl, true);

int statusCode = urlConnection.getResponseCode();
if (statusCode == HttpStatus.SC_OK) {

inputStream = urlConnection.getInputStream();

String responseString = IOUtils.toString(inputStream);

return gson.fromJson(responseString, PractitionerRole[].class);
}

} catch (IOException | URISyntaxException e) {
Timber.e(e);
} finally {

closeConnection(urlConnection);
closeIOStream(inputStream);

}
return null;
}

private void closeConnection(HttpURLConnection urlConnection) {
if (urlConnection != null) {
try {
Expand Down