<commit_before>/**
 *
 */
package org.commcare.entity;

import org.commcare.suite.model.Detail;
import org.commcare.suite.model.DetailField;
import org.commcare.suite.model.Text;
import org.javarosa.core.model.Constants;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.services.Logger;
import org.javarosa.core.services.locale.Localizer;
import org.javarosa.core.util.DataUtil;
import org.javarosa.entity.model.Entity;
import org.javarosa.xpath.XPathException;
import org.javarosa.xpath.XPathTypeMismatchException;
import org.javarosa.xpath.expr.XPathExpression;
import org.javarosa.xpath.expr.XPathFuncExpr;

import java.lang.Object;
import java.lang.RuntimeException;
import java.lang.String;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 * @author ctsims
 *
 */
public class CommCareEntity extends Entity<TreeReference> {

    Detail shortDetail;
    Detail longDetail;
    String[] shortText;
    String[] sortText;
    EvaluationContext context;
    NodeEntitySet set;

    public CommCareEntity(Detail shortDetail, Detail longDetail, EvaluationContext context, NodeEntitySet set) {
        this.shortDetail = shortDetail;
        this.longDetail = longDetail;
        this.context = context;
        this.set = set;
    }

    protected int readEntityId(TreeReference element) {
        return set.getId(element);
    }

    /* (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#entityType()
     */
    public String entityType() {
        return Localizer.clearArguments(shortDetail.getTitle().getText().evaluate(context));
    }

    /* (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#factory()
     */
    public Entity<TreeReference> factory() {
        CommCareEntity entity = new CommCareEntity(shortDetail,longDetail, context, set);
        return entity;
    }

    /* (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#getHeaders(boolean)
     */
    public String[] getHeaders(boolean detailed) {
        Detail d;
        if(!detailed) {
            d = shortDetail;
        } else{
            if(longDetail == null) { return null;}
            d = longDetail;
        }

        if (d.getNodeset() != null) {
            throw new RuntimeException("Entity subnodes not supported: " + d.getNodeset().toString());
        }

        //jls
        Vector<Detail> details = d.getDetails();
        if (details.size() == 0) {
            details.addElement(d);
        }
        int totalFields = 0;
        for (int i = 0; i < details.size(); i++) {
            totalFields += details.elementAt(i).getFields().length;
        }
        String[] output = new String[totalFields];
        int i = 0;
        for (int j = 0; j < details.size(); j++) {
            for (int k = 0; k < details.elementAt(j).getFields().length; k++) {
                output[i] = details.elementAt(j).getFields()[k].getHeader().evaluate();
                i++;
            }
        }
        return output;
    }

    /* (non-Javadoc)
     * @see org.javarosa.patient.select.activity.IEntity#matchID(java.lang.String)
     */
    public boolean match (String key) {
        key = key.toLowerCase();
        String[] fields = this.getShortFields();
        for(int i = 0; i < fields.length; ++i) {
            // don't match to images or graphs
            String form = shortDetail.getFields()[i].getTemplateForm();
            if(form.equals("image") || form.equals("graph")) {
                continue;
            }
            if(fields[i].toLowerCase().startsWith(key)) {
                return true;
            }
        }
        return false;
    }

    /*
     * (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#getForms(boolean)
     */
    public String[] getForms(boolean header) {
        return header ? shortDetail.getHeaderForms() : shortDetail.getTemplateForms();
    }

    /*
     * (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#getForms(boolean)
     */
    public String[] getLongForms(boolean header) {
        if(longDetail == null) { return null;}
        //jls
        Vector<String> v = new Vector<String>();
        Vector<Detail> details = longDetail.getDetails();
        if (details.size() == 0) {
            details.addElement(longDetail);
        }
        for (int i = 0; i < details.size(); i++) {
            String[] forms = header ? details.elementAt(i).getHeaderForms(true) : details.elementAt(i).getTemplateForms(true);
            for (int j = 0; j < forms.length; j++) {
                v.addElement(forms[j]);
            }
        }

        String[] a = new String[v.size()];
        for (int i = 0; i < v.size(); i++) {
            a[i] = v.elementAt(i);
        }
        return a;
    }

    /* (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#getLongFields(org.javarosa.core.services.storage.Persistable)
     */
    public String[] getLongFields(TreeReference element) {
        if(longDetail == null) { return null;}
        EvaluationContext ec = new EvaluationContext(context, element);
        loadVars(ec, longDetail);
        //jls
        Vector<Object> templates = new Vector<Object>();
        if (longDetail.isCompound()) {
            Logger.log("d", "compound");
            for (int i = 0; i < longDetail.getDetails().length; i++) {
                for (int j = 0; j < longDetail.getDetails()[i].getFields().length; j++) {
                    templates.addElement(longDetail.getDetails()[i].getFields()[j].getTemplate());
                }
            }
        }
        else {
            Logger.log("d", "single");
            for (int i = 0; i < longDetail.getFields().length; i++) {
                templates.addElement(longDetail.getFields()[i].getTemplate());
            }
        }
        Logger.log("d", "size=" + templates.size());
        String[] output = new String[templates.size()];
        Logger.log("d", "size2=" + output.length);
        for(int i = 0 ; i < output.length ; i++) {
            Logger.log("i", i + "");
            if (templates.elementAt(i) instanceof Text) {
                Logger.log("i2", i + "");
                output[i] = ((Text) templates.elementAt(i)).evaluate(ec);
                Logger.log("y", "y");
            }
            else {
                output[i] = "";
            }
        }
        return output;
    }

    /* (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#getShortFields()
     */
    public String[] getShortFields() {
        return shortText;
    }

    public String[] getStyleHints (boolean header) {
        if(header) {
            return shortDetail.getHeaderSizeHints();
        } else {
            return shortDetail.getTemplateSizeHints();
        }
    }

    /* (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#loadEntity(org.javarosa.core.services.storage.Persistable)
     */
    protected void loadEntity(TreeReference element) {
        EvaluationContext ec = new EvaluationContext(context, element);<commit_msg>different logging'<commit_after>/**
 *
 */
package org.commcare.entity;

import org.commcare.suite.model.Detail;
import org.commcare.suite.model.DetailField;
import org.commcare.suite.model.Text;
import org.javarosa.core.model.Constants;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.services.Logger;
import org.javarosa.core.services.locale.Localizer;
import org.javarosa.core.util.DataUtil;
import org.javarosa.entity.model.Entity;
import org.javarosa.xpath.XPathException;
import org.javarosa.xpath.XPathTypeMismatchException;
import org.javarosa.xpath.expr.XPathExpression;
import org.javarosa.xpath.expr.XPathFuncExpr;

import java.lang.Object;
import java.lang.RuntimeException;
import java.lang.String;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 * @author ctsims
 *
 */
public class CommCareEntity extends Entity<TreeReference> {

    Detail shortDetail;
    Detail longDetail;
    String[] shortText;
    String[] sortText;
    EvaluationContext context;
    NodeEntitySet set;

    public CommCareEntity(Detail shortDetail, Detail longDetail, EvaluationContext context, NodeEntitySet set) {
        this.shortDetail = shortDetail;
        this.longDetail = longDetail;
        this.context = context;
        this.set = set;
    }

    protected int readEntityId(TreeReference element) {
        return set.getId(element);
    }

    /* (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#entityType()
     */
    public String entityType() {
        return Localizer.clearArguments(shortDetail.getTitle().getText().evaluate(context));
    }

    /* (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#factory()
     */
    public Entity<TreeReference> factory() {
        CommCareEntity entity = new CommCareEntity(shortDetail,longDetail, context, set);
        return entity;
    }

    /* (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#getHeaders(boolean)
     */
    public String[] getHeaders(boolean detailed) {
        Detail d;
        if(!detailed) {
            d = shortDetail;
        } else{
            if(longDetail == null) { return null;}
            d = longDetail;
        }

        if (d.getNodeset() != null) {
            throw new RuntimeException("Entity subnodes not supported: " + d.getNodeset().toString());
        }

        //jls
        Vector<Detail> details = d.getDetails();
        if (details.size() == 0) {
            details.addElement(d);
        }
        int totalFields = 0;
        for (int i = 0; i < details.size(); i++) {
            totalFields += details.elementAt(i).getFields().length;
        }
        String[] output = new String[totalFields];
        int i = 0;
        for (int j = 0; j < details.size(); j++) {
            for (int k = 0; k < details.elementAt(j).getFields().length; k++) {
                output[i] = details.elementAt(j).getFields()[k].getHeader().evaluate();
                i++;
            }
        }
        return output;
    }

    /* (non-Javadoc)
     * @see org.javarosa.patient.select.activity.IEntity#matchID(java.lang.String)
     */
    public boolean match (String key) {
        key = key.toLowerCase();
        String[] fields = this.getShortFields();
        for(int i = 0; i < fields.length; ++i) {
            // don't match to images or graphs
            String form = shortDetail.getFields()[i].getTemplateForm();
            if(form.equals("image") || form.equals("graph")) {
                continue;
            }
            if(fields[i].toLowerCase().startsWith(key)) {
                return true;
            }
        }
        return false;
    }

    /*
     * (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#getForms(boolean)
     */
    public String[] getForms(boolean header) {
        return header ? shortDetail.getHeaderForms() : shortDetail.getTemplateForms();
    }

    /*
     * (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#getForms(boolean)
     */
    public String[] getLongForms(boolean header) {
        if(longDetail == null) { return null;}
        //jls
        Vector<String> v = new Vector<String>();
        Vector<Detail> details = longDetail.getDetails();
        if (details.size() == 0) {
            details.addElement(longDetail);
        }
        for (int i = 0; i < details.size(); i++) {
            String[] forms = header ? details.elementAt(i).getHeaderForms(true) : details.elementAt(i).getTemplateForms(true);
            for (int j = 0; j < forms.length; j++) {
                v.addElement(forms[j]);
            }
        }

        String[] a = new String[v.size()];
        for (int i = 0; i < v.size(); i++) {
            a[i] = v.elementAt(i);
        }
        return a;
    }

    /* (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#getLongFields(org.javarosa.core.services.storage.Persistable)
     */
    public String[] getLongFields(TreeReference element) {
        if(longDetail == null) { return null;}
        EvaluationContext ec = new EvaluationContext(context, element);
        loadVars(ec, longDetail);
        //jls
        Vector<Object> templates = new Vector<Object>();
        if (longDetail.isCompound()) {
            for (int i = 0; i < longDetail.getDetails().length; i++) {
                for (int j = 0; j < longDetail.getDetails()[i].getFields().length; j++) {
                    templates.addElement(longDetail.getDetails()[i].getFields()[j].getTemplate());
                }
            }
        }
        else {
            for (int i = 0; i < longDetail.getFields().length; i++) {
                templates.addElement(longDetail.getFields()[i].getTemplate());
            }
        }
        String[] output = new String[templates.size()];
        for(int i = 0 ; i < output.length ; i++) {
            if (templates.elementAt(i) instanceof Text) {
                output[i] = ((Text) templates.elementAt(i)).evaluate(ec);
            }
            else {
                output[i] = "";
            }
        }
        return output;
    }

    /* (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#getShortFields()
     */
    public String[] getShortFields() {
        return shortText;
    }

    public String[] getStyleHints (boolean header) {
        if(header) {
            return shortDetail.getHeaderSizeHints();
        } else {
            return shortDetail.getTemplateSizeHints();
        }
    }

    /* (non-Javadoc)
     * @see org.javarosa.entity.model.Entity#loadEntity(org.javarosa.core.services.storage.Persistable)
     */
    protected void loadEntity(TreeReference element) {
        EvaluationContext ec = new EvaluationContext(context, element);
{'fraction_non_alphanumeric': 0.1355956678700361, 'fraction_numerical': 0.0023104693140794225, 'mean_word_length': 2.2174216027874563, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 19, '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': '4623276', 'n_tokens_mistral': 4184, 'n_tokens_neox': 4058, 'n_words': 792}