Skip to content

Commit

Permalink
Fetch user practitioner role during login
Browse files Browse the repository at this point in the history
- 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 opensrp/opensrp-client-goldsmith#80
  • Loading branch information
ekigamba committed Mar 2, 2021
1 parent f64655c commit a07e8c5
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.smartregister.domain;


import java.io.Serializable;

/**
* Created by Ephraim Kigamba - [email protected] 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;
}

}
Original file line number Diff line number Diff line change
@@ -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 - [email protected] 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.smartregister.domain;

import java.io.Serializable;

/**
* Created by Ephraim Kigamba - [email protected] 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;
}
}
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
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 @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1017,6 +1024,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 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 {
Expand Down

0 comments on commit a07e8c5

Please sign in to comment.