forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathitem-data.service.ts
More file actions
519 lines (468 loc) · 21.2 KB
/
item-data.service.ts
File metadata and controls
519 lines (468 loc) · 21.2 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
/* eslint-disable max-classes-per-file */
import { HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { RestRequestMethod } from '@dspace/config/rest-request-method';
import {
hasValue,
isNotEmpty,
isNotEmptyOperator,
} from '@dspace/shared/utils/empty.util';
import { Operation } from 'fast-json-patch';
import { Observable } from 'rxjs';
import {
distinctUntilChanged,
filter,
find,
map,
switchMap,
take,
} from 'rxjs/operators';
import { validate as uuidValidate } from 'uuid';
import { BrowseService } from '../browse/browse.service';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { RequestParam } from '../cache/models/request-param.model';
import { ObjectCacheService } from '../cache/object-cache.service';
import { HttpOptions } from '../dspace-rest/dspace-rest.service';
import { NotificationsService } from '../notification-system/notifications.service';
import { Bundle } from '../shared/bundle.model';
import { Collection } from '../shared/collection.model';
import { ExternalSourceEntry } from '../shared/external-source-entry.model';
import { FollowLinkConfig } from '../shared/follow-link-config.model';
import { GenericConstructor } from '../shared/generic-constructor';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { Item } from '../shared/item.model';
import { MetadataMap } from '../shared/metadata.models';
import { NoContent } from '../shared/NoContent.model';
import { sendRequest } from '../shared/request.operators';
import { PaginatedSearchOptions } from '../shared/search/models/paginated-search-options.model';
import { URLCombiner } from '../url-combiner/url-combiner';
import {
CreateData,
CreateDataImpl,
} from './base/create-data';
import {
DeleteData,
DeleteDataImpl,
} from './base/delete-data';
import {
ConstructIdEndpoint,
IdentifiableDataService,
} from './base/identifiable-data.service';
import {
PatchData,
PatchDataImpl,
} from './base/patch-data';
import { SearchDataImpl } from './base/search-data';
import { BundleDataService } from './bundle-data.service';
import { DSOChangeAnalyzer } from './dso-change-analyzer.service';
import { FindListOptions } from './find-list-options.model';
import { PaginatedList } from './paginated-list.model';
import { ResponseParsingService } from './parsing.service';
import { RemoteData } from './remote-data';
import {
DeleteRequest,
GetRequest,
PostRequest,
PutRequest,
} from './request.models';
import { RequestService } from './request.service';
import { RestRequest } from './rest-request.model';
import { StatusCodeOnlyResponseParsingService } from './status-code-only-response-parsing.service';
/**
* An abstract service for CRUD operations on Items
* Doesn't specify an endpoint because multiple endpoints support Item-like functionality (e.g. items, itemtemplates)
* Extend this class to implement data services for Items
*/
export abstract class BaseItemDataService extends IdentifiableDataService<Item> implements CreateData<Item>, PatchData<Item>, DeleteData<Item> {
private createData: CreateData<Item>;
private patchData: PatchData<Item>;
private deleteData: DeleteData<Item>;
private searchData: SearchDataImpl<Item>;
protected constructor(
protected linkPath,
protected requestService: RequestService,
protected rdbService: RemoteDataBuildService,
protected objectCache: ObjectCacheService,
protected halService: HALEndpointService,
protected notificationsService: NotificationsService,
protected comparator: DSOChangeAnalyzer<Item>,
protected browseService: BrowseService,
protected bundleService: BundleDataService,
protected constructIdEndpoint: ConstructIdEndpoint = (endpoint, resourceID) => `${endpoint}/${resourceID}`,
) {
super(linkPath, requestService, rdbService, objectCache, halService, undefined, constructIdEndpoint);
this.createData = new CreateDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, notificationsService, this.responseMsToLive);
this.patchData = new PatchDataImpl<Item>(this.linkPath, requestService, rdbService, objectCache, halService, comparator, this.responseMsToLive, this.constructIdEndpoint);
this.deleteData = new DeleteDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, notificationsService, this.responseMsToLive, this.constructIdEndpoint);
this.searchData = new SearchDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, this.responseMsToLive);
}
/**
* Get the endpoint for browsing items
* (When options.sort.field is empty, the default field to browse by will be 'dc.date.issued')
* @param {FindListOptions} options
* @param linkPath
* @returns {Observable<string>}
*/
public getBrowseEndpoint(options: FindListOptions = {}, linkPath: string = this.linkPath): Observable<string> {
let field = 'dc.date.issued';
if (options.sort && options.sort.field) {
field = options.sort.field;
}
return this.browseService.getBrowseURLFor(field, linkPath).pipe(
filter((href: string) => isNotEmpty(href)),
map((href: string) => new URLCombiner(href, `?scope=${options.scopeID}`).toString()),
distinctUntilChanged(),
);
}
/**
* Fetches the endpoint used for mapping an item to a collection,
* or for fetching all collections the item is mapped to if no collection is provided
* @param itemId The item's id
* @param collectionId The collection's id (optional)
*/
public getMappedCollectionsEndpoint(itemId: string, collectionId?: string): Observable<string> {
return this.halService.getEndpoint(this.linkPath).pipe(
map((endpoint: string) => this.getIDHref(endpoint, itemId)),
map((endpoint: string) => `${endpoint}/mappedCollections${collectionId ? `/${collectionId}` : ''}`),
);
}
/**
* Removes the mapping of an item from a collection
* @param itemId The item's id
* @param collectionId The collection's id
*/
public removeMappingFromCollection(itemId: string, collectionId: string): Observable<RemoteData<NoContent>> {
return this.getMappedCollectionsEndpoint(itemId, collectionId).pipe(
isNotEmptyOperator(),
distinctUntilChanged(),
map((endpointURL: string) => new DeleteRequest(this.requestService.generateRequestId(), endpointURL)),
sendRequest(this.requestService),
switchMap((request: RestRequest) => this.rdbService.buildFromRequestUUID(request.uuid)),
);
}
/**
* Maps an item to a collection
* @param itemId The item's id
* @param collectionHref The collection's self link
*/
public mapToCollection(itemId: string, collectionHref: string): Observable<RemoteData<NoContent>> {
return this.getMappedCollectionsEndpoint(itemId).pipe(
isNotEmptyOperator(),
distinctUntilChanged(),
map((endpointURL: string) => {
const options: HttpOptions = Object.create({});
let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'text/uri-list');
options.headers = headers;
return new PostRequest(this.requestService.generateRequestId(), endpointURL, collectionHref, options);
}),
sendRequest(this.requestService),
switchMap((request: RestRequest) => this.rdbService.buildFromRequestUUID(request.uuid)),
);
}
/**
* Set the isWithdrawn state of an item to a specified state
* @param item
* @param withdrawn
*/
public setWithDrawn(item: Item, withdrawn: boolean): Observable<RemoteData<Item>> {
const patchOperation = {
op: 'replace', path: '/withdrawn', value: withdrawn,
} as Operation;
this.requestService.removeByHrefSubstring('/discover');
return this.patch(item, [patchOperation]);
}
/**
* Set the isDiscoverable state of an item to a specified state
* @param item
* @param discoverable
*/
public setDiscoverable(item: Item, discoverable: boolean): Observable<RemoteData<Item>> {
const patchOperation = {
op: 'replace', path: '/discoverable', value: discoverable,
} as Operation;
this.requestService.removeByHrefSubstring('/discover');
return this.patch(item, [patchOperation]);
}
/**
* Get the endpoint for an item's bundles
* @param itemId
*/
public getBundlesEndpoint(itemId: string): Observable<string> {
return this.halService.getEndpoint(this.linkPath).pipe(
switchMap((url: string) => this.halService.getEndpoint('bundles', `${url}/${itemId}`)),
);
}
/**
* Get an item's bundles using paginated search options
* @param itemId The item's ID
* @param searchOptions The search options to use
*/
public getBundles(itemId: string, searchOptions?: PaginatedSearchOptions): Observable<RemoteData<PaginatedList<Bundle>>> {
const hrefObs = this.getBundlesEndpoint(itemId).pipe(
map((href) => searchOptions ? searchOptions.toRestUrl(href) : href),
);
hrefObs.pipe(
take(1),
).subscribe((href) => {
const request = new GetRequest(this.requestService.generateRequestId(), href);
this.requestService.send(request);
});
return this.rdbService.buildList<Bundle>(hrefObs);
}
/**
* Create a new bundle on an item
* @param itemId The item's ID
* @param bundleName The new bundle's name
* @param metadata Optional metadata for the bundle
*/
public createBundle(itemId: string, bundleName: string, metadata?: MetadataMap): Observable<RemoteData<Bundle>> {
const requestId = this.requestService.generateRequestId();
const hrefObs = this.getBundlesEndpoint(itemId);
const bundleJson = {
name: bundleName,
metadata: metadata ? metadata : {},
};
hrefObs.pipe(
take(1),
).subscribe((href) => {
const options: HttpOptions = Object.create({});
let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'application/json');
options.headers = headers;
const request = new PostRequest(requestId, href, JSON.stringify(bundleJson), options);
this.requestService.send(request);
});
return this.rdbService.buildFromRequestUUID(requestId);
}
/**
* Get the endpoint for an item's identifiers
* @param itemId
*/
public getIdentifiersEndpoint(itemId: string): Observable<string> {
return this.halService.getEndpoint(this.linkPath).pipe(
switchMap((url: string) => this.halService.getEndpoint('identifiers', `${url}/${itemId}`)),
);
}
/**
* Get the endpoint to move the item
* @param itemId
*/
public getMoveItemEndpoint(itemId: string, inheritPolicies: boolean): Observable<string> {
return this.halService.getEndpoint(this.linkPath).pipe(
map((endpoint: string) => this.getIDHref(endpoint, itemId)),
map((endpoint: string) => `${endpoint}/owningCollection?inheritPolicies=${inheritPolicies}`),
);
}
/**
* Move the item to a different owning collection
* @param itemId
* @param collection
*/
public moveToCollection(itemId: string, collection: Collection, inheritPolicies: boolean): Observable<RemoteData<any>> {
const options: HttpOptions = Object.create({});
let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'text/uri-list');
options.headers = headers;
const requestId = this.requestService.generateRequestId();
const hrefObs = this.getMoveItemEndpoint(itemId, inheritPolicies);
hrefObs.pipe(
find((href: string) => hasValue(href)),
map((href: string) => {
const request = new PutRequest(requestId, href, collection._links.self.href, options);
Object.assign(request, {
// TODO: for now, the move Item endpoint returns a malformed collection -- only look at the status code
getResponseParser(): GenericConstructor<ResponseParsingService> {
return StatusCodeOnlyResponseParsingService;
},
});
return request;
}),
).subscribe((request) => {
this.requestService.send(request);
});
return this.rdbService.buildFromRequestUUID(requestId);
}
/**
* Import an external source entry into a collection
* @param externalSourceEntry
* @param collectionId
*/
public importExternalSourceEntry(externalSourceEntry: ExternalSourceEntry, collectionId: string): Observable<RemoteData<Item>> {
const options: HttpOptions = Object.create({});
let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'text/uri-list');
options.headers = headers;
const requestId = this.requestService.generateRequestId();
const href$ = this.halService.getEndpoint(this.linkPath).pipe(map((href) => `${href}?owningCollection=${collectionId}`));
href$.pipe(
find((href: string) => hasValue(href)),
map((href: string) => {
const request = new PostRequest(requestId, href, externalSourceEntry._links.self.href, options);
this.requestService.send(request);
}),
).subscribe();
return this.rdbService.buildFromRequestUUID(requestId);
}
/**
* Get the endpoint for an item's bitstreams
* @param itemId
*/
public getBitstreamsEndpoint(itemId: string): Observable<string> {
return this.halService.getEndpoint(this.linkPath).pipe(
switchMap((url: string) => this.halService.getEndpoint('bitstreams', `${url}/${itemId}`)),
);
}
/**
* Invalidate the cache of the item
* @param itemUUID
*/
invalidateItemCache(itemUUID: string) {
this.requestService.setStaleByHrefSubstring('item/' + itemUUID);
}
/**
* Commit current object changes to the server
* @param method The RestRequestMethod for which de server sync buffer should be committed
*/
public commitUpdates(method?: RestRequestMethod): void {
this.patchData.commitUpdates(method);
}
/**
* Send a patch request for a specified object
* @param {T} object The object to send a patch request for
* @param {Operation[]} operations The patch operations to be performed
*/
public patch(object: Item, operations: Operation[]): Observable<RemoteData<Item>> {
return this.patchData.patch(object, operations);
}
/**
* Add a new patch to the object cache
* The patch is derived from the differences between the given object and its version in the object cache
* @param {DSpaceObject} object The given object
*/
public update(object: Item): Observable<RemoteData<Item>> {
return this.patchData.update(object);
}
/**
* Return a list of operations representing the difference between an object and its latest value in the cache.
* @param object the object to resolve to a list of patch operations
*/
public createPatchFromCache(object: Item): Observable<Operation[]> {
return this.patchData.createPatchFromCache(object);
}
/**
* Delete an existing object on the server
* @param objectId The id of the object to be removed
* @param copyVirtualMetadata (optional parameter) the identifiers of the relationship types for which the virtual
* metadata should be saved as real metadata
* @return A RemoteData observable with an empty payload, but still representing the state of the request: statusCode,
* errorMessage, timeCompleted, etc
*/
public delete(objectId: string, copyVirtualMetadata?: string[]): Observable<RemoteData<NoContent>> {
return this.deleteData.delete(objectId, copyVirtualMetadata);
}
/**
* Delete an existing object on the server
* @param href The self link of the object to be removed
* @param copyVirtualMetadata (optional parameter) the identifiers of the relationship types for which the virtual
* metadata should be saved as real metadata
* @return A RemoteData observable with an empty payload, but still representing the state of the request: statusCode,
* errorMessage, timeCompleted, etc
* Only emits once all request related to the DSO has been invalidated.
*/
public deleteByHref(href: string, copyVirtualMetadata?: string[]): Observable<RemoteData<NoContent>> {
return this.deleteData.deleteByHref(href, copyVirtualMetadata);
}
/**
* Create a new object on the server, and store the response in the object cache
*
* @param object The object to create
* @param params Array with additional params to combine with query string
*/
public create(object: Item, ...params: RequestParam[]): Observable<RemoteData<Item>> {
return this.createData.create(object, ...params);
}
/**
* Returns an observable of {@link RemoteData} of an object, based on its custom URL, with a list of
* {@link FollowLinkConfig}, to automatically resolve {@link HALLink}s of the object
* @param id custom URL of object we want to retrieve
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
* no valid cached version. Defaults to true
* @param reRequestOnStale Whether or not the request should automatically be re-
* requested after the response becomes stale
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which
* {@link HALLink}s should be automatically resolved
* @param projections List of {@link projections} used to pass as parameters
*/
public findByCustomUrl(id: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, linksToFollow: FollowLinkConfig<Item>[], projections: string[] = []): Observable<RemoteData<Item>> {
const searchHref = 'findByCustomURL';
const options = Object.assign({}, {
searchParams: [
new RequestParam('q', id),
],
});
projections.forEach((projection) => {
options.searchParams.push(new RequestParam('projection', projection));
});
const hrefObs = this.searchData.getSearchByHref(searchHref, options, ...linksToFollow);
return this.findByHref(hrefObs, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
/**
* Returns an observable of {@link RemoteData} of an object, based on its ID, with a list of
* {@link FollowLinkConfig}, to automatically resolve {@link HALLink}s of the object
* @param id ID of object we want to retrieve
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
* no valid cached version. Defaults to true
* @param reRequestOnStale Whether or not the request should automatically be re-
* requested after the response becomes stale
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which
* {@link HALLink}s should be automatically resolved
*/
public findById(id: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<Item>[]): Observable<RemoteData<Item>> {
const href$ = this.getIDHrefObs(encodeURIComponent(id), ...linksToFollow);
return this.findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
/**
* Returns an observable of {@link RemoteData} of an object, based on its ID or custom URL if the parameter is not a valid id/uuid, with a list of
* {@link FollowLinkConfig}, to automatically resolve {@link HALLink}s of the object
* @param id ID of object we want to retrieve
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
* no valid cached version. Defaults to true
* @param reRequestOnStale Whether or not the request should automatically be re-
* requested after the response becomes stale
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which
* {@link HALLink}s should be automatically resolved
*/
public findByIdOrCustomUrl(id: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<Item>[]): Observable<RemoteData<Item>> {
if (uuidValidate(id)) {
return this.findById(id, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
} else {
return this.findByCustomUrl(id, useCachedVersionIfAvailable, reRequestOnStale, linksToFollow);
}
}
}
/**
* A service for CRUD operations on Items
*/
@Injectable({ providedIn: 'root' })
export class ItemDataService extends BaseItemDataService {
constructor(
protected requestService: RequestService,
protected rdbService: RemoteDataBuildService,
protected objectCache: ObjectCacheService,
protected halService: HALEndpointService,
protected notificationsService: NotificationsService,
protected comparator: DSOChangeAnalyzer<Item>,
protected browseService: BrowseService,
protected bundleService: BundleDataService,
) {
super('items', requestService, rdbService, objectCache, halService, notificationsService, comparator, browseService, bundleService);
}
}