-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #881 from opensrp/clean-duplicate-mother-ids
clean duplicate mother ids
- Loading branch information
Showing
15 changed files
with
417 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
opensrp-core/src/main/java/org/smartregister/domain/DuplicateZeirIdStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.smartregister.domain; | ||
|
||
public enum DuplicateZeirIdStatus { | ||
CLEANED("CLEANED"), PENDING("PENDING"); | ||
private String value; | ||
|
||
DuplicateZeirIdStatus(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String value() { | ||
return value; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
opensrp-core/src/main/java/org/smartregister/job/DuplicateCleanerWorker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.smartregister.job; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.work.WorkManager; | ||
import androidx.work.Worker; | ||
import androidx.work.WorkerParameters; | ||
|
||
import org.smartregister.domain.DuplicateZeirIdStatus; | ||
import org.smartregister.util.AppHealthUtils; | ||
|
||
import timber.log.Timber; | ||
|
||
public class DuplicateCleanerWorker extends Worker { | ||
private Context mContext; | ||
|
||
public DuplicateCleanerWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { | ||
super(context, workerParams); | ||
mContext = context; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Result doWork() { | ||
DuplicateZeirIdStatus duplicateZeirIdStatus = AppHealthUtils.cleanUniqueZeirIds(); | ||
Timber.i("Doing some cleaning work"); | ||
if (duplicateZeirIdStatus != null && duplicateZeirIdStatus.equals(DuplicateZeirIdStatus.CLEANED)) | ||
WorkManager.getInstance(mContext).cancelWorkById(this.getId()); | ||
|
||
return Result.success(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
opensrp-core/src/main/java/org/smartregister/repository/ZeirIdCleanupRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.smartregister.repository; | ||
|
||
import net.sqlcipher.Cursor; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import timber.log.Timber; | ||
|
||
public class ZeirIdCleanupRepository extends BaseRepository { | ||
|
||
private static final String BASE_ENTITY_ID = "baseEntityId"; | ||
|
||
private static final String DUPLICATES_SQL = | ||
"WITH duplicates AS ( " + | ||
" WITH clients AS ( " + | ||
" SELECT baseEntityId, COALESCE(json_extract(json, '$.identifiers.ZEIR_ID'), json_extract(json, '$.identifiers.M_ZEIR_ID')) zeir_id " + | ||
" FROM client " + | ||
" ) " + | ||
" SELECT b.* FROM (SELECT baseEntityId, zeir_id FROM clients GROUP BY zeir_id HAVING count(zeir_id) > 1) a " + | ||
" INNER JOIN clients b ON a.zeir_id=b.zeir_id " + | ||
" UNION " + | ||
" SELECT * FROM clients WHERE zeir_id IS NULL " + | ||
") " + | ||
"SELECT baseEntityId, zeir_id, lag(zeir_id) over(order by zeir_id) AS prev_zeir_id FROM duplicates"; | ||
|
||
public Map<String, String> getClientsWithDuplicateZeirIds() { | ||
Map<String, String> duplicates = new HashMap<>(); | ||
|
||
Cursor cursor = null; | ||
try { | ||
cursor = getWritableDatabase().rawQuery(DUPLICATES_SQL, new String[]{}); | ||
|
||
while (cursor.moveToNext()) { | ||
String baseEntityId = cursor.getString(cursor.getColumnIndex(BASE_ENTITY_ID)); | ||
String zeirId = cursor.getString(cursor.getColumnIndex("zeir_id")); | ||
|
||
duplicates.put(baseEntityId, zeirId); | ||
|
||
String prevZeirId = null; | ||
try { | ||
prevZeirId = cursor.getString(cursor.getColumnIndex("prev_zeir_id")); | ||
} catch (NullPointerException e) { | ||
Timber.e(e, "null prev_zeir_id"); | ||
} | ||
|
||
if (StringUtils.isNotEmpty(prevZeirId) && (prevZeirId.equals(zeirId))) { | ||
duplicates.put(baseEntityId, prevZeirId); | ||
} | ||
} | ||
} catch (Exception e) { | ||
Timber.e(e); | ||
} finally { | ||
if (cursor != null && !cursor.isClosed()) | ||
cursor.close(); | ||
} | ||
|
||
return duplicates; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.