<commit_before>package org.commcare.util.screen;

import org.commcare.cases.entity.EntityUtil;
import org.commcare.modern.session.SessionWrapper;
import org.commcare.session.CommCareSession;
import org.commcare.suite.model.Action;
import org.commcare.suite.model.Detail;
import org.commcare.suite.model.EntityDatum;
import org.commcare.suite.model.SessionDatum;
import org.commcare.util.CommCarePlatform;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.instance.AbstractTreeElement;
import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.model.trace.EvaluationTraceReporter;
import org.javarosa.core.model.utils.InstrumentationUtils;
import org.javarosa.core.util.NoLocalizedTextException;
import org.javarosa.model.xform.XPathReference;

import java.util.Hashtable;
import java.util.Vector;

/**
 * Compound Screen to select an entity from a list and then display the one or more details that
 * are associated with the entity.
 *
 * Does not currently support tile based selects
 *
 * @author ctsims
 */
public class EntityScreen extends CompoundScreenHost {

    private TreeReference mCurrentSelection;

    private SessionWrapper mSession;
    private CommCarePlatform mPlatform;

    private Detail mShortDetail;

    private EntityDatum mNeededDatum;
    private Action mPendingAction;

    private Subscreen<EntityScreen> mCurrentScreen;

    private boolean readyToSkip = false;
    private EvaluationContext evalContext;

    private Hashtable<String, TreeReference> referenceMap;

    private boolean handleCaseIndex;

    public EntityScreen(boolean handleCaseIndex) {
        this.handleCaseIndex = handleCaseIndex;
    }

    public void init(SessionWrapper session) throws CommCareSessionException {
        SessionDatum datum = session.getNeededDatum();
        if (!(datum instanceof EntityDatum)) {
            throw new CommCareSessionException("Didn't find an entity select action where one is expected.");
        }
        mNeededDatum = (EntityDatum)datum;

        this.mSession = session;
        this.mPlatform = mSession.getPlatform();

        String detailId = mNeededDatum.getShortDetail();
        if (detailId == null) {
            throw new CommCareSessionException("Can't handle entity selection with blank detail definition for datum " + mNeededDatum.getDataId());
        }

        mShortDetail = this.mPlatform.getDetail(detailId);

        if (mShortDetail == null) {
            throw new CommCareSessionException("Missing detail definition for: " + detailId);
        }

        evalContext = mSession.getEvaluationContext();

        Vector<TreeReference> references = expandEntityReferenceSet(evalContext);
        referenceMap = new Hashtable<>();
        for(TreeReference reference: references) {
            referenceMap.put(getReturnValueFromSelection(reference, (EntityDatum) session.getNeededDatum(), evalContext), reference);
        }

        // for now override 'here()' with the coords of Sao Paulo, eventually allow dynamic setting
        evalContext.addFunctionHandler(new ScreenUtils.HereDummyFunc(-23.56, -46.66));

        if (mNeededDatum.isAutoSelectEnabled() && references.size() == 1) {
            this.setHighlightedEntity(references.firstElement());
            if (!this.setCurrentScreenToDetail()) {
                this.updateSession(session);
                readyToSkip = true;
            }
        } else {<commit_msg>apply bulk query contexts to cli based loads<commit_after>package org.commcare.util.screen;

import org.commcare.cases.entity.EntityUtil;
import org.commcare.cases.query.QueryContext;
import org.commcare.cases.query.queryset.CurrentModelQuerySet;
import org.commcare.modern.session.SessionWrapper;
import org.commcare.session.CommCareSession;
import org.commcare.suite.model.Action;
import org.commcare.suite.model.Detail;
import org.commcare.suite.model.EntityDatum;
import org.commcare.suite.model.SessionDatum;
import org.commcare.util.CommCarePlatform;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.instance.AbstractTreeElement;
import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.model.trace.EvaluationTraceReporter;
import org.javarosa.core.model.trace.ReducingTraceReporter;
import org.javarosa.core.model.utils.InstrumentationUtils;
import org.javarosa.core.util.NoLocalizedTextException;
import org.javarosa.model.xform.XPathReference;

import java.util.Hashtable;
import java.util.Vector;

/**
 * Compound Screen to select an entity from a list and then display the one or more details that
 * are associated with the entity.
 *
 * Does not currently support tile based selects
 *
 * @author ctsims
 */
public class EntityScreen extends CompoundScreenHost {

    private TreeReference mCurrentSelection;

    private SessionWrapper mSession;
    private CommCarePlatform mPlatform;

    private Detail mShortDetail;

    private EntityDatum mNeededDatum;
    private Action mPendingAction;

    private Subscreen<EntityScreen> mCurrentScreen;

    private boolean readyToSkip = false;
    private EvaluationContext evalContext;

    private Hashtable<String, TreeReference> referenceMap;

    private boolean handleCaseIndex;

    public EntityScreen(boolean handleCaseIndex) {
        this.handleCaseIndex = handleCaseIndex;
    }

    public void init(SessionWrapper session) throws CommCareSessionException {
        SessionDatum datum = session.getNeededDatum();
        if (!(datum instanceof EntityDatum)) {
            throw new CommCareSessionException("Didn't find an entity select action where one is expected.");
        }
        mNeededDatum = (EntityDatum)datum;

        this.mSession = session;
        this.mPlatform = mSession.getPlatform();

        String detailId = mNeededDatum.getShortDetail();
        if (detailId == null) {
            throw new CommCareSessionException("Can't handle entity selection with blank detail definition for datum " + mNeededDatum.getDataId());
        }

        mShortDetail = this.mPlatform.getDetail(detailId);

        if (mShortDetail == null) {
            throw new CommCareSessionException("Missing detail definition for: " + detailId);
        }

        evalContext = mSession.getEvaluationContext();

        Vector<TreeReference> references = expandEntityReferenceSet(evalContext);

        //Pulled from NodeEntityFactory. We should likely replace this whole functonality with
        //that from nodeentityfactory
        QueryContext newContext = evalContext.getCurrentQueryContext()
                .checkForDerivativeContextAndReturn(references.size());

        newContext.setHackyOriginalContextBody(new CurrentModelQuerySet(references));

        evalContext.setQueryContext(newContext);

        referenceMap = new Hashtable<>();
        for(TreeReference reference: references) {
            referenceMap.put(getReturnValueFromSelection(reference, (EntityDatum) session.getNeededDatum(), evalContext), reference);
        }

        // for now override 'here()' with the coords of Sao Paulo, eventually allow dynamic setting
        evalContext.addFunctionHandler(new ScreenUtils.HereDummyFunc(-23.56, -46.66));

        if (mNeededDatum.isAutoSelectEnabled() && references.size() == 1) {
            this.setHighlightedEntity(references.firstElement());
            if (!this.setCurrentScreenToDetail()) {
                this.updateSession(session);
                readyToSkip = true;
            }
        } else {
{'fraction_non_alphanumeric': 0.08396339037007561, 'fraction_numerical': 0.00238758456028651, 'mean_word_length': 3.7125, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6198916', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1964, 'n_words': 472}