Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ConnectJobAssessmentRecord() {

}

public static ConnectJobAssessmentRecord fromJson(JSONObject json, int jobId) throws JSONException, ParseException {
public static ConnectJobAssessmentRecord fromJson(JSONObject json, int jobId) throws JSONException {
ConnectJobAssessmentRecord record = new ConnectJobAssessmentRecord();

record.lastUpdate = new Date();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ConnectJobLearningRecord extends Persisted implements Serializable
public ConnectJobLearningRecord() {
}

public static ConnectJobLearningRecord fromJson(JSONObject json, int jobId) throws JSONException, ParseException {
public static ConnectJobLearningRecord fromJson(JSONObject json, int jobId) throws JSONException {
ConnectJobLearningRecord record = new ConnectJobLearningRecord();

record.lastUpdate = new Date();
Expand Down
12 changes: 9 additions & 3 deletions app/src/org/commcare/connect/ConnectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,9 @@ public void processSuccess(int responseCode, InputStream responseData) {
ConnectDatabaseHelper.handleReceivedDbPassphrase(context, json.getString(key));
}
}
} catch (IOException | JSONException e) {
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) {
Logger.exception("Parsing return from DB key request", e);
}
}
Expand Down Expand Up @@ -433,7 +435,9 @@ public void processSuccess(int responseCode, InputStream responseData) {

ConnectJobUtils.updateJobLearnProgress(context, job);
}
} catch (IOException | JSONException | ParseException e) {
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) {
Logger.exception("Parsing return from learn_progress request", e);
}

Expand Down Expand Up @@ -557,7 +561,9 @@ public void processSuccess(int responseCode, InputStream responseData) {
job.setPayments(payments);
}
}
} catch (IOException | JSONException e) {
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) {
Logger.exception("Parsing return from delivery progress request", e);
success = false;
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/org/commcare/connect/PersonalIdManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,9 @@ public void processSuccess(int responseCode, InputStream responseData) {
ConnectDatabaseHelper.handleReceivedDbPassphrase(context, json.getString(key));
}
}
} catch (IOException | JSONException e) {
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) {
Logger.exception("Parsing return from DB key request", e);
Comment on lines -512 to 515
Copy link
Contributor

Choose a reason for hiding this comment

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

this needs to be PR'ed to master

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}
}
Expand Down
11 changes: 4 additions & 7 deletions app/src/org/commcare/connect/database/ConnectDatabaseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.commcare.CommCareApplication;
import org.commcare.android.database.global.models.ConnectKeyRecord;
import org.commcare.util.Base64;
import org.commcare.util.Base64DecoderException;
import org.commcare.util.EncryptionUtils;
import org.commcare.utils.CrashUtil;
import org.commcare.utils.EncryptionKeyAndTransform;
Expand Down Expand Up @@ -36,13 +37,11 @@ record = new ConnectKeyRecord(encoded, isLocal);
}

CommCareApplication.instance().getGlobalStorage(ConnectKeyRecord.class).write(record);
} catch (Exception e) {
Logger.exception("Storing DB passphrase", e);
} catch (EncryptionUtils.EncryptionException e) {
throw new RuntimeException(e);
}
}


public static ConnectKeyRecord getKeyRecord(boolean local) {
Vector<ConnectKeyRecord> records = CommCareApplication.instance()
.getGlobalStorage(ConnectKeyRecord.class)
Expand All @@ -55,8 +54,7 @@ public static void storeConnectDbPassphrase(Context context, String base64Encode
try {
byte[] bytes = Base64.decode(base64EncodedPassphrase);
storeConnectDbPassphrase(context, bytes, isLocal);
} catch (Exception e) {
Logger.exception("Encoding DB passphrase to Base64", e);
} catch (Base64DecoderException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -88,8 +86,7 @@ public static byte[] getConnectDbPassphrase(Context context, boolean isLocal) {
CrashUtil.log("We don't find paraphrase in db");
throw new RuntimeException("We don't find a record in db to get passphrase");
}
} catch (Exception e) {
Logger.exception("Getting DB passphrase", e);
} catch (Base64DecoderException | EncryptionUtils.EncryptionException e) {
throw new RuntimeException(e);
}
}
Expand Down
55 changes: 16 additions & 39 deletions app/src/org/commcare/connect/database/ConnectUserDatabaseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,15 @@ public static ConnectUserRecord getUser(Context context) {
if (context == null) {
throw new IllegalArgumentException("Context must not be null");
}
if (!ConnectDatabaseHelper.dbExists(context)) {
return null;
}
try {
Iterable<ConnectUserRecord> records = ConnectDatabaseHelper.getConnectStorage(
context, ConnectUserRecord.class);
if (records.iterator().hasNext()) {
return records.iterator().next();
}
return null;
} catch (Exception e) {
Logger.exception("Corrupt Connect DB trying to get user", e);
ConnectDatabaseHelper.dbBroken = true;
throw new RuntimeException("Failed to access Connect database", e);
}
if (!ConnectDatabaseHelper.dbExists(context)) {
return null;
}
Iterable<ConnectUserRecord> records = ConnectDatabaseHelper.getConnectStorage(
context, ConnectUserRecord.class);
if (records.iterator().hasNext()) {
return records.iterator().next();
}
return null;
}

public static void storeUser(Context context, ConnectUserRecord user) {
Expand All @@ -38,34 +32,17 @@ public static void storeUser(Context context, ConnectUserRecord user) {
if (user == null) {
throw new IllegalArgumentException("User must not be null");
}
try {
ConnectDatabaseHelper.getConnectStorage(context, ConnectUserRecord.class).write(user);
} catch (Exception e) {
Logger.exception("Failed to store user", e);
throw new RuntimeException("Failed to store user in Connect database", e);
}
}

ConnectDatabaseHelper.getConnectStorage(context, ConnectUserRecord.class).write(user);
}

public static void forgetUser(Context context) {
if (context == null) {
throw new IllegalArgumentException("Context must not be null");
}
try {
DatabaseConnectOpenHelper.deleteDb(context);
CommCareApplication.instance().getGlobalStorage(ConnectKeyRecord.class).removeAll();
ConnectDatabaseHelper.dbBroken = false;
ConnectDatabaseHelper.teardown();
} catch (IllegalStateException e) {
Logger.exception("Database access error while forgetting user", e);
throw new RuntimeException("Failed to access database while cleaning up", e);
} catch (SecurityException e) {
Logger.exception("Permission denied while deleting database", e);
throw new RuntimeException("Failed to delete database due to permissions", e);
} catch (Exception e) {
Logger.exception("Failed to forget user", e);
throw new RuntimeException("Failed to clean up Connect database", e);
}
}

DatabaseConnectOpenHelper.deleteDb(context);
CommCareApplication.instance().getGlobalStorage(ConnectKeyRecord.class).removeAll();
ConnectDatabaseHelper.dbBroken = false;
ConnectDatabaseHelper.teardown();
}
}
46 changes: 20 additions & 26 deletions app/src/org/commcare/connect/database/JobStoreManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int storeJobs(Context context, List<ConnectJobRecord> jobs, boolean prune
int newJob = processAndStoreJobs(existingList, jobs);
ConnectDatabaseHelper.connectDatabase.setTransactionSuccessful();
return newJob;
} catch (Exception e) {
} catch(Exception e) {
Logger.exception("Error storing jobs", e);
throw e;
} finally {
Expand Down Expand Up @@ -96,35 +96,29 @@ private int processAndStoreJobs(List<ConnectJobRecord> existingJobs, List<Connec
}

private boolean storeOrUpdateJob(List<ConnectJobRecord> existingJobs, ConnectJobRecord job) {
try {
// Check if the job already exists
boolean isExisting = false;
for (ConnectJobRecord existingJob : existingJobs) {
if (existingJob.getJobId() == job.getJobId()) {
job.setID(existingJob.getID()); // Set ID for updating
isExisting = true;
break;
}
// Check if the job already exists
boolean isExisting = false;
for (ConnectJobRecord existingJob : existingJobs) {
if (existingJob.getJobId() == job.getJobId()) {
job.setID(existingJob.getID()); // Set ID for updating
isExisting = true;
break;
}
}

// If not existing, create a new record
if (!isExisting) {
if (job.getStatus() == ConnectJobRecord.STATUS_AVAILABLE) {
job.setStatus(ConnectJobRecord.STATUS_AVAILABLE_NEW);
}
// If not existing, create a new record
if (!isExisting) {
if (job.getStatus() == ConnectJobRecord.STATUS_AVAILABLE) {
job.setStatus(ConnectJobRecord.STATUS_AVAILABLE_NEW);
}
job.setLastUpdate(new Date());
jobStorage.write(job);
// Store or update related entities
storeAppInfo(job);
storeModules(job);
storePaymentUnits(job);
return isExisting;

} catch (Exception e) {
Logger.exception("Error storing or updating job: " + job.getTitle(), e);
throw e;
}
job.setLastUpdate(new Date());
jobStorage.write(job);
// Store or update related entities
storeAppInfo(job);
storeModules(job);
storePaymentUnits(job);
return isExisting;
}

private void storeAppInfo(ConnectJobRecord job) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public void processSuccess(int responseCode, InputStream responseData) {
try (InputStream in = responseData) {
JSONObject json = new JSONObject(new String(StreamsUtil.inputStreamToByteArray(in)));
parser.parse(json, sessionData);
} catch (IOException | JSONException e) {
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) {
Logger.exception("Error parsing API response", e);
onFailure(PersonalIdApiErrorCodes.JSON_PARSING_ERROR);
}
Expand All @@ -65,7 +67,9 @@ public void processFailure(int responseCode, @Nullable InputStream errorResponse
Toast.makeText(CommCareApplication.instance(), json.optString("error"),
Toast.LENGTH_LONG).show();
}
} catch (IOException | JSONException e) {
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) {
Logger.exception("Error parsing API error response", e);
onFailure(defaultFailureCode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ public void processSuccess(int responseCode, InputStream responseData) {
newJobs = ConnectJobUtils.storeJobs(getContext(), jobs, true);
setJobListData(jobs);
}
} catch (IOException | JSONException e) {
Logger.exception("Parsing / database error return from Opportunities request", e);
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) {
Logger.exception("Error parsing return from Opportunities request", e);
}

reportApiCall(true, totalJobs, newJobs);
Expand Down Expand Up @@ -424,19 +425,15 @@ private String getOrganizationForType(ConnectJobRecord job, String jobType) {

public Date processJobRecords(ConnectJobRecord job, String jobType) {
Date lastAssessedDate = new Date();
try {
String learnAppId = job.getLearnAppInfo().getAppId();
String deliverAppId = job.getDeliveryAppInfo().getAppId();
if (jobType.equalsIgnoreCase(JOB_LEARNING)) {
ConnectLinkedAppRecord learnRecord = ConnectAppDatabaseUtil.getConnectLinkedAppRecord(getActivity(), learnAppId, "");
return learnRecord != null ? learnRecord.getLastAccessed() : lastAssessedDate;

} else if (jobType.equalsIgnoreCase(JOB_DELIVERY)) {
ConnectLinkedAppRecord deliverRecord = ConnectAppDatabaseUtil.getConnectLinkedAppRecord(getActivity(), deliverAppId, "");
return deliverRecord != null ? deliverRecord.getLastAccessed() : lastAssessedDate;
}
} catch (Exception e) {
e.printStackTrace();
String learnAppId = job.getLearnAppInfo().getAppId();
String deliverAppId = job.getDeliveryAppInfo().getAppId();
if (jobType.equalsIgnoreCase(JOB_LEARNING)) {
ConnectLinkedAppRecord learnRecord = ConnectAppDatabaseUtil.getConnectLinkedAppRecord(getActivity(), learnAppId, "");
return learnRecord != null ? learnRecord.getLastAccessed() : lastAssessedDate;

} else if (jobType.equalsIgnoreCase(JOB_DELIVERY)) {
ConnectLinkedAppRecord deliverRecord = ConnectAppDatabaseUtil.getConnectLinkedAppRecord(getActivity(), deliverAppId, "");
return deliverRecord != null ? deliverRecord.getLastAccessed() : lastAssessedDate;
}
return lastAssessedDate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,17 @@ public void processSuccess(int responseCode, InputStream responseData) {
JSONObject obj = (JSONObject) json.get(i);
ConnectJobRecord job = ConnectJobRecord.fromJson(obj);
jobs.add(job);
}catch (JSONException e) {
} catch (JSONException e) {
Logger.exception("Parsing return from Opportunities request", e);
}
}
new JobStoreManager(requireContext()).storeJobs(requireContext(), jobs, true);
}
} catch (IOException | JSONException e) {
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) {
Toast.makeText(requireContext(), R.string.connect_job_list_api_failure, Toast.LENGTH_SHORT).show();
Logger.exception("Parsing return from Opportunities request", e);
throw new RuntimeException(e);
}

setFragmentRedirection();
Expand Down