001/*
002 * (C) Copyright 2012 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 *     Olivier Grisel <ogrisel@nuxeo.com>
018 *     Antoine Taillefer <ataillefer@nuxeo.com>
019 */
020package org.nuxeo.drive.service;
021
022import java.security.Principal;
023import java.util.Map;
024import java.util.Set;
025
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.IdRef;
029
030/**
031 * Manage list of NuxeoDrive synchronization roots and devices for a given nuxeo user.
032 */
033public interface NuxeoDriveManager {
034
035    public static final String LOCALLY_EDITED_COLLECTION_NAME = "Locally Edited";
036
037    /**
038     * @param principal the Nuxeo Drive user
039     * @param newRootContainer the folderish document to be used as synchronization root: must be bound to an active
040     *            session
041     * @throws SecurityException if the user does not have write permissions to the container.
042     */
043    public void registerSynchronizationRoot(Principal principal, DocumentModel newRootContainer, CoreSession session);
044
045    /**
046     * @param principal the Nuxeo Drive user
047     * @param rootContainer the folderish document that should no longer be used as a synchronization root
048     */
049    public void unregisterSynchronizationRoot(Principal principal, DocumentModel rootContainer, CoreSession session);
050
051    /**
052     * Fetch the list of synchronization root refs for a given user and a given session repository. This list is assumed
053     * to be short enough (in the order of 100 folder max) so that no paging API is required. The user is taken from the
054     * session.getPrincipal() method.
055     *
056     * @param session active CoreSession instance to the repository hosting the roots.
057     * @return the ordered set of non deleted synchronization root references for that user
058     * @see #getSynchronizationRootPaths(String, CoreSession)
059     */
060    public Set<IdRef> getSynchronizationRootReferences(CoreSession session);
061
062    /**
063     * Fetch all the synchronization root references and paths for a given user. This list is assumed to be short enough
064     * (in the order of 100 folder max) so that no paging API is required.
065     *
066     * @param principal the user to fetch the roots for
067     * @return the map keyed by repository names all active roots definitions for the current user.
068     */
069    public Map<String, SynchronizationRoots> getSynchronizationRoots(Principal principal);
070
071    /**
072     * Fetch all the collection sync root member ids for a given user.
073     *
074     * @param principal the user to fetch the ids for
075     * @return the map keyed by repository names all collection sync root member ids for the current user.
076     */
077    public Map<String, Set<String>> getCollectionSyncRootMemberIds(Principal principal);
078
079    /**
080     * Checks if the given {@link DocumentModel} is a synchronization root for the given user.
081     */
082    public boolean isSynchronizationRoot(Principal principal, DocumentModel doc);
083
084    /**
085     * Method to be called by a CoreEvent listener monitoring documents deletions to cleanup references to recently
086     * deleted documents and invalidate the caches.
087     */
088    public void handleFolderDeletion(IdRef ref);
089
090    /**
091     * Gets a summary of document changes in all repositories for the given user's synchronization roots, since the
092     * user's device last successful synchronization date.
093     * <p>
094     * The summary includes:
095     * <ul>
096     * <li>The list of sync root paths</li>
097     * <li>A list of document changes</li>
098     * <li>The document models that have changed</li>
099     * <li>A status code</li>
100     * </ul>
101     *
102     * @param principal
103     * @param lastSyncRootRefs the map keyed by repository names of document refs for the synchronization roots that
104     *            were active during last synchronization
105     * @param lastSuccessfulSync the last successful synchronization date of the user's device. This time is expected to
106     *            be in milliseconds since 1970-01-01 UTC as measured on the Nuxeo server clock, typically set to the
107     *            value returned by {@link FileSystemChangeSummary#getSyncDate()} of the previous call to
108     *            {@link NuxeoDriveManager#getChangeSummary(Principal, Map, long)} or 0 for catching every event since
109     *            the repository initialization.
110     * @return the summary of document changes
111     */
112    public FileSystemChangeSummary getChangeSummary(Principal principal, Map<String, Set<IdRef>> lastSyncRootRefs,
113            long lastSuccessfulSync);
114
115    /**
116     * Gets a summary of document changes in all repositories for the given user's synchronization roots, from the lower
117     * bound sent by the user's device.
118     * <p>
119     * The summary includes:
120     * <ul>
121     * <li>The list of sync root paths</li>
122     * <li>A list of document changes</li>
123     * <li>The document models that have changed</li>
124     * <li>A status code</li>
125     * </ul>
126     *
127     * @param principal
128     * @param lastSyncRootRefs the map keyed by repository names of document refs for the synchronization roots that
129     *            were active during last synchronization
130     * @param lowerBound the lower bound sent by the user's device. Typically set to the value returned by
131     *            {@link FileSystemChangeSummary#getUpperBound()} of the previous call to
132     *            {@link NuxeoDriveManager#getChangeSummaryIntegerBounds(Principal, Map, long)} or 0 for catching every
133     *            event since the repository initialization.
134     * @return the summary of document changes
135     */
136    public FileSystemChangeSummary getChangeSummaryIntegerBounds(Principal principal,
137            Map<String, Set<IdRef>> lastSyncRootRefs, long lowerBound);
138
139    /**
140     * Gets the {@link FileSystemChangeFinder} member.
141     */
142    public FileSystemChangeFinder getChangeFinder();
143
144    /**
145     * Sets the {@link FileSystemChangeFinder} member.
146     *
147     * @deprecated since 7.3, use {@code changeFinder} extension point instead
148     */
149    @Deprecated
150    public void setChangeFinder(FileSystemChangeFinder changeFinder);
151
152    /**
153     * Invalidate the synchronization roots cache for a given user so as to query the repository next time
154     * {@link #getSynchronizationRoots(Principal)} is called.
155     *
156     * @param userName the principal name of the user to invalidate the cache for.
157     */
158    void invalidateSynchronizationRootsCache(String userName);
159
160    /**
161     * Invalidate the collection sync root member cache for a given user so as to query the repository next time
162     * {@link #getCollectionSyncRootMemberIds(Principal)} is called.
163     *
164     * @param userName the principal name of the user to invalidate the cache for.
165     */
166    void invalidateCollectionSyncRootMemberCache(String userName);
167
168    /**
169     * Invalidate the collection sync root member cache for all users so as to query the repository next time
170     * {@link #getCollectionSyncRootMemberIds(Principal)} is called.
171     */
172    void invalidateCollectionSyncRootMemberCache();
173
174    /**
175     * Adds the given {@link DocumentModel} to the {@link #LOCALLY_EDITED_COLLECTION_NAME} collection.
176     *
177     * @since 6.0
178     */
179    void addToLocallyEditedCollection(CoreSession session, DocumentModel doc);
180
181}