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