DBInstallerConsole.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.tools.dbinstaller.console;
  16. import java.sql.Connection;
  17. import java.util.Scanner;
  18. import java.util.prefs.Preferences;
  19. import com.l2jserver.tools.dbinstaller.DBOutputInterface;
  20. import com.l2jserver.tools.dbinstaller.RunTasks;
  21. import com.l2jserver.tools.dbinstaller.util.mysql.MySqlConnect;
  22. /**
  23. * @author mrTJO
  24. */
  25. public class DBInstallerConsole implements DBOutputInterface
  26. {
  27. Connection _con;
  28. public DBInstallerConsole(String db, String dir, String cleanUp)
  29. {
  30. System.out.println("Welcome to L2J DataBase installer");
  31. Preferences prop = Preferences.userRoot();
  32. RunTasks rt = null;
  33. try (Scanner scn = new Scanner(System.in))
  34. {
  35. while (_con == null)
  36. {
  37. System.out.printf("%s (%s): ", "Host", prop.get("dbHost_" + db, "localhost"));
  38. String dbHost = scn.nextLine();
  39. System.out.printf("%s (%s): ", "Port", prop.get("dbPort_" + db, "3306"));
  40. String dbPort = scn.nextLine();
  41. System.out.printf("%s (%s): ", "Username", prop.get("dbUser_" + db, "root"));
  42. String dbUser = scn.nextLine();
  43. System.out.printf("%s (%s): ", "Password", "");
  44. String dbPass = scn.nextLine();
  45. System.out.printf("%s (%s): ", "Database", prop.get("dbDbse_" + db, db));
  46. String dbDbse = scn.nextLine();
  47. dbHost = dbHost.isEmpty() ? prop.get("dbHost_" + db, "localhost") : dbHost;
  48. dbPort = dbPort.isEmpty() ? prop.get("dbPort_" + db, "3306") : dbPort;
  49. dbUser = dbUser.isEmpty() ? prop.get("dbUser_" + db, "root") : dbUser;
  50. dbDbse = dbDbse.isEmpty() ? prop.get("dbDbse_" + db, db) : dbDbse;
  51. MySqlConnect connector = new MySqlConnect(dbHost, dbPort, dbUser, dbPass, dbDbse, true);
  52. _con = connector.getConnection();
  53. }
  54. System.out.print("(C)lean install, (U)pdate or (E)xit? ");
  55. String resp = scn.next();
  56. if (resp.equalsIgnoreCase("c"))
  57. {
  58. System.out.print("Do you really want to destroy your db (Y/N)?");
  59. if (scn.next().equalsIgnoreCase("y"))
  60. {
  61. rt = new RunTasks(this, db, dir, cleanUp, true);
  62. }
  63. }
  64. else if (resp.equalsIgnoreCase("u"))
  65. {
  66. rt = new RunTasks(this, db, dir, cleanUp, false);
  67. }
  68. }
  69. if (rt != null)
  70. {
  71. rt.run();
  72. }
  73. else
  74. {
  75. System.exit(0);
  76. }
  77. }
  78. @Override
  79. public void appendToProgressArea(String text)
  80. {
  81. System.out.println(text);
  82. }
  83. @Override
  84. public Connection getConnection()
  85. {
  86. return _con;
  87. }
  88. @Override
  89. public void setProgressIndeterminate(boolean value)
  90. {
  91. }
  92. @Override
  93. public void setProgressMaximum(int maxValue)
  94. {
  95. }
  96. @Override
  97. public void setProgressValue(int value)
  98. {
  99. }
  100. @Override
  101. public void setFrameVisible(boolean value)
  102. {
  103. }
  104. @Override
  105. public int requestConfirm(String title, String message, int type)
  106. {
  107. System.out.print(message);
  108. try (Scanner scn = new Scanner(System.in))
  109. {
  110. String res = scn.next();
  111. if (res.equalsIgnoreCase("y"))
  112. {
  113. return 0;
  114. }
  115. }
  116. return 1;
  117. }
  118. @Override
  119. public void showMessage(String title, String message, int type)
  120. {
  121. System.out.println(message);
  122. }
  123. }