001/*
002 *  Copyright 2020 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.io.InputStream;
020
021import javax.jcr.ImportUUIDBehavior;
022import javax.jcr.RepositoryException;
023import javax.jcr.Session;
024
025/**
026 * A merger, which is responsible to handle UUID collision when encountered.
027 */
028public interface Merger
029{
030    /**
031     * Returns <code>true</code> if all existing objects concerned by the {@link Archiver} must be deleted before {@link Archiver#partialImport importing}
032     * @return <code>true</code> if existing objects must be deleted
033     */
034    default boolean deleteBeforePartialImport()
035    {
036        return false;
037    }
038    
039    /**
040     * Returns <code>true</code> if the object with the given id needs to be merged
041     * @param id The id
042     * @return <code>true</code> if the object with the given id needs to be merged
043     */
044    boolean needsMerge(String id);
045    
046    /**
047     * The result of {@link Merger#merge}.
048     * <br>It tells whether to continue or stop the process of importing the given id.
049     */
050    public static enum AfterMerge
051    {
052        /** Stop importing of the object after the merge was processed. */
053        STOP_PROCESS,
054        /** Continue importing the object after the merge was processed. */
055        CONTINUE_PROCESS,
056    }
057    
058    /**
059     * Merges the existing object with given id
060     * @param id The id
061     * @return What the caller <b>must</b> do after this method is finished.
062     * @throws MergeException If the merge cannot be done. This exception must propagate and stop the global import process.
063     */
064    AfterMerge merge(String id) throws MergeException;
065    
066    /**
067     * Gets the UUID Behavior for JCR import
068     * <br>See {@link ImportUUIDBehavior} and {@link Session#importXML(String, java.io.InputStream, int)}
069     * @return the UUID Behavior
070     */
071    int getImportUuidBehavior();
072    
073    /**
074     * Does a JCR import from the XML read from the given {@link InputStream}
075     * <br>The save operation must be made by the caller.
076     * <br>The {@link InputStream} must be closed by the caller.
077     * <br>Default implementation uses {@link Session#importXML}
078     * @param session The JCR {@link Session}
079     * @param parentAbsPath the absolute path of the node below which the deserialized subgraph is added.
080     * @param in The {@link InputStream} from which the XML to be deserialized is read.
081     * @throws RepositoryException if another error occurs.
082     * @throws IOException if an error during an I/O operation occurs.
083     */
084    default void jcrImportXml(Session session, String parentAbsPath, InputStream in) throws RepositoryException, IOException
085    {
086        session.importXML(parentAbsPath, in, getImportUuidBehavior());
087    }
088}