-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathscript-data.service.ts
More file actions
109 lines (99 loc) · 5.03 KB
/
Copy pathscript-data.service.ts
File metadata and controls
109 lines (99 loc) · 5.03 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
import { Injectable } from '@angular/core';
import { Script } from '@dspace/core/shared/scripts/script.model';
import { SCRIPT } from '@dspace/core/shared/scripts/script.resource-type';
import { hasValue } from '@dspace/shared/utils/empty.util';
import { Observable } from 'rxjs';
import {
map,
take,
} from 'rxjs/operators';
import { dataService } from '../../cache/builders/build-decorators';
import { RemoteDataBuildService } from '../../cache/builders/remote-data-build.service';
import { ObjectCacheService } from '../../cache/object-cache.service';
import { Process } from '../../processes/process.model';
import { ProcessParameter } from '../../processes/process-parameter.model';
import { FollowLinkConfig } from '../../shared/follow-link-config.model';
import { HALEndpointService } from '../../shared/hal-endpoint.service';
import { getFirstCompletedRemoteData } from '../../shared/operators';
import { URLCombiner } from '../../url-combiner/url-combiner';
import {
FindAllData,
FindAllDataImpl,
} from '../base/find-all-data';
import { IdentifiableDataService } from '../base/identifiable-data.service';
import { FindListOptions } from '../find-list-options.model';
import { PaginatedList } from '../paginated-list.model';
import { RemoteData } from '../remote-data';
import { MultipartPostRequest } from '../request.models';
import { RequestService } from '../request.service';
import { RestRequest } from '../rest-request.model';
export const METADATA_IMPORT_SCRIPT_NAME = 'metadata-import';
export const METADATA_EXPORT_SCRIPT_NAME = 'metadata-export';
export const BATCH_IMPORT_SCRIPT_NAME = 'import';
export const BATCH_EXPORT_SCRIPT_NAME = 'export';
export const DSPACE_OBJECT_DELETION_SCRIPT_NAME = 'object-deletion';
export const ITEM_EXPORT_SCRIPT_NAME = 'item-export';
export const BULK_ITEM_EXPORT_SCRIPT_NAME = 'bulk-item-export';
@Injectable({ providedIn: 'root' })
@dataService(SCRIPT)
export class ScriptDataService extends IdentifiableDataService<Script> implements FindAllData<Script> {
private findAllData: FindAllDataImpl<Script>;
constructor(
protected requestService: RequestService,
protected rdbService: RemoteDataBuildService,
protected objectCache: ObjectCacheService,
protected halService: HALEndpointService,
) {
super('scripts', requestService, rdbService, objectCache, halService);
this.findAllData = new FindAllDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, this.responseMsToLive);
}
public invoke(scriptName: string, parameters: ProcessParameter[], files: File[]): Observable<RemoteData<Process>> {
const requestId = this.requestService.generateRequestId();
this.getBrowseEndpoint().pipe(
take(1),
map((endpoint: string) => new URLCombiner(endpoint, scriptName, 'processes').toString()),
map((endpoint: string) => {
const body = this.getInvocationFormData(parameters, files);
return new MultipartPostRequest(requestId, endpoint, body);
}),
).subscribe((request: RestRequest) => this.requestService.send(request));
return this.rdbService.buildFromRequestUUID<Process>(requestId);
}
private getInvocationFormData(parameters: ProcessParameter[], files: File[]): FormData {
const form: FormData = new FormData();
form.set('properties', JSON.stringify(parameters));
files.forEach((file: File) => {
form.append('file', file);
});
return form;
}
/**
* Check whether a script with given name exist; user needs to be allowed to execute script for this to to not throw a 401 Unauthorized
* @param scriptName script we want to check exists (and we can execute)
*/
public scriptWithNameExistsAndCanExecute(scriptName: string): Observable<boolean> {
return this.findById(scriptName).pipe(
getFirstCompletedRemoteData(),
map((rd: RemoteData<Script>) => {
return hasValue(rd.payload);
}),
);
}
/**
* Returns {@link RemoteData} of all object with a list of {@link FollowLinkConfig}, to indicate which embedded
* info should be added to the objects
*
* @param options Find list options object
* @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
* @return {Observable<RemoteData<PaginatedList<T>>>}
* Return an observable that emits object list
*/
public findAll(options?: FindListOptions, useCachedVersionIfAvailable?: boolean, reRequestOnStale?: boolean, ...linksToFollow: FollowLinkConfig<Script>[]): Observable<RemoteData<PaginatedList<Script>>> {
return this.findAllData.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
}