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