All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.powsybl.afs.AbstractNodeBase Maven / Gradle / Ivy

There is a newer version: 6.1.0
Show newest version
/**
 * Copyright (c) 2017, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.powsybl.afs;

import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.AppStorageArchive;
import com.powsybl.afs.storage.Utils;
import com.powsybl.afs.storage.NodeInfo;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Stream;

/**
 * Base class for all node objects stored in an AFS tree.
 *
 * @author Geoffroy Jamgotchian 
 */
public abstract class AbstractNodeBase {

    protected final NodeInfo info;

    protected final AppStorage storage;

    protected int codeVersion;

    public AbstractNodeBase(NodeInfo info, AppStorage storage, int codeVersion) {
        this.info = Objects.requireNonNull(info);
        this.storage = Objects.requireNonNull(storage);
        this.codeVersion = codeVersion;
    }

    public abstract Optional getParent();

    protected Optional getParentInfo() {
        return storage.getParentNode(info.getId());
    }

    /**
     * An ID uniquely identifying this node in the file system tree.
     */
    public String getId() {
        return info.getId();
    }

    public String getName() {
        return info.getName();
    }

    public String getDescription() {
        return info.getDescription();
    }

    public void setDescription(String description) {
        storage.setDescription(info.getId(), description);
        info.setDescription(description);
        storage.flush();
    }

    public ZonedDateTime getCreationDate() {
        return Instant.ofEpochMilli(info.getCreationTime()).atZone(ZoneId.systemDefault());
    }

    public ZonedDateTime getModificationDate() {
        return Instant.ofEpochMilli(info.getModificationTime()).atZone(ZoneId.systemDefault());
    }

    public int getVersion() {
        return info.getVersion();
    }

    protected int getCodeVersion() {
        return codeVersion;
    }

    public boolean isAheadOfVersion() {
        return info.getVersion() > getCodeVersion();
    }

    public abstract NodePath getPath();

    public abstract boolean isFolder();

    private NodeInfo getChildInfo(NodeInfo nodeInfo, String name) {
        Objects.requireNonNull(name);
        NodeInfo childInfo = nodeInfo;
        for (String name2 : name.split(AppFileSystem.PATH_SEPARATOR)) {
            childInfo = storage.getChildNode(childInfo.getId(), name2).orElse(null);
            if (childInfo == null) {
                return null;
            }
        }
        return childInfo;
    }

    protected NodeInfo getChildInfo(String name, String... more) {
        NodeInfo childInfo = getChildInfo(info, name);
        if (childInfo == null) {
            return null;
        }
        for (String name2 : more) {
            childInfo = getChildInfo(childInfo, name2);
            if (childInfo == null) {
                return null;
            }
        }
        return childInfo;
    }

    @Override
    public String toString() {
        return getName();
    }

    public void moveTo(AbstractNodeBase folder) {
        Objects.requireNonNull(folder);
        if (!folder.isParentOf(this)) {
            if (isMovableTo(folder)) {
                storage.setParentNode(info.getId(), folder.getId());
                storage.flush();
            } else {
                throw new AfsException("The source node is an ancestor of the target node");
            }
        }
    }

    private boolean isMovableTo(AbstractNodeBase node) {
        return node.isFolder() && !isAncestorOf(node);
    }

    public boolean isAncestorOf(AbstractNodeBase node) {
        Optional current = storage.getParentNode(node.getId());
        while (current.isPresent()) {
            if (current.get().getId().equals(info.getId())) {
                return true;
            } else {
                current = storage.getParentNode(current.get().getId());
            }
        }
        return false;
    }

    boolean isParentOf(AbstractNodeBase node) {
        return node.getParentInfo().map(n -> n.getId().equals(info.getId())).orElse(false);
    }

    public void rename(String name) {
        Objects.requireNonNull(name);
        if (!nodeNameAlreadyExists(name)) {
            storage.renameNode(info.getId(), name);
            info.setName(name);
            storage.flush();
        } else {
            throw new AfsException("name already exists");
        }
    }

    private boolean nodeNameAlreadyExists(String name) {
        Objects.requireNonNull(name);
        Optional parentNode = storage.getParentNode(getId());
        List childNodes = new ArrayList<>();
        if (parentNode.isPresent()) {
            childNodes = storage.getChildNodes(parentNode.get().getId());
        }
        return childNodes.stream().filter(nodeInfo -> !nodeInfo.getId().equals(getId())).anyMatch(nodeInfo -> nodeInfo.getName().equals(name));
    }

    public void archive(Path dir, boolean useZip, boolean archiveDependencies, Map> outputBlackList, List removeTs) {

        Objects.requireNonNull(dir);

        try {
            new AppStorageArchive(storage).archive(info.getId(), dir, archiveDependencies, outputBlackList, removeTs);
            if (useZip) {
                Path zipPath = dir.getParent().resolve(dir.getFileName() + ".zip");
                Utils.zip(dir, zipPath, true);
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    public void archive(Path dir, boolean useZip, boolean archiveDependencies, Map> outputBlackList) {
        archive(dir, useZip, archiveDependencies, outputBlackList, Collections.emptyList());
    }

    public void unarchive(Path dir, boolean isZipped) {
        if (isZipped) {
            String filename = dir.getFileName().toString().substring(0, dir.getFileName().toString().length() - 4);
            Path nodeDir = dir.getParent().resolve(filename);
            try {
                if (Files.exists(nodeDir)) {
                    throw new FileAlreadyExistsException("Archive already exist.");
                }
                try {
                    Utils.unzip(dir, nodeDir);
                    try (Stream stream = Files.list(nodeDir)) {
                        stream.forEach(childNodeDir -> new AppStorageArchive(storage).unarchive(info, childNodeDir));
                    }
                } finally {
                    Utils.deleteDirectory(nodeDir);
                }
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        } else {
            new AppStorageArchive(storage).unarchive(info, dir);
        }
    }

    public void archive(Path dir, Map> outputBlackList, List keepTs) {
        archive(dir, false, false, outputBlackList, keepTs);
    }

    public void archive(Path dir, Map> outputBlackList) {
        archive(dir, false, false, outputBlackList, Collections.emptyList());
    }

    public void archive(Path dir) {
        archive(dir, false, false, Collections.emptyMap(), Collections.emptyList());
    }

    public void unarchive(Path dir) {
        unarchive(dir, false);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy