Skip to content

Commit

Permalink
wrap client details db insertion in transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
hilpitome committed Oct 17, 2024
1 parent 6adb177 commit 8e99d5c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=6.1.5-ALPHA42-SNAPSHOT
VERSION_NAME=6.1.5-ALPHA48-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public void batchInsertDetails(Map<String, String> values, long timestamp) {

try {
database = masterRepository().getWritableDatabase();
// Start transaction
database.beginTransaction();

// Prepare the SQL for inserts and updates
String insertSQL = "INSERT INTO " + TABLE_NAME + " (" +
Expand All @@ -84,6 +86,7 @@ public void batchInsertDetails(Map<String, String> values, long timestamp) {

String baseEntityId = values.get(BASE_ENTITY_ID_COLUMN);


for (String key : values.keySet()) {
String val = values.get(key);
if(val == null ) continue;
Expand Down Expand Up @@ -115,9 +118,14 @@ public void batchInsertDetails(Map<String, String> values, long timestamp) {
insertStatement.clearBindings();
}
}
database.setTransactionSuccessful();
} catch (Exception e) {
Timber.e(e);
} finally {
// End the transaction
if (database != null) {
database.endTransaction();
}
// Close the prepared statements
if (insertStatement != null) {
insertStatement.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.json.JSONArray;
import org.smartregister.AllConstants;
import org.smartregister.CoreLibrary;
import org.smartregister.SyncConfiguration;
import org.smartregister.commonregistry.AllCommonsRepository;
import org.smartregister.commonregistry.CommonRepository;
import org.smartregister.converters.ClientConverter;
Expand Down Expand Up @@ -66,9 +67,13 @@ public class ClientProcessorForJava {

private AppExecutors appExecutors;

private Boolean updatesDetailsTable;


public ClientProcessorForJava(Context context) {
mContext = context;
appExecutors = new AppExecutors();
updatesDetailsTable = CoreLibrary.getInstance().getSyncConfiguration().updateClientDetailsTable();
}

public static ClientProcessorForJava getInstance(Context context) {
Expand Down Expand Up @@ -644,7 +649,7 @@ protected String getFormattedValue(Column column, String columnValue) {
* @param eventDate
*/
protected void addContentValuesToDetailsTable(ContentValues values, Long eventDate) {
if (!CoreLibrary.getInstance().getSyncConfiguration().updateClientDetailsTable())
if (!updatesDetailsTable)
return;

try {
Expand Down Expand Up @@ -674,26 +679,24 @@ public void updateClientDetailsTable(Event event, Client client) {
Timber.d("Started updateClientDetailsTable");
long updateClientDetailsStart = System.currentTimeMillis();

if (CoreLibrary.getInstance().getSyncConfiguration().updateClientDetailsTable()) {
if (updatesDetailsTable) {
String baseEntityId = client.getBaseEntityId();
Long timestamp = getEventDate(event.getEventDate());

Map<String, String> genderInfo = getGender(client);

Map<String, String> addressInfo = getClientAddressAsMap(client);

Map<String, String> attributes = getClientAttributes(client);

Map<String, String> obs = getObsFromEvent(event);
Map<String, String> clientDetails = new HashMap<>();
clientDetails.putAll(genderInfo);
clientDetails.putAll(addressInfo);
clientDetails.putAll(attributes);
clientDetails.putAll(obs);
// Retrieve necessary client information in a single step
Map<String, String> clientDetails = new HashMap<>(getGender(client));
clientDetails.putAll(getClientAddressAsMap(client));
clientDetails.putAll(getClientAttributes(client));
clientDetails.putAll(getObsFromEvent(event));
clientDetails.put("base_entity_id", baseEntityId);

// Save client details in a batch process
batchSaveClientDetails(clientDetails, timestamp);

// Clear the map to free memory
clientDetails.clear();
}
Timber.i("Updating client details took, %s ", System.currentTimeMillis()-updateClientDetailsStart);
Timber.i("Updating client details took, %d ", System.currentTimeMillis()-updateClientDetailsStart);
event.addDetails(detailsUpdated, Boolean.TRUE.toString());

Timber.d("Finished updateClientDetailsTable");
Expand Down Expand Up @@ -850,9 +853,7 @@ public Map<String, String> getClientAddressAsMap(Client client) {
Address address = addressList.get(0);
Map<String, String> addressFieldMap = address.getAddressFields();
if (addressFieldMap != null) {
for (Map.Entry<String, String> entry : addressFieldMap.entrySet()) {
addressMap.put(entry.getKey(), entry.getValue());
}
addressMap.putAll(addressFieldMap);
}

List<Field> fields = getFields(address.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,13 +472,14 @@ public void fetchAllLocations(int recordCount) {
location.setSyncStatus(BaseRepository.TYPE_Synced);

locationRepository.addOrUpdate(location);

for (LocationTag tag : location.getLocationTags()) {
LocationTag locationTag = new LocationTag();
locationTag.setLocationId(location.getId());
locationTag.setName(tag.getName());

locationTagRepository.addOrUpdate(locationTag);
if(location.getLocationTags() != null){
for (LocationTag tag : location.getLocationTags()) {
LocationTag locationTag = new LocationTag();
locationTag.setLocationId(location.getId());
locationTag.setName(tag.getName());

locationTagRepository.addOrUpdate(locationTag);
}
}
} catch (Exception e) {
Timber.e(e, "EXCEPTION %s", e.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,7 @@ private void processFetchedEvents(Response resp, ECSyncHelper ecSyncUpdater, fin
ecSyncUpdater.updateLastSyncTimeStamp(lastServerVersion);
}
sendSyncProgressBroadcast(eCount);
SyncServiceJob.scheduleJobImmediately(SyncServiceJob.TAG);

fetchRetry(0, true);

}
}
Expand Down

0 comments on commit 8e99d5c

Please sign in to comment.