Files
VisualSapfor/src/_VisualDVM/Passes/All/ResurrectComponentFromServer.java
2024-10-14 15:19:13 +03:00

122 lines
5.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package _VisualDVM.Passes.All;
import Common.Passes.Pass;
import Common.Passes.PassException;
import Common.Visual.Controls.PassMenuItem;
import Common.Visual.Trees.DataTree;
import Common.Visual.Windows.Dialog.Dialog;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.RemoteFile.RemoteFile;
import _VisualDVM.Passes.CurrentComponentPass;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.Server.ComponentsRepositoryPass;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.Repository.Server.ServerExchangeUnit_2021;
import _VisualDVM.Utils;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Vector;
public class ResurrectComponentFromServer extends CurrentComponentPass {
Vector<RemoteFile> backups; //не забывать что файлы на СЕРВЕРЕ.
RemoteFile remoteFile;
File localFile;
@Override
public JMenuItem createMenuItem() {
if (menuItem == null)
menuItem = new PassMenuItem(this);
return menuItem;
}
@Override
protected boolean canStart(Object... args) throws Exception {
remoteFile = null;
localFile = null;
if (super.canStart() && Global.mainModule.getPass(PassCode.GetComponentsBackupsFromServer).Do()) {
//1. Получить список бекапов.
backups = (Vector<RemoteFile>) Global.mainModule.getPass(PassCode.GetComponentsBackupsFromServer).target;
backups.sort((o1, o2) -> o2.name.compareTo(o1.name));
Dialog d = new Dialog(null) {
@Override
public int getDefaultWidth() {
return 300;
}
@Override
public int getDefaultHeight() {
return 400;
}
@Override
public void CreateContent() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("(скрыть)");
for (RemoteFile file : backups) {
root.add(new DefaultMutableTreeNode(file));
}
DataTree tree = new DataTree(root) {
{
setRootVisible(false);
setCellRenderer(new DefaultTreeCellRenderer() {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
Object o = ((DefaultMutableTreeNode) value).getUserObject();
if (o instanceof RemoteFile) {
RemoteFile file = (RemoteFile) o;
setText(file.name);
}
return this;
}
});
}
@Override
public Current getCurrent() {
return Current.ComponentServerBackup;
}
};
content = tree;
}
};
if (d.ShowDialog("Выбор версии для восстановления") && Global.mainModule.Check(Log, Current.ComponentServerBackup)) {
remoteFile = Global.mainModule.getComponentServerBackup();
return true;
}
;
}
return false;
}
@Override
protected void body() throws Exception {
//1. Скачать файл.
Pass receivePass = new ComponentsRepositoryPass() {
@Override
public String getDescription() {
return "Скачивание файла с сервера";
}
@Override
protected void ServerAction() throws Exception {
Command(new ServerExchangeUnit_2021(ServerCode.ReceiveFile, remoteFile.full_name));
localFile = Utils.getTempFileName(remoteFile.name);
response.Unpack(localFile);
}
};
if (!receivePass.Do())
throw new PassException("Не удалось скачать файл!");
//------------>>
Files.copy(localFile.toPath(), target.getNewFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
target.Update();
}
@Override
protected void performDone() throws Exception {
target.InitialVersionCheck();
if (target.CanBeUpdated())
target.CheckIfNeedsUpdateOrPublish();
}
@Override
protected void showDone() throws Exception {
Global.RefreshUpdatesStatus();
}
}