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