diff --git a/gradle.properties b/gradle.properties index 0787731d6..8d4c6d3cd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/opensrp-app/build.gradle b/opensrp-app/build.gradle index 1b9f44e5c..724d867c7 100644 --- a/opensrp-app/build.gradle +++ b/opensrp-app/build.gradle @@ -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' 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/repository/AllSharedPreferences.java b/opensrp-app/src/main/java/org/smartregister/repository/AllSharedPreferences.java index 2b083f3bb..dc8371f3f 100644 --- a/opensrp-app/src/main/java/org/smartregister/repository/AllSharedPreferences.java +++ b/opensrp-app/src/main/java/org/smartregister/repository/AllSharedPreferences.java @@ -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) { @@ -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(); + } } diff --git a/opensrp-app/src/main/java/org/smartregister/repository/TaskRepository.java b/opensrp-app/src/main/java/org/smartregister/repository/TaskRepository.java index 45213771c..1eb466ab7 100644 --- a/opensrp-app/src/main/java/org/smartregister/repository/TaskRepository.java +++ b/opensrp-app/src/main/java/org/smartregister/repository/TaskRepository.java @@ -718,6 +718,7 @@ public int getUnsyncedCreatedTasksAndTaskStatusCount() { return unsyncedRecordsCount; } + @NonNull public Set getTasksByJurisdictionAndPlan(@NonNull String jurisdictionId, String planIdentifier) { String query = "SELECT * FROM " + TASK_TABLE + " WHERE " + GROUP_ID + " = ? AND " + PLAN_ID + " = ?"; diff --git a/opensrp-app/src/main/java/org/smartregister/repository/dao/TaskDaoImpl.java b/opensrp-app/src/main/java/org/smartregister/repository/dao/TaskDaoImpl.java index 4cb9d61b1..e3b24f067 100644 --- a/opensrp-app/src/main/java/org/smartregister/repository/dao/TaskDaoImpl.java +++ b/opensrp-app/src/main/java/org/smartregister/repository/dao/TaskDaoImpl.java @@ -92,7 +92,7 @@ public org.smartregister.domain.Task updateTask(org.smartregister.domain.Task ta @Override public List findTasksByJurisdiction(String jurisdiction,String planIdentifier) { - return getTasksByJurisdictionAndPlan(jurisdiction,planIdentifier) + return getTasksByJurisdictionAndPlan(jurisdiction, planIdentifier) .stream() .map(TaskConverter::convertTasktoFihrResource) .collect(Collectors.toList()); 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 cec29ee99..799f8a443 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; @@ -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; @@ -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 @@ -1022,6 +1029,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 | 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 {