This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
restapi.js
329 lines (277 loc) · 9.08 KB
/
restapi.js
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
var RestAPI = {};
// Root Document
var documentRoot = "/default-domain/workspaces/workspace";
////////////////////////////// NUXEO JS CONFIGURATION
// Configure Nuxeo Client
RestAPI.config = function () {
//Instantiate Nuxeo Client
var client = new nuxeo.Client({
baseURL: 'http://localhost:8080/nuxeo',
username: 'Administrator',
password: 'Administrator'
})
// Client schema and timeout configuration
client.schema("dublincore");
client.timeout(3000);
return client;
}
////////////////////////////// CURRENT USER
// Callback: callbackCurrentUser
RestAPI.getCurrentUser = function () {
this.client.request('user/Administrator').get(callbackCurrentUser);
}
////////////////////////////// EXECUTE QUERY
// Callback: callbackQuery
RestAPI.executeQuery = function (query) {
this.client.request('query/?query=' + query).get(callbackQuery);
}
////////////////////////////// DISPLAY WORKSPACE CHILDREN
// Callback: callbackRootChildren
RestAPI.getRootChildren = function () {
this.client.document(documentRoot).children(callbackRootChildren);
}
////////////////////////////// DISPLAY DOCUMENT PROPERTIES
// Callback: callbackFetchDocument
RestAPI.fetchDocument = function (id) {
this.client.document(id).fetch(callbackFetchDocument);
//this.client.document(id).schemas(["common","dublincore"]).fetch(callbackFetchDocument);
//this.client.document(id).header('X-NXContext-Category', 'acls').fetch(callbackFetchDocument);
}
////////////////////////////// UPDATE DOCUMENT
// Callback: callbackUpdateDocument
RestAPI.updateDocument = function (map) {
this.currentDocument.set(map).save(callbackUpdateDocument)
}
////////////////////////////// CREATE DOCUMENT
// Callback: callbackCreateDocument
RestAPI.createDocument = function (map) {
this.client.document(documentRoot)
.create({
type: 'File',
name: map["dc:title"],
properties: {
"dc:title": map["dc:title"],
"dc:description": map["dc:description"],
"dc:nature": map["dc:nature"],
"dc:language": map["dc:language"]
}
}, callbackCreateDocument);
}
////////////////////////////// DELETE DOCUMENT
RestAPI.deleteDocument = function () {
this.currentDocument.delete(callbackDeleteDocument);
}
////////////////////////////// FILE IMPORT
// Callback: callbackImportFile
RestAPI.importFile = function (file) {
var uploader = this.client.operation("FileManager.Import").context({ currentDocument: documentRoot }).uploader();
uploader.uploadFile(file, function () {
uploader.execute(callbackImportFile);
});
}
////////////////////////////// ATTACH BLOB
// Callback: callbackAttachBlob
RestAPI.attachBlob = function (file) {
var uploader = this.client.operation("Blob.Attach")
.params({ document: this.currentDocument.uid, save: true,
xpath: "file:content"}).uploader();
uploader.uploadFile(file, function () {
uploader.execute(callbackAttachBlob)
});
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////// DO NOT EDIT THE CODE BELOW //////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
function callbackCreateDocument(error, file) {
if (error) {
throwError("Cannot create document -> " + error);
throw error;
}
console.log('Created ' + file.title + ' file')
location.reload();
}
function importFiles(files) {
for (var i = 0; i < files.length; i++) {
RestAPI.importFile(files[i]);
}
}
function callbackDeleteDocument(error, data) {
if (error) {
throwError("Cannot delete document -> " + error);
throw error;
}
location.reload();
console.log("Document has been deleted");
}
function callbackAttachBlob(error, data) {
$('#modalBlob').modal('hide');
if (error) {
throwError("Cannot attach blob -> " + error);
throw error;
}
console.log("Blob has been attached");
}
function callbackImportFile(error, data) {
if (error) {
throwError("Cannot import files -> " + error);
throw error;
}
location.reload();
console.log("Files has been imported");
}
function save() {
var map = new Object();
$("#docForm :input").each(function () {
var name = $(this).attr('name');
var input = $(this).val();
map[name] = input;
});
RestAPI.updateDocument(map);
}
function callbackCurrentUser(error, user) {
if (error) {
throwError("Cannot get user -> " + error);
throw error;
}
console.log(user);
var template = $('#userId').html();
Mustache.parse(template);
var rendered = Mustache.render(template, {userId: user.id});
$('#targetUserId').html(rendered);
template = $('#userInfos').html();
Mustache.parse(template);
rendered = Mustache.render(template, {user: user});
$('#targetUserInfos').html(rendered);
}
function callbackRootChildren(error, children) {
if (error) {
throwError("Cannot fetch documents -> " + error);
throw error;
}
console.log('Root document has ' + children.entries.length + ' children');
var template = $('#children').html();
Mustache.parse(template);
var rendered = Mustache.render(template, {children: children.entries});
$('#targetChildren').html(rendered);
}
function callbackQuery(error, result) {
if (error) {
throwError("Cannot run query -> " + error);
throw error;
}
console.log('Query has ' + result.entries.length + ' results');
var template = $('#children').html();
Mustache.parse(template);
var rendered = Mustache.render(template, {children: result.entries});
$('#targetChildren').html(rendered);
}
function callbackFetchDocument(error, doc) {
if (error) {
throwError("Cannot display document metadata -> " + error);
throw error;
}
RestAPI.currentDocument = doc;
if (doc.contextParameters.acls != undefined) {
var ace = doc.contextParameters.acls[0].ace;
}
console.log('Selecting ' + doc.title);
var template = $('#displayMetadata').html();
Mustache.parse(template);
var rendered = Mustache.render(template, {doc: doc, ace: ace});
$('#displayForm').html(rendered);
template = $('#editionMetadata').html();
Mustache.parse(template);
rendered = Mustache.render(template, {doc: doc});
$('#editionForm').html(rendered);
}
function callbackUpdateDocument(error, doc) {
if (error) {
throwError("Cannot display document metadata -> " + error);
throw error;
}
RestAPI.currentDocument = doc;
if (doc.contextParameters.acls != undefined) {
var ace = doc.contextParameters.acls[0].ace;
}
console.log('Selecting ' + doc.title);
var template = $('#displayMetadata').html();
Mustache.parse(template);
var rendered = Mustache.render(template, {doc: doc, ace: ace});
$('#displayForm').html(rendered);
template = $('#editionMetadata').html();
Mustache.parse(template);
rendered = Mustache.render(template, {doc: doc});
$('#editionForm').html(rendered);
toggle();
}
// Show modal for error
function throwError(error) {
$('#errorValue').append(error);
$('#modalError').modal('show');
}
// Show modal confirm of deletion
function displayDelete() {
$('#modalDelete').modal('show');
}
// Show modal for attaching blob
function displayBlob() {
$('#modalBlob').modal('show');
}
// Toggle edit form
function toggle() {
$('#editionForm').toggle();
$('#displayForm').toggle();
}
$(document)
.ready(function () {
//Build Nuxeo Client
RestAPI.client = RestAPI.config();
// Call and display default domain children
RestAPI.getRootChildren();
// Call and display current user
RestAPI.getCurrentUser();
// Call query when clicking on search
$('#search').click(function () {
RestAPI.executeQuery($('#searchInput').val());
});
$('#importAction').click(function () {
$('#modalImport').modal('show');
});
$('#importFiles').click(function () {
console.log("Files size: " + $('#files')[0].files.length);
importFiles($('#files')[0].files);
});
$('attachBlob').click(function () {
$('#blobModal').modal('show');
})
$('#importBlob').click(function () {
RestAPI.attachBlob($('#blob')[0].files[0]);
});
$('#refresh').click(function () {
location.reload();
});
$('#deleteDoc').click(function () {
RestAPI.deleteDocument();
});
$('#create').click(function () {
$('#modalCreate').modal('show');
});
$('#createDoc').click(function () {
var map = new Object();
$("#createDocForm :input").each(function () {
var name = $(this).attr('name');
var input = $(this).val();
map[name] = input;
});
RestAPI.createDocument(map);
});
}
)
;
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////// DO NOT EDIT THE CODE ABOVE //////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////