RunTasks.java 4.0 KB

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