001/*
002 * (C) Copyright 2006-2011 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.trash;
020
021import java.security.Principal;
022import java.util.List;
023import java.util.Set;
024
025import org.nuxeo.common.utils.Path;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentModelList;
029import org.nuxeo.ecm.core.api.DocumentRef;
030
031/**
032 * Service containing the logic about deleting/purging/undeleting a document.
033 */
034public interface TrashService {
035
036    /**
037     * Can a child of the folder be deleted?
038     *
039     * @param folder the folder
040     * @return {@code true} if the folder allows its children to be deleted
041     */
042    boolean folderAllowsDelete(DocumentModel folder);
043
044    /**
045     * Is at least one doc deletable according to its container?
046     *
047     * @param docs the documents
048     * @return {@code true} if one doc is in a folder that allows its children to be deleted
049     */
050    boolean checkDeletePermOnParents(List<DocumentModel> docs);
051
052    /**
053     * Is at least one doc deletable?
054     *
055     * @param docs the documents
056     * @param principal the current user (to check locks)
057     * @param checkProxies {@code true} to count proxies as non-deletable
058     * @return {@code true} if at least one doc is deletable
059     */
060    boolean canDelete(List<DocumentModel> docs, Principal principal, boolean checkProxies);
061
062    /**
063     * Are all documents purgeable/undeletable?
064     * <p>
065     * Documents need to be in the deleted lifecycle state for this to be true, in addition to the standard permission
066     * checks.
067     *
068     * @param docs the documents
069     * @param principal the current user (to check locks)
070     * @return {@code true} if the documents are purgeable/undeletable
071     */
072    boolean canPurgeOrUndelete(List<DocumentModel> docs, Principal principal);
073
074    /**
075     * Gets the trash info for a list of documents.
076     *
077     * @param docs the documents
078     * @param principal the current user (to check locks)
079     * @param checkProxies {@code true} to count proxies as non-deletable
080     * @param checkDeleted {@code true} if documents have to be in the deleted state to be considered (otherwise
081     *            forbidden)
082     * @return the trash info
083     */
084    TrashInfo getTrashInfo(List<DocumentModel> docs, Principal principal, boolean checkProxies, boolean checkDeleted);
085
086    /**
087     * Gets the closest document's ancestor above all the paths.
088     * <p>
089     * This is used to find what safe document to redirect to when deleting some.
090     *
091     * @param doc the document
092     * @param paths the paths
093     * @return the closer document above doc and above all the paths
094     */
095    DocumentModel getAboveDocument(DocumentModel doc, Set<Path> paths);
096
097    /**
098     * Moves documents to the trash, or directly deletes them if their lifecycle does not allow trash use.
099     *
100     * Do nothing if the document current state is deleted.
101     *
102     * @param docs the documents to trash
103     */
104    void trashDocuments(List<DocumentModel> docs);
105
106    /**
107     * Purges (completely deletes) documents .
108     *
109     * @param session the session
110     * @param docRefs the documents to purge
111     */
112    void purgeDocuments(CoreSession session, List<DocumentRef> docRefs);
113
114    /**
115     * Undeletes documents (and ancestors if needed to make them visible).
116     * <p>
117     * Also fires async events to undelete the children.
118     *
119     * @param docs the documents to undelete
120     * @return the set of ancestors whose children have been undeleted (for UI notification)
121     */
122    Set<DocumentRef> undeleteDocuments(List<DocumentModel> docs);
123
124    /**
125     * Get all documents from the trash of the current document.
126     *
127     * @since 7.1
128     * @param currentDoc The current/parent document of trash document.
129     * @return All documents in the trash of the current document.
130     */
131    DocumentModelList getDocuments(DocumentModel currentDoc);
132
133    /**
134     * Mangles the name of a document to avoid collisions with non-trashed documents when it's in the trash.
135     *
136     * @param doc the document
137     * @return
138     * @since 7.3
139     */
140    String mangleName(DocumentModel doc);
141
142    /**
143     * Unmangles the name of a document in the trash to find its un-trashed name.
144     *
145     * @param doc the trashed document
146     * @return the unmangled name
147     * @since 7.3
148     */
149    String unmangleName(DocumentModel doc);
150
151}