package org.fiteagle.core.federationManager;

import com.hp.hpl.jena.rdf.model.Model;

import info.openmultinet.ontology.exceptions.InvalidModelException;

import org.fiteagle.api.core.IMessageBus;
import org.fiteagle.api.core.OntologyModelUtil;
import org.fiteagle.core.tripletStoreAccessor.TripletStoreAccessor;

import javax.ejb.Singleton;
import javax.ejb.Startup;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

@Startup
@Singleton
public class FederationManager {


    private static final Logger LOGGER = Logger.getLogger(FederationManager.class.getName());
    private Model federationModel;
    private static FederationManager manager;


    boolean initialized;

    private Timer timer;
    @javax.annotation.PostConstruct
    public void setup() {
    	manager = this;
        initialized = false;
        
        File federationOntologie = Paths.get(System.getProperty("user.home")).resolve(".fiteagle").resolve("Federation.ttl").toFile();
        if(!federationOntologie.exists()){
        	try {
				federationOntologie.createNewFile();
			} catch (IOException e) {
                LOGGER.log(Level.SEVERE,"Couldn't load Federation-Ontology. Tried to create new file '/home/User/.fiteagle/Federation.ttl' but it already existed");
			}
        	federationModel = OntologyModelUtil.loadModel(federationOntologie.toString(), IMessageBus.SERIALIZATION_TURTLE);
            LOGGER.log(Level.SEVERE,"Please add your Federation-Ontology to the '/home/User/.fiteagle/Federation.ttl' File and Re-Deploy the FederationManager ");
        }else {
        	federationModel = OntologyModelUtil.loadModel("ontologies/defaultFederation.ttl", IMessageBus.SERIALIZATION_TURTLE);
        }
        
        
        timer = new Timer();
        runSetup();
    }


    public void runSetup(){

        if(!initialized){
            try{
                SetupTask task = new SetupTask(500);
                timer.schedule(task, 0);
            } catch(IllegalStateException e){
                LOGGER.log(Level.INFO,"Attempt to schedule task on canceled timer, doesn't matter ");
            }
        }
    }

   private class SetupTask extends TimerTask {

       long delay;

       SetupTask(long delay){
           this.delay = delay;
       }
       @Override
       public void run() {

           if(!initialized && delay < 3600000 ) {
               try {
                  TripletStoreAccessor.addModel(federationModel);
                   initialized = true;
                   timer.cancel();
               } catch (TripletStoreAccessor.ResourceRepositoryException e) {
                   LOGGER.log(Level.INFO, e.getMessage());
                   delay = delay + delay;
                   timer.schedule(new SetupTask(delay), delay);

               } catch (InvalidModelException e) {
                   LOGGER.log(Level.INFO, e.getMessage());
               }
           }else{
               timer.cancel();
           }
       }


   }
   
   public static FederationManager getManager(){
	   return manager;
   }
}

@@ -1108,17 +1108,16 @@
      if(
-!
 federati
@@ -1139,16 +1139,590 @@
 sts())%7B%0A
+        %09if(federationModel != null)%7B%0A            %09federationModel = OntologyModelUtil.loadModel(federationOntologie.toString(), IMessageBus.SERIALIZATION_TURTLE);%0A        %09%09%0A            %09if(federationModel.isEmpty())%7B%0A                %09federationModel = OntologyModelUtil.loadModel(%22ontologies/defaultFederation.ttl%22, IMessageBus.SERIALIZATION_TURTLE);%0A                    LOGGER.log(Level.SEVERE,%22Please add your Federation-Ontology to the '/home/User/.fiteagle/Federation.ttl' File and Re-Deploy the FederationManager %22);%0A            %09%7D   %0A        %09%7D%0A    %0A        %7Delse %7B%0A
         
@@ -1945,24 +1945,13 @@
 but 
-it already exist
+Error
 ed%22)
@@ -2016,38 +2016,42 @@
 del(
-federationOntologie.toString()
+%22ontologies/defaultFederation.ttl%22
 , IM
@@ -2250,150 +2250,8 @@
 %22);%0A
-        %7Delse %7B%0A        %09federationModel = OntologyModelUtil.loadModel(%22ontologies/defaultFederation.ttl%22, IMessageBus.SERIALIZATION_TURTLE);%0A
     

correct if/else for old/new/created ontology