/**
 * PODD is an OWL ontology database used for scientific project management
 * 
 * Copyright (C) 2009-2013 The University Of Queensland
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU Affero General Public License as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with this program.
 * If not, see <http://www.gnu.org/licenses/>.
 */
package com.github.podd.impl;

import org.openrdf.OpenRDFException;
import org.openrdf.model.URI;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.sail.memory.MemoryStore;

import com.github.podd.api.PoddRepositoryManager;
import com.github.podd.utils.PoddRdfConstants;

/**
 * @author Peter Ansell p_ansell@yahoo.com
 * 
 */
public class PoddRepositoryManagerImpl implements PoddRepositoryManager
{
    
    private Repository repository;
    
    private URI artifactGraph = PoddRdfConstants.DEFAULT_ARTIFACT_MANAGEMENT_GRAPH;
    
    private URI schemaGraph = PoddRdfConstants.DEFAULT_SCHEMA_MANAGEMENT_GRAPH;
    
    private URI dataRepositoryGraph = PoddRdfConstants.DEFAULT_FILE_REPOSITORY_MANAGEMENT_GRAPH;
    
    /**
     * Default constructor, which sets up an in-memory MemoryStore repository.
     */
    public PoddRepositoryManagerImpl()
    {
        this.repository = new SailRepository(new MemoryStore());
        try
        {
            this.repository.initialize();
        }
        catch(final RepositoryException e)
        {
            try
            {
                this.repository.shutDown();
            }
            catch(final RepositoryException e1)
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
            throw new RuntimeException("Could not initialise PoddRepositoryManager with an in-memory repository", e);
        }
    }
    
    /**
     * 
     * @param repository
     *            An initialized implementation of Repository.
     */
    public PoddRepositoryManagerImpl(final Repository repository)
    {
        this.repository = repository;
    }
    
    @Override
    public URI getArtifactManagementGraph()
    {
        return this.artifactGraph;
    }
    
    @Override
    public URI getFileRepositoryManagementGraph()
    {
        return this.dataRepositoryGraph;
    }
    
    @Override
    public Repository getNewTemporaryRepository() throws OpenRDFException
    {
        final Repository result = new SailRepository(new MemoryStore());
        result.initialize();
        
        return result;
    }
    
    @Override
    public Repository getRepository() throws OpenRDFException
    {
        return this.repository;
    }
    
    @Override
    public URI getSchemaManagementGraph()
    {
        return this.schemaGraph;
    }
    
    @Override
    public void setArtifactManagementGraph(final URI artifactManagementGraph)
    {
        this.artifactGraph = artifactManagementGraph;
    }
    
    @Override
    public void setFileRepositoryManagementGraph(final URI dataRepositoryManagementGraph)
    {
        this.dataRepositoryGraph = dataRepositoryManagementGraph;
    }
    
    @Override
    public void setRepository(final Repository repository) throws OpenRDFException
    {
        this.repository = repository;
    }
    
    @Override
    public void setSchemaManagementGraph(final URI schemaManagementGraph)
    {
        this.schemaGraph = schemaManagementGraph;
    }
    
    @Override
    public void shutDown() throws RepositoryException
    {
        if(this.repository != null)
        {
            this.repository.shutDown();
        }
    }
    
}

@@ -832,16 +832,46 @@
 .impl;%0A%0A
+import java.util.Collection;%0A%0A
 import o
@@ -1116,16 +1116,67 @@
 ryStore;
+%0Aimport org.semanticweb.owlapi.model.OWLOntologyID;
 %0A%0Aimport
@@ -2962,31 +2962,208 @@
  get
-NewTemporaryRepository(
+ManagementRepository() throws OpenRDFException%0A    %7B%0A        return this.repository;%0A    %7D%0A    %0A    @Override%0A    public Repository getNewTemporaryRepository(final Collection%3COWLOntologyID%3E ontologies
 ) th
@@ -3374,16 +3374,25 @@
 tory get
+Permanent
 Reposito
@@ -3386,32 +3386,74 @@
 anentRepository(
+final Collection%3COWLOntologyID%3E ontologies
 ) throws OpenRDF
@@ -4000,16 +4000,26 @@
 void set
+Management
 Reposito

Implement new methods in PoddRepositoryManagerImpl