package _VisualDVM.ComponentsServer; import Common.Database.SQLITE.SQLiteDatabase; import Common.Utils.Utils_; import _VisualDVM.ComponentsServer.BugReport.BugReport; import _VisualDVM.ComponentsServer.BugReport.BugReportsDBTable; import _VisualDVM.ComponentsServer.BugReportRecipient.BugReportRecipient; import _VisualDVM.ComponentsServer.BugReportRecipient.BugReportRecipientsDBTable; import _VisualDVM.ComponentsServer.BugReportSetting.BugReportSetting; import _VisualDVM.ComponentsServer.BugReportSetting.BugReportSettingsDBTable; import _VisualDVM.ComponentsServer.Recipient.RecipientsDataSet; import _VisualDVM.Global; import _VisualDVM.Passes.PassCode; import _VisualDVM.Utils; import org.apache.commons.io.FileUtils; import java.io.File; import java.nio.file.Paths; import java.util.Comparator; import java.util.Vector; public class BugReportsDatabase extends SQLiteDatabase { public BugReportsDBTable bugReports; public BugReportSettingsDBTable bugReportSettings; public BugReportRecipientsDBTable bugReportRecipients; public RecipientsDataSet recipients = new RecipientsDataSet(); public BugReportsDatabase() { super(Paths.get(System.getProperty("user.dir"), "Data", "bug_reports.sqlite").toFile()); } @Override protected void initAllTables() throws Exception { addTable(bugReports = new BugReportsDBTable()); addTable(bugReportSettings = new BugReportSettingsDBTable()); addTable(bugReportRecipients = new BugReportRecipientsDBTable()); } @Override public void Init() throws Exception { DeleteDrafts(); //-- Patch(); } @Override public PassCode getSynchronizePassCode() { return PassCode.SynchronizeBugReports; } public void DeleteDrafts() throws Exception { Vector drafts = bugReports.getAllDrafts(); for (BugReport draft : drafts) Delete(draft); } @Override public void DropUI() { super.DropUI(); bugReports.ClearUI(); recipients.ClearUI(); } @Override public void ResetUI() { bugReports.ShowUI(); recipients.ShowUI(); //todo временно. super.ResetUI(); } public void saveBugreportRecipients(BugReport bugReport) throws Exception { if (bugReport.recipients != null) { for (BugReportRecipient recipient : bugReport.recipients) { recipient.bugreport_id = bugReport.id; Insert(recipient); } } } public void Patch() throws Exception { int i = 0; Vector sortedBugs = new Vector<>(bugReports.Data.values()); sortedBugs.sort(new Comparator() { @Override public int compare(BugReport o1, BugReport o2) { return Long.compare(o1.date,o2.date); } }); //-- for (BugReport bugReport: sortedBugs){ bugReport.id_ = i; ++i; Update(bugReport); } //-- for (BugReport bugReport: sortedBugs){ Vector bugSettings = getVectorByFK(bugReport, BugReportSetting.class); Vector bugReportRecipients = getVectorByFK(bugReport, BugReportRecipient.class); for (BugReportSetting setting: bugSettings){ setting.bugreport_id_ = bugReport.id_; Update(setting); } for (BugReportRecipient recipient: bugReportRecipients){ recipient.bugreport_id_ = bugReport.id_; Update(recipient); } } //перенос архивов for (BugReport bugReport: sortedBugs){ System.out.println("bugreport_id="+bugReport.id+"/"+bugReport.id_); File new_home = new File(Global.BugReportsDirectory,String.valueOf(bugReport.id_)); //-- if (!new_home.exists()) FileUtils.forceMkdir(new_home); //-- Utils.CleanDirectory(new_home); //-- File old_archive = new File(Global.BugReportsDirectory_OLD, bugReport.id); File new_archive = new File(new_home,String.valueOf(bugReport.id_)+".zip"); if (old_archive.exists()) { FileUtils.copyFile(old_archive, new_archive); } } //-- } }