001/*
002 * (C) Copyright 2006-2014 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 *     Bogdan Stefanescu
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.core.model;
021
022import java.io.Serializable;
023import java.util.List;
024import java.util.Map;
025
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentNotFoundException;
028import org.nuxeo.ecm.core.api.IterableQueryResult;
029import org.nuxeo.ecm.core.api.PartialList;
030import org.nuxeo.ecm.core.api.VersionModel;
031import org.nuxeo.ecm.core.api.security.ACP;
032import org.nuxeo.ecm.core.query.QueryFilter;
033
034/**
035 * Internal Session accessing the low-level storage.
036 */
037public interface Session {
038
039    // parameters for the session contexts
040    String USER_NAME = "username";
041
042    /**
043     * Gets the repository that created this session.
044     *
045     * @return the repository
046     */
047    String getRepositoryName();
048
049    /**
050     * Does a query.
051     *
052     * @since 5.9.4
053     */
054    PartialList<Document> query(String query, String queryType, QueryFilter queryFilter, long countUpTo);
055
056    /**
057     * Does a query and fetch the individual results as maps.
058     */
059    IterableQueryResult queryAndFetch(String query, String queryType, QueryFilter queryFilter,
060            boolean distinctDocuments, Object[] params);
061
062    /**
063     * Gets the lock manager for this session.
064     *
065     * @return the lock manager
066     * @since 7.4
067     */
068    LockManager getLockManager();
069
070    /**
071     * Saves this session.
072     */
073    void save();
074
075    /**
076     * Checks whether the session is alive.
077     *
078     * @return true if the session is closed, false otherwise
079     */
080    boolean isLive();
081
082    /**
083     * Returns {@code true} if all sessions in the current thread share the same state.
084     */
085    boolean isStateSharedByAllThreadSessions();
086
087    /**
088     * Closes this session. Does not save.
089     */
090    void close();
091
092    /**
093     * Gets the document at the given path, if any.
094     *
095     * @param path
096     * @return
097     * @throws DocumentNotFoundException if the document doesn't exist
098     */
099    Document resolvePath(String path) throws DocumentNotFoundException;
100
101    /**
102     * Gets a document given its ID.
103     *
104     * @param uuid the document id
105     * @return the document
106     * @throws DocumentNotFoundException if the document doesn't exist
107     */
108    Document getDocumentByUUID(String uuid) throws DocumentNotFoundException;
109
110    /**
111     * Gets the root document in this repository.
112     *
113     * @return the root document
114     */
115    Document getRootDocument();
116
117    /**
118     * Gets the null document, to be used as a fake parent to add placeless children.
119     *
120     * @return the null document
121     */
122    Document getNullDocument();
123
124    /**
125     * Copies the source document to the given folder.
126     * <p>
127     * If the destination document is not a folder, an exception is thrown.
128     *
129     * @param src
130     * @param dst
131     * @param name
132     */
133    Document copy(Document src, Document dst, String name);
134
135    /**
136     * Moves the source document to the given folder.
137     * <p>
138     * If the destination document is not a folder an exception is thrown.
139     *
140     * @param src the source document to move
141     * @param dst the destination folder
142     * @param name the new name of the document or null if the original name should be preserved
143     */
144    Document move(Document src, Document dst, String name);
145
146    /**
147     * Creates a generic proxy to the given document inside the given folder.
148     *
149     * @param doc the document
150     * @param folder the folder
151     * @return the proxy
152     */
153    Document createProxy(Document doc, Document folder);
154
155    /**
156     * Finds the proxies for a document. If the folder is not null, the search will be limited to its children.
157     * <p>
158     * If the document is a version, then only proxies to that version will be looked up.
159     *
160     * @param doc the document or version
161     * @param folder the folder, or null
162     * @return the list of proxies if any is found otherwise an empty list
163     * @since 1.4.1 for the case where doc is a proxy
164     */
165    List<Document> getProxies(Document doc, Document folder);
166
167    /**
168     * Sets a proxies' target.
169     * <p>
170     * The target must have the same version series as the proxy.
171     *
172     * @param proxy the proxy
173     * @param target the new target
174     * @since 5.5
175     */
176    void setProxyTarget(Document proxy, Document target);
177
178    /**
179     * Imports a document with a given id and parent.
180     * <p>
181     * The document can then be filled with the normal imported properties.
182     *
183     * @param uuid the document uuid
184     * @param parent the document parent, or {@code null} for a version
185     * @param name the document name in its parent
186     * @param typeName the document type, or {@code ecm:proxy} for a proxy
187     * @param properties system properties of the document, which will vary depending whether it's a live document, a
188     *            version or a proxy (see the various {@code IMPORT_*} constants of {@link CoreSession})
189     * @return a writable {@link Document}, even for proxies and versions
190     */
191    Document importDocument(String uuid, Document parent, String name, String typeName,
192            Map<String, Serializable> properties);
193
194    /**
195     * Gets a version of a document, given its versionable id and label.
196     * <p>
197     * The version model contains the label of the version to look for. On return, it is filled with the version's
198     * description and creation date.
199     *
200     * @param versionableId the versionable id
201     * @param versionModel the version model
202     * @return the version, or {@code null} if not found
203     */
204    Document getVersion(String versionableId, VersionModel versionModel);
205
206    /**
207     * Returns {@code true} if negative ACLs are allowed.
208     * <p>
209     * Negative ACLs are ACLs that include an ACE with a deny (isGranted=false). This does not include the full-blocking
210     * ACE for Everyone/Everything, which is always allowed.
211     *
212     * @return {@code true} if negative ACLs are allowed
213     * @since 6.0
214     */
215    boolean isNegativeAclAllowed();
216
217    ACP getMergedACP(Document doc);
218
219    void setACP(Document doc, ACP acp, boolean overwrite);
220
221    /**
222     * Gets the fulltext extracted from the binary fields.
223     *
224     * @since 5.9.3
225     */
226    Map<String, String> getBinaryFulltext(String id);
227
228}