<commit_before>package org.javarosa.demo.shell;

import java.util.Hashtable;

import javax.microedition.midlet.MIDlet;

import org.javarosa.activity.splashscreen.SplashScreenActivity;
import org.javarosa.communication.http.HttpTransportModule;
import org.javarosa.communication.http.HttpTransportProperties;
import org.javarosa.communication.ui.CommunicationUIModule;
import org.javarosa.core.Context;
import org.javarosa.core.JavaRosaServiceProvider;
import org.javarosa.core.api.Constants;
import org.javarosa.core.api.IActivity;
import org.javarosa.core.api.IShell;
import org.javarosa.core.api.IView;
import org.javarosa.core.model.CoreModelModule;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.instance.DataModelTree;
import org.javarosa.core.model.storage.FormDefRMSUtility;
import org.javarosa.core.services.properties.JavaRosaPropertyRules;
import org.javarosa.core.services.transport.TransportMethod;
import org.javarosa.core.util.PropertyUtils;
import org.javarosa.core.util.WorkflowStack;
import org.javarosa.formmanager.FormManagerModule;
import org.javarosa.formmanager.activity.DisplayFormsHttpActivity;
import org.javarosa.formmanager.activity.FormEntryActivity;
import org.javarosa.formmanager.activity.FormEntryContext;
import org.javarosa.formmanager.activity.FormListActivity;
import org.javarosa.formmanager.activity.FormTransportActivity;
import org.javarosa.formmanager.activity.GetFormHttpActivity;
import org.javarosa.formmanager.activity.GetFormListHttpActivity;
import org.javarosa.formmanager.activity.MemoryCheckActivity;
import org.javarosa.formmanager.activity.ModelListActivity;
import org.javarosa.formmanager.utility.FormDefSerializer;
import org.javarosa.formmanager.utility.TransportContext;
import org.javarosa.formmanager.view.Commands;
import org.javarosa.j2me.storage.rms.RMSStorageModule;
import org.javarosa.model.xform.XFormSerializingVisitor;
import org.javarosa.model.xform.XFormsModule;
import org.javarosa.services.properties.activity.PropertyScreenActivity;
import org.javarosa.user.activity.AddUserActivity;
import org.javarosa.user.activity.RemoteLoginActivity;
import org.javarosa.user.activity.UpdateProfileActivity;
import org.javarosa.user.model.User;
import org.javarosa.xform.util.XFormUtils;
/**
 * This is the shell for the JavaRosa demo that handles switching all of the views
 * @author <NAME>
 *
 */
public class JavaRosaDemoShell implements IShell {
	// List of views that are used by this shell
	MIDlet midlet;

	WorkflowStack stack;
	Context context;

	IActivity currentActivity;
	IActivity mostRecentListActivity; //should never be accessed, only checked for type

	public JavaRosaDemoShell() {
		stack = new WorkflowStack();
		context = new Context();
	}

	public void exitShell() {
		midlet.notifyDestroyed();
	}

	public void run() {
		init();
		workflow(null, null, null);
	}

	private void init() {
		loadModules();
		loadProperties();

		FormDefRMSUtility formDef = (FormDefRMSUtility)JavaRosaServiceProvider.instance().getStorageManager().getRMSStorageProvider().getUtility(FormDefRMSUtility.getUtilityName());
		if (formDef.getNumberOfRecords() == 0) {
			formDef.writeToRMS(XFormUtils.getFormFromResource("/CHMTTL_Help.xhtml"));
//			formDef.writeToRMS(XFormUtils.getFormFromResource("/onlineTest.xhtml"));
//			formDef.writeToRMS(XFormUtils.getFormFromResource("/CHMTOpenDay2.xhtml"));
//			formDef.writeToRMS(XFormUtils.getFormFromResource("/CHMTTLT2.xhtml"));
//		formDef.writeToRMS(XFormUtils.getFormFromResource("/hmis-a_draft.xhtml"));
//		formDef.writeToRMS(XFormUtils.getFormFromResource("/MobileSurvey.xhtml"));
		}
	}

	private void loadModules() {
		new RMSStorageModule().registerModule(context);
		new XFormsModule().registerModule(context);
		new CoreModelModule().registerModule(context);
		new HttpTransportModule().registerModule(context);
		new FormManagerModule().registerModule(context);
		new CommunicationUIModule().registerModule(context);
	}

	private void generateSerializedForms(String originalResource) {
		FormDef a = XFormUtils.getFormFromResource(originalResource);
		FormDefSerializer fds = new FormDefSerializer();
		fds.setForm(a);
		fds.setFname(originalResource+".serialized");
		new Thread(fds).start();
	}

	private void workflow(IActivity lastActivity, String returnCode, Hashtable returnVals) {
		if (returnVals == null)
			returnVals = new Hashtable(); //for easier processing

		if (lastActivity != currentActivity) {
			System.out.println("Received 'return' event from activity other than the current activity" +
					" (such as a background process). Can't handle this yet");
			return;
		}

		if (returnCode == Constants.ACTIVITY_SUSPEND || returnCode == Constants.ACTIVITY_NEEDS_RESOLUTION) {
			stack.push(lastActivity);
			workflowLaunch(lastActivity, returnCode, returnVals);
		} else {
			if (stack.size() > 0) {
				workflowResume(stack.pop(), lastActivity, returnCode, returnVals);
			} else {
				workflowLaunch(lastActivity, returnCode, returnVals);
				if (lastActivity != null)
					lastActivity.destroy();
			}
		}
	}



	private void workflowLaunch (IActivity returningActivity, String returnCode, Hashtable returnVals) {
		if (returningActivity == null) {

			launchActivity(new SplashScreenActivity(this, "/splash.gif"), context);

		} else if (returningActivity instanceof SplashScreenActivity) {

			//#if javarosa.dev.shortcuts
			launchActivity(new FormListActivity(this, "Forms List"), context);
			//#else
			String passwordVAR = midlet.getAppProperty("username");
            String usernameVAR = midlet.getAppProperty("password");
            if ((usernameVAR == null) || (passwordVAR == null))
            {
            context.setElement("username","admin");
            context.setElement("password","p");
            }
            else{
                    context.setElement("username",usernameVAR);
                    context.setElement("password",passwordVAR);
            }
            context.setElement("authorization", "admin");
			//launchActivity(new LoginActivity(this, "Login"), context);
			launchActivity(new RemoteLoginActivity(this, "Login"), context);
			//#endif

		} else if (returningActivity instanceof RemoteLoginActivity) {

			Object returnVal = returnVals.get(RemoteLoginActivity.COMMAND_KEY);
			if (returnVal == "USER_VALIDATED") {
				User user = (User)returnVals.get(RemoteLoginActivity.USER);
				Object loginType =returnVals.get(RemoteLoginActivity.TYPE);
				Hashtable profile = (Hashtable)returnVals.get(RemoteLoginActivity.PROFILE);
				UpdateProfileActivity addProp = new UpdateProfileActivity(this);
				MemoryCheckActivity memcheck = new MemoryCheckActivity(this);
				if (user != null){
					context.setCurrentUser(user.getUsername());
					context.setElement("USER", user);
				}
				if(loginType == "REMOTE_AUTH"){
					context.addAllValues(profile);
					launchActivity(addProp, context);//only launch profile update if remotely authenticated
				}
				else
				{//loginType == "LOCAL_AUTH"
					launchActivity(memcheck, context);
				}
			} else if (returnVal == "USER_CANCELLED") {
				exitShell();
			}

		}
		else if (returningActivity instanceof UpdateProfileActivity)
		{
			launchActivity(new MemoryCheckActivity(this), context);<commit_msg>[r1816] recover from incorrect logins better<commit_after>package org.javarosa.demo.shell;

import java.util.Hashtable;

import javax.microedition.midlet.MIDlet;

import org.javarosa.activity.splashscreen.SplashScreenActivity;
import org.javarosa.communication.http.HttpTransportModule;
import org.javarosa.communication.http.HttpTransportProperties;
import org.javarosa.communication.ui.CommunicationUIModule;
import org.javarosa.core.Context;
import org.javarosa.core.JavaRosaServiceProvider;
import org.javarosa.core.api.Constants;
import org.javarosa.core.api.IActivity;
import org.javarosa.core.api.IShell;
import org.javarosa.core.api.IView;
import org.javarosa.core.model.CoreModelModule;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.instance.DataModelTree;
import org.javarosa.core.model.storage.FormDefRMSUtility;
import org.javarosa.core.services.properties.JavaRosaPropertyRules;
import org.javarosa.core.services.transport.TransportMethod;
import org.javarosa.core.util.PropertyUtils;
import org.javarosa.core.util.WorkflowStack;
import org.javarosa.formmanager.FormManagerModule;
import org.javarosa.formmanager.activity.DisplayFormsHttpActivity;
import org.javarosa.formmanager.activity.FormEntryActivity;
import org.javarosa.formmanager.activity.FormEntryContext;
import org.javarosa.formmanager.activity.FormListActivity;
import org.javarosa.formmanager.activity.FormTransportActivity;
import org.javarosa.formmanager.activity.GetFormHttpActivity;
import org.javarosa.formmanager.activity.GetFormListHttpActivity;
import org.javarosa.formmanager.activity.MemoryCheckActivity;
import org.javarosa.formmanager.activity.ModelListActivity;
import org.javarosa.formmanager.utility.FormDefSerializer;
import org.javarosa.formmanager.utility.TransportContext;
import org.javarosa.formmanager.view.Commands;
import org.javarosa.j2me.storage.rms.RMSStorageModule;
import org.javarosa.model.xform.XFormSerializingVisitor;
import org.javarosa.model.xform.XFormsModule;
import org.javarosa.services.properties.activity.PropertyScreenActivity;
import org.javarosa.user.activity.AddUserActivity;
import org.javarosa.user.activity.RemoteLoginActivity;
import org.javarosa.user.activity.UpdateProfileActivity;
import org.javarosa.user.model.User;
import org.javarosa.xform.util.XFormUtils;
/**
 * This is the shell for the JavaRosa demo that handles switching all of the views
 * @author <NAME>
 *
 */
public class JavaRosaDemoShell implements IShell {
	// List of views that are used by this shell
	MIDlet midlet;

	WorkflowStack stack;
	Context context;

	IActivity currentActivity;
	IActivity mostRecentListActivity; //should never be accessed, only checked for type

	public JavaRosaDemoShell() {
		stack = new WorkflowStack();
		context = new Context();
	}

	public void exitShell() {
		midlet.notifyDestroyed();
	}

	public void run() {
		init();
		workflow(null, null, null);
	}

	private void init() {
		loadModules();
		loadProperties();

		FormDefRMSUtility formDef = (FormDefRMSUtility)JavaRosaServiceProvider.instance().getStorageManager().getRMSStorageProvider().getUtility(FormDefRMSUtility.getUtilityName());
		if (formDef.getNumberOfRecords() == 0) {
			formDef.writeToRMS(XFormUtils.getFormFromResource("/CHMTTL_Help.xhtml"));
//			formDef.writeToRMS(XFormUtils.getFormFromResource("/onlineTest.xhtml"));
//			formDef.writeToRMS(XFormUtils.getFormFromResource("/CHMTOpenDay2.xhtml"));
//			formDef.writeToRMS(XFormUtils.getFormFromResource("/CHMTTLT2.xhtml"));
//		formDef.writeToRMS(XFormUtils.getFormFromResource("/hmis-a_draft.xhtml"));
//		formDef.writeToRMS(XFormUtils.getFormFromResource("/MobileSurvey.xhtml"));
		}
	}

	private void loadModules() {
		new RMSStorageModule().registerModule(context);
		new XFormsModule().registerModule(context);
		new CoreModelModule().registerModule(context);
		new HttpTransportModule().registerModule(context);
		new FormManagerModule().registerModule(context);
		new CommunicationUIModule().registerModule(context);
	}

	private void generateSerializedForms(String originalResource) {
		FormDef a = XFormUtils.getFormFromResource(originalResource);
		FormDefSerializer fds = new FormDefSerializer();
		fds.setForm(a);
		fds.setFname(originalResource+".serialized");
		new Thread(fds).start();
	}

	private void workflow(IActivity lastActivity, String returnCode, Hashtable returnVals) {
		if (returnVals == null)
			returnVals = new Hashtable(); //for easier processing

		if (lastActivity != currentActivity) {
			System.out.println("Received 'return' event from activity other than the current activity" +
					" (such as a background process). Can't handle this yet");
			return;
		}

		if (returnCode == Constants.ACTIVITY_SUSPEND || returnCode == Constants.ACTIVITY_NEEDS_RESOLUTION) {
			stack.push(lastActivity);
			workflowLaunch(lastActivity, returnCode, returnVals);
		} else {
			if (stack.size() > 0) {
				workflowResume(stack.pop(), lastActivity, returnCode, returnVals);
			} else {
				workflowLaunch(lastActivity, returnCode, returnVals);
				if (lastActivity != null)
					lastActivity.destroy();
			}
		}
	}



	private void workflowLaunch (IActivity returningActivity, String returnCode, Hashtable returnVals) {
		if (returningActivity == null) {

			launchActivity(new SplashScreenActivity(this, "/splash.gif"), context);

		} else if (returningActivity instanceof SplashScreenActivity) {

			//#if javarosa.dev.shortcuts
			launchActivity(new FormListActivity(this, "Forms List"), context);
			//#else
			String passwordVAR = midlet.getAppProperty("username");
            String usernameVAR = midlet.getAppProperty("password");
            if ((usernameVAR == null) || (passwordVAR == null))
            {
            context.setElement("username","admin");
            context.setElement("password","p");
            }
            else{
                    context.setElement("username",usernameVAR);
                    context.setElement("password",passwordVAR);
            }
            context.setElement("authorization", "admin");
			//launchActivity(new LoginActivity(this, "Login"), context);
			launchActivity(new RemoteLoginActivity(this, "Login"), context);
			//#endif

		} else if (returningActivity instanceof RemoteLoginActivity) {

			Object returnVal = returnVals.get(RemoteLoginActivity.COMMAND_KEY);
			if (returnVal == "USER_VALIDATED") {
				User user = (User)returnVals.get(RemoteLoginActivity.USER);
				Object loginType =returnVals.get(RemoteLoginActivity.TYPE);
				Hashtable profile = (Hashtable)returnVals.get(RemoteLoginActivity.PROFILE);
				UpdateProfileActivity addProp = new UpdateProfileActivity(this);
				MemoryCheckActivity memcheck = new MemoryCheckActivity(this);
				if (user != null){
					context.setCurrentUser(user.getUsername());
					context.setElement("USER", user);
				}
				if(loginType == "REMOTE_AUTH"){
					context.addAllValues(profile);
					launchActivity(addProp, context);//only launch profile update if remotely authenticated
				}
				else
				{//loginType == "LOCAL_AUTH"
					launchActivity(memcheck, context);
				}
			}
			else if (returnVal == "USER_NOT_VALIDATED")
			{
				launchActivity(new RemoteLoginActivity(this, "Login"), context);
			}
			else if (returnVal == "USER_CANCELLED") {
				exitShell();
			}

		}
		else if (returningActivity instanceof UpdateProfileActivity)
		{
			launchActivity(new MemoryCheckActivity(this), context);
{'fraction_non_alphanumeric': 0.11113382393022622, 'fraction_numerical': 0.0009539384028345599, 'mean_word_length': 5.112869637650979, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '833221', 'n_tokens_mistral': 4780, 'n_tokens_neox': 4366, 'n_words': 782}