/*
 * Copyright (c) 2009, University of Bristol
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1) Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2) Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3) Neither the name of the University of Bristol nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */
package org.ilrt.mca.servlet;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
import org.apache.log4j.Logger;
import org.ilrt.mca.harvester.Harvester;
import org.ilrt.mca.harvester.geo.OpenStreetMapHarvesterImpl;
import org.ilrt.mca.rdf.ConnPoolStoreWrapperManagerImpl;
import org.ilrt.mca.rdf.DataManager;
import org.ilrt.mca.rdf.DataSourceManager;
import org.ilrt.mca.rdf.SdbManagerImpl;
import org.ilrt.mca.rdf.StoreWrapper;
import org.ilrt.mca.rdf.StoreWrapperManager;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author Mike Jones (mike.a.jones@bristol.ac.uk)
 */
public class RegistryInitServlet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {

        log.info("RegistryInitServlet started.");

        super.init(config);

        // find the configuration files
        String configLocation = config.getInitParameter("configLocation");
        String registryLocation = config.getInitParameter("registryLocation");

        // create the dataManager
        DataSourceManager dataSourceManager = new DataSourceManager();
        StoreWrapperManager manager =
                new ConnPoolStoreWrapperManagerImpl(configLocation, dataSourceManager.getDataSource());

        // log the database being used
        StoreWrapper wrapper = manager.getStoreWrapper();
        log.info("Database used: " + wrapper.getStore().getDatabaseType().getName());
        wrapper.close();

        DataManager repository = new SdbManagerImpl(manager);

        // clear existing registry
        log.info("Clearing existing registry details");
        repository.deleteAllInGraph(null);

        // load configuration files
        Model model = ModelFactory.createDefaultModel();

        // load the registry and add it to the database
        log.info("Loading registry details");

        String[] locations = registryLocation.split(",");

        for (String location : locations) {
            log.info("Adding ... " + location);
            model.add(FileManager.get().loadModel(location));
        }

        repository.add(model);
        log.info("Added " + model.size() + " triples.");

/*
        try {
            log.info("Harvesting OSM geo data");
            Harvester osm = new OpenStreetMapHarvesterImpl(repository);
            osm.harvest();
        } catch (IOException e) {
            e.printStackTrace();
        }
*/
        log.info("Registry servlet finished loading data");
//        // TODO - replace (just used for dev)
//        Model geoData = ModelFactory.createDefaultModel();
//        geoData.read(getClass().getResourceAsStream("/data/test-geodata.xml"), null);
//        repository.deleteAllInGraph("mca://testgeo");
//        repository.add("mca://testgeo", geoData);

    }

    @Override
    public void destroy() {
        log.info("RegistryInitServlet shutdown.");
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }

    private final Logger log = Logger.getLogger(RegistryInitServlet.class);
}

@@ -4024,19 +4024,19 @@
 es.%22);%0A%0A
-/*%0A
+%0A//
         
@@ -4041,16 +4041,18 @@
   try %7B%0A
+//
         
@@ -4088,24 +4088,26 @@
 geo data%22);%0A
+//
             
@@ -4166,16 +4166,18 @@
 itory);%0A
+//
         
@@ -4191,24 +4191,26 @@
 .harvest();%0A
+//
         %7D ca
@@ -4227,24 +4227,26 @@
 eption e) %7B%0A
+//
             
@@ -4262,24 +4262,26 @@
 ackTrace();%0A
+//
         %7D%0A*/
@@ -4278,18 +4278,16 @@
       %7D%0A
-*/
 %0A       
@@ -4387,18 +4387,16 @@
 or dev)%0A
-//
         
@@ -4442,26 +4442,24 @@
 ultModel();%0A
-//
         geoD
@@ -4532,18 +4532,16 @@
  null);%0A
-//
         
@@ -4582,26 +4582,24 @@
 /testgeo%22);%0A
-//
         repo

load local geodata since the OSM server can be unresponsive