<commit_before>package org.commcare.core.process;

import org.commcare.cases.instance.CaseDataInstance;
import org.commcare.cases.instance.CaseInstanceTreeElement;
import org.commcare.cases.ledger.instance.LedgerInstanceTreeElement;
import org.commcare.core.interfaces.UserSandbox;
import org.commcare.core.sandbox.SandboxUtils;
import org.commcare.util.CommCarePlatform;
import org.javarosa.core.model.User;
import org.commcare.session.CommCareSession;
import org.javarosa.core.model.instance.AbstractTreeElement;
import org.javarosa.core.model.instance.ExternalDataInstance;
import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.core.model.instance.InstanceInitializationFactory;
import org.javarosa.core.model.instance.TreeElement;

/**
 *  Initializes a CommCare DataInstance against a UserDataInterface and (sometimes) optional
 *  CommCareSession/Platform
 *
 * @author ctsims
 * @author wspride
 */
public class CommCareInstanceInitializer extends InstanceInitializationFactory {

    protected final CommCareSession session;
    protected CaseInstanceTreeElement casebase;
    protected LedgerInstanceTreeElement stockbase;
    protected final UserSandbox mSandbox;
    protected final CommCarePlatform mPlatform;

    // default constructor because Jython is annoying
    public CommCareInstanceInitializer() {
        this(null, null, null);
    }

    public CommCareInstanceInitializer(UserSandbox sandbox) {
        this(null, sandbox, null);
    }

    public CommCareInstanceInitializer(UserSandbox sandbox, CommCarePlatform platform) {
        this(null, sandbox, platform);
    }

    public CommCareInstanceInitializer(UserSandbox sandbox, CommCareSession session) {
        this(session, sandbox, null);
    }

    public CommCareInstanceInitializer(CommCareSession session, UserSandbox sandbox, CommCarePlatform platform) {
        this.session = session;
        this.mSandbox = sandbox;
        this.mPlatform = platform;
    }

    public ExternalDataInstance getSpecializedExternalDataInstance(ExternalDataInstance instance) {
        if (CaseInstanceTreeElement.MODEL_NAME.equals(instance.getInstanceId())) {
            return new CaseDataInstance(instance);
        } else {
            return instance;
        }
    }

    public AbstractTreeElement generateRoot(ExternalDataInstance instance) {
        String ref = instance.getReference();
        if (ref.indexOf(LedgerInstanceTreeElement.MODEL_NAME) != -1) {
            return setupLedgerData(instance);
        } else if (ref.indexOf(CaseInstanceTreeElement.MODEL_NAME) != -1) {
            return setupCaseData(instance);
        } else if (instance.getReference().indexOf("fixture") != -1) {
            return setupFixtureData(instance);
        } else if (instance.getReference().indexOf("session") != -1) {
            return setupSessionData(instance);
        }
        return null;
    }

    protected AbstractTreeElement setupLedgerData(ExternalDataInstance instance) {
        if (stockbase == null) {
            stockbase = new LedgerInstanceTreeElement(instance.getBase(), mSandbox.getLedgerStorage());
        } else {
            //re-use the existing model if it exists.
            stockbase.rebase(instance.getBase());
        }
        return stockbase;
    }

    protected AbstractTreeElement setupCaseData(ExternalDataInstance instance) {
        if (casebase == null) {
            casebase = new CaseInstanceTreeElement(instance.getBase(), mSandbox.getCaseStorage(), false);
        } else {
            //re-use the existing model if it exists.
            casebase.rebase(instance.getBase());
        }
        return casebase;
    }


    protected AbstractTreeElement setupFixtureData(ExternalDataInstance instance) {
        String ref = instance.getReference();
        //TODO: This is all just copied from J2ME code. that's pretty silly. unify that.
        String userId = "";
        User u = mSandbox.getLoggedInUser();

        if (u != null) {
            userId = u.getUniqueId();
        }

        String refId = ref.substring(ref.lastIndexOf('/') + 1, ref.length());
        try {
            FormInstance fixture = SandboxUtils.loadFixture(mSandbox, refId, userId);

            if (fixture == null) {
                throw new FixtureInitializationException("Could not find an lookup table for src: " + ref);
            }

            TreeElement root = fixture.getRoot();
            root.setParent(instance.getBase());
            return root;

        } catch (IllegalStateException ise) {
            throw new FixtureInitializationException("Could not load fixture for src: " + ref);
        }
    }

    protected AbstractTreeElement setupSessionData(ExternalDataInstance instance) {
        if (this.mPlatform == null) {
            throw new RuntimeException("Cannot generate session instance with undeclared platform!");
        }
        User u = mSandbox.getLoggedInUser();
        TreeElement root =
                session.getSessionInstance(getDeviceId(),
                        getVersionString(), u.getUsername(), u.getUniqueId(),
                        u.getProperties()).getRoot();
        root.setParent(instance.getBase());
        return root;
    }

    protected String getDeviceId(){
        return "----";
    }

    protected String getVersionString(){
        return "CommCare Version: " + mPlatform.getMajorVersion() + "." + mPlatform.getMinorVersion();
    }

    public static class FixtureInitializationException extends RuntimeException {

        public FixtureInitializationException(String message) {
            super(message);
        }
    }
}
<commit_msg>Migration instance setup<commit_after>package org.commcare.core.process;

import org.commcare.cases.instance.CaseDataInstance;
import org.commcare.cases.instance.CaseInstanceTreeElement;
import org.commcare.cases.ledger.instance.LedgerInstanceTreeElement;
import org.commcare.core.interfaces.UserSandbox;
import org.commcare.core.sandbox.SandboxUtils;
import org.commcare.util.CommCarePlatform;
import org.javarosa.core.model.User;
import org.commcare.session.CommCareSession;
import org.javarosa.core.model.instance.AbstractTreeElement;
import org.javarosa.core.model.instance.ExternalDataInstance;
import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.core.model.instance.InstanceInitializationFactory;
import org.javarosa.core.model.instance.TreeElement;

/**
 *  Initializes a CommCare DataInstance against a UserDataInterface and (sometimes) optional
 *  CommCareSession/Platform
 *
 * @author ctsims
 * @author wspride
 */
public class CommCareInstanceInitializer extends InstanceInitializationFactory {

    protected final CommCareSession session;
    protected CaseInstanceTreeElement casebase;
    protected LedgerInstanceTreeElement stockbase;
    protected final UserSandbox mSandbox;
    protected final CommCarePlatform mPlatform;

    // default constructor because Jython is annoying
    public CommCareInstanceInitializer() {
        this(null, null, null);
    }

    public CommCareInstanceInitializer(UserSandbox sandbox) {
        this(null, sandbox, null);
    }

    public CommCareInstanceInitializer(UserSandbox sandbox, CommCarePlatform platform) {
        this(null, sandbox, platform);
    }

    public CommCareInstanceInitializer(UserSandbox sandbox, CommCareSession session) {
        this(session, sandbox, null);
    }

    public CommCareInstanceInitializer(CommCareSession session, UserSandbox sandbox, CommCarePlatform platform) {
        this.session = session;
        this.mSandbox = sandbox;
        this.mPlatform = platform;
    }

    public ExternalDataInstance getSpecializedExternalDataInstance(ExternalDataInstance instance) {
        if (CaseInstanceTreeElement.MODEL_NAME.equals(instance.getInstanceId())) {
            return new CaseDataInstance(instance);
        } else {
            return instance;
        }
    }

    public AbstractTreeElement generateRoot(ExternalDataInstance instance) {
        String ref = instance.getReference();
        if (ref.indexOf(LedgerInstanceTreeElement.MODEL_NAME) != -1) {
            return setupLedgerData(instance);
        } else if (ref.indexOf(CaseInstanceTreeElement.MODEL_NAME) != -1) {
            return setupCaseData(instance);
        } else if (instance.getReference().indexOf("fixture") != -1) {
            return setupFixtureData(instance);
        } else if (instance.getReference().indexOf("session") != -1) {
            return setupSessionData(instance);
        } else if (ref.indexOf("migration") != -1) {
            return setupMigrationData(instance);
        }
        return null;
    }

    protected AbstractTreeElement setupLedgerData(ExternalDataInstance instance) {
        if (stockbase == null) {
            stockbase = new LedgerInstanceTreeElement(instance.getBase(), mSandbox.getLedgerStorage());
        } else {
            //re-use the existing model if it exists.
            stockbase.rebase(instance.getBase());
        }
        return stockbase;
    }

    protected AbstractTreeElement setupCaseData(ExternalDataInstance instance) {
        if (casebase == null) {
            casebase = new CaseInstanceTreeElement(instance.getBase(), mSandbox.getCaseStorage(), false);
        } else {
            //re-use the existing model if it exists.
            casebase.rebase(instance.getBase());
        }
        return casebase;
    }


    protected AbstractTreeElement setupFixtureData(ExternalDataInstance instance) {
        String ref = instance.getReference();
        //TODO: This is all just copied from J2ME code. that's pretty silly. unify that.
        String userId = "";
        User u = mSandbox.getLoggedInUser();

        if (u != null) {
            userId = u.getUniqueId();
        }

        String refId = ref.substring(ref.lastIndexOf('/') + 1, ref.length());
        try {
            FormInstance fixture = SandboxUtils.loadFixture(mSandbox, refId, userId);

            if (fixture == null) {
                throw new FixtureInitializationException("Could not find an lookup table for src: " + ref);
            }

            TreeElement root = fixture.getRoot();
            root.setParent(instance.getBase());
            return root;
        } catch (IllegalStateException ise) {
            throw new FixtureInitializationException("Could not load fixture for src: " + ref);
        }
    }

    protected AbstractTreeElement setupSessionData(ExternalDataInstance instance) {
        if (this.mPlatform == null) {
            throw new RuntimeException("Cannot generate session instance with undeclared platform!");
        }
        User u = mSandbox.getLoggedInUser();
        TreeElement root =
                session.getSessionInstance(getDeviceId(),
                        getVersionString(), u.getUsername(), u.getUniqueId(),
                        u.getProperties()).getRoot();
        root.setParent(instance.getBase());
        return root;
    }

    protected String getDeviceId(){
        return "----";
    }

    protected String getVersionString(){
        return "CommCare Version: " + mPlatform.getMajorVersion() + "." + mPlatform.getMinorVersion();
    }

    protected AbstractTreeElement setupMigrationData(ExternalDataInstance instance) {
        return null;
    }

    public static class FixtureInitializationException extends RuntimeException {

        public FixtureInitializationException(String message) {
            super(message);
        }
    }
}

{'fraction_non_alphanumeric': 0.09065647794373047, 'fraction_numerical': 0.0011288641889544981, 'mean_word_length': 2.938782489740082, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3108317', 'n_tokens_mistral': 3064, 'n_tokens_neox': 2942, 'n_words': 666}