RunTasks.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.util.file.filter.SQLFilter;
  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 SQLFilter());
  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. {
  60. _frame.appendToProgressArea("Database Cleaning Script Not Found!");
  61. }
  62. if (updDir.exists())
  63. {
  64. StringBuilder sb = new StringBuilder();
  65. for (File cf : files)
  66. {
  67. sb.append(cf.getName() + ';');
  68. }
  69. prefs.put(_db + "_upd", sb.toString());
  70. }
  71. }
  72. else
  73. {
  74. if (!_cleanInstall && updDir.exists())
  75. {
  76. _frame.appendToProgressArea("Installing Updates...");
  77. for (File cf : files)
  78. {
  79. if (!prefs.get(_db + "_upd", "").contains(cf.getName()))
  80. {
  81. exec.execSqlFile(cf, true);
  82. prefs.put(_db + "_upd", prefs.get(_db + "_upd", "") + cf.getName() + ";");
  83. }
  84. }
  85. _frame.appendToProgressArea("Database Updates Installed!");
  86. }
  87. }
  88. _frame.appendToProgressArea("Installing Database Content...");
  89. exec.execSqlBatch(new File(_sqlDir));
  90. _frame.appendToProgressArea("Database Installation Complete!");
  91. File cusDir = new File(_sqlDir, "custom");
  92. if (cusDir.exists())
  93. {
  94. int ch = _frame.requestConfirm("Install Custom", "Do you want to install custom tables?", JOptionPane.YES_NO_OPTION);
  95. if (ch == 0)
  96. {
  97. _frame.appendToProgressArea("Installing Custom Tables...");
  98. exec.execSqlBatch(cusDir);
  99. _frame.appendToProgressArea("Custom Tables Installed!");
  100. }
  101. }
  102. File modDir = new File(_sqlDir, "mods");
  103. if (modDir.exists())
  104. {
  105. int ch = _frame.requestConfirm("Install Mods", "Do you want to install mod tables?", JOptionPane.YES_NO_OPTION);
  106. if (ch == 0)
  107. {
  108. _frame.appendToProgressArea("Installing Mods Tables...");
  109. exec.execSqlBatch(modDir);
  110. _frame.appendToProgressArea("Mods Tables Installed!");
  111. }
  112. }
  113. try
  114. {
  115. _frame.getConnection().close();
  116. }
  117. catch (SQLException e)
  118. {
  119. JOptionPane.showMessageDialog(null, "Cannot close MySQL Connection: " + e.getMessage(), "Connection Error", JOptionPane.ERROR_MESSAGE);
  120. }
  121. _frame.setFrameVisible(false);
  122. _frame.showMessage("Done!", "Database Installation Complete!", JOptionPane.INFORMATION_MESSAGE);
  123. System.exit(0);
  124. }
  125. }