<commit_before>/*
 * Copyright (C) 2009 JavaRosa
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.javarosa.user.view;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;

import org.javarosa.core.services.locale.Localization;
import org.javarosa.core.services.storage.IStorageIterator;
import org.javarosa.core.services.storage.IStorageUtility;
import org.javarosa.core.services.storage.StorageManager;
import org.javarosa.user.api.AddUserState;
import org.javarosa.user.model.User;

import de.enough.polish.ui.FramedForm;
import de.enough.polish.ui.Item;
import de.enough.polish.ui.StringItem;
import de.enough.polish.ui.TextField;

public class LoginForm extends FramedForm {

	private final static int DEFAULT_COMMAND_PRIORITY = 1;

	// #if javarosa.login.demobutton
	private StringItem demoButton;
	public final static Command CMD_DEMO_BUTTON = new Command(Localization.get("menu.Demo"),
			Command.ITEM, DEFAULT_COMMAND_PRIORITY);
	// #endif

	public final static Command CMD_CANCEL_LOGIN = new Command(Localization.get("menu.Exit"),
			Command.SCREEN, DEFAULT_COMMAND_PRIORITY);
	public final static Command CMD_LOGIN_BUTTON = new Command(Localization.get("menu.Login"),
			Command.ITEM, DEFAULT_COMMAND_PRIORITY);

	private StringItem loginButton;
	private TextField usernameField;
	private TextField passwordField;
	private IStorageUtility users;
	private User loggedInUser;
	
	/**
	 * @param loginActivity
	 * @param title
	 */
	private final static int DEFAULT_ADMIN_USERID = -1;

	public LoginForm() {
		super(Localization.get("form.login.login"));
		init();
	}

	/**
	 * @param loginActivity
	 * @param title
	 */
	public LoginForm(String title) {
		super(title);
		init();
	}

	/**
	 * @param loginActivity
	 */
	private void init() {
		this.users = StorageManager.getStorage(User.STORAGE_KEY);

		boolean recordsExist = this.users.getNumRecords() > 0;

		// #debug debug
		System.out.println("records in RMS:" + recordsExist);

		User tempUser = new User();
		tempUser.setUsername("");// ? needed

		if (recordsExist) {
			int lowestID = -1;
			IStorageIterator ui = users.iterate();
			while (ui.hasMore()) {
				int userID = ui.nextID();
				if (lowestID == -1 || userID < lowestID)
					lowestID = userID;
			}
			
			tempUser = (User)users.read(lowestID);

		}
		initLoginControls(tempUser.getUsername());

		showVersions();<commit_msg>fix bug in selecting default user for login screen<commit_after>/*
 * Copyright (C) 2009 JavaRosa
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.javarosa.user.view;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;

import org.javarosa.core.services.locale.Localization;
import org.javarosa.core.services.storage.IStorageIterator;
import org.javarosa.core.services.storage.IStorageUtility;
import org.javarosa.core.services.storage.StorageManager;
import org.javarosa.user.api.AddUserState;
import org.javarosa.user.model.User;

import de.enough.polish.ui.FramedForm;
import de.enough.polish.ui.Item;
import de.enough.polish.ui.StringItem;
import de.enough.polish.ui.TextField;

public class LoginForm extends FramedForm {

	private final static int DEFAULT_COMMAND_PRIORITY = 1;

	// #if javarosa.login.demobutton
	private StringItem demoButton;
	public final static Command CMD_DEMO_BUTTON = new Command(Localization.get("menu.Demo"),
			Command.ITEM, DEFAULT_COMMAND_PRIORITY);
	// #endif

	public final static Command CMD_CANCEL_LOGIN = new Command(Localization.get("menu.Exit"),
			Command.SCREEN, DEFAULT_COMMAND_PRIORITY);
	public final static Command CMD_LOGIN_BUTTON = new Command(Localization.get("menu.Login"),
			Command.ITEM, DEFAULT_COMMAND_PRIORITY);

	private StringItem loginButton;
	private TextField usernameField;
	private TextField passwordField;
	private IStorageUtility users;
	private User loggedInUser;
	
	/**
	 * @param loginActivity
	 * @param title
	 */
	private final static int DEFAULT_ADMIN_USERID = -1;

	public LoginForm() {
		super(Localization.get("form.login.login"));
		init();
	}

	/**
	 * @param loginActivity
	 * @param title
	 */
	public LoginForm(String title) {
		super(title);
		init();
	}

	/**
	 * @param loginActivity
	 */
	private void init() {
		this.users = StorageManager.getStorage(User.STORAGE_KEY);

		boolean recordsExist = this.users.getNumRecords() > 0;

		// #debug debug
		System.out.println("records in RMS:" + recordsExist);

		User tempUser = new User();
		tempUser.setUsername("");// ? needed

		if (recordsExist) {
			int highestID = -1;
			IStorageIterator ui = users.iterate();
			while (ui.hasMore()) {
				int userID = ui.nextID();
				if (highestID == -1 || userID > highestID)
					highestID = userID;
			}
			
			tempUser = (User)users.read(highestID);

		}
		initLoginControls(tempUser.getUsername());

		showVersions();
{'fraction_non_alphanumeric': 0.10744358369821488, 'fraction_numerical': 0.004378578646008757, 'mean_word_length': 4.355275022542831, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3754060', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1817, 'n_words': 487}