public class YamlConfigParser {
    private Datasource datasource;
    private Global global;
    private JavaFile javaFile;
    private List<Table> tables;

    public YamlConfigParser(String userConfigPath) {
        Yaml yaml = new Yaml();
        try {
            HashMap config = (HashMap)yaml.load(new FileInputStream(userConfigPath));

            HashMap dsConfig = (HashMap)config.get("datasource");
            datasource = new Datasource();
            datasource.setDbType((String)dsConfig.get("dbtype"));
            datasource.setUrl((String)dsConfig.get("url"));
            datasource.setDriverClass((String)dsConfig.get("driverClass"));
            datasource.setUsername((String)dsConfig.get("username"));
            datasource.setPassword((String)dsConfig.get("password"));

            HashMap globalConfig = (HashMap)config.get("global");
            global = new Global();
            String rootPath = (String)globalConfig.get("rootPath");
            if (rootPath != null) {
                if (!rootPath.trim().endsWith("/")) {
                    rootPath = rootPath.trim() + "/";
                }
                global.setRootPath(rootPath);
            }


            HashMap javaFileConfig = (HashMap)config.get("java");
            javaFile = new JavaFile();
            javaFile.setModelPackage((String)((HashMap)javaFileConfig.get("model")).get("package_name"));
            javaFile.setModelTrimString(((HashMap)javaFileConfig.get("model")).get("setter_string_strim").equals(true));
            javaFile.setImplPackage((String)((HashMap)javaFileConfig.get("dao")).get("impl_package_name"));
            javaFile.setInterfacePackage((String)((HashMap)javaFileConfig.get("dao")).get("interface_package_name"));
            javaFile.setSqlmapPackage((String)((HashMap)javaFileConfig.get("sqlmap")).get("package_name"));

            tables = new ArrayList<Table>();
            List tablesConfig = (List)config.get("tables");
            for (Object tableConfigObj : tablesConfig) {
                Map tableConfig = (Map)tableConfigObj;
                Table table = new Table();
                table.setDefualtValue(tableConfig.get("supportDefault").equals(true));
                table.setGenIdKey(tableConfig.get("genIdKey").equals(true));
                table.setSchema((String)tableConfig.get("schema"));
                table.setSupportHis(tableConfig.get("historySupport").equals(true));
                table.setTableName((String)tableConfig.get("name"));
                tables.add(table);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Datasource getDatasource() {
        return datasource;
    }

    public Global getGlobal() {
        return global;
    }

    public JavaFile getJavaFile() {
        return javaFile;
    }

    public List<Table> getTables() {
        return tables;
    }
}
