// Start of user code Copyright
/*******************************************************************************
 * Copyright (c) 2011, 2012 IBM Corporation and others.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 *  
 *  The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *  and the Eclipse Distribution License is available at
 *  http://www.eclipse.org/org/documents/edl-v10.php.
 *  
 *  Contributors:
 *  
 *	   Sam Padgett	       - initial API and implementation
 *     Michael Fiedler     - adapted for OSLC4J
 *     Jad El-khoury        - initial implementation of code generator (https://bugs.eclipse.org/bugs/show_bug.cgi?id=422448)
 *     Matthieu Helleboid   - Support for multiple Service Providers.
 *     Anass Radouani       - Support for multiple Service Providers.
 *
 * This file is generated by org.eclipse.lyo.oslc4j.codegenerator
 *******************************************************************************/
// End of user code

package se.ericsson.cf.scott.sandbox.twin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletContextEvent;
import java.util.List;

import org.eclipse.lyo.oslc4j.core.model.ServiceProvider;
import org.eclipse.lyo.oslc4j.core.model.AbstractResource;
import se.ericsson.cf.scott.sandbox.twin.servlet.ServiceProviderCatalogSingleton;
import se.ericsson.cf.scott.sandbox.twin.ServiceProviderInfo;
import eu.scott.warehouse.domains.pddl.Action;
import eu.scott.warehouse.domains.pddl.Plan;
import eu.scott.warehouse.domains.pddl.PlanExecutionResult;
import eu.scott.warehouse.domains.pddl.Step;


// Start of user code imports
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.jena.sparql.ARQException;
import java.io.IOException;
import javax.servlet.ServletContext;
import org.eclipse.lyo.store.Store;
import org.eclipse.lyo.store.StoreFactory;
// End of user code

// Start of user code pre_class_code
// End of user code

public class RobotTwinManager {

    // Start of user code class_attributes
    private final static String PACKAGE_ROOT = RobotTwinManager.class.getPackage().getName();
    private final static Logger log = LoggerFactory.getLogger(RobotTwinManager.class);
    private static Store store;
    private static ServletContext context;
    // End of user code
    
    
    // Start of user code class_methods
    private static String parameterFQDN(final String s) {
        return PACKAGE_ROOT + "." + s;
    }

    private static String p(final String s) {
        return context.getInitParameter(parameterFQDN(s));
    }
    // End of user code

    public static void contextInitializeServletListener(final ServletContextEvent servletContextEvent)
    {
        
        // Start of user code contextInitializeServletListener
        context = servletContextEvent.getServletContext();
        try {
            store = StoreFactory.sparql(p("store.query"), p("store.update"));
            // TODO Andrew@2017-07-18: Remember to deactivate when switch to more persistent arch
            store.removeAll();
        } catch (IOException |ARQException e) {
            log.error("SPARQL Store failed to initialise with the URIs query={};update={}",
                    p("store.query"), p("store.update"), e);
        }
        // End of user code
    }

    public static void contextDestroyServletListener(ServletContextEvent servletContextEvent) 
    {
        
        // Start of user code contextDestroyed
        // TODO Implement code to shutdown connections to data backbone etc...
        // End of user code
    }

    public static ServiceProviderInfo[] getServiceProviderInfos(HttpServletRequest httpServletRequest)
    {
        ServiceProviderInfo[] serviceProviderInfos = {};
        
        // Start of user code "ServiceProviderInfo[] getServiceProviderInfos(...)"
            final ServiceProviderInfo serviceProviderInfo = new ServiceProviderInfo();
            serviceProviderInfo.serviceProviderId = "default";
            serviceProviderInfo.name = "Default Service Provider";
            serviceProviderInfos = new ServiceProviderInfo[]{serviceProviderInfo};
        // End of user code
        return serviceProviderInfos;
    }



    public static PlanExecutionResult getPlanExecutionResult(HttpServletRequest httpServletRequest, final String serviceProviderId, final String planExecutionResultId)
    {
        PlanExecutionResult aResource = null;
        
        // Start of user code getPlanExecutionResult
        // TODO Implement code to return a resource
        // End of user code
        return aResource;
    }




    public static String getETagFromPlanExecutionResult(final PlanExecutionResult aResource)
    {
        String eTag = null;
        // Start of user code getETagFromPlanExecutionResult
        // TODO Implement code to return an ETag for a particular resource
        // End of user code
        return eTag;
    }

}

@@ -3031,16 +3031,82 @@
         
+// FIXME Andrew@2018-04-06: debug the connection params%0A        /*
 try %7B%0A  
@@ -3516,24 +3516,26 @@
 );%0A        %7D
+*/
 %0A        // 

Stop clearing the triplestore in the twin