forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvocabulary.service.ts
More file actions
390 lines (362 loc) · 21.2 KB
/
Copy pathvocabulary.service.ts
File metadata and controls
390 lines (362 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
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { map, switchMap, mergeMap, catchError } from 'rxjs/operators';
import { FollowLinkConfig, followLink } from '../../../shared/utils/follow-link-config.model';
import { RequestService } from '../../data/request.service';
import { RemoteData } from '../../data/remote-data';
import { PaginatedList } from '../../data/paginated-list.model';
import { Vocabulary } from './models/vocabulary.model';
import { VocabularyEntry } from './models/vocabulary-entry.model';
import { isNotEmpty } from '../../../shared/empty.util';
import { ExternalSourceDataService } from '../../data/external-source-data.service';
import { ExternalSourceEntry } from '../../shared/external-source-entry.model';
import { PaginatedSearchOptions } from '../../../shared/search/models/paginated-search-options.model';
import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model';
import {
getFirstSucceededRemoteDataPayload,
getFirstSucceededRemoteListPayload,
} from '../../shared/operators';
import { VocabularyFindOptions } from './models/vocabulary-find-options.model';
import { VocabularyEntryDetail } from './models/vocabulary-entry-detail.model';
import { RequestParam } from '../../cache/models/request-param.model';
import { VocabularyOptions } from './models/vocabulary-options.model';
import { PageInfo } from '../../shared/page-info.model';
import { FindListOptions } from '../../data/find-list-options.model';
import { VocabularyEntryDetailsDataService } from './vocabulary-entry-details.data.service';
import { VocabularyDataService } from './vocabulary.data.service';
import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils';
import { buildPaginatedList } from '../../data/paginated-list.model';
/**
* A service responsible for fetching/sending data from/to the REST API on the vocabularies endpoint
*/
@Injectable()
export class VocabularyService {
protected searchTopMethod = 'top';
constructor(
protected requestService: RequestService,
protected vocabularyDataService: VocabularyDataService,
protected vocabularyEntryDetailDataService: VocabularyEntryDetailsDataService,
protected externalSourceDataService: ExternalSourceDataService,
) {
}
/**
* Returns an observable of {@link RemoteData} of a {@link Vocabulary}, based on an href, with a list of {@link FollowLinkConfig},
* to automatically resolve {@link HALLink}s of the {@link Vocabulary}
* @param href The 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
* @return {Observable<RemoteData<Vocabulary>>}
* Return an observable that emits vocabulary object
*/
findVocabularyByHref(href: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<Vocabulary>[]): Observable<RemoteData<any>> {
return this.vocabularyDataService.findByHref(href, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
/**
* Returns an observable of {@link RemoteData} of a {@link Vocabulary}, based on its ID, with a list of {@link FollowLinkConfig},
* to automatically resolve {@link HALLink}s of the object
* @param name The name of {@link Vocabulary} 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
* @return {Observable<RemoteData<Vocabulary>>}
* Return an observable that emits vocabulary object
*/
findVocabularyById(name: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<Vocabulary>[]): Observable<RemoteData<Vocabulary>> {
return this.vocabularyDataService.findById(name, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
/**
* 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<Vocabulary>>>}
* Return an observable that emits object list
*/
findAllVocabularies(options: FindListOptions = {}, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<Vocabulary>[]): Observable<RemoteData<PaginatedList<Vocabulary>>> {
return this.vocabularyDataService.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
/**
* Return the {@link VocabularyEntry} list for a given {@link Vocabulary}
*
* @param vocabularyOptions The {@link VocabularyOptions} for the request to which the entries belong
* @param pageInfo The {@link PageInfo} for the request
* @return {Observable<RemoteData<PaginatedList<VocabularyEntry>>>}
* Return an observable that emits object list
*/
getVocabularyEntries(vocabularyOptions: VocabularyOptions, pageInfo: PageInfo): Observable<RemoteData<PaginatedList<VocabularyEntry>>> {
const options: VocabularyFindOptions = new VocabularyFindOptions(
null,
null,
null,
null,
pageInfo.elementsPerPage,
pageInfo.currentPage
);
// TODO remove false for the entries embed when https://github.com/DSpace/DSpace/issues/3096 is solved
return this.findVocabularyById(vocabularyOptions.name, true, true, followLink('entries', { findListOptions: options, shouldEmbed: false })).pipe(
getFirstSucceededRemoteDataPayload(),
switchMap((vocabulary: Vocabulary) => vocabulary.entries),
);
}
/**
* Return the {@link VocabularyEntry} list for a given value
*
* @param value The entry value to retrieve
* @param exact If true force the vocabulary to provide only entries that match exactly with the value
* @param vocabularyOptions The {@link VocabularyOptions} for the request to which the entries belong
* @param pageInfo The {@link PageInfo} for the request
* @return {Observable<RemoteData<PaginatedList<VocabularyEntry>>>}
* Return an observable that emits object list
*/
getVocabularyEntriesByValue(value: string, exact: boolean, vocabularyOptions: VocabularyOptions, pageInfo: PageInfo): Observable<RemoteData<PaginatedList<VocabularyEntry>>> {
// Handle authority fields specially for DSpace 7 compatibility
const authorityFields = ['dc.contributor.author', 'dc.creator', 'dc.contributor.editor', 'dc.contributor.advisor'];
if (authorityFields.includes(vocabularyOptions.name)) {
// For authority fields, use ORCID external source if there's search text
if (value && value.length >= 2) {
// Create search options for ORCID
const paginationOptions = Object.assign(new PaginationComponentOptions(), {
id: 'orcid-search',
currentPage: pageInfo.currentPage || 1,
pageSize: pageInfo.elementsPerPage || 10
});
const searchOptions = new PaginatedSearchOptions({
query: value,
pagination: paginationOptions
});
// Use ORCID external source with proper completion handling
return this.externalSourceDataService.getExternalSourceEntries('orcid', searchOptions, false, false).pipe(
// Use the existing DSpace operator to ensure completion
getFirstSucceededRemoteDataPayload(),
switchMap((paginatedList: PaginatedList<ExternalSourceEntry>) => {
if (paginatedList && paginatedList.page.length > 0) {
// Convert ExternalSourceEntry to VocabularyEntry
const vocabularyEntries: VocabularyEntry[] = paginatedList.page.map(entry => {
const vocabEntry = new VocabularyEntry();
// Display shows author name + ORCID for selection
vocabEntry.display = `${entry.display} (ORCID: ${entry.id})`;
// Value should be just the author name (what goes in the main field)
vocabEntry.value = entry.display;
// Authority is the ORCID ID (what goes in the authority field)
vocabEntry.authority = entry.id;
vocabEntry.otherInformation = { orcid: entry.id };
return vocabEntry;
});
const resultList = buildPaginatedList(pageInfo, vocabularyEntries);
return createSuccessfulRemoteDataObject$(resultList);
} else {
// Return empty list if no results
const emptyList = buildPaginatedList(new PageInfo(), []);
return createSuccessfulRemoteDataObject$(emptyList);
}
}),
catchError((error) => {
console.warn('ORCID lookup failed:', error);
// Return empty list on error rather than falling back
const emptyList = buildPaginatedList(new PageInfo(), []);
return createSuccessfulRemoteDataObject$(emptyList);
})
);
} else {
// Return empty list if no search text
const emptyList = buildPaginatedList(new PageInfo(), []);
return createSuccessfulRemoteDataObject$(emptyList);
}
}
const options: VocabularyFindOptions = new VocabularyFindOptions(
null,
value,
exact,
null,
pageInfo.elementsPerPage,
pageInfo.currentPage
);
// TODO remove false for the entries embed when https://github.com/DSpace/DSpace/issues/3096 is solved
return this.findVocabularyById(vocabularyOptions.name, true, true, followLink('entries', { findListOptions: options, shouldEmbed: false })).pipe(
getFirstSucceededRemoteDataPayload(),
switchMap((vocabulary: Vocabulary) => vocabulary.entries),
);
}
/**
* Return the {@link VocabularyEntry} list for a given value
*
* @param value The entry value to retrieve
* @param vocabularyOptions The {@link VocabularyOptions} for the request to which the entry belongs
* @return {Observable<RemoteData<PaginatedList<VocabularyEntry>>>}
* Return an observable that emits {@link VocabularyEntry} object
*/
getVocabularyEntryByValue(value: string, vocabularyOptions: VocabularyOptions): Observable<VocabularyEntry> {
return this.getVocabularyEntriesByValue(value, true, vocabularyOptions, new PageInfo()).pipe(
getFirstSucceededRemoteListPayload(),
map((list: VocabularyEntry[]) => {
if (isNotEmpty(list)) {
return list[0];
} else {
return null;
}
})
);
}
/**
* Return the {@link VocabularyEntry} list for a given ID
*
* @param ID The entry ID to retrieve
* @param vocabularyOptions The {@link VocabularyOptions} for the request to which the entry belongs
* @return {Observable<RemoteData<PaginatedList<VocabularyEntry>>>}
* Return an observable that emits {@link VocabularyEntry} object
*/
getVocabularyEntryByID(ID: string, vocabularyOptions: VocabularyOptions): Observable<VocabularyEntry> {
const pageInfo = new PageInfo();
const options: VocabularyFindOptions = new VocabularyFindOptions(
null,
null,
null,
ID,
pageInfo.elementsPerPage,
pageInfo.currentPage
);
// TODO remove false for the entries embed when https://github.com/DSpace/DSpace/issues/3096 is solved
return this.findVocabularyById(vocabularyOptions.name, true, true, followLink('entries', { findListOptions: options, shouldEmbed: false })).pipe(
getFirstSucceededRemoteDataPayload(),
switchMap((vocabulary: Vocabulary) => vocabulary.entries),
getFirstSucceededRemoteListPayload(),
map((list: VocabularyEntry[]) => {
if (isNotEmpty(list)) {
return list[0];
} else {
return null;
}
})
);
}
/**
* Returns an observable of {@link RemoteData} of a {@link VocabularyEntryDetail}, based on an href, with a list of {@link FollowLinkConfig},
* to automatically resolve {@link HALLink}s of the {@link VocabularyEntryDetail}
* @param href The 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
* @return {Observable<RemoteData<VocabularyEntryDetail>>}
* Return an observable that emits vocabulary object
*/
findEntryDetailByHref(href: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<VocabularyEntryDetail>[]): Observable<RemoteData<VocabularyEntryDetail>> {
return this.vocabularyEntryDetailDataService.findByHref(href, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
/**
* Returns an observable of {@link RemoteData} of a {@link VocabularyEntryDetail}, based on its ID, with a list of {@link FollowLinkConfig},
* to automatically resolve {@link HALLink}s of the object
* @param id The entry id for which to provide detailed information.
* @param name The name of {@link Vocabulary} to which the entry belongs
* @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 constructId Whether constructing the full vocabularyDetail ID
* ({vocabularyName}:{detailName}) is still necessary
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which
* {@link HALLink}s should be automatically resolved
* @return {Observable<RemoteData<VocabularyEntryDetail>>}
* Return an observable that emits VocabularyEntryDetail object
*/
findEntryDetailById(id: string, name: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, constructId: boolean = true, ...linksToFollow: FollowLinkConfig<VocabularyEntryDetail>[]): Observable<RemoteData<VocabularyEntryDetail>> {
const findId: string = (constructId ? `${name}:${id}` : id);
return this.vocabularyEntryDetailDataService.findById(findId, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
/**
* Returns the parent detail entry for a given detail entry, with a list of {@link FollowLinkConfig},
* to automatically resolve {@link HALLink}s of the object
* @param value The entry value for which to provide parent.
* @param name The name of {@link Vocabulary} to which the entry belongs
* @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<VocabularyEntryDetail>>>}
* Return an observable that emits a PaginatedList of VocabularyEntryDetail
*/
getEntryDetailParent(value: string, name: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<VocabularyEntryDetail>[]): Observable<RemoteData<VocabularyEntryDetail>> {
const linkPath = `${name}:${value}/parent`;
return this.vocabularyEntryDetailDataService.getBrowseEndpoint().pipe(
map((href: string) => `${href}/${linkPath}`),
mergeMap((href) => this.vocabularyEntryDetailDataService.findByHref(href, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow))
);
}
/**
* Returns the list of children detail entries for a given detail entry, with a list of {@link FollowLinkConfig},
* to automatically resolve {@link HALLink}s of the object
* @param value The entry value for which to provide children list.
* @param name The name of {@link Vocabulary} to which the entry belongs
* @param pageInfo The {@link PageInfo} for the request
* @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<VocabularyEntryDetail>>>}
* Return an observable that emits a PaginatedList of VocabularyEntryDetail
*/
getEntryDetailChildren(value: string, name: string, pageInfo: PageInfo, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<VocabularyEntryDetail>[]): Observable<RemoteData<PaginatedList<VocabularyEntryDetail>>> {
const options: VocabularyFindOptions = new VocabularyFindOptions(
null,
null,
null,
null,
pageInfo.elementsPerPage,
pageInfo.currentPage
);
return this.vocabularyEntryDetailDataService.getBrowseEndpoint().pipe(
map(href => `${href}/${name}:${value}/children`),
switchMap(href => this.vocabularyEntryDetailDataService.findListByHref(href, options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow))
);
}
/**
* Return the top level {@link VocabularyEntryDetail} list for a given hierarchical vocabulary
*
* @param name The name of hierarchical {@link Vocabulary} to which the
* entries belongs
* @param pageInfo The {@link PageInfo} for the request
* @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
*/
searchTopEntries(name: string, pageInfo: PageInfo, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<VocabularyEntryDetail>[]): Observable<RemoteData<PaginatedList<VocabularyEntryDetail>>> {
const options: VocabularyFindOptions = new VocabularyFindOptions(
null,
null,
null,
null,
pageInfo.elementsPerPage,
pageInfo.currentPage
);
options.searchParams = [new RequestParam('vocabulary', name)];
return this.vocabularyEntryDetailDataService.searchBy(this.searchTopMethod, options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
/**
* Clear all search Top Requests
*/
clearSearchTopRequests(): void {
this.requestService.removeByHrefSubstring(`search/${this.searchTopMethod}`);
}
}