-
Notifications
You must be signed in to change notification settings - Fork 16
/
nuxeo-audit-page-provider.js
356 lines (318 loc) · 8.71 KB
/
nuxeo-audit-page-provider.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
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
/**
@license
(C) Copyright Nuxeo Corp. (http://nuxeo.com/)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
import { Debouncer } from '@polymer/polymer/lib/utils/debounce.js';
import { timeOut } from '@polymer/polymer/lib/utils/async.js';
import './nuxeo-element.js';
import './nuxeo-operation.js';
import './nuxeo-resource.js';
{
/**
* `nuxeo-audit-page-provider` performs an audit query with the given parameters against audit logs.
* If the property docId is set, the provider DOCUMENT_HISTORY_PROVIDER is used with docId as queryParameter.
* If the property docId is not set, the EVENTS_VIEW provider is used instead.
*
* <nuxeo-audit-page-provider
* current-page="{{entries}}" params="{...}" page-size="40'">
* </nuxeo-audit-page-provider>
*
* <nuxeo-audit-page-provider
* current-page="{{entries}}" docId="..." page-size="40'">
* </nuxeo-audit-page-provider>
*
* @memberof Nuxeo
*/
class AuditPageProvider extends Nuxeo.Element {
static get template() {
return html`
<style>
:host {
display: none;
}
</style>
<nuxeo-resource
id="res"
path="/id/[[docId]]/@audit"
enrichers="{{enrichers}}"
schemas="[[schemas]]"
loading="{{loading}}"
headers="{{headers}}"
>
</nuxeo-resource>
<nuxeo-operation
id="auditOp"
op="Audit.QueryWithPageProvider"
enrichers="{{enrichers}}"
schemas="[[schemas]]"
loading="{{loading}}"
headers="{{headers}}"
on-error="_preventPropagation"
>
</nuxeo-operation>
`;
}
static get is() {
return 'nuxeo-audit-page-provider';
}
static get properties() {
return {
/**
* The id of a nuxeo-connection to use.
*/
connectionId: {
type: String,
value: '',
},
/**
* If true, automatically execute the operation when either `docId` or `params` change.
*/
auto: {
type: Boolean,
value: false,
},
/**
* The delay in milliseconds to debounce the auto fetch call when `docId`, `params`, etc. changes.
*/
autoDelay: {
type: Number,
value: 300,
},
/**
* The query parameters object.
*/
params: {
type: Object,
value: {},
},
/**
* The document id to retrieve the history from.
* When set, the provider DOCUMENT_HISTORY_PROVIDER is used.
*/
docId: {
type: String,
},
/**
* The number of results per page.
*/
pageSize: {
type: Number,
value: -1,
},
/**
* The current page.
*/
page: {
type: Number,
value: 1,
},
/**
* The current page entries.
*/
currentPage: {
type: Array,
value: [],
notify: true,
},
/**
* Map of properties and direction 'asc' / 'desc'
*/
sort: {
type: Object,
value: {},
notify: true,
},
/**
* Total number of pages.
*/
numberOfPages: {
type: Number,
notify: true,
},
/**
* Total number of results.
*/
resultsCount: {
type: Number,
notify: true,
},
/**
* Returns true if a next page is available.
*/
isNextPageAvailable: {
type: Boolean,
value: false,
notify: true,
},
/**
* Current page's size
*/
currentPageSize: {
type: Number,
notify: true,
},
/**
* The `content enricher` of the resource.
* Can be an object with entity type as keys or list or string (which defaults to `document` entity type).
*/
enrichers: {
type: Object,
value: {},
},
/**
* List of comma separated values of the document schemas to be returned.
* All document schemas are returned by default.
*/
schemas: {
type: String,
},
/**
* The headers of the request.
* 'Accept': 'text/plain,application/json' is already set by default.
*/
headers: {
type: Object,
value: null,
},
/**
* True while requests are in flight.
*/
loading: {
type: Boolean,
notify: true,
readOnly: true,
},
};
}
static get observers() {
return ['_resetAndAutoFetch(params.*, docId, pageSize, sort)', '_autoFetch(page)'];
}
/**
* Fired when the current page is fetched.
*
* @event update
*/
/**
* Stringifies the elements of a given object
*/
_stringifyJSONObject(input) {
const result = input;
if (input !== null) {
Object.keys(input).forEach((key) => {
if (typeof input[key] === 'string') {
result[key] = input[key];
} else {
result[key] = JSON.stringify(input[key]);
}
});
}
return result;
}
/**
* Fetch the currentPage.
* @method fetch
*/
fetch() {
return this._isForDoc ? this._fetchRes() : this._fetchOp();
}
_fetchOp() {
const params = {
providerName: 'EVENTS_VIEW',
namedQueryParams: this._stringifyJSONObject(this.params),
currentPageIndex: this.page - 1,
pageSize: this.pageSize,
};
return this._fetch(this.$.auditOp, params);
}
_fetchRes() {
const params = {};
if (this.params.startDate) {
params.startEventDate = this.params.startDate;
}
if (this.params.endDate) {
params.endEventDate = this.params.endDate;
}
if (this.params.eventCategory) {
params.category = this.params.eventCategory;
}
if (this.params.eventIds) {
params.eventId = this.params.eventIds;
}
if (this.params.principalName) {
params.principalName = this.params.principalName;
}
params.currentPageIndex = this.page - 1;
params.pageSize = this.pageSize;
return this._fetch(this.$.res, params);
}
_fetch(exec, params) {
if (this._sortKeys.length > 0) {
params.sortBy = this._sortKeys.join(',');
params.sortOrder = this._sortValues.join(',');
}
exec.params = params;
return exec
.execute()
.then((response) => {
this.currentPage = response.entries.slice(0);
this.numberOfPages = response.numberOfPages;
this.resultsCount = response.resultsCount;
this.isNextPageAvailable = response.isNextPageAvailable;
this.currentPageSize = response.currentPageSize;
this.dispatchEvent(
new CustomEvent('update', {
bubbles: true,
composed: true,
}),
);
return response;
})
.catch((error) => {
this.dispatchEvent(
new CustomEvent('notify', {
composed: true,
bubbles: true,
detail: {
error,
},
}),
);
throw error;
});
}
get _sortKeys() {
return Object.keys(this.sort);
}
get _sortValues() {
return this._sortKeys.map((k) => this.sort[k]);
}
get _isForDoc() {
return this.docId && this.docId.length > 0;
}
_resetAndAutoFetch() {
this.page = 1;
this._autoFetch();
}
_autoFetch() {
if (this.auto) {
// debounce in case of multiple param changes
this._debouncer = Debouncer.debounce(this._debouncer, timeOut.after(this.autoDelay), () => this.fetch());
}
}
_preventPropagation(e) {
e.stopPropagation();
}
}
customElements.define(AuditPageProvider.is, AuditPageProvider);
Nuxeo.AuditPageProvider = AuditPageProvider;
}