/
DocumentService.java
424 lines (340 loc) · 14.3 KB
/
DocumentService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* bstefanescu
*/
package org.nuxeo.ecm.automation.client.adapters;
import java.util.Arrays;
import org.apache.commons.lang.StringUtils;
import org.nuxeo.ecm.automation.client.Constants;
import org.nuxeo.ecm.automation.client.OperationRequest;
import org.nuxeo.ecm.automation.client.Session;
import org.nuxeo.ecm.automation.client.model.Blob;
import org.nuxeo.ecm.automation.client.model.Blobs;
import org.nuxeo.ecm.automation.client.model.DocRef;
import org.nuxeo.ecm.automation.client.model.Document;
import org.nuxeo.ecm.automation.client.model.Documents;
import org.nuxeo.ecm.automation.client.model.FileBlob;
import org.nuxeo.ecm.automation.client.model.PathRef;
import org.nuxeo.ecm.automation.client.model.PropertyMap;
/**
* @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
*/
public class DocumentService {
public static final String FetchDocument = "Document.Fetch";
public static final String CreateDocument = "Document.Create";
public static final String DeleteDocument = "Document.Delete";
public static final String CopyDocument = "Document.Copy";
public static final String MoveDocument = "Document.Move";
public static final String GetDocumentChildren = "Document.GetChildren";
public static final String GetDocumentChild = "Document.GetChild";
public static final String GetDocumentParent = "Document.GetParent";
public static final String Query = "Document.Query";
public static final String SetPermission = "Document.SetACE";
public static final String RemoveAcl = "Document.RemoveACL";
public static final String SetDocumentState = "Document.SetLifeCycle";
public static final String LockDocument = "Document.Lock";
public static final String UnlockDocument = "Document.Unlock";
public static final String SetProperty = "Document.SetProperty";
public static final String RemoveProperty = "Document.RemoveProperty";
public static final String UpdateDocument = "Document.Update";
public static final String PublishDocument = "Document.Publish";
public static final String CreateRelation = "Relations.CreateRelation";
public static final String GetRelations = "Relations.GetRelations";
public static final String SetBlob = "Blob.Attach";
public static final String RemoveBlob = "Blob.Remove";
public static final String GetBlob = "Blob.Get";
public static final String GetBlobs = "Blob.GetList";
public static final String CreateVersion = "Document.CreateVersion";
public static final String FireEvent = "Notification.SendEvent";
// The following are not yet implemented
public static final String CheckOut = "Document.CheckOut";
public static final String CheckIn = "Document.CheckIn";
// //TODO GetAcl?
protected Session session;
public DocumentService(Session session) {
this.session = session;
}
public Session getSession() {
return session;
}
public Document getDocument(String ref) throws Exception {
return getDocument(DocRef.newRef(ref), null);
}
/**
* @since 5.7
* @param document document to fetch
* @param schemas schemas related to the document to fetch (* for all)
* @return the document returned by server
* @throws Exception
*/
public Document getDocument(Document document, String... schemas)
throws Exception {
return getDocument(new DocRef(document.getId()),
StringUtils.join(Arrays.asList(schemas), ","));
}
public Document getDocument(DocRef ref) throws Exception {
return getDocument(ref, null);
}
public Document getDocument(DocRef ref, String schemas) throws Exception {
OperationRequest req = session.newRequest(FetchDocument).set("value",
ref);
if (schemas != null) {
req.setHeader(Constants.HEADER_NX_SCHEMAS, schemas);
}
return (Document) req.execute();
}
public Document getRootDocument() throws Exception {
return getDocument(new PathRef("/"));
}
/**
* @since 5.7
* @param parent can be PathRef or IdRef
* @param document the document to create
* @return the document created
* @throws Exception
*/
public Document createDocument(String parent, Document document)
throws Exception {
return createDocument(DocRef.newRef(parent), document.getType(),
document.getId(), document.getDirties());
}
public Document createDocument(DocRef parent, String type, String name)
throws Exception {
return createDocument(parent, type, name, null);
}
public Document createDocument(DocRef parent, String type, String name,
PropertyMap properties) throws Exception {
OperationRequest req = session.newRequest(CreateDocument).setInput(
parent).set("type", type).set("name", name);
if (properties != null && !properties.isEmpty()) {
req.set("properties", properties);
}
return (Document) req.execute();
}
/**
* @since 5.7
* @param document the document to remove
* @throws Exception
*/
public void remove(Document document) throws Exception {
remove(new DocRef(document.getId()));
}
public void remove(DocRef doc) throws Exception {
session.newRequest(DeleteDocument).setInput(doc).execute();
}
public void remove(String ref) throws Exception {
session.newRequest(DeleteDocument).setInput(DocRef.newRef(ref)).execute();
}
public Document copy(DocRef src, DocRef targetParent) throws Exception {
return copy(src, targetParent, null);
}
public Document copy(DocRef src, DocRef targetParent, String name)
throws Exception {
OperationRequest req = session.newRequest(CopyDocument).setInput(src).set(
"target", targetParent);
if (name != null) {
req.set("name", name);
}
return (Document) req.execute();
}
public Document move(DocRef src, DocRef targetParent) throws Exception {
return move(src, targetParent, null);
}
public Document move(DocRef src, DocRef targetParent, String name)
throws Exception {
OperationRequest req = session.newRequest(MoveDocument).setInput(src).set(
"target", targetParent);
if (name != null) {
req.set("name", name);
}
return (Document) req.execute();
}
public Documents getChildren(DocRef docRef) throws Exception {
return (Documents) session.newRequest(GetDocumentChildren).setInput(
docRef).execute();
}
public Document getChild(DocRef docRef, String name) throws Exception {
return (Document) session.newRequest(GetDocumentChild).setInput(docRef).set(
"name", name).execute();
}
public Document getParent(DocRef docRef) throws Exception {
return (Document) session.newRequest(GetDocumentParent).setInput(docRef).execute();
}
public Documents getParent(DocRef docRef, String type) throws Exception {
return (Documents) session.newRequest(GetDocumentParent).setInput(
docRef).set("type", type).execute();
}
public Documents query(String query) throws Exception {
return (Documents) session.newRequest(Query).set("query", query).execute();
}
public Document setPermission(DocRef doc, String user, String permission)
throws Exception {
return setPermission(doc, user, permission, null, true);
}
public Document setPermission(DocRef doc, String user, String permission,
boolean granted) throws Exception {
return setPermission(doc, user, permission, null, granted);
}
public Document setPermission(DocRef doc, String user, String permission,
String acl, boolean granted) throws Exception {
OperationRequest req = session.newRequest(SetPermission).setInput(doc).set(
"user", user).set("permission", permission).set("grant",
granted);
if (acl != null) {
req.set("acl", acl);
}
return (Document) req.execute();
}
public Document removeAcl(DocRef doc, String acl) throws Exception {
return (Document) session.newRequest(RemoveAcl).setInput(doc).set(
"acl", acl).execute();
}
public Document setState(DocRef doc, String state) throws Exception {
return (Document) session.newRequest(SetDocumentState).setInput(doc).set(
"value", state).execute();
}
public Document lock(DocRef doc) throws Exception {
return lock(doc, null);
}
public Document lock(DocRef doc, String lock) throws Exception {
OperationRequest req = session.newRequest(LockDocument).setInput(doc);
if (lock != null) {
req.set("owner", lock);
}
return (Document) req.execute();
}
public Document unlock(DocRef doc) throws Exception {
return (Document) session.newRequest(UnlockDocument).setInput(doc).execute();
}
// TODO: value Serializable?
public Document setProperty(DocRef doc, String key, String value)
throws Exception {
return (Document) session.newRequest(SetProperty).setInput(doc).set(
"xpath", key).set("value", value).execute();
}
public Document removeProperty(DocRef doc, String key) throws Exception {
return (Document) session.newRequest(RemoveProperty).setInput(doc).set(
"xpath", key).execute();
}
/**
* This method sends the dirty properties to server
*
* @since 5.7
* @param document the document to update
* @return the document returned by the server
* @throws Exception
*/
public Document update(Document document) throws Exception {
return update(new DocRef(document.getId()), document.getDirties());
}
public Document update(DocRef doc, PropertyMap properties) throws Exception {
return (Document) session.newRequest(UpdateDocument).setInput(doc).set(
"properties", properties).execute();
}
public Document publish(DocRef doc, DocRef section) throws Exception {
return publish(doc, section, true);
}
public Document publish(DocRef doc, DocRef section, boolean override)
throws Exception {
return (Document) session.newRequest(PublishDocument).setInput(doc).set(
"target", section).set("override", override).execute();
}
public Document createRelation(DocRef subject, String predicate,
DocRef object) throws Exception {
return (Document) session.newRequest(CreateRelation).setInput(subject).set(
"object", object).set("predicate", predicate).execute();
}
public Documents getRelations(DocRef doc, String predicate)
throws Exception {
return getRelations(doc, predicate, true);
}
public Documents getRelations(DocRef doc, String predicate, boolean outgoing)
throws Exception {
return (Documents) session.newRequest(GetRelations).setInput(doc).set(
"predicate", predicate).set("outgoing", outgoing).execute();
}
/**
* @since 5.5
*/
public Documents getRelations(DocRef doc, String predicate,
boolean outgoing, String graphName) throws Exception {
return (Documents) session.newRequest(GetRelations).setInput(doc).set(
"predicate", predicate).set("outgoing", outgoing).set(
"graphName", graphName).execute();
}
public void setBlob(DocRef doc, Blob blob) throws Exception {
setBlob(doc, blob, null);
}
public void setBlob(DocRef doc, Blob blob, String xpath) throws Exception {
OperationRequest req = session.newRequest(SetBlob).setInput(blob).set(
"document", doc);
if (xpath != null) {
req.set("xpath", xpath);
}
req.setHeader(Constants.HEADER_NX_VOIDOP, "true");
req.execute();
}
public void removeBlob(DocRef doc) throws Exception {
removeBlob(doc, null);
}
public void removeBlob(DocRef doc, String xpath) throws Exception {
OperationRequest req = session.newRequest(RemoveBlob).setInput(doc);
if (xpath != null) {
req.set("xpath", xpath);
}
req.setHeader(Constants.HEADER_NX_VOIDOP, "true");
req.execute();
}
public FileBlob getBlob(DocRef doc) throws Exception {
return getBlob(doc, null);
}
public FileBlob getBlob(DocRef doc, String xpath) throws Exception {
OperationRequest req = session.newRequest(GetBlob).setInput(doc);
if (xpath != null) {
req.set("xpath", xpath);
}
return (FileBlob) req.execute();
}
public Blobs getBlobs(DocRef doc) throws Exception {
return getBlobs(doc, null);
}
public Blobs getBlobs(DocRef doc, String xpath) throws Exception {
OperationRequest req = session.newRequest(GetBlobs).setInput(doc);
if (xpath != null) {
req.set("xpath", xpath);
}
return (Blobs) req.execute();
}
public Document createVersion(DocRef doc) throws Exception {
return createVersion(doc, null);
}
/**
* Increment is one of "None", "Major", "Minor". If null the server default
* will be used.
*
* See {@link VersionIncrement}
*/
public Document createVersion(DocRef doc, String increment)
throws Exception {
OperationRequest req = session.newRequest(CreateVersion).setInput(doc);
if (increment != null) {
req.set("increment", increment);
}
return (Document) req.execute();
}
public void fireEvent(String event) throws Exception {
fireEvent(null, event);
}
public void fireEvent(DocRef doc, String event) throws Exception {
OperationRequest req = session.newRequest(CreateVersion).setInput(doc);
req.setHeader(Constants.HEADER_NX_VOIDOP, "true");
req.execute();
}
}