Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Zip when archive #266

Open
wants to merge 5 commits into
base: integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions gse-app/src/main/java/com/powsybl/gse/app/ProjectPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.powsybl.afs.*;
import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.NodeInfo;
import com.powsybl.afs.storage.Utils;
import com.powsybl.afs.storage.events.*;
import com.powsybl.commons.util.ServiceLoaderCache;
import com.powsybl.gse.copy_paste.afs.CopyManager;
Expand Down Expand Up @@ -41,13 +42,15 @@
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.Pair;
import org.apache.commons.lang3.NotImplementedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.Field;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -85,7 +88,6 @@ public class ProjectPane extends Tab {
private final TaskItemList taskItems;
private final TaskMonitorPane taskMonitorPane;
private final Map<String, ProjectPaneProjectFolderListener> lCache = new HashMap<>();
private final CopyManager localArchiveManager = CopyManager.getInstance();
private final AppStorageListener appStorageListener;

public ProjectPane(Scene scene, Project project, GseContext context) {
Expand Down Expand Up @@ -721,9 +723,12 @@ private void archive(AbstractNodeBase item) {
context.getExecutor().execute(() -> {
TaskMonitor.Task task = project.getFileSystem().getTaskMonitor().startTask(String.format(RESOURCE_BUNDLE.getString("ArchiveTask"), selectedDirectory.getName()), project);
try {
localArchiveManager.copy(Collections.singletonList(item), selectedDirectory);
} catch (CopyPasteException e) {
Utils.checkDiskSpace(selectedDirectory.toPath());
item.archive(selectedDirectory.toPath(), true);
LOGGER.info("Archiving node {} ({}) is complete", item.getName(), item.getId());
} catch (AfsException | IOException e) {
GseAlerts.showDialogError(e.getMessage());
LOGGER.error("Archiving has failed for node {}", item.getId(), e);
} finally {
project.getFileSystem().getTaskMonitor().stopTask(task.getId());
}
Expand All @@ -737,13 +742,14 @@ private void unarchiveItems(TreeItem<Object> folderItem) {
throw new IllegalStateException("Can't unarchive item if target is not a project folder!");
}

DirectoryChooser directoryChooser = new DirectoryChooser();
java.io.File selectedDirectory = directoryChooser.showDialog(getContent().getScene().getWindow());
if (selectedDirectory != null) {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("ZIP", "*.zip"));
java.io.File selectedFile = fileChooser.showOpenDialog(getContent().getScene().getWindow());
if (selectedFile != null) {
context.getExecutor().execute(() -> {
TaskMonitor.Task task = project.getFileSystem().getTaskMonitor().startTask(String.format(RESOURCE_BUNDLE.getString("UnarchiveTask"), selectedDirectory.getName()), project);
TaskMonitor.Task task = project.getFileSystem().getTaskMonitor().startTask(String.format(RESOURCE_BUNDLE.getString("UnarchiveTask"), selectedFile.getName()), project);
try {
((ProjectFolder) folder).unarchive(selectedDirectory.toPath());
((ProjectFolder) folder).unarchive(selectedFile.toPath(), true);
Platform.runLater(() -> refresh(folderItem));
} catch (Exception e) {
Platform.runLater(() -> GseAlerts.showDialogError(e.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.powsybl.gse.copy_paste.afs;

import com.powsybl.afs.*;
import com.powsybl.afs.storage.Utils;
import com.powsybl.commons.util.ServiceLoaderCache;
import com.powsybl.computation.local.LocalComputationManager;
import com.powsybl.gse.copy_paste.afs.exceptions.*;
Expand Down Expand Up @@ -37,7 +38,6 @@ public final class CopyManager {
private static final String NODE_COPY_TYPE = "@NODE@";
private static final String COPY_SIGNATURE = "@COPY_SIGNATURE@";
private static final long COPY_EXPIRATION_TIME = 6;
private static final long MIN_DISK_SPACE_THRESHOLD = 10;
private static final Logger LOGGER = LoggerFactory.getLogger(CopyManager.class);
private static final String TEMP_DIR_PREFIX = "powsybl_node_export";
private static final long CLEANUP_DELAY = 36000;
Expand Down Expand Up @@ -105,21 +105,14 @@ public Map<String, CopyInfo> copy(List<? extends AbstractNodeBase> nodes, @Nulla
copyInfo.nodeId = node.getId();
copyInfo.node = node;
copyInfo.archivePath = resolveArchiveTargetDirectory(targetDirectory);

File archiveFile = copyInfo.archivePath.toFile();
long freeSpacePercent = 100 * archiveFile.getFreeSpace() / archiveFile.getTotalSpace();
LOGGER.info("Copying into drive with {}% free space", freeSpacePercent);
if (freeSpacePercent < MIN_DISK_SPACE_THRESHOLD) {
throw new CopyPasteException("Not enough space");
}

copyInfo.expirationDate = ZonedDateTime.now().plusHours(COPY_EXPIRATION_TIME);

currentCopies.put(copyInfo.nodeId, copyInfo);

LOGGER.info("Copying (archiving) node {} ({})", node.getName(), node.getId());
logger.accept(String.format("Copying node %s", copyInfo.getNode().getName()));
try {
Utils.checkDiskSpace(copyInfo.archivePath);
node.archive(copyInfo.archivePath);
copyInfo.archiveSuccess = true;
LOGGER.info("Copying (archiving) node {} ({}) is complete", node.getName(), node.getId());
Expand Down Expand Up @@ -276,7 +269,7 @@ private String renameAndPaste(AbstractNodeBase folder, List<? extends AbstractNo
throw new CopyPasteException("Failed to rename new node");
}

private String renameSameTypeNode(ProjectFolder projectFolder, AbstractNodeBase child, CopyInfo info) throws CopyPasteFileAlreadyExistException {
private String renameSameTypeNode(ProjectFolder projectFolder, AbstractNodeBase child, CopyInfo info) {
String name = info.node.getName();
String copyDuplicated = " - " + "Copy";
String copyNameBaseName = name + copyDuplicated;
Expand Down Expand Up @@ -304,7 +297,6 @@ private String renameSameTypeNode(ProjectFolder projectFolder, AbstractNodeBase

newNode.rename(copyName.get());
});

} finally {
child.rename(name);
}
Expand Down
27 changes: 14 additions & 13 deletions gse-util/src/main/java/com/powsybl/gse/util/NodeChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package com.powsybl.gse.util;

import com.powsybl.afs.*;
import com.powsybl.afs.storage.Utils;
import com.powsybl.gse.copy_paste.afs.CopyManager;
import com.powsybl.gse.copy_paste.afs.CopyService;
import com.powsybl.gse.copy_paste.afs.exceptions.CopyPasteException;
import com.powsybl.gse.spi.GseContext;
import impl.org.controlsfx.skin.BreadCrumbBarSkin;
import javafx.application.Platform;
Expand All @@ -25,15 +25,13 @@
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.Text;
import javafx.stage.DirectoryChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.stage.*;
import javafx.util.Callback;
import org.controlsfx.control.BreadCrumbBar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.prefs.Preferences;
Expand All @@ -54,7 +52,6 @@ public class NodeChooser<N, F extends N, D extends N, T extends N> extends GridP
private boolean success;
private Set<String> openedProjects = new HashSet<>();
private SimpleBooleanProperty deleteMenuItemDisableProperty = new SimpleBooleanProperty(false);
private final CopyManager localArchiveManager = CopyManager.getInstance();

private BooleanProperty copied = new SimpleBooleanProperty(false);

Expand Down Expand Up @@ -691,11 +688,14 @@ private void archive(AbstractNodeBase item) {
context.getExecutor().execute(() -> {
InfoDialog infoDialog = new InfoDialog(String.format(RESOURCE_BUNDLE.getString("ArchiveTask"), item.getName()), true);
try {
localArchiveManager.copy(Collections.singletonList(item), selectedDirectory);
Utils.checkDiskSpace(selectedDirectory.toPath());
item.archive(selectedDirectory.toPath(), true);
infoDialog.updateStage(RESOURCE_BUNDLE.getString("CompleteTask"));
} catch (CopyPasteException e) {
LOGGER.info("Archiving node {} ({}) is complete", item.getName(), item.getId());
} catch (AfsException | IOException e) {
Platform.runLater(() -> GseAlerts.showDialogError(e.getMessage()));
infoDialog.updateStage(RESOURCE_BUNDLE.getString("ErrorTask"), Color.RED);
LOGGER.error("Archiving has failed for node {}", item.getId(), e);
}
});
}
Expand All @@ -707,13 +707,14 @@ private void unarchiveItems(TreeItem<N> folderItem) {
throw new IllegalStateException("Can't unarchive item if target is not a folder!");
}

DirectoryChooser directoryChooser = new DirectoryChooser();
java.io.File selectedDirectory = directoryChooser.showDialog(window);
if (selectedDirectory != null) {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("ZIP", "*.zip"));
java.io.File selectedFile = fileChooser.showOpenDialog(window);
if (selectedFile != null) {
context.getExecutor().execute(() -> {
InfoDialog infoDialog = new InfoDialog(String.format(RESOURCE_BUNDLE.getString("UnarchiveTask"), selectedDirectory.getName()), true);
InfoDialog infoDialog = new InfoDialog(String.format(RESOURCE_BUNDLE.getString("UnarchiveTask"), selectedFile.getName()), true);
try {
((Folder) folder).unarchive(selectedDirectory.toPath());
((Folder) folder).unarchive(selectedFile.toPath(), true);
infoDialog.updateStage(RESOURCE_BUNDLE.getString("CompleteTask"));
Platform.runLater(() -> refresh(folderItem));
} catch (Exception e) {
Expand Down