package nl.idgis.publisher.service;

import scala.concurrent.Future;
import nl.idgis.publisher.database.GeometryDatabase;
import nl.idgis.publisher.database.PublisherDatabase;
import nl.idgis.publisher.database.messages.GetVersion;
import nl.idgis.publisher.database.messages.Version;
import nl.idgis.publisher.harvester.Harvester;
import nl.idgis.publisher.monitor.messages.Tree;
import nl.idgis.publisher.service.admin.Admin;
import nl.idgis.publisher.service.init.Initiator;
import nl.idgis.publisher.service.loader.Loader;
import nl.idgis.publisher.utils.Boot;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.dispatch.OnSuccess;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.pattern.Patterns;

import com.typesafe.config.Config;

public class App extends UntypedActor {
	
	private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
	
	private final Config config;
	
	private ActorRef geometryDatabase, database, harvester, loader;
	
	public App(Config config) {
		this.config = config;
	}
	
	public static Props props(Config config) {
		return Props.create(App.class, config);
	}
	
	@Override
	public void preStart() throws Exception {
		Config geometryDatabaseConfig = config.getConfig("geometry-database");
		geometryDatabase = getContext().actorOf(GeometryDatabase.props(geometryDatabaseConfig), "geometryDatabase");
		
		Config databaseConfig = config.getConfig("database");
		database = getContext().actorOf(PublisherDatabase.props(databaseConfig), "database");
		
		Future<Object> versionFuture = Patterns.ask(database, new GetVersion(), 15000);
		versionFuture.onSuccess(new OnSuccess<Object>() {

			@Override
			public void onSuccess(Object msg) throws Throwable {
				Version version = (Version)msg;
				log.debug("database version: " + version);
				
				Config harvesterConfig = config.getConfig("harvester");
				harvester = getContext().actorOf(Harvester.props(database, harvesterConfig), "harvester");
				
				loader = getContext().actorOf(Loader.props(geometryDatabase, database, harvester), "loader");
				
				getContext().actorOf(Admin.props(database, harvester), "admin");
				
				getContext().actorOf(Initiator.props(database, harvester, loader), "init");
			}
			
		}, getContext().dispatcher());
	}
	
	@Override
	public void onReceive(Object msg) throws Exception {
		if(msg instanceof Tree) {
			log.debug(msg.toString());
		} else {
			unhandled(msg);
		}
	}

	public static void main(String[] args) {
		Boot boot = Boot.init("service");
		boot.startApplication(App.props(boot.getConfig()));
	}
}

@@ -43,31 +43,116 @@
 ort 
-scala.concurrent.Future
+java.util.concurrent.TimeUnit;%0D%0A%0D%0Aimport scala.concurrent.Future;%0D%0Aimport scala.concurrent.duration.Duration
 ;%0D%0Ai
@@ -613,24 +613,83 @@
 er.Loader;%0D%0A
+import nl.idgis.publisher.service.messages.GetActiveJobs;%0D%0A
 import nl.id
@@ -744,16 +744,48 @@
 orRef;%0D%0A
+import akka.actor.ActorSystem;%0D%0A
 import a
@@ -2565,135 +2565,723 @@
 ;%0D%0A%09
-%7D%0D%0A%09%0D%0A%09@Override%0D%0A%09public void onReceive(Object msg) throws Exception %7B%0D%0A%09%09if(msg instanceof Tree) %7B%0D%0A%09%09%09log.debug(msg.toString
+%09%0D%0A%09%09if(log.isDebugEnabled()) %7B%0D%0A%09%09%09ActorSystem system = getContext().system();%0D%0A%09%09%09system.scheduler().schedule(%0D%0A%09%09%09%09%09Duration.Zero(), %0D%0A%09%09%09%09%09Duration.create(5, TimeUnit.SECONDS), %0D%0A%09%09%09%09%09getSelf(), %0D%0A%09%09%09%09%09new GetActiveJobs(), %0D%0A%09%09%09%09%09getContext().dispatcher(), %0D%0A%09%09%09%09%09getSelf());%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A%09@Override%0D%0A%09public void onReceive(Object msg) throws Exception %7B%0D%0A%09%09if(msg instanceof Tree) %7B%0D%0A%09%09%09log.debug(msg.toString());%0D%0A%09%09%7D else if(msg instanceof GetActiveJobs) %7B%0D%0A%09%09%09Patterns.ask(loader, msg, 15000)%0D%0A%09%09%09%09.onSuccess(new OnSuccess%3CObject%3E() %7B%0D%0A%0D%0A%09%09%09%09%09@Override%0D%0A%09%09%09%09%09public void onSuccess(Object msg) throws Throwable %7B%0D%0A%09%09%09%09%09%09log.debug(%22active loader tasks: %22 + msg);%0D%0A%09%09%09%09%09%7D%0D%0A%09%09%09%09%09%0D%0A%09%09%09%09%7D, getContext().dispatcher
 ());

active loader tasks debug output added