From a07e8c56a71ef066ff4d83c6cb6f7be89bf49be7 Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Tue, 2 Mar 2021 09:04:12 +0300 Subject: [PATCH] Fetch user practitioner role during login - Add PractitionerRole and Practitioner models - Fetch practitioners and practitioner roles to map the current user practitioner-role - Save the current user practitioner-role-code in preferences Fixes tasks in https://github.com/OpenSRP/opensrp-client-goldsmith/issues/80 --- .../smartregister/domain/Practitioner.java | 63 +++++++++++++ .../domain/PractitionerRole.java | 70 +++++++++++++++ .../domain/PractitionerRoleCode.java | 21 +++++ .../login/task/RemoteLoginTask.java | 37 ++++++++ .../org/smartregister/service/HTTPAgent.java | 89 +++++++++++++++++++ 5 files changed, 280 insertions(+) create mode 100644 opensrp-app/src/main/java/org/smartregister/domain/Practitioner.java create mode 100644 opensrp-app/src/main/java/org/smartregister/domain/PractitionerRole.java create mode 100644 opensrp-app/src/main/java/org/smartregister/domain/PractitionerRoleCode.java diff --git a/opensrp-app/src/main/java/org/smartregister/domain/Practitioner.java b/opensrp-app/src/main/java/org/smartregister/domain/Practitioner.java new file mode 100644 index 000000000..847cffdea --- /dev/null +++ b/opensrp-app/src/main/java/org/smartregister/domain/Practitioner.java @@ -0,0 +1,63 @@ +package org.smartregister.domain; + + +import java.io.Serializable; + +/** + * Created by Ephraim Kigamba - nek.eam@gmail.com on 01-03-2021. + */ +public class Practitioner implements Serializable { + + private static final long serialVersionUID = -8367551045898354954L; + + private String identifier; + + private Boolean active; + + private String name; + + private String userId; + + private String username; + + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + public Boolean getActive() { + return active; + } + + public void setActive(Boolean active) { + this.active = active; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + +} \ No newline at end of file diff --git a/opensrp-app/src/main/java/org/smartregister/domain/PractitionerRole.java b/opensrp-app/src/main/java/org/smartregister/domain/PractitionerRole.java new file mode 100644 index 000000000..b2415875c --- /dev/null +++ b/opensrp-app/src/main/java/org/smartregister/domain/PractitionerRole.java @@ -0,0 +1,70 @@ +package org.smartregister.domain; + +import java.io.Serializable; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.gson.annotations.SerializedName; + +/** + * Created by Ephraim Kigamba - nek.eam@gmail.com on 01-03-2021. + */ +public class PractitionerRole implements Serializable { + + private static final long serialVersionUID = -2472589757270251270L; + @JsonProperty + private String identifier; + + @JsonProperty + private Boolean active; + + @JsonProperty + @SerializedName("organization") + private String organizationIdentifier; + + @JsonProperty + @SerializedName("practitioner") + private String practitionerIdentifier; + + @JsonProperty + private PractitionerRoleCode code; + + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + public Boolean getActive() { + return active; + } + + public void setActive(Boolean active) { + this.active = active; + } + + public String getOrganizationIdentifier() { + return organizationIdentifier; + } + + public void setOrganizationIdentifier(String organizationIdentifier) { + this.organizationIdentifier = organizationIdentifier; + } + + public String getPractitionerIdentifier() { + return practitionerIdentifier; + } + + public void setPractitionerIdentifier(String practitionerIdentifier) { + this.practitionerIdentifier = practitionerIdentifier; + } + + public PractitionerRoleCode getCode() { + return code; + } + + public void setCode(PractitionerRoleCode code) { + this.code = code; + } +} \ No newline at end of file diff --git a/opensrp-app/src/main/java/org/smartregister/domain/PractitionerRoleCode.java b/opensrp-app/src/main/java/org/smartregister/domain/PractitionerRoleCode.java new file mode 100644 index 000000000..5f40b33b8 --- /dev/null +++ b/opensrp-app/src/main/java/org/smartregister/domain/PractitionerRoleCode.java @@ -0,0 +1,21 @@ +package org.smartregister.domain; + +import java.io.Serializable; + +/** + * Created by Ephraim Kigamba - nek.eam@gmail.com on 01-03-2021. + */ + +public class PractitionerRoleCode implements Serializable { + + private static final long serialVersionUID = 5814439241291810987L; + private String text; + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +} \ No newline at end of file diff --git a/opensrp-app/src/main/java/org/smartregister/login/task/RemoteLoginTask.java b/opensrp-app/src/main/java/org/smartregister/login/task/RemoteLoginTask.java index df45bee1b..5c258294e 100644 --- a/opensrp-app/src/main/java/org/smartregister/login/task/RemoteLoginTask.java +++ b/opensrp-app/src/main/java/org/smartregister/login/task/RemoteLoginTask.java @@ -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; @@ -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)); @@ -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])); diff --git a/opensrp-app/src/main/java/org/smartregister/service/HTTPAgent.java b/opensrp-app/src/main/java/org/smartregister/service/HTTPAgent.java index fc16d37da..b3740fdfb 100644 --- a/opensrp-app/src/main/java/org/smartregister/service/HTTPAgent.java +++ b/opensrp-app/src/main/java/org/smartregister/service/HTTPAgent.java @@ -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; @@ -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; @@ -59,7 +62,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; @@ -105,6 +110,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 @@ -1017,6 +1024,88 @@ public AccountConfiguration fetchOAuthConfiguration() { return null; } + private void initializeAdapter(Set iviews) { + Set 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 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 e) { + Timber.e(e); + } finally { + + closeConnection(urlConnection); + closeIOStream(inputStream); + + } + return null; + } + private void closeConnection(HttpURLConnection urlConnection) { if (urlConnection != null) { try {