001/*
002 *  Copyright 2019 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.contentio.archive;
017
018import java.io.IOException;
019import java.nio.file.Path;
020import java.util.zip.ZipOutputStream;
021
022import javax.jcr.Node;
023import javax.jcr.RepositoryException;
024import javax.jcr.Session;
025
026/**
027 * Component responsible to archive plugin data.
028 */
029public interface PluginArchiver
030{
031    /**
032     * Export plugin data into the destination archive
033     * @param pluginName the plugin name.
034     * @param pluginNode the plugin {@link Node}.
035     * @param zos the output data stream.
036     * @param prefix the prefix for ZIP entries.
037     * @throws IOException if an error occurs while writing entries to the archive.
038     */
039    public void export(String pluginName, Node pluginNode, ZipOutputStream zos, String prefix) throws IOException;
040    
041    /**
042     * When in {@link MergePolicy#DELETE_BEFORE}, the plugin needs to delete its Node before importing (if it exists).
043     * @param pluginName the plugin name.
044     * @param allPluginsNode the {@link Node} for all plugins.
045     * @throws RepositoryException if a Repository error occured
046     */
047    public default void deleteBeforePartialImport(String pluginName, Node allPluginsNode) throws RepositoryException
048    {
049        if (allPluginsNode.hasNode(pluginName))
050        {
051            Session session = allPluginsNode.getSession();
052            Node pluginNode = allPluginsNode.getNode(pluginName);
053            pluginNode.remove();
054            session.save();
055        }
056    }
057    
058    /**
059     * Import plugin data from the source archive
060     * @param pluginName the plugin name.
061     * @param allPluginsNode the {@link Node} for all plugins.
062     * @param zipPath The input ZIP file
063     * @param zipPluginEntryPath The input ZIP entry
064     * @param merger The {@link Merger}
065     * @return The {@link ImportReport}
066     * @throws IOException if an error occurs while reading the archive.
067     */
068    public ImportReport partialImport(String pluginName, Node allPluginsNode, Path zipPath, String zipPluginEntryPath, Merger merger) throws IOException;
069}