001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.client.adapters;
013
014import java.io.IOException;
015import java.util.Arrays;
016
017import org.apache.commons.lang.StringUtils;
018import org.nuxeo.ecm.automation.client.Constants;
019import org.nuxeo.ecm.automation.client.OperationRequest;
020import org.nuxeo.ecm.automation.client.Session;
021import org.nuxeo.ecm.automation.client.model.Blob;
022import org.nuxeo.ecm.automation.client.model.Blobs;
023import org.nuxeo.ecm.automation.client.model.DocRef;
024import org.nuxeo.ecm.automation.client.model.Document;
025import org.nuxeo.ecm.automation.client.model.Documents;
026import org.nuxeo.ecm.automation.client.model.FileBlob;
027import org.nuxeo.ecm.automation.client.model.PathRef;
028import org.nuxeo.ecm.automation.client.model.PropertyMap;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public class DocumentService {
034
035    public static final String FetchDocument = "Repository.GetDocument";
036
037    public static final String CreateDocument = "Document.Create";
038
039    public static final String DeleteDocument = "Document.Delete";
040
041    public static final String CopyDocument = "Document.Copy";
042
043    public static final String MoveDocument = "Document.Move";
044
045    public static final String GetDocumentChildren = "Document.GetChildren";
046
047    public static final String GetDocumentChild = "Document.GetChild";
048
049    public static final String GetDocumentParent = "Document.GetParent";
050
051    public static final String Query = "Repository.Query";
052
053    public static final String SetPermission = "Document.AddACE";
054
055    public static final String RemoveAcl = "Document.RemoveACL";
056
057    public static final String SetDocumentState = "Document.FollowLifecycleTransition";
058
059    public static final String LockDocument = "Document.Lock";
060
061    public static final String UnlockDocument = "Document.Unlock";
062
063    public static final String SetProperty = "Document.SetProperty";
064
065    public static final String RemoveProperty = "Document.RemoveProperty";
066
067    public static final String UpdateDocument = "Document.Update";
068
069    public static final String PublishDocument = "Document.Publish";
070
071    public static final String CreateRelation = "Document.AddRelation";
072
073    public static final String GetRelations = "Document.GetLinkedDocuments";
074
075    public static final String SetBlob = "Blob.AttachOnDocument";
076
077    public static final String RemoveBlob = "Blob.RemoveFromDocument";
078
079    public static final String GetBlob = "Document.GetBlob";
080
081    public static final String GetBlobs = "Document.GetBlobsByProperty";
082
083    public static final String CreateVersion = "Document.CreateVersion";
084
085    public static final String FireEvent = "Event.Fire";
086
087    // The following are not yet implemented
088    public static final String CheckOut = "Document.CheckOut";
089
090    public static final String CheckIn = "Document.CheckIn";
091
092    // //TODO GetAcl?
093    protected Session session;
094
095    public DocumentService(Session session) {
096        this.session = session;
097    }
098
099    public Session getSession() {
100        return session;
101    }
102
103    public Document getDocument(String ref) throws IOException {
104        return getDocument(DocRef.newRef(ref), null);
105    }
106
107    /**
108     * @since 5.7
109     * @param document document to fetch
110     * @param schemas schemas related to the document to fetch (* for all)
111     * @return the document returned by server
112     */
113    public Document getDocument(Document document, String... schemas) throws IOException {
114        return getDocument(new DocRef(document.getId()), StringUtils.join(Arrays.asList(schemas), ","));
115    }
116
117    public Document getDocument(DocRef ref) throws IOException {
118        return getDocument(ref, null);
119    }
120
121    public Document getDocument(DocRef ref, String schemas) throws IOException {
122        OperationRequest req = session.newRequest(FetchDocument).set("value", ref);
123        if (schemas != null) {
124            req.setHeader(Constants.HEADER_NX_SCHEMAS, schemas);
125        }
126        return (Document) req.execute();
127    }
128
129    public Document getRootDocument() throws IOException {
130        return getDocument(new PathRef("/"));
131    }
132
133    /**
134     * @since 5.7
135     * @param parent can be PathRef or IdRef
136     * @param document the document to create
137     * @return the document created
138     */
139    public Document createDocument(String parent, Document document) throws IOException {
140        return createDocument(DocRef.newRef(parent), document.getType(), document.getId(), document.getDirties());
141    }
142
143    public Document createDocument(DocRef parent, String type, String name) throws IOException {
144        return createDocument(parent, type, name, null);
145    }
146
147    public Document createDocument(DocRef parent, String type, String name, PropertyMap properties) throws IOException {
148        OperationRequest req = session.newRequest(CreateDocument).setInput(parent).set("type", type).set("name", name);
149        if (properties != null && !properties.isEmpty()) {
150            req.set("properties", properties);
151        }
152        return (Document) req.execute();
153    }
154
155    /**
156     * @since 5.7
157     * @param document the document to remove
158     */
159    public void remove(Document document) throws IOException {
160        remove(new DocRef(document.getId()));
161    }
162
163    public void remove(DocRef doc) throws IOException {
164        session.newRequest(DeleteDocument).setInput(doc).execute();
165    }
166
167    public void remove(String ref) throws IOException {
168        session.newRequest(DeleteDocument).setInput(DocRef.newRef(ref)).execute();
169    }
170
171    public Document copy(DocRef src, DocRef targetParent) throws IOException {
172        return copy(src, targetParent, null);
173    }
174
175    public Document copy(DocRef src, DocRef targetParent, String name) throws IOException {
176        OperationRequest req = session.newRequest(CopyDocument).setInput(src).set("target", targetParent);
177        if (name != null) {
178            req.set("name", name);
179        }
180        return (Document) req.execute();
181    }
182
183    public Document move(DocRef src, DocRef targetParent) throws IOException {
184        return move(src, targetParent, null);
185    }
186
187    public Document move(DocRef src, DocRef targetParent, String name) throws IOException {
188        OperationRequest req = session.newRequest(MoveDocument).setInput(src).set("target", targetParent);
189        if (name != null) {
190            req.set("name", name);
191        }
192        return (Document) req.execute();
193    }
194
195    public Documents getChildren(DocRef docRef) throws IOException {
196        return (Documents) session.newRequest(GetDocumentChildren).setInput(docRef).execute();
197    }
198
199    public Document getChild(DocRef docRef, String name) throws IOException {
200        return (Document) session.newRequest(GetDocumentChild).setInput(docRef).set("name", name).execute();
201    }
202
203    public Document getParent(DocRef docRef) throws IOException {
204        return (Document) session.newRequest(GetDocumentParent).setInput(docRef).execute();
205    }
206
207    public Documents getParent(DocRef docRef, String type) throws IOException {
208        return (Documents) session.newRequest(GetDocumentParent).setInput(docRef).set("type", type).execute();
209    }
210
211    public Documents query(String query) throws IOException {
212        return (Documents) session.newRequest(Query).set("query", query).execute();
213    }
214
215    public Document setPermission(DocRef doc, String user, String permission) throws IOException {
216        return setPermission(doc, user, permission, null, true);
217    }
218
219    public Document setPermission(DocRef doc, String user, String permission, boolean granted) throws IOException {
220        return setPermission(doc, user, permission, null, granted);
221    }
222
223    public Document setPermission(DocRef doc, String user, String permission, String acl, boolean granted)
224            throws IOException {
225        OperationRequest req = session.newRequest(SetPermission).setInput(doc).set("user", user).set("permission",
226                permission).set("grant", granted);
227        if (acl != null) {
228            req.set("acl", acl);
229        }
230        return (Document) req.execute();
231    }
232
233    public Document removeAcl(DocRef doc, String acl) throws IOException {
234        return (Document) session.newRequest(RemoveAcl).setInput(doc).set("acl", acl).execute();
235    }
236
237    public Document setState(DocRef doc, String state) throws IOException {
238        return (Document) session.newRequest(SetDocumentState).setInput(doc).set("value", state).execute();
239    }
240
241    public Document lock(DocRef doc) throws IOException {
242        return lock(doc, null);
243    }
244
245    public Document lock(DocRef doc, String lock) throws IOException {
246        OperationRequest req = session.newRequest(LockDocument).setInput(doc);
247        if (lock != null) {
248            req.set("owner", lock);
249        }
250        return (Document) req.execute();
251    }
252
253    public Document unlock(DocRef doc) throws IOException {
254        return (Document) session.newRequest(UnlockDocument).setInput(doc).execute();
255    }
256
257    // TODO: value Serializable?
258    public Document setProperty(DocRef doc, String key, String value) throws IOException {
259        return (Document) session.newRequest(SetProperty).setInput(doc).set("xpath", key).set("value", value).execute();
260    }
261
262    public Document removeProperty(DocRef doc, String key) throws IOException {
263        return (Document) session.newRequest(RemoveProperty).setInput(doc).set("xpath", key).execute();
264    }
265
266    /**
267     * This method sends the dirty properties to server
268     *
269     * @since 5.7
270     * @param document the document to update
271     * @return the document returned by the server
272     */
273    public Document update(Document document) throws IOException {
274        return update(new DocRef(document.getId()), document.getDirties());
275    }
276
277    public Document update(DocRef doc, PropertyMap properties) throws IOException {
278        return (Document) session.newRequest(UpdateDocument).setInput(doc).set("properties", properties).execute();
279    }
280
281    public Document publish(DocRef doc, DocRef section) throws IOException {
282        return publish(doc, section, true);
283    }
284
285    public Document publish(DocRef doc, DocRef section, boolean override) throws IOException {
286        return (Document) session.newRequest(PublishDocument).setInput(doc).set("target", section).set("override",
287                override).execute();
288    }
289
290    public Document createRelation(DocRef subject, String predicate, DocRef object) throws IOException {
291        return (Document) session.newRequest(CreateRelation).setInput(subject).set("object", object).set("predicate",
292                predicate).execute();
293    }
294
295    public Documents getRelations(DocRef doc, String predicate) throws IOException {
296        return getRelations(doc, predicate, true);
297    }
298
299    public Documents getRelations(DocRef doc, String predicate, boolean outgoing) throws IOException {
300        return (Documents) session.newRequest(GetRelations).setInput(doc).set("predicate", predicate).set("outgoing",
301                outgoing).execute();
302    }
303
304    /**
305     * @since 5.5
306     */
307    public Documents getRelations(DocRef doc, String predicate, boolean outgoing, String graphName) throws IOException {
308        return (Documents) session.newRequest(GetRelations).setInput(doc).set("predicate", predicate).set("outgoing",
309                outgoing).set("graphName", graphName).execute();
310    }
311
312    public void setBlob(DocRef doc, Blob blob) throws IOException {
313        setBlob(doc, blob, null);
314    }
315
316    public void setBlob(DocRef doc, Blob blob, String xpath) throws IOException {
317        OperationRequest req = session.newRequest(SetBlob).setInput(blob).set("document", doc);
318        if (xpath != null) {
319            req.set("xpath", xpath);
320        }
321        req.setHeader(Constants.HEADER_NX_VOIDOP, "true");
322        req.execute();
323    }
324
325    public void removeBlob(DocRef doc) throws IOException {
326        removeBlob(doc, null);
327    }
328
329    public void removeBlob(DocRef doc, String xpath) throws IOException {
330        OperationRequest req = session.newRequest(RemoveBlob).setInput(doc);
331        if (xpath != null) {
332            req.set("xpath", xpath);
333        }
334        req.setHeader(Constants.HEADER_NX_VOIDOP, "true");
335        req.execute();
336    }
337
338    public FileBlob getBlob(DocRef doc) throws IOException {
339        return getBlob(doc, null);
340    }
341
342    public FileBlob getBlob(DocRef doc, String xpath) throws IOException {
343        OperationRequest req = session.newRequest(GetBlob).setInput(doc);
344        if (xpath != null) {
345            req.set("xpath", xpath);
346        }
347        return (FileBlob) req.execute();
348    }
349
350    public Blobs getBlobs(DocRef doc) throws IOException {
351        return getBlobs(doc, null);
352    }
353
354    public Blobs getBlobs(DocRef doc, String xpath) throws IOException {
355        OperationRequest req = session.newRequest(GetBlobs).setInput(doc);
356        if (xpath != null) {
357            req.set("xpath", xpath);
358        }
359        return (Blobs) req.execute();
360    }
361
362    public Document createVersion(DocRef doc) throws IOException {
363        return createVersion(doc, null);
364    }
365
366    /**
367     * Increment is one of "None", "Major", "Minor". If null the server default will be used. See
368     * {@link VersionIncrement}
369     */
370    public Document createVersion(DocRef doc, String increment) throws IOException {
371        OperationRequest req = session.newRequest(CreateVersion).setInput(doc);
372        if (increment != null) {
373            req.set("increment", increment);
374        }
375        return (Document) req.execute();
376    }
377
378    public void fireEvent(String event) throws IOException {
379        fireEvent(null, event);
380    }
381
382    public void fireEvent(DocRef doc, String event) throws IOException {
383        OperationRequest req = session.newRequest(CreateVersion).setInput(doc);
384        req.setHeader(Constants.HEADER_NX_VOIDOP, "true");
385        req.execute();
386    }
387}