/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2014 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */
package org.geomajas.plugin.deskmanager.client.gwt.geodesk;

import org.geomajas.annotation.Api;
import org.geomajas.gwt.client.command.AbstractCommandCallback;
import org.geomajas.gwt.client.command.GwtCommand;
import org.geomajas.gwt.client.command.GwtCommandDispatcher;
import org.geomajas.gwt.client.command.TokenRequestHandler;
import org.geomajas.plugin.deskmanager.client.gwt.common.impl.DeskmanagerTokenRequestHandler;
import org.geomajas.plugin.deskmanager.command.geodesk.dto.InitializeGeodeskRequest;
import org.geomajas.plugin.deskmanager.command.geodesk.dto.InitializeGeodeskResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * Loads a geodesk from the server, this command will get all geodesk information from the server, and check access for
 * the current user. Typically this is the first command to be executed when starting a deskmanager application. The
 * load command provides a callback where you may trigger custom geodesk implementations, handled by
 * {@link GeodeskInitializationHandler}.
 * 
 * @author Oliver May
 * @since 1.0.0
 */
@Api (allMethods = true)
public class GeodeskInitializer {

	private List<GeodeskInitializationHandler> handlers = new ArrayList<GeodeskInitializationHandler>();
	private TokenRequestHandler fallbackHandler;

	/**
	 *
	 * @param fallbackHandler Handler that is called when no (external) default authorization is set and
	 *                        user tries to enter a non-public geodesk.
	 */
	public GeodeskInitializer(TokenRequestHandler fallbackHandler) {
		this.fallbackHandler = fallbackHandler;
	}

	/**
	 * Load a geodesk. This will execute the InitializationCommand, and ask to select a valid user if needed, using
	 * the default token request handler.
	 */
	public void loadApplication(String geodeskId) {
		loadApplication(geodeskId, new DeskmanagerTokenRequestHandler(geodeskId, fallbackHandler));
	}

	
	/**
	 * Load a geodesk. This will execute the InitializationCommand, and ask to select a valid user if needed.
	 * 
	 * @param geodeskId
	 *            the geodesk to load
	 * @param tokenRequestHandler
	 *            the login handler.
	 */
	public void loadApplication(String geodeskId, TokenRequestHandler tokenRequestHandler) {
		GwtCommandDispatcher.getInstance().setTokenRequestHandler(tokenRequestHandler);

		GwtCommand initializeGeodeskCommand = new GwtCommand(InitializeGeodeskResponse.COMMAND);
		final AbstractCommandCallback<InitializeGeodeskResponse> openLoketCallback = 
			new AbstractCommandCallback<InitializeGeodeskResponse>() {

			public void execute(InitializeGeodeskResponse response) {
				fireGeodeskInitialized(response);
			}
		};

		InitializeGeodeskRequest request = new InitializeGeodeskRequest();
		request.setGeodeskId(geodeskId);
		initializeGeodeskCommand.setCommandRequest(request);
		GwtCommandDispatcher.getInstance().execute(initializeGeodeskCommand, openLoketCallback);
	}

	/**
	 * Add a {@link GeodeskInitializationHandler}.
	 * 
	 * @param handler
	 *            the handler to add.
	 */
	public void addHandler(GeodeskInitializationHandler handler) {
		handlers.add(handler);
	}

	/**
	 * Remove a {@link GeodeskInitializationHandler}.
	 * 
	 * @param handler
	 *            the handler to remove.
	 */
	public void removeHandler(GeodeskInitializationHandler handler) {
		handlers.remove(handler);
	}

	private void fireGeodeskInitialized(InitializeGeodeskResponse response) {
		for (GeodeskInitializationHandler handler : handlers) {
			handler.initialized(response);
		}
	}

}

@@ -509,16 +509,131 @@
 on.Api;%0A
+import org.geomajas.command.dto.GetConfigurationRequest;%0Aimport org.geomajas.command.dto.GetConfigurationResponse;%0A
 import o
@@ -957,179 +957,8 @@
 ler;
-%0Aimport org.geomajas.plugin.deskmanager.command.geodesk.dto.InitializeGeodeskRequest;%0Aimport org.geomajas.plugin.deskmanager.command.geodesk.dto.InitializeGeodeskResponse;
 %0A%0Aim
@@ -2728,33 +2728,31 @@
 and(
-InitializeGeodeskResponse
+GetConfigurationRequest
 .COM
@@ -2790,33 +2790,32 @@
 allback%3C
-InitializeGeodesk
+GetConfiguration
 Response
@@ -2835,17 +2835,16 @@
 llback =
- 
 %0A%09%09%09new 
@@ -2867,33 +2867,32 @@
 allback%3C
-InitializeGeodesk
+GetConfiguration
 Response
@@ -2921,33 +2921,32 @@
 execute(
-InitializeGeodesk
+GetConfiguration
 Response
@@ -3009,33 +3009,32 @@
 %09%09%7D;%0A%0A%09%09
-InitializeGeodesk
+GetConfiguration
 Request 
@@ -3047,33 +3047,32 @@
 t = new 
-InitializeGeodesk
+GetConfiguration
 Request(
@@ -3087,23 +3087,27 @@
 uest.set
-Geodesk
+Application
 Id(geode
@@ -3736,25 +3736,24 @@
 zed(
-InitializeGeodesk
+GetConfiguration
 Resp

remove initializegeodeskcommand