/***
 * Excerpted from "Domain-Driven Design Using Naked Objects",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/dhnako for more book information.
***/
package com.pragprog.dhnako.carserv.wicket.ui;

import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.nakedobjects.metamodel.services.ServicesInjector;
import org.nakedobjects.runtime.context.NakedObjectsContext;
import org.nakedobjects.runtime.persistence.PersistenceSession;

import com.pragprog.dhnako.carserv.wicket.app.NakedObjectsAuthenticatedWebSession;

public abstract class NakedObjectsPanel extends Panel {

	private static final long serialVersionUID = 1L;

	public NakedObjectsPanel(String id) {
		super(id, null);
		init();
	}

	public NakedObjectsPanel(String id, IModel model) {
		super(id, model);
		init();
	}

	private void init() {
		injectServices();
	}

	@Override
	protected void onBeforeRender() {
		init();
		super.onBeforeRender();
	}
	

	protected void injectServices() {
		getServicesInjector().injectDependencies(this);
	}


	/////////////////////////////////////////////////////////////////////
	// Convenience
	/////////////////////////////////////////////////////////////////////

	protected NakedObjectsAuthenticatedWebSession getNakedObjectsAuthenticatedWebSession() {
		return (NakedObjectsAuthenticatedWebSession) getSession();
	}

	/////////////////////////////////////////////////////////////////////
	// Dependencies (from NakedObjectsContext)
	/////////////////////////////////////////////////////////////////////
	
	private static ServicesInjector getServicesInjector() {
		return getPersistenceSession().getServicesInjector();
	}

	protected static PersistenceSession getPersistenceSession() {
		return NakedObjectsContext.getPersistenceSession();
	}
	
}
