001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.blob;
020
021import java.util.Calendar;
022import java.util.Collection;
023import java.util.Map;
024import java.util.Set;
025
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.model.Document;
028
029/**
030 * Interface for a dispatcher of blobs to different blob providers according to metadata.
031 *
032 * @since 7.3
033 */
034public interface BlobDispatcher {
035
036    static final class BlobDispatch {
037        public final String providerId;
038
039        public final boolean addPrefix;
040
041        public BlobDispatch(String providerId, boolean addPrefix) {
042            this.providerId = providerId;
043            this.addPrefix = addPrefix;
044        }
045    }
046
047    /**
048     * Initializes this blob dispatcher.
049     */
050    void initialize(Map<String, String> properties);
051
052    /**
053     * Gets the provider ids to which this dispatcher can dispatch.
054     * <p>
055     * Blobs already having a provider id not listed here won't be touched on write.
056     *
057     * @return a collection containing the provider ids
058     */
059    Collection<String> getBlobProviderIds();
060
061    /**
062     * Decides which {@link BlobProvider} to use to read a blob from the given repository if no prefix is specified in
063     * the blob key.
064     *
065     * @param repositoryName the repository name
066     * @return the blob provider id
067     */
068    String getBlobProvider(String repositoryName);
069
070    /**
071     * Decides which {@link BlobProvider} to use to write the given blob, and whether the provider id should be added as
072     * prefix to the managed blob key.
073     *
074     * @param doc the document containing the blob
075     * @param blob the blob
076     * @return the blob provider id and whether it should be added as prefix
077     * @deprecated since 9.1, use {@link #getBlobProvider(Document, Blob, String)} instead
078     */
079    @Deprecated
080    default BlobDispatch getBlobProvider(Document doc, Blob blob) {
081        return getBlobProvider(doc, blob, null);
082    }
083
084    /**
085     * Decides which {@link BlobProvider} to use to write the given blob, and whether the provider id should be added as
086     * prefix to the managed blob key.
087     *
088     * @param doc the document containing the blob
089     * @param blob the blob
090     * @param xpath the xpath of the blob in the document
091     * @return the blob provider id and whether it should be added as prefix
092     * @since 9.1
093     */
094    BlobDispatch getBlobProvider(Document doc, Blob blob, String xpath);
095
096    /**
097     * Notifies the blob dispatcher that a set of xpaths have changed on a document.
098     *
099     * @param doc the document
100     * @param xpaths the set of changed xpaths
101     * @since 7.3
102     */
103    void notifyChanges(Document doc, Set<String> xpaths);
104
105    /**
106     * Notifies the blob dispatcher that the document was made a record.
107     *
108     * @param doc the document
109     * @since 11.1
110     */
111    default void notifyMakeRecord(Document doc) {
112        // do nothing, for forward compatibility of non-default implementations
113    }
114
115    /**
116     * Notifies the blob dispatcher that the document has been copied.
117     *
118     * @param doc the new document, the result of the copy
119     * @since 11.1
120     */
121    default void notifyAfterCopy(Document doc) {
122        // do nothing, for forward compatibility of non-default implementations
123    }
124
125    /**
126     * Notifies the blob dispatcher that the document is about to be removed.
127     *
128     * @param doc the document
129     * @since 11.1
130     */
131    default void notifyBeforeRemove(Document doc) {
132        // do nothing, for forward compatibility of non-default implementations
133    }
134
135}