package com.vcarecity.cvs; import com.google.inject.Guice; import com.google.inject.Injector; import com.vcarecity.cvs.core.ReflectionUtil; import com.vcarecity.cvs.module.PropertiesModule; import com.vcarecity.cvs.module.SQLModule; import com.vcarecity.cvs.properties.AppProperties; import com.vcarecity.cvs.properties.DbProperties; import com.vcarecity.cvs.starter.SQLStarter; import com.vcarecity.cvs.type.ResultHandlerEnum; import me.yuxiaoyao.config.loader.ConfigLoader; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * @author Kerry on 19/11/27 */ public class FileExporterApp { public static Injector injector; public static Map TABLE_COLUMN = new ConcurrentHashMap<>(); public static void main(String[] args) throws Exception { ReflectionUtil.initTableClassMapper("com.vcarecity.cvs.entity"); AppProperties appProperties = ConfigLoader.parseConfig(args, AppProperties.class, null); jdbcArgs(args, appProperties); String table = getTableByArgs(args); if (table == null || table.length() == 0) { throw new Error("table name must not be null or empty!"); } Class tableClass = ReflectionUtil.getTableClass(table); boolean isMysql; Object properties; final ResultHandlerEnum type = isImportToMysql(args); if (type == ResultHandlerEnum.MYSQL || type == ResultHandlerEnum.MYSQL_PS) { properties = "mysql"; } else { String basePath = table; final AppProperties.Export export = appProperties.getExport(); if (export != null) { basePath = export.getFolder() + table; } properties = basePath; } injector = Guice.createInjector(new PropertiesModule(appProperties), new SQLModule(type)); final SQLStarter instance = injector.getInstance(SQLStarter.class); instance.exportDataAndConvert(table, properties, tableClass); } private static ResultHandlerEnum isImportToMysql(String[] args) { String type = null; for (String arg : args) { if (arg.startsWith("--type=")) { type = arg.substring("--type=".length()).trim(); break; } } return ResultHandlerEnum.fromType(type); } private static String getTableByArgs(String[] args) { for (String arg : args) { if (arg.startsWith("--table=")) { return arg.substring("--table=".length()).trim(); } else if (arg.startsWith("-t=")) { return arg.substring("-t=".length()).trim(); } } return null; } private static void jdbcArgs(String[] args, AppProperties properties) { final DbProperties database = properties.getFromDb(); for (String arg : args) { if (arg.startsWith("jdbc.url=")) { database.setUrl(arg.substring("jdbc.url=".length()).trim()); } else if (arg.startsWith("jdbc.user=")) { database.setUser(arg.substring("jdbc.user=".length()).trim()); } else if (arg.startsWith("jdbc.password=")) { database.setPassword(arg.substring("jdbc.password=".length()).trim()); } } properties.setFromDb(database); } }