-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Process skipped clients #506
base: master
Are you sure you want to change the base?
Changes from 11 commits
b5b5dbb
21b014b
16470ec
443625a
b322055
77e7122
c405d49
51d9247
0402130
d3d2cfd
277e50c
89fb705
35ca072
fa8589f
eb00e3d
e2a4098
0c4106b
2f54b4b
5c5cf23
b4bb240
6bd8dfa
a834abb
07c67a8
4626b37
7e93ddf
27b7787
502694f
eeab005
57aa2d3
af4d112
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.smartregister.giz.job; | ||
|
||
import android.content.Intent; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.evernote.android.job.Job; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.smartregister.AllConstants; | ||
import org.smartregister.giz.service.ReProcessSyncIntentService; | ||
import org.smartregister.job.BaseJob; | ||
|
||
public class GIZClientReprocessJob extends BaseJob { | ||
|
||
public static final String TAG = "GIZClientReprocessJob"; | ||
|
||
@NonNull | ||
@NotNull | ||
@Override | ||
protected Result onRunJob(@NonNull @NotNull Job.Params params) { | ||
Intent intent = new Intent(getApplicationContext(), ReProcessSyncIntentService.class); | ||
getApplicationContext().startService(intent); | ||
return params != null && params.getExtras().getBoolean(AllConstants.INTENT_KEY.TO_RESCHEDULE, false) ? Result.RESCHEDULE : Result.SUCCESS; | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.smartregister.giz.recievers; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
|
||
import org.smartregister.giz.job.GIZClientReprocessJob; | ||
|
||
import timber.log.Timber; | ||
|
||
import static org.smartregister.receiver.SyncStatusBroadcastReceiver.EXTRA_COMPLETE_STATUS; | ||
|
||
public class GizReprocessSyncStatusReceiver extends BroadcastReceiver { | ||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
Bundle data = intent.getExtras(); | ||
if (data != null) { | ||
boolean isComplete = data.getBoolean(EXTRA_COMPLETE_STATUS, false); | ||
if (isComplete) { | ||
GIZClientReprocessJob.scheduleJobImmediately(GIZClientReprocessJob.TAG); | ||
Timber.d("Sync complete"); | ||
} | ||
} | ||
|
||
Timber.d("broadcast Recieved"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the log is too vague and it would be easier to understand if it was a bit more specific on the broadcast that was received |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,14 @@ | |
import net.sqlcipher.Cursor; | ||
import net.sqlcipher.database.SQLiteDatabase; | ||
|
||
import org.smartregister.giz.util.GizUtils; | ||
import org.smartregister.repository.BaseRepository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
|
||
import timber.log.Timber; | ||
|
||
public class GizEventRepository extends BaseRepository { | ||
|
||
public boolean hasEvent(@NonNull String baseEntityId, @NonNull String eventType) { | ||
|
@@ -21,4 +27,33 @@ public boolean hasEvent(@NonNull String baseEntityId, @NonNull String eventType) | |
} | ||
return hasEvent; | ||
} | ||
|
||
public void processSkippedClients() { | ||
String missingClientRegister = "SELECT DISTINCT baseEntityId, formSubmissionId AS formSubmissionId FROM event WHERE baseEntityId IN (SELECT baseEntityId FROM client WHERE baseEntityId NOT IN (SELECT DISTINCT base_entity_id FROM client_register_type)) AND eventType LIKE '%Registration%'"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure we need to alias formSubmissionId here |
||
String missingECClient = "SELECT DISTINCT baseEntityId, formSubmissionId AS formSubmissionId FROM event WHERE baseEntityId IN (SELECT baseEntityId FROM client WHERE baseEntityId NOT IN (SELECT DISTINCT base_entity_id FROM ec_client)) AND eventType LIKE '%Registration%'"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check previous comment on formSubmissionId |
||
HashSet<String> formSubmissionIDs = new HashSet<>(); | ||
addFormSubmissionIds(formSubmissionIDs, missingClientRegister); | ||
addFormSubmissionIds(formSubmissionIDs, missingECClient); | ||
|
||
try { | ||
if (formSubmissionIDs.size() > 0) | ||
GizUtils.initiateEventProcessing(new ArrayList<>(formSubmissionIDs)); | ||
} catch (Exception e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should be generally catching Exception here |
||
Timber.e(e); | ||
} | ||
} | ||
|
||
|
||
public void addFormSubmissionIds(HashSet<String> ids, String query) { | ||
SQLiteDatabase database = getReadableDatabase(); | ||
Cursor cursor = database.rawQuery(query, null); | ||
int columnIndex = cursor.getColumnIndex("formSubmissionId"); | ||
if (cursor.getCount() > 0) { | ||
cursor.moveToFirst(); | ||
do { | ||
ids.add(cursor.getString(columnIndex)); | ||
} while (cursor.moveToNext()); | ||
} | ||
cursor.close(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.smartregister.giz.service; | ||
|
||
import android.app.IntentService; | ||
import android.content.Intent; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
import org.smartregister.giz.application.GizMalawiApplication; | ||
|
||
public class ReProcessSyncIntentService extends IntentService { | ||
|
||
public static final String TAG = "ReProcessSyncIntentService"; | ||
|
||
public ReProcessSyncIntentService() | ||
{ | ||
super(TAG); | ||
} | ||
|
||
public ReProcessSyncIntentService(String name) { | ||
super(name); | ||
} | ||
|
||
@Override | ||
protected void onHandleIntent(@Nullable Intent intent) { | ||
GizMalawiApplication.getInstance().gizEventRepository().processSkippedClients(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.smartregister.giz.repository; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.smartregister.giz.util.GizUtils; | ||
import org.smartregister.repository.Repository; | ||
import org.smartregister.view.activity.DrishtiApplication; | ||
|
||
import net.sqlcipher.Cursor; | ||
import net.sqlcipher.database.SQLiteDatabase; | ||
import java.util.HashSet; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({DrishtiApplication.class}) | ||
public class GizEventRepositoryTest { | ||
|
||
private GizEventRepository repository; | ||
private static final String BASE_ENTITY_ID = "base_entity_id"; | ||
private static final String EVENT_TYPE = "event_type"; | ||
private static final String FORM_SUBMISSION_ID = "form_submission_id"; | ||
|
||
@Mock | ||
private SQLiteDatabase sqLiteDatabase; | ||
|
||
@Mock | ||
private GizUtils gizUtils; | ||
|
||
@Mock | ||
private Cursor cursor; | ||
|
||
@Mock | ||
private Repository baseRepository; | ||
|
||
@Mock | ||
private DrishtiApplication drishtiApplication; | ||
|
||
@Before | ||
public void setUp() { | ||
repository = new GizEventRepository(); | ||
MockitoAnnotations.initMocks(this); | ||
PowerMockito.mockStatic(DrishtiApplication.class); | ||
when(DrishtiApplication.getInstance()).thenReturn(drishtiApplication); | ||
when(drishtiApplication.getRepository()).thenReturn(baseRepository); | ||
when(baseRepository.getReadableDatabase()).thenReturn(sqLiteDatabase); | ||
} | ||
|
||
@Test | ||
public void testHasEvent(){ | ||
when(sqLiteDatabase.query( | ||
"event", | ||
new String[]{"baseEntityId"}, | ||
"baseEntityId = ? and eventType = ? ", | ||
new String[]{BASE_ENTITY_ID, EVENT_TYPE}, | ||
null, | ||
null, | ||
null, | ||
"1" | ||
)).thenReturn(cursor); | ||
|
||
when(cursor.getCount()).thenReturn(1); | ||
|
||
boolean hasEvent = repository.hasEvent(BASE_ENTITY_ID, EVENT_TYPE); | ||
|
||
assertTrue(hasEvent); | ||
} | ||
|
||
|
||
|
||
@Test | ||
public void testAddFormSubmissionIds() { | ||
when(baseRepository.getReadableDatabase()).thenReturn(sqLiteDatabase); | ||
when(sqLiteDatabase.rawQuery("query", null)).thenReturn(cursor); | ||
when(cursor.getCount()).thenReturn(1); | ||
when(cursor.getColumnIndex("formSubmissionId")).thenReturn(0); | ||
when(cursor.moveToFirst()).thenReturn(true); | ||
when(cursor.getString(0)).thenReturn("formSubmissionId"); | ||
|
||
HashSet<String> formSubmissionIds = new HashSet<>(); | ||
repository.addFormSubmissionIds(formSubmissionIds, "query"); | ||
|
||
assertFalse(formSubmissionIds.isEmpty()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are these services set as
exported
and why is this explicitly declared?