/**
 * 
 */
package org.commcare.android.util;

import org.commcare.android.models.ACase;
import org.commcare.android.models.User;
import org.commcare.cases.instance.CaseInstanceTreeElement;
import org.commcare.dalvik.application.CommCareApplication;
import org.commcare.util.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;

/**
 * @author ctsims
 *
 */
public class CommCareInstanceInitializer extends InstanceInitializationFactory {
	CommCareSession session;
	CaseInstanceTreeElement casebase;
	
	public CommCareInstanceInitializer(){ 
		this(null);
	}
	public CommCareInstanceInitializer(CommCareSession session) {
		this.session = session;
	}
	
	public AbstractTreeElement generateRoot(ExternalDataInstance instance) {
		CommCareApplication app = CommCareApplication._();
		String ref = instance.getReference();
		if(ref.indexOf("case") != -1) {
			if(casebase == null) {
				casebase =  new CaseInstanceTreeElement(instance.getBase(), app.getStorage(ACase.STORAGE_KEY, ACase.class), false);
			} else {
				casebase.rebase(instance.getBase());
			}
			return casebase;
		}
		if(instance.getReference().indexOf("fixture") != -1) {
			
			//TODO: This is all just copied from J2ME code. that's pretty silly. unify that.
			
			String userId = "";
			User u = CommCareApplication._().getSession().getLoggedInUser();
			if(u != null) {
				userId = u.getUniqueId();
			}
			
			String refId = ref.substring(ref.lastIndexOf('/') + 1, ref.length());
			
			FormInstance fixture = CommCareUtil.loadFixture(refId, userId);
			
			if(fixture == null) {
				throw new RuntimeException("Could not find an appropriate fixture for src: " + ref);
			}
			
			TreeElement root = fixture.getRoot();
			root.setParent(instance.getBase());
			return root;
		}
		if(instance.getReference().indexOf("session") != -1) {
			User u = app.getSession().getLoggedInUser();
			TreeElement root = session.getSessionInstance(app.getPhoneId(), app.getCurrentVersionString(), u.getUsername(), u.getUniqueId(), u.getProperties()).getRoot();
			root.setParent(instance.getBase());
			return root;
		}
		return null;
	}
}

@@ -46,16 +46,71 @@
 til;%0D%0A%0D%0A
+import java.util.HashSet;%0D%0Aimport java.util.Vector;%0D%0A%0D%0A
 import o
@@ -140,24 +140,24 @@
 els.ACase;%0D%0A
-
 import org.c
@@ -656,16 +656,57 @@
 lement;%0D
+%0Aimport org.javarosa.core.util.DataUtil;%0D
 %0A%0D%0A/**%0D%0A
@@ -1267,24 +1267,24 @@
 == null) %7B%0D%0A
-
 %09%09%09%09casebase
@@ -1389,16 +1389,710 @@
 , false)
+ %7B%0D%0A%09%09%09%09%09@Override%0D%0A%09%09%09%09%09protected Vector%3CInteger%3E union(Vector%3CInteger%3E selectedCases, Vector%3CInteger%3E cases) %7B%0D%0A%09%09%09%09%09%09//This is kind of (ok, so really) awkward looking, but we can't use sets in %0D%0A%09%09%09%09%09%09//ccj2me (Thanks, Nokia!) also, there's no _collections_ interface in%0D%0A%09%09%09%09%09%09//j2me (thanks Sun!) so this is what we get.%0D%0A%09%09%09%09%09%09HashSet%3CInteger%3E selected = new HashSet%3CInteger%3E(selectedCases);%0D%0A%09%09%09%09%09%09selected.addAll(selectedCases);%0D%0A%09%09%09%09%09%09%0D%0A%09%09%09%09%09%09HashSet%3CInteger%3E other = new HashSet%3CInteger%3E();%0D%0A%09%09%09%09%09%09other.addAll(cases);%0D%0A%09%09%09%09%09%09%0D%0A%09%09%09%09%09%09selected.retainAll(selected);%0D%0A%09%09%09%09%09%09%0D%0A%09%09%09%09%09%09selectedCases.clear();%0D%0A%09%09%09%09%09%09selectedCases.addAll(selected);%0D%0A%09%09%09%09%09%09return selectedCases;%0D%0A%09%09%09%09%09%7D%0D%0A%09%09%09%09%7D
 ;%0D%0A%09%09%09%7D 

use a better platform specific union procedure for establishing bulk case queries