package fr.jeci.alfresco.saod.sql;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.init.ScriptUtils;

import fr.jeci.alfresco.saod.SaodException;

/**
 * Chargement des fichiers SQL
 * 
 * @author jlesage
 */
public class SqlQueries {
	static final Logger LOG = LoggerFactory.getLogger(SqlQueries.class);

	public final static String COMMENT_PREFIX = "--";
	public final static String SEPARATOR = ";";

	private HashMap<String, String> cacheQeries;

	private String sqlBasePath = "sql/hsqldb";

	public SqlQueries() {
		this.cacheQeries = new HashMap<>();
	}

	public void setSqlBasePath(String sqlBasePath) {
		this.sqlBasePath = sqlBasePath;
	}

	public String getQuery(String id) throws SaodException {
		String query = this.cacheQeries.get(id);

		if (query == null) {
			query = loadQuery(id);
		}

		return query;
	}

	private String loadQuery(final String id) throws SaodException {
		String ressourcePath = this.sqlBasePath + "/" + id;
		LOG.info("Loading sql file : {}", ressourcePath);
		try {
			InputStream resourceAsStream = ClassLoader.getSystemResourceAsStream(ressourcePath);
			if (resourceAsStream == null) {
				File file = new File(ressourcePath);
				if (!file.exists()) {
					throw new SaodException("Fichier sql '" + ressourcePath + "' introuvable");
				}
				resourceAsStream = new FileInputStream(file);
			}

			try (InputStreamReader reader = new InputStreamReader(resourceAsStream)) {
				loadSQL(id, reader);
				return this.cacheQeries.get(id);
			}
		} catch (IOException e) {
			throw new SaodException("Erreur lors du chargement du fichier sql : " + ressourcePath, e);
		}
	}

	private void loadSQL(String id, InputStreamReader reader) throws IOException {
		LineNumberReader fileReader = new LineNumberReader(new BufferedReader(reader));
		String query = ScriptUtils.readScript(fileReader, COMMENT_PREFIX, SEPARATOR);
		cacheQeries.put(id, query);
	}

}

@@ -320,16 +320,141 @@
 actory;%0A
+import org.springframework.core.io.Resource;%0Aimport org.springframework.core.io.support.PathMatchingResourcePatternResolver;%0A
 import o
@@ -506,16 +506,16 @@
 tUtils;%0A
-
 %0Aimport 
@@ -853,16 +853,71 @@
 eQeries;
+%0A%09private PathMatchingResourcePatternResolver resolver;
 %0A%0A%09priva
@@ -1016,16 +1016,77 @@
 ap%3C%3E();%0A
+%09%09this.resolver = new PathMatchingResourcePatternResolver();%0A
 %09%7D%0A%0A%09pub
@@ -1532,61 +1532,102 @@
 %0A%09%09%09
-InputStream
+Resource%5B%5D
  res
+s
 ource
-AsStream = ClassLoader.getSystemR
+s = resolver.getResources(%22classpath*:%22 + ressourcePath);%0A%09%09%09InputStream r
 esou
@@ -1637,17 +1637,19 @@
 AsStream
-(
+ = 
 ressourc
@@ -1645,28 +1645,44 @@
  = ressource
-Path
+s%5B0%5D.getInputStream(
 );%0A%09%09%09if (re

use PathMatchingResourcePatternResolver