FileExporterApp.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.vcarecity.cvs;
  2. import com.google.inject.Guice;
  3. import com.google.inject.Injector;
  4. import com.vcarecity.cvs.core.ReflectionUtil;
  5. import com.vcarecity.cvs.module.PropertiesModule;
  6. import com.vcarecity.cvs.module.SQLModule;
  7. import com.vcarecity.cvs.properties.AppProperties;
  8. import com.vcarecity.cvs.properties.DbProperties;
  9. import com.vcarecity.cvs.starter.SQLStarter;
  10. import me.yuxiaoyao.config.loader.ConfigLoader;
  11. import java.util.Map;
  12. import java.util.concurrent.ConcurrentHashMap;
  13. /**
  14. * @author Kerry on 19/11/27
  15. */
  16. public class FileExporterApp {
  17. public static Injector injector;
  18. public static Map<String, String[]> TABLE_COLUMN = new ConcurrentHashMap<>();
  19. public static void main(String[] args) throws Exception {
  20. ReflectionUtil.initTableClassMapper("com.vcarecity.cvs.entity");
  21. AppProperties appProperties = ConfigLoader.parseConfig(args, AppProperties.class, null);
  22. jdbcArgs(args, appProperties);
  23. String table = getTableByArgs(args);
  24. if (table == null || table.length() == 0) {
  25. throw new Error("table name must not be null or empty!");
  26. }
  27. Class<?> tableClass = ReflectionUtil.getTableClass(table);
  28. boolean isMysql;
  29. Object properties;
  30. if (isImportToMysql(args)) {
  31. properties = "mysql";
  32. isMysql = true;
  33. } else {
  34. String basePath = table;
  35. final AppProperties.Export export = appProperties.getExport();
  36. if (export != null) {
  37. basePath = export.getFolder() + table;
  38. }
  39. properties = basePath;
  40. isMysql = false;
  41. }
  42. injector = Guice.createInjector(new PropertiesModule(appProperties), new SQLModule(isMysql));
  43. final SQLStarter instance = injector.getInstance(SQLStarter.class);
  44. instance.exportDataAndConvert(table, properties, tableClass);
  45. }
  46. private static boolean isImportToMysql(String[] args) {
  47. for (String arg : args) {
  48. if (arg.startsWith("--type=")) {
  49. return !"csv".equalsIgnoreCase(arg.substring("--type=".length()));
  50. }
  51. }
  52. return true;
  53. }
  54. private static String getTableByArgs(String[] args) {
  55. for (String arg : args) {
  56. if (arg.startsWith("--table=")) {
  57. return arg.substring("--table=".length()).trim();
  58. } else if (arg.startsWith("-t=")) {
  59. return arg.substring("-t=".length()).trim();
  60. }
  61. }
  62. return null;
  63. }
  64. private static void jdbcArgs(String[] args, AppProperties properties) {
  65. final DbProperties database = properties.getFromDb();
  66. for (String arg : args) {
  67. if (arg.startsWith("jdbc.url=")) {
  68. database.setUrl(arg.substring("jdbc.url=".length()).trim());
  69. } else if (arg.startsWith("jdbc.user=")) {
  70. database.setUser(arg.substring("jdbc.user=".length()).trim());
  71. } else if (arg.startsWith("jdbc.password=")) {
  72. database.setPassword(arg.substring("jdbc.password=".length()).trim());
  73. }
  74. }
  75. properties.setFromDb(database);
  76. }
  77. }