package org.nybatis.core.db.configuration.builder;

import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.nybatis.core.conf.Const;
import org.nybatis.core.log.NLogger;
import org.nybatis.core.file.FileUtil;

/**
 * Database Configuration Builder
 *
 * @author nayasis@gmail.com
 *
 */
public class DatabaseConfigurator {

	/**
	 * Build Database Configration from given path<br><br>
	 *
	 * First it builds from given path itself.<br>
	 * but if given path dose not exist, it will build from basepath({@link Const.path.getConfigDatabase}) + given path.
	 *
	 * @param filePath given configuration path
	 */
	public static void build( String filePath ) {

		filePath = Const.profile.getFileName( filePath );

		if( FileUtil.isExist( filePath ) ) {
			new ConfigurationBuilder( filePath );

		} else {

			try {

				String modifiedPath = Paths.get( Const.path.getConfigDatabase(), filePath ).toString();

				if( FileUtil.isExist(modifiedPath) ) {
					new ConfigurationBuilder( modifiedPath );
					return;
				}

				NLogger.error( "Database configuration file is not exist.\n\t - in [{}]\n\t - in [{}]", filePath, modifiedPath );

			} catch( InvalidPathException e ) {
				NLogger.error( e, "Database configuration file is not exist.\n\t - in [{}]\n\t - in [{}]", filePath );

			}

		}

	}

	/**
	 * Build All Database Configration from files in dafault path<br><br>
	 *
	 * default path is DatabaseConfigurationPath({@link Const.path.getConfigDatabase})
	 *
	 */
	public static void build() {

		List<Path> confLists = FileUtil.getList( Const.path.getConfigDatabase(), true, false, 0, "*.xml" );

		for( Path confPath : confLists ) {
			build( confPath.toString() );
		}

	}

}

@@ -203,16 +203,62 @@
 .Const;%0A
+import org.nybatis.core.context.NThreadLocal;%0A
 import o
@@ -1405,24 +1405,88 @@
 %0A%09%09%09%7D%0A%0A%09%09%7D%0A%0A
+%09%09// Delete temporary thread local key%0A%09%09NThreadLocal.clear();%0A%0A
 %09%7D%0A%0A%09/**%0A%09 *

Delete temporary thread local key when building sql configuration.