RunTasks.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.tools.dbinstaller;
  20. import java.io.File;
  21. import java.sql.SQLException;
  22. import java.util.prefs.Preferences;
  23. import javax.swing.JOptionPane;
  24. import com.l2jserver.tools.dbinstaller.util.mysql.DBDumper;
  25. import com.l2jserver.tools.dbinstaller.util.mysql.ScriptExecutor;
  26. import com.l2jserver.util.file.filter.SQLFilter;
  27. /**
  28. * @author mrTJO
  29. */
  30. public class RunTasks extends Thread
  31. {
  32. DBOutputInterface _frame;
  33. boolean _cleanInstall;
  34. String _db;
  35. String _sqlDir;
  36. String _cleanUpFile;
  37. public RunTasks(DBOutputInterface frame, String db, String sqlDir, String cleanUpFile, boolean cleanInstall)
  38. {
  39. _frame = frame;
  40. _db = db;
  41. _cleanInstall = cleanInstall;
  42. _sqlDir = sqlDir;
  43. _cleanUpFile = cleanUpFile;
  44. }
  45. @Override
  46. public void run()
  47. {
  48. new DBDumper(_frame, _db);
  49. ScriptExecutor exec = new ScriptExecutor(_frame);
  50. File clnFile = new File(_cleanUpFile);
  51. File updDir = new File(_sqlDir, "updates");
  52. File[] files = updDir.listFiles(new SQLFilter());
  53. Preferences prefs = Preferences.userRoot();
  54. if (_cleanInstall)
  55. {
  56. if (clnFile.exists())
  57. {
  58. _frame.appendToProgressArea("Cleaning Database...");
  59. exec.execSqlFile(clnFile);
  60. _frame.appendToProgressArea("Database Cleaned!");
  61. }
  62. else
  63. {
  64. _frame.appendToProgressArea("Database Cleaning Script Not Found!");
  65. }
  66. if (updDir.exists())
  67. {
  68. StringBuilder sb = new StringBuilder();
  69. for (File cf : files)
  70. {
  71. sb.append(cf.getName() + ';');
  72. }
  73. prefs.put(_db + "_upd", sb.toString());
  74. }
  75. }
  76. else
  77. {
  78. if (!_cleanInstall && updDir.exists())
  79. {
  80. _frame.appendToProgressArea("Installing Updates...");
  81. for (File cf : files)
  82. {
  83. if (!prefs.get(_db + "_upd", "").contains(cf.getName()))
  84. {
  85. exec.execSqlFile(cf, true);
  86. prefs.put(_db + "_upd", prefs.get(_db + "_upd", "") + cf.getName() + ";");
  87. }
  88. }
  89. _frame.appendToProgressArea("Database Updates Installed!");
  90. }
  91. }
  92. _frame.appendToProgressArea("Installing Database Content...");
  93. exec.execSqlBatch(new File(_sqlDir));
  94. _frame.appendToProgressArea("Database Installation Complete!");
  95. File cusDir = new File(_sqlDir, "custom");
  96. if (cusDir.exists())
  97. {
  98. int ch = _frame.requestConfirm("Install Custom", "Do you want to install custom tables?", JOptionPane.YES_NO_OPTION);
  99. if (ch == 0)
  100. {
  101. _frame.appendToProgressArea("Installing Custom Tables...");
  102. exec.execSqlBatch(cusDir);
  103. _frame.appendToProgressArea("Custom Tables Installed!");
  104. }
  105. }
  106. File modDir = new File(_sqlDir, "mods");
  107. if (modDir.exists())
  108. {
  109. int ch = _frame.requestConfirm("Install Mods", "Do you want to install mod tables?", JOptionPane.YES_NO_OPTION);
  110. if (ch == 0)
  111. {
  112. _frame.appendToProgressArea("Installing Mods Tables...");
  113. exec.execSqlBatch(modDir);
  114. _frame.appendToProgressArea("Mods Tables Installed!");
  115. }
  116. }
  117. try
  118. {
  119. _frame.getConnection().close();
  120. }
  121. catch (SQLException e)
  122. {
  123. JOptionPane.showMessageDialog(null, "Cannot close MySQL Connection: " + e.getMessage(), "Connection Error", JOptionPane.ERROR_MESSAGE);
  124. }
  125. _frame.setFrameVisible(false);
  126. _frame.showMessage("Done!", "Database Installation Complete!", JOptionPane.INFORMATION_MESSAGE);
  127. System.exit(0);
  128. }
  129. }