/*
 * Databinder: a simple bridge from Wicket to Hibernate
 * Copyright (C) 2006  Nathan Hamblen nathan@technically.us
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package net.databinder;

import java.net.URL;
import java.util.Locale;

import net.databinder.util.URLConverter;

import org.hibernate.cfg.AnnotationConfiguration;

import wicket.ISessionFactory;
import wicket.Session;
import wicket.protocol.http.WebApplication;
import wicket.util.convert.Converter;
import wicket.util.convert.IConverterFactory;

/**
 * Databinder Application subclass for request cycle hooks and a basic configuration.
 * @author Nathan Hamblen
 */
public abstract class DataApplication extends WebApplication {
	private boolean development;
	
	/**
	 * Configures this application for development or production, sets a home page,
	 * turns off default page versioning, and establishes a DataSession factory. 
	 * Override for customization.
	 */
	@Override
	protected void init() {
		String configuration = System.getProperty("net.databinder.configuration", DEVELOPMENT);
		development = configuration.equalsIgnoreCase(DEVELOPMENT);
		configure(configuration);
		// versioning doesn't do so much for database driven pages 
		getPageSettings().setVersionPagesByDefault(false);

		getApplicationSettings().setConverterFactory(new IConverterFactory() {
			/** Registers URLConverter in addition to the Wicket defaults. */
			public wicket.util.convert.IConverter newConverter(Locale locale) {
				Converter conv = new Converter(locale);
				conv.set(URL.class, new URLConverter());
				return conv;
			}
		});
		
		setSessionFactory(new ISessionFactory() {
			public Session newSession()
			{
				return newDataSession();
			}
    	});
	}
		
	/**
	 * Returns a new instance of a DataSession. Override if your application uses
	 * its own DataSession subclass. 
	 * @return new instance of DataSession
	 */
	protected DataSession newDataSession() {
		return new DataSession(DataApplication.this);
	}
	
	/**
	 * Reports if the program is running in a development environment, as determined by the
	 * "net.databinder.configuration" environment variable. If that variable is unset or set to 
	 * "development", the app is considered to be running in development.  
	 * @return true if running in a development environment
	 */
	protected boolean isDevelopment() {
		return development;
	}
	
	/**
	 * Override to add annotated classes for persistent storage by Hibernate, but don't forget
	 * to call this super-implementation. If running in a development environment,
	 * the session factory is set for hbm2ddl auto updating to create and add columns to tables 
	 * as required. Otherwise, it is configured for C3P0 connection pooling. (At present the two
	 * seem to be incompatible.) If you don't want this behaviour, don't call the 
	 * super-implementation.
	 * @param config used to build Hibernate session factory
	 */
	protected  void configureHibernate(AnnotationConfiguration config) {
    	if (isDevelopment())
    		config.setProperty("hibernate.hbm2ddl.auto", "update");
    	else {
    		config.setProperty("hibernate.c3p0.max_size", "20")
    		.setProperty("hibernate.c3p0.timeout","3000")
    		.setProperty("hibernate.c3p0.idle_test_period", "300");
    	}
	}
}
@@ -1390,16 +1390,73 @@
 ation %7B%0A
+%09/** true if in development mode, false if deployment */%0A
 %09private
@@ -1550,26 +1550,8 @@
 ion,
- sets a home page,
 %0A%09 *
@@ -1634,166 +1634,713 @@
 %09 * 
-Override for customization.%0A%09 */%0A%09@Override%0A%09protected void init() %7B%0A%09%09String configuration = System.getProperty(%22net.databinder.configuration%22, DEVELOPMENT);
+Development configuration is the default; set a JVM property of%0A%09 * wicket.configuration=deployment for production. (The context and init params%0A%09 * in wicket.protocol.http.WebApplication are not supported here). Override %0A%09 * this method for further customization.%0A%09 */%0A%09@Override%0A%09protected void init() %7B%0A%09%09// this databinder-specific parameter will eventually be dropped in favor%0A%09%09// of wicket.configuration%0A%09%09String configuration = System.getProperty(%22net.databinder.configuration%22);%0A%09%09if (configuration != null)%0A%09%09%09configure(configuration);%0A%09%09else%0A%09%09%09configuration = System.getProperty(%22wicket.%22 + CONFIGURATION, DEVELOPMENT);%0A%09%09// (if using wicket.configuration, calling configure() is unnecessary)
 %0A%09%09d
@@ -2401,36 +2401,8 @@
 T);%0A
-%09%09configure(configuration);%0A
 %09%09//

use wicket.configuration