RunTasks.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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;
  16. import java.io.File;
  17. import java.sql.SQLException;
  18. import java.util.prefs.Preferences;
  19. import javax.swing.JOptionPane;
  20. import com.l2jserver.tools.dbinstaller.util.mysql.DBDumper;
  21. import com.l2jserver.tools.dbinstaller.util.mysql.ScriptExecutor;
  22. import com.l2jserver.tools.dbinstaller.util.mysql.ScriptExecutor.SqlFileFilter;
  23. /**
  24. * @author mrTJO
  25. */
  26. public class RunTasks extends Thread
  27. {
  28. DBOutputInterface _frame;
  29. boolean _cleanInstall;
  30. String _db;
  31. String _sqlDir;
  32. String _cleanUpFile;
  33. public RunTasks(DBOutputInterface frame, String db, String sqlDir, String cleanUpFile, boolean cleanInstall)
  34. {
  35. _frame = frame;
  36. _db = db;
  37. _cleanInstall = cleanInstall;
  38. _sqlDir = sqlDir;
  39. _cleanUpFile = cleanUpFile;
  40. }
  41. @Override
  42. public void run()
  43. {
  44. new DBDumper(_frame, _db);
  45. ScriptExecutor exec = new ScriptExecutor(_frame);
  46. File clnFile = new File(_cleanUpFile);
  47. File updDir = new File(_sqlDir, "updates");
  48. File[] files = updDir.listFiles(new SqlFileFilter());
  49. Preferences prefs = Preferences.userRoot();
  50. if (_cleanInstall)
  51. {
  52. if (clnFile.exists())
  53. {
  54. _frame.appendToProgressArea("Cleaning Database...");
  55. exec.execSqlFile(clnFile);
  56. _frame.appendToProgressArea("Database Cleaned!");
  57. }
  58. else
  59. _frame.appendToProgressArea("Database Cleaning Script Not Found!");
  60. if (updDir.exists())
  61. {
  62. StringBuilder sb = new StringBuilder();
  63. for (File cf : files)
  64. sb.append(cf.getName() + ';');
  65. prefs.put(_db + "_upd", sb.toString());
  66. }
  67. }
  68. else
  69. {
  70. if (!_cleanInstall && updDir.exists())
  71. {
  72. _frame.appendToProgressArea("Installing Updates...");
  73. for (File cf : files)
  74. {
  75. if (!prefs.get(_db + "_upd", "").contains(cf.getName()))
  76. {
  77. exec.execSqlFile(cf, true);
  78. prefs.put(_db + "_upd", prefs.get(_db + "_upd", "") + cf.getName() + ";");
  79. }
  80. }
  81. _frame.appendToProgressArea("Database Updates Installed!");
  82. }
  83. }
  84. _frame.appendToProgressArea("Installing Database Content...");
  85. exec.execSqlBatch(new File(_sqlDir));
  86. _frame.appendToProgressArea("Database Installation Complete!");
  87. File cusDir = new File(_sqlDir, "custom");
  88. if (cusDir.exists())
  89. {
  90. int ch = _frame.requestConfirm("Install Custom", "Do you want to install custom tables?", JOptionPane.YES_NO_OPTION);
  91. if (ch == 0)
  92. {
  93. _frame.appendToProgressArea("Installing Custom Tables...");
  94. exec.execSqlBatch(cusDir);
  95. _frame.appendToProgressArea("Custom Tables Installed!");
  96. }
  97. }
  98. File modDir = new File(_sqlDir, "mods");
  99. if (modDir.exists())
  100. {
  101. int ch = _frame.requestConfirm("Install Mods", "Do you want to install mod tables?", JOptionPane.YES_NO_OPTION);
  102. if (ch == 0)
  103. {
  104. _frame.appendToProgressArea("Installing Mods Tables...");
  105. exec.execSqlBatch(modDir);
  106. _frame.appendToProgressArea("Mods Tables Installed!");
  107. }
  108. }
  109. try
  110. {
  111. _frame.getConnection().close();
  112. }
  113. catch (SQLException e)
  114. {
  115. JOptionPane.showMessageDialog(null, "Cannot close MySQL Connection: " + e.getMessage(), "Connection Error", JOptionPane.ERROR_MESSAGE);
  116. }
  117. _frame.setFrameVisible(false);
  118. _frame.showMessage("Done!", "Database Installation Complete!", JOptionPane.INFORMATION_MESSAGE);
  119. System.exit(0);
  120. }
  121. }