Skip to content
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

[No op] Refactor entityLoaderTask to use a helper class #2907

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
79 changes: 79 additions & 0 deletions app/src/org/commcare/tasks/EntityLoaderHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.commcare.tasks

import android.util.Pair
import io.reactivex.functions.Cancellable
import org.commcare.activities.EntitySelectActivity
import org.commcare.cases.entity.AsyncNodeEntityFactory
import org.commcare.cases.entity.Entity
import org.commcare.cases.entity.EntityStorageCache
import org.commcare.cases.entity.NodeEntityFactory
import org.commcare.models.database.user.models.CommCareEntityStorageCache
import org.commcare.preferences.DeveloperPreferences
import org.commcare.suite.model.Detail
import org.javarosa.core.model.condition.EvaluationContext
import org.javarosa.core.model.instance.TreeReference

class EntityLoaderHelper(
detail: Detail,
evalCtx: EvaluationContext
) : Cancellable {

var focusTargetIndex: Int = -1
private var stopLoading: Boolean = false
var factory: NodeEntityFactory

init {
evalCtx.addFunctionHandler(EntitySelectActivity.getHereFunctionHandler())
if (detail.useAsyncStrategy()) {
val entityStorageCache: EntityStorageCache = CommCareEntityStorageCache("case")
factory = AsyncNodeEntityFactory(detail, evalCtx, entityStorageCache)
} else {
factory = NodeEntityFactory(detail, evalCtx)
if (DeveloperPreferences.collectAndDisplayEntityTraces()) {
factory.activateDebugTraceOutput()
}
}
}

/**
* Loads and prepares a list of entities derived from the given nodeset
*/
fun loadEntities(nodeset: TreeReference): Pair<List<Entity<TreeReference>>, List<TreeReference>>? {
val references = factory.expandReferenceList(nodeset)
val entities = loadEntitiesWithReferences(references)
entities?.let {
factory.prepareEntities(entities)
factory.printAndClearTraces("build")
return Pair<List<Entity<TreeReference>>, List<TreeReference>>(entities, references)
}
return null
}


/**
* Loads a list of entities corresponding to the given references
*/
private fun loadEntitiesWithReferences(references: List<TreeReference>): MutableList<Entity<TreeReference>>? {
val entities: MutableList<Entity<TreeReference>> = ArrayList()
focusTargetIndex = -1
var indexInFullList = 0
for (ref in references) {
if (stopLoading) {
return null
}
val e = factory.getEntity(ref)
if (e != null) {
entities.add(e)
if (e.shouldReceiveFocus()) {
focusTargetIndex = indexInFullList
}
indexInFullList++
}
}
return entities
}

override fun cancel() {
stopLoading = true
}
}
58 changes: 14 additions & 44 deletions app/src/org/commcare/tasks/EntityLoaderTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@

import android.util.Pair;

import org.commcare.activities.EntitySelectActivity;
import org.commcare.android.logging.ForceCloseLogger;
import org.commcare.cases.entity.AsyncNodeEntityFactory;
import org.commcare.cases.entity.Entity;
import org.commcare.cases.entity.EntityStorageCache;
import org.commcare.cases.entity.NodeEntityFactory;
import org.commcare.logging.XPathErrorLogger;
import org.commcare.models.database.user.models.CommCareEntityStorageCache;
import org.commcare.preferences.DeveloperPreferences;
import org.commcare.suite.model.Detail;
import org.commcare.tasks.templates.ManagedAsyncTask;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.services.Logger;
import org.javarosa.xpath.XPathException;

import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -30,53 +23,20 @@ public class EntityLoaderTask
private final static Object lock = new Object();
private static EntityLoaderTask pendingTask = null;

private final NodeEntityFactory factory;
private EntityLoaderListener listener;
private final EntityLoaderHelper entityLoaderHelper;
private Exception mException = null;
private int focusTargetIndex;

public EntityLoaderTask(Detail detail, EvaluationContext evalCtx) {
evalCtx.addFunctionHandler(EntitySelectActivity.getHereFunctionHandler());
if (detail.useAsyncStrategy()) {
EntityStorageCache entityStorageCache = new CommCareEntityStorageCache("case");
this.factory = new AsyncNodeEntityFactory(detail, evalCtx, entityStorageCache);
} else {
this.factory = new NodeEntityFactory(detail, evalCtx);
if (DeveloperPreferences.collectAndDisplayEntityTraces()) {
this.factory.activateDebugTraceOutput();
}
}
entityLoaderHelper = new EntityLoaderHelper(detail, evalCtx);
}

@Override
protected Pair<List<Entity<TreeReference>>, List<TreeReference>> doInBackground(TreeReference... nodeset) {
try {
List<TreeReference> references = factory.expandReferenceList(nodeset[0]);

List<Entity<TreeReference>> full = new ArrayList<>();
focusTargetIndex = -1;
int indexInFullList = 0;
for (TreeReference ref : references) {
if (this.isCancelled()) {
return null;
}

Entity<TreeReference> e = factory.getEntity(ref);
if (e != null) {
full.add(e);
if (e.shouldReceiveFocus()) {
focusTargetIndex = indexInFullList;
}
indexInFullList++;
}
}

factory.prepareEntities(full);
factory.printAndClearTraces("build");
return new Pair<>(full, references);
return entityLoaderHelper.loadEntities(nodeset[0]);
} catch (XPathException xe) {
XPathErrorLogger.INSTANCE.logErrorToCurrentApp(xe);
xe.printStackTrace();
Logger.exception("Error during EntityLoaderTask: " + ForceCloseLogger.getStackTrace(xe), xe);
mException = xe;
return null;
Expand All @@ -99,8 +59,12 @@ protected void onPostExecute(Pair<List<Entity<TreeReference>>, List<TreeReferenc
return;
}

listener.deliverLoadResult(result.first, result.second, factory, focusTargetIndex);
if (result == null) {
return;
}

listener.deliverLoadResult(result.first, result.second, entityLoaderHelper.getFactory(),
entityLoaderHelper.getFocusTargetIndex());
shubham1g5 marked this conversation as resolved.
Show resolved Hide resolved
return;
}
}
Expand Down Expand Up @@ -142,4 +106,10 @@ public void attachListener(EntityLoaderListener listener) {
this.listener = listener;
listener.attachLoader(this);
}

@Override
protected void onCancelled() {
super.onCancelled();
entityLoaderHelper.cancel();
}
}
Loading