/*
 * Copyright 2018 Humboldt-Universität zu Berlin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package annis.dao;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteDataSource;

public class DBProvider {
    
    public enum DB {
        CORPUS_REGISTRY("corpus_registry.sqlite3"),
        SERVICE_DATA("service_data.sqlite3");
        
        final String filename;
        
        DB(String filename) {
            this.filename = filename;
        }
    }

    private final QueryRunner queryRunner = new QueryRunner();

    public Connection createConnection(DB db) throws SQLException {
        return createConnection(db, false);
    }

    public File getDBFile(DB db) {
        return new File(getANNISDir(), db.filename);
    }

    public Connection createConnection(DB db, boolean readonly) throws SQLException {
        // TODO: use a connection pool
        // TODO: split into two databases, one "corpus_registry" and a
        // "service_data" file
        File dbFile = getDBFile(db);
        return createConnection(dbFile, readonly);
    }

    public Connection createConnection(File dbFile, boolean readonly) throws SQLException {
        SQLiteConfig conf = new SQLiteConfig();
        conf.setReadOnly(readonly);
        SQLiteDataSource source = new SQLiteDataSource(conf);
        source.setUrl("jdbc:sqlite:" + dbFile.getAbsolutePath() + "?journal_mode=wal");
        return source.getConnection();
    }

    public File getANNISDir() {
        // TODO: make the annis folder location configurable
        // (like the external data dir)
        return new File(System.getProperty("user.home"), ".annis");
    }

    public File getGraphANNISDir() {
        return new File(getANNISDir(), "graphannis");
    }

    public QueryRunner getQueryRunner() {
        return queryRunner;
    }
}

@@ -1346,32 +1346,37 @@
 urn new File(get
+Graph
 ANNISDir(), db.f
@@ -2375,18 +2375,10 @@
 ), %22
-graphannis
+v4
 %22);%0A

Move sqlite database to the graphANNIS folder and use the version number as name.