	private void doPost(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
		String page = httpRequest.getParameter("page");
		Map<String, Object> referenceMap = new HashMap<String, Object>();
		Writer writer = httpResponse.getWriter();
		wizardModel.errors.clear();
		if ("databasesetup.vm".equals(page)) {
			wizardModel.databaseConnection = httpRequest.getParameter("database_connection");
			checkForEmptyValue(wizardModel.databaseConnection, wizardModel.errors, "Database connection string");
			if ("yes".equals(httpRequest.getParameter("current_openmrs_database"))) {
				wizardModel.databaseName = httpRequest.getParameter("openmrs_current_database_name");
				checkForEmptyValue(wizardModel.databaseName, wizardModel.errors, "Current database name");
				wizardModel.hasCurrentOpenmrsDatabase = true;
			} else {
				wizardModel.hasCurrentOpenmrsDatabase = false;
				wizardModel.createTables = true;
				wizardModel.databaseName = httpRequest.getParameter("openmrs_new_database_name");
				checkForEmptyValue(wizardModel.databaseName, wizardModel.errors, "New database name");
				wizardModel.createDatabaseUsername = httpRequest.getParameter("create_database_username");
				checkForEmptyValue(wizardModel.createDatabaseUsername, wizardModel.errors,
				    "A user that has 'CREATE DATABASE' privileges");
				wizardModel.createDatabasePassword = httpRequest.getParameter("create_database_password");
				checkForEmptyValue(wizardModel.createDatabasePassword, wizardModel.errors,
				    "Password for user with 'CREATE DATABASE' privileges");
			}
			if (wizardModel.errors.isEmpty()) {
				page = "databasetablesanduser.vm";
			}
			renderTemplate(page, referenceMap, writer);
		} 
		else if ("databasetablesanduser.vm".equals(page)) {
			if ("Back".equals(httpRequest.getParameter("back"))) {
				renderTemplate("databasesetup.vm", referenceMap, writer);
				return;
			}
			if (wizardModel.hasCurrentOpenmrsDatabase) {
				wizardModel.createTables = "yes".equals(httpRequest.getParameter("create_tables"));
			}
			wizardModel.addDemoData = "yes".equals(httpRequest.getParameter("add_demo_data"));
			if ("yes".equals(httpRequest.getParameter("current_database_user"))) {
				wizardModel.currentDatabaseUsername = httpRequest.getParameter("current_database_username");
				checkForEmptyValue(wizardModel.currentDatabaseUsername, wizardModel.errors, "Curent user account");
				wizardModel.currentDatabasePassword = httpRequest.getParameter("current_database_password");
				checkForEmptyValue(wizardModel.currentDatabasePassword, wizardModel.errors, "Current user account password");
				wizardModel.hasCurrentDatabaseUser = true;
				wizardModel.createDatabaseUser = false;
			} else {
				wizardModel.hasCurrentDatabaseUser = false;
				wizardModel.createDatabaseUser = true;
				wizardModel.createUserUsername = httpRequest.getParameter("create_user_username");
				checkForEmptyValue(wizardModel.createUserUsername, wizardModel.errors,
				    "A user that has 'CREATE USER' privileges");
				wizardModel.createUserPassword = httpRequest.getParameter("create_user_password");
				checkForEmptyValue(wizardModel.createUserPassword, wizardModel.errors,
				    "Password for user that has 'CREATE USER' privileges");
			}
			if (wizardModel.errors.isEmpty()) { 
				page = "otherruntimeproperties.vm";
			}
			renderTemplate(page, referenceMap, writer);
		} 
		else if ("otherruntimeproperties.vm".equals(page)) {
			if ("Back".equals(httpRequest.getParameter("back"))) {
				renderTemplate("databasetablesanduser.vm", referenceMap, writer);
				return;
			}
			wizardModel.moduleWebAdmin = "yes".equals(httpRequest.getParameter("module_web_admin"));
			wizardModel.autoUpdateDatabase = "yes".equals(httpRequest.getParameter("auto_update_database"));
			if (wizardModel.createTables) { 
				page = "adminusersetup.vm";
			} else { 
				page = "implementationidsetup.vm";
			}
			renderTemplate(page, referenceMap, writer);
		} 
		else if ("adminusersetup.vm".equals(page)) {
			if ("Back".equals(httpRequest.getParameter("back"))) {
				renderTemplate("otherruntimeproperties.vm", referenceMap, writer);
				return;
			}
			wizardModel.adminUserPassword = httpRequest.getParameter("new_admin_password");
			String adminUserConfirm = httpRequest.getParameter("new_admin_password_confirm");
			if (!wizardModel.adminUserPassword.equals(adminUserConfirm)) {
				wizardModel.errors.add("Admin passwords don't match");
				renderTemplate("adminusersetup.vm", referenceMap, writer);
				return;
			}
			if (wizardModel.adminUserPassword.equals("")) {
				wizardModel.errors.add("An admin password is required");
				renderTemplate("adminusersetup.vm", referenceMap, writer);
				return;
			}
			try {
				OpenmrsUtil.validatePassword("admin", wizardModel.adminUserPassword, "admin");
			}
			catch (PasswordException p) {
				wizardModel.errors.add("The password is not long enough, does not contain both uppercase characters and a number, or matches the username.");
				renderTemplate("adminusersetup.vm", referenceMap, writer);
				return;
			}
			if (wizardModel.errors.isEmpty()) { 
				page = "implementationidsetup.vm";
			}
			renderTemplate(page, referenceMap, writer);
		} 
		else if ("implementationidsetup.vm".equals(page)) {
			if ("Back".equals(httpRequest.getParameter("back"))) {
				if (wizardModel.createTables)
					renderTemplate("adminusersetup.vm", referenceMap, writer);
				else
					renderTemplate("otherruntimeproperties.vm", referenceMap, writer);
				return;
			}
			wizardModel.implementationIdName = httpRequest.getParameter("implementation_name");
			wizardModel.implementationId = httpRequest.getParameter("implementation_id");
			wizardModel.implementationIdPassPhrase = httpRequest.getParameter("pass_phrase");
			wizardModel.implementationIdDescription = httpRequest.getParameter("description");
			if (wizardModel.implementationId.indexOf('^') != -1 || wizardModel.implementationId.indexOf('|') != -1) {
				wizardModel.errors.add("Implementation ID cannot contain '^' or '|'");
				renderTemplate("implementationidsetup.vm", referenceMap, writer);
				return;
			}
			if (wizardModel.errors.isEmpty()) { 
				page = "wizardcomplete.vm";
			}
			renderTemplate(page, referenceMap, writer);
		} else if ("wizardcomplete.vm".equals(page)) {
			if ("Back".equals(httpRequest.getParameter("back"))) {
				renderTemplate("implementationidsetup.vm", referenceMap, writer);
				return;
			}
			Properties runtimeProperties = new Properties();
			String connectionUsername;
			String connectionPassword;
			if (!wizardModel.hasCurrentOpenmrsDatabase) {
				String sql = "create database `?` default character set utf8";
				int result = executeStatement(false, wizardModel.createDatabaseUsername, wizardModel.createDatabasePassword,
				    sql, wizardModel.databaseName);
				if (result < 0) {
					renderTemplate(DEFAULT_PAGE, null, writer);
					return;
				} else {
					wizardModel.workLog.add("Created database " + wizardModel.databaseName);
				}
			}
			if (wizardModel.createDatabaseUser) {
				connectionUsername = wizardModel.databaseName + "_user";
				if (connectionUsername.length() > 16)
					connectionUsername = wizardModel.databaseName.substring(0, 11) + "_user"; 
				connectionPassword = "";
				String chars = "acdeghijklmnopqrtvwxyzACDEGHIJKLMNOPQRTVWXYZ0123456789.|~@#^&";
				Random r = new Random();
				for (int x = 0; x < 12; x++) {
					connectionPassword += chars.charAt(r.nextInt(chars.length()));
				}
				String sql = "drop user '?'@'localhost'";
				executeStatement(true, wizardModel.createUserUsername, wizardModel.createUserPassword, sql,
				    connectionUsername);
				sql = "create user '?'@'localhost' identified by '?'";
				if (-1 != executeStatement(false, wizardModel.createUserUsername, wizardModel.createUserPassword, sql,
				    connectionUsername, connectionPassword)) {
					wizardModel.workLog.add("Created user " + connectionUsername);
				} else {
					renderTemplate(DEFAULT_PAGE, null, writer);
					return;
				}
				sql = "GRANT ALL ON `?`.* TO '?'@'localhost'";
				int result = executeStatement(false, wizardModel.createUserUsername, wizardModel.createUserPassword, sql,
				    wizardModel.databaseName, connectionUsername);
				if (result < 0) {
					renderTemplate(DEFAULT_PAGE, null, writer);
					return;
				} else {
					wizardModel.workLog.add("Granted user " + connectionUsername + " all privileges to database "
					        + wizardModel.databaseName);
				}
			} else {
				connectionUsername = wizardModel.currentDatabaseUsername;
				connectionPassword = wizardModel.currentDatabasePassword;
			}
			String finalDatabaseConnectionString = wizardModel.databaseConnection.replace("@DBNAME@",
			    wizardModel.databaseName);
			if (!verifyConnection(connectionUsername, connectionPassword, finalDatabaseConnectionString)) {
				renderTemplate(DEFAULT_PAGE, null, writer);
				return;
			}
			runtimeProperties.put("connection.url", finalDatabaseConnectionString);
			runtimeProperties.put("connection.username", connectionUsername);
			runtimeProperties.put("connection.password", connectionPassword);
			runtimeProperties.put("module.allow_web_admin", wizardModel.moduleWebAdmin.toString());
			runtimeProperties.put("auto_update_database", wizardModel.autoUpdateDatabase.toString());
			runtimeProperties.put(SchedulerConstants.SCHEDULER_USERNAME_PROPERTY, "admin");
			runtimeProperties.put(SchedulerConstants.SCHEDULER_PASSWORD_PROPERTY, wizardModel.adminUserPassword);
			Context.setRuntimeProperties(runtimeProperties);
			if (wizardModel.createTables) {
				try {
					DatabaseUpdater.executeChangelog(LIQUIBASE_SCHEMA_DATA, null);
					DatabaseUpdater.executeChangelog(LIQUIBASE_CORE_DATA, null);
					wizardModel.workLog.add("Created database tables and added core data");
				}
				catch (Exception e) {
					wizardModel.errors.add(e.getMessage() + " See the error log for more details"); 
					log.warn("Error while trying to create tables and demo data", e);
				}
			}
			if (wizardModel.createTables && wizardModel.addDemoData) {
				try {
					DatabaseUpdater.executeChangelog(LIQUIBASE_DEMO_DATA, null);
					wizardModel.workLog.add("Added demo data");
				}
				catch (Exception e) {
					wizardModel.errors.add(e.getMessage() + " See the error log for more details"); 
					log.warn("Error while trying to add demo data", e);
				}
			}
			try {
				DatabaseUpdater.update();
			}
			catch (Exception e) {
				wizardModel.errors.add(e.getMessage() + " Error while trying to update to the latest database version"); 
				log.warn("Error while trying to update to the latest database version", e);
				renderTemplate(DEFAULT_PAGE, null, writer);
				return;
			}
			ContextLoader contextLoader = new ContextLoader();
			contextLoader.initWebApplicationContext(filterConfig.getServletContext());
			try {
				Context.startup(runtimeProperties);
			}
			catch (DatabaseUpdateException updateEx) {
				log.warn("Error while running the database update file", updateEx);
				wizardModel.errors.add(updateEx.getMessage()
				        + " There was an error while running the database update file: " + updateEx.getMessage()); 
				renderTemplate(DEFAULT_PAGE, null, writer);
				return;
			}
			catch (InputRequiredException inputRequiredEx) {
				log
				        .warn("Unable to continue because user input is required for the db updates, but I am not doing anything about that right now");
				wizardModel.errors
				        .add("Unable to continue because user input is required for the db updates, but I am not doing anything about that right now");
				renderTemplate(DEFAULT_PAGE, null, writer);
				return;
			}
			if (!wizardModel.implementationId.equals("")) {
				try {
					Context.addProxyPrivilege(OpenmrsConstants.PRIV_MANAGE_GLOBAL_PROPERTIES);
					Context.addProxyPrivilege(OpenmrsConstants.PRIV_MANAGE_CONCEPT_SOURCES);
					Context.addProxyPrivilege(OpenmrsConstants.PRIV_VIEW_CONCEPT_SOURCES);
					ImplementationId implId = new ImplementationId();
					implId.setName(wizardModel.implementationIdName);
					implId.setImplementationId(wizardModel.implementationId);
					implId.setPassphrase(wizardModel.implementationIdPassPhrase);
					implId.setDescription(wizardModel.implementationIdDescription);
					Context.getAdministrationService().setImplementationId(implId);
				}
				catch (Throwable t) {
					wizardModel.errors.add(t.getMessage() + " Implementation ID could not be set.");
					log.warn("Implementation ID could not be set.", t);
					renderTemplate(DEFAULT_PAGE, null, writer);
					Context.shutdown();
					WebModuleUtil.shutdownModules(filterConfig.getServletContext());
					contextLoader.closeWebApplicationContext(filterConfig.getServletContext());
					return;
				}
				finally {
					Context.removeProxyPrivilege(OpenmrsConstants.PRIV_MANAGE_GLOBAL_PROPERTIES);
					Context.removeProxyPrivilege(OpenmrsConstants.PRIV_MANAGE_CONCEPT_SOURCES);
					Context.removeProxyPrivilege(OpenmrsConstants.PRIV_VIEW_CONCEPT_SOURCES);
				}
			}
			try {
				if (wizardModel.createTables) {
					Context.authenticate("admin", "test");
					Context.getUserService().changePassword("test", wizardModel.adminUserPassword);
					Context.logout();
				}
				Listener.loadCoreModules(filterConfig.getServletContext());
				Listener.performWebStartOfModules(filterConfig.getServletContext());
				SchedulerUtil.startup(runtimeProperties);
			}
			catch (Throwable t) {
				Context.shutdown();
				WebModuleUtil.shutdownModules(filterConfig.getServletContext());
				contextLoader.closeWebApplicationContext(filterConfig.getServletContext());
				wizardModel.errors.add(t.getMessage() + " Unable to complete the startup.");
				log.warn("Unable to complete the startup.", t);
				renderTemplate(DEFAULT_PAGE, null, writer);
				return;
			}
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(getRuntimePropertiesFile());
				runtimeProperties.store(fos, "Auto generated by OpenMRS initialization wizard");
				wizardModel.workLog.add("Saved runtime properties file " + getRuntimePropertiesFile());
			}
			finally {
				if (fos != null) {
					fos.close();
				}
			}
			initializationComplete = true;
			httpResponse.sendRedirect("/" + WebConstants.WEBAPP_NAME);
		}
	}

	private void doPost(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
		String page = httpRequest.getParameter("page");
		Map<String, Object> referenceMap = new HashMap<String, Object>();
		Writer writer = httpResponse.getWriter();
		wizardModel.errors.clear();
		if ("databasesetup.vm".equals(page)) {
			wizardModel.databaseConnection = httpRequest.getParameter("database_connection");
			checkForEmptyValue(wizardModel.databaseConnection, wizardModel.errors, "Database connection string");
			if ("yes".equals(httpRequest.getParameter("current_openmrs_database"))) {
				wizardModel.databaseName = httpRequest.getParameter("openmrs_current_database_name");
				checkForEmptyValue(wizardModel.databaseName, wizardModel.errors, "Current database name");
				wizardModel.hasCurrentOpenmrsDatabase = true;
			} else {
				wizardModel.hasCurrentOpenmrsDatabase = false;
				wizardModel.createTables = true;
				wizardModel.databaseName = httpRequest.getParameter("openmrs_new_database_name");
				checkForEmptyValue(wizardModel.databaseName, wizardModel.errors, "New database name");
				wizardModel.createDatabaseUsername = httpRequest.getParameter("create_database_username");
				checkForEmptyValue(wizardModel.createDatabaseUsername, wizardModel.errors,
				    "A user that has 'CREATE DATABASE' privileges");
				wizardModel.createDatabasePassword = httpRequest.getParameter("create_database_password");
				checkForEmptyValue(wizardModel.createDatabasePassword, wizardModel.errors,
				    "Password for user with 'CREATE DATABASE' privileges");
			}
			if (wizardModel.errors.isEmpty()) {
				page = "databasetablesanduser.vm";
			}
			renderTemplate(page, referenceMap, writer);
		} 
		else if ("databasetablesanduser.vm".equals(page)) {
			if ("Back".equals(httpRequest.getParameter("back"))) {
				renderTemplate("databasesetup.vm", referenceMap, writer);
				return;
			}
			if (wizardModel.hasCurrentOpenmrsDatabase) {
				wizardModel.createTables = "yes".equals(httpRequest.getParameter("create_tables"));
			}
			wizardModel.addDemoData = "yes".equals(httpRequest.getParameter("add_demo_data"));
			if ("yes".equals(httpRequest.getParameter("current_database_user"))) {
				wizardModel.currentDatabaseUsername = httpRequest.getParameter("current_database_username");
				checkForEmptyValue(wizardModel.currentDatabaseUsername, wizardModel.errors, "Curent user account");
				wizardModel.currentDatabasePassword = httpRequest.getParameter("current_database_password");
				checkForEmptyValue(wizardModel.currentDatabasePassword, wizardModel.errors, "Current user account password");
				wizardModel.hasCurrentDatabaseUser = true;
				wizardModel.createDatabaseUser = false;
			} else {
				wizardModel.hasCurrentDatabaseUser = false;
				wizardModel.createDatabaseUser = true;
				wizardModel.createUserUsername = httpRequest.getParameter("create_user_username");
				checkForEmptyValue(wizardModel.createUserUsername, wizardModel.errors,
				    "A user that has 'CREATE USER' privileges");
				wizardModel.createUserPassword = httpRequest.getParameter("create_user_password");
				checkForEmptyValue(wizardModel.createUserPassword, wizardModel.errors,
				    "Password for user that has 'CREATE USER' privileges");
			}
			if (wizardModel.errors.isEmpty()) { 
				page = "otherruntimeproperties.vm";
			}
			renderTemplate(page, referenceMap, writer);
		} 
		else if ("otherruntimeproperties.vm".equals(page)) {
			if ("Back".equals(httpRequest.getParameter("back"))) {
				renderTemplate("databasetablesanduser.vm", referenceMap, writer);
				return;
			}
			wizardModel.moduleWebAdmin = "yes".equals(httpRequest.getParameter("module_web_admin"));
			wizardModel.autoUpdateDatabase = "yes".equals(httpRequest.getParameter("auto_update_database"));
			if (wizardModel.createTables) { 
				page = "adminusersetup.vm";
			} else { 
				page = "implementationidsetup.vm";
			}
			renderTemplate(page, referenceMap, writer);
		} 
		else if ("adminusersetup.vm".equals(page)) {
			if ("Back".equals(httpRequest.getParameter("back"))) {
				renderTemplate("otherruntimeproperties.vm", referenceMap, writer);
				return;
			}
			wizardModel.adminUserPassword = httpRequest.getParameter("new_admin_password");
			String adminUserConfirm = httpRequest.getParameter("new_admin_password_confirm");
			if (!wizardModel.adminUserPassword.equals(adminUserConfirm)) {
				wizardModel.errors.add("Admin passwords don't match");
				renderTemplate("adminusersetup.vm", referenceMap, writer);
				return;
			}
			if (wizardModel.adminUserPassword.equals("")) {
				wizardModel.errors.add("An admin password is required");
				renderTemplate("adminusersetup.vm", referenceMap, writer);
				return;
			}
			try {
				OpenmrsUtil.validatePassword("admin", wizardModel.adminUserPassword, "admin");
			}
			catch (PasswordException p) {
				wizardModel.errors.add("The password is not long enough, does not contain both uppercase characters and a number, or matches the username.");
				renderTemplate("adminusersetup.vm", referenceMap, writer);
				return;
			}
			if (wizardModel.errors.isEmpty()) { 
				page = "implementationidsetup.vm";
			}
			renderTemplate(page, referenceMap, writer);
		} 
		else if ("implementationidsetup.vm".equals(page)) {
			if ("Back".equals(httpRequest.getParameter("back"))) {
				if (wizardModel.createTables)
					renderTemplate("adminusersetup.vm", referenceMap, writer);
				else
					renderTemplate("otherruntimeproperties.vm", referenceMap, writer);
				return;
			}
			wizardModel.implementationIdName = httpRequest.getParameter("implementation_name");
			wizardModel.implementationId = httpRequest.getParameter("implementation_id");
			wizardModel.implementationIdPassPhrase = httpRequest.getParameter("pass_phrase");
			wizardModel.implementationIdDescription = httpRequest.getParameter("description");
			if (wizardModel.implementationId.indexOf('^') != -1 || wizardModel.implementationId.indexOf('|') != -1) {
				wizardModel.errors.add("Implementation ID cannot contain '^' or '|'");
				renderTemplate("implementationidsetup.vm", referenceMap, writer);
				return;
			}
			if (wizardModel.errors.isEmpty()) { 
				page = "wizardcomplete.vm";
			}
			renderTemplate(page, referenceMap, writer);
		} else if ("wizardcomplete.vm".equals(page)) {
			if ("Back".equals(httpRequest.getParameter("back"))) {
				renderTemplate("implementationidsetup.vm", referenceMap, writer);
				return;
			}
			Properties runtimeProperties = new Properties();
			String connectionUsername;
			String connectionPassword;
			if (!wizardModel.hasCurrentOpenmrsDatabase) {
				String sql = "create database `?` default character set utf8";
				int result = executeStatement(false, wizardModel.createDatabaseUsername, wizardModel.createDatabasePassword,
				    sql, wizardModel.databaseName);
				if (result < 0) {
					renderTemplate(DEFAULT_PAGE, null, writer);
					return;
				} else {
					wizardModel.workLog.add("Created database " + wizardModel.databaseName);
				}
			}
			if (wizardModel.createDatabaseUser) {
				connectionUsername = wizardModel.databaseName + "_user";
				if (connectionUsername.length() > 16)
					connectionUsername = wizardModel.databaseName.substring(0, 11) + "_user"; 
				connectionPassword = "";
				String chars = "acdeghijklmnopqrtvwxyzACDEGHIJKLMNOPQRTVWXYZ0123456789.|~@#^&";
				Random r = new Random();
				for (int x = 0; x < 12; x++) {
					connectionPassword += chars.charAt(r.nextInt(chars.length()));
				}
				String sql = "drop user '?'@'localhost'";
				executeStatement(true, wizardModel.createUserUsername, wizardModel.createUserPassword, sql,
				    connectionUsername);
				sql = "create user '?'@'localhost' identified by '?'";
				if (-1 != executeStatement(false, wizardModel.createUserUsername, wizardModel.createUserPassword, sql,
				    connectionUsername, connectionPassword)) {
					wizardModel.workLog.add("Created user " + connectionUsername);
				} else {
					renderTemplate(DEFAULT_PAGE, null, writer);
					return;
				}
				sql = "GRANT ALL ON `?`.* TO '?'@'localhost'";
				int result = executeStatement(false, wizardModel.createUserUsername, wizardModel.createUserPassword, sql,
				    wizardModel.databaseName, connectionUsername);
				if (result < 0) {
					renderTemplate(DEFAULT_PAGE, null, writer);
					return;
				} else {
					wizardModel.workLog.add("Granted user " + connectionUsername + " all privileges to database "
					        + wizardModel.databaseName);
				}
			} else {
				connectionUsername = wizardModel.currentDatabaseUsername;
				connectionPassword = wizardModel.currentDatabasePassword;
			}
			String finalDatabaseConnectionString = wizardModel.databaseConnection.replace("@DBNAME@",
			    wizardModel.databaseName);
			if (!verifyConnection(connectionUsername, connectionPassword, finalDatabaseConnectionString)) {
				renderTemplate(DEFAULT_PAGE, null, writer);
				return;
			}
			runtimeProperties.put("connection.url", finalDatabaseConnectionString);
			runtimeProperties.put("connection.username", connectionUsername);
			runtimeProperties.put("connection.password", connectionPassword);
			runtimeProperties.put("module.allow_web_admin", wizardModel.moduleWebAdmin.toString());
			runtimeProperties.put("auto_update_database", wizardModel.autoUpdateDatabase.toString());
			runtimeProperties.put(SchedulerConstants.SCHEDULER_USERNAME_PROPERTY, "admin");
			runtimeProperties.put(SchedulerConstants.SCHEDULER_PASSWORD_PROPERTY, wizardModel.adminUserPassword);
			Context.setRuntimeProperties(runtimeProperties);
			if (wizardModel.createTables) {
				try {
					DatabaseUpdater.executeChangelog(LIQUIBASE_SCHEMA_DATA, null);
					DatabaseUpdater.executeChangelog(LIQUIBASE_CORE_DATA, null);
					wizardModel.workLog.add("Created database tables and added core data");
				}
				catch (Exception e) {
					wizardModel.errors.add(e.getMessage() + " See the error log for more details"); 
					log.warn("Error while trying to create tables and demo data", e);
				}
			}
			if (wizardModel.createTables && wizardModel.addDemoData) {
				try {
					DatabaseUpdater.executeChangelog(LIQUIBASE_DEMO_DATA, null);
					wizardModel.workLog.add("Added demo data");
				}
				catch (Exception e) {
					wizardModel.errors.add(e.getMessage() + " See the error log for more details"); 
					log.warn("Error while trying to add demo data", e);
				}
			}
			try {
				DatabaseUpdater.update();
			}
			catch (Exception e) {
				wizardModel.errors.add(e.getMessage() + " Error while trying to update to the latest database version"); 
				log.warn("Error while trying to update to the latest database version", e);
				renderTemplate(DEFAULT_PAGE, null, writer);
				return;
			}
			ContextLoader contextLoader = new ContextLoader();
			contextLoader.initWebApplicationContext(filterConfig.getServletContext());
			try {
				Context.openSession();
				Context.startup(runtimeProperties);
			}
			catch (DatabaseUpdateException updateEx) {
				log.warn("Error while running the database update file", updateEx);
				wizardModel.errors.add(updateEx.getMessage()
				        + " There was an error while running the database update file: " + updateEx.getMessage()); 
				renderTemplate(DEFAULT_PAGE, null, writer);
				return;
			}
			catch (InputRequiredException inputRequiredEx) {
				log
				        .warn("Unable to continue because user input is required for the db updates, but I am not doing anything about that right now");
				wizardModel.errors
				        .add("Unable to continue because user input is required for the db updates, but I am not doing anything about that right now");
				renderTemplate(DEFAULT_PAGE, null, writer);
				return;
			}
			if (!wizardModel.implementationId.equals("")) {
				try {
					Context.addProxyPrivilege(OpenmrsConstants.PRIV_MANAGE_GLOBAL_PROPERTIES);
					Context.addProxyPrivilege(OpenmrsConstants.PRIV_MANAGE_CONCEPT_SOURCES);
					Context.addProxyPrivilege(OpenmrsConstants.PRIV_VIEW_CONCEPT_SOURCES);
					ImplementationId implId = new ImplementationId();
					implId.setName(wizardModel.implementationIdName);
					implId.setImplementationId(wizardModel.implementationId);
					implId.setPassphrase(wizardModel.implementationIdPassPhrase);
					implId.setDescription(wizardModel.implementationIdDescription);
					Context.getAdministrationService().setImplementationId(implId);
				}
				catch (Throwable t) {
					wizardModel.errors.add(t.getMessage() + " Implementation ID could not be set.");
					log.warn("Implementation ID could not be set.", t);
					renderTemplate(DEFAULT_PAGE, null, writer);
					Context.shutdown();
					WebModuleUtil.shutdownModules(filterConfig.getServletContext());
					contextLoader.closeWebApplicationContext(filterConfig.getServletContext());
					return;
				}
				finally {
					Context.removeProxyPrivilege(OpenmrsConstants.PRIV_MANAGE_GLOBAL_PROPERTIES);
					Context.removeProxyPrivilege(OpenmrsConstants.PRIV_MANAGE_CONCEPT_SOURCES);
					Context.removeProxyPrivilege(OpenmrsConstants.PRIV_VIEW_CONCEPT_SOURCES);
				}
			}
			try {
				if (wizardModel.createTables) {
					Context.authenticate("admin", "test");
					Context.getUserService().changePassword("test", wizardModel.adminUserPassword);
					Context.logout();
				}
				Listener.loadCoreModules(filterConfig.getServletContext());
				Listener.performWebStartOfModules(filterConfig.getServletContext());
				SchedulerUtil.startup(runtimeProperties);
			}
			catch (Throwable t) {
				Context.shutdown();
				WebModuleUtil.shutdownModules(filterConfig.getServletContext());
				contextLoader.closeWebApplicationContext(filterConfig.getServletContext());
				wizardModel.errors.add(t.getMessage() + " Unable to complete the startup.");
				log.warn("Unable to complete the startup.", t);
				renderTemplate(DEFAULT_PAGE, null, writer);
				return;
			}
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(getRuntimePropertiesFile());
				runtimeProperties.store(fos, "Auto generated by OpenMRS initialization wizard");
				wizardModel.workLog.add("Saved runtime properties file " + getRuntimePropertiesFile());
			}
			finally {
				if (fos != null) {
					fos.close();
				}
			}
			initializationComplete = true;
			Context.closeSession();
			httpResponse.sendRedirect("/" + WebConstants.WEBAPP_NAME);
		}
	}
