|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { FollowAuthorityMetadata } from '@dspace/config/search-follow-metadata.interface'; |
| 3 | +import { |
| 4 | + hasValue, |
| 5 | + isNotEmpty, |
| 6 | +} from '@dspace/shared/utils/empty.util'; |
| 7 | +import isArray from 'lodash/isArray'; |
| 8 | +import { |
| 9 | + Observable, |
| 10 | + of, |
| 11 | +} from 'rxjs'; |
| 12 | +import { |
| 13 | + map, |
| 14 | + switchMap, |
| 15 | +} from 'rxjs/operators'; |
| 16 | +import { SearchService } from 'src/app/shared/search/search.service'; |
| 17 | + |
| 18 | +import { environment } from '../../../environments/environment'; |
| 19 | +import { ItemDataService } from '../data/item-data.service'; |
| 20 | +import { PaginatedList } from '../data/paginated-list.model'; |
| 21 | +import { RemoteData } from '../data/remote-data'; |
| 22 | +import { WORKFLOWITEM } from '../eperson/models/workflowitem.resource-type'; |
| 23 | +import { WORKSPACEITEM } from '../eperson/models/workspaceitem.resource-type'; |
| 24 | +import { DSpaceObject } from '../shared/dspace-object.model'; |
| 25 | +import { FollowLinkConfig } from '../shared/follow-link-config.model'; |
| 26 | +import { Item } from '../shared/item.model'; |
| 27 | +import { ITEM } from '../shared/item.resource-type'; |
| 28 | +import { MetadataValue } from '../shared/metadata.models'; |
| 29 | +import { Metadata } from '../shared/metadata.utils'; |
| 30 | +import { getFirstCompletedRemoteData } from '../shared/operators'; |
| 31 | +import { PaginatedSearchOptions } from '../shared/search/models/paginated-search-options.model'; |
| 32 | +import { SearchObjects } from '../shared/search/models/search-objects.model'; |
| 33 | +import { BrowseService } from './browse.service'; |
| 34 | + |
| 35 | +/** |
| 36 | + * The service aims to manage browse requests and subsequent extra fetch requests. |
| 37 | + */ |
| 38 | +@Injectable({ providedIn: 'root' }) |
| 39 | +export class SearchManager { |
| 40 | + |
| 41 | + constructor( |
| 42 | + protected itemService: ItemDataService, |
| 43 | + protected browseService: BrowseService, |
| 44 | + protected searchService: SearchService, |
| 45 | + ) { |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Method to retrieve a paginated list of search results from the server |
| 50 | + * @param {PaginatedSearchOptions} searchOptions The configuration necessary to perform this search |
| 51 | + * @param responseMsToLive The amount of milliseconds for the response to live in cache |
| 52 | + * @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's |
| 53 | + * no valid cached version. Defaults to true |
| 54 | + * @param reRequestOnStale Whether or not the request should automatically be re-requested after |
| 55 | + * the response becomes stale |
| 56 | + * @param linksToFollow List of {@link FollowLinkConfig} that indicate which {@link HALLink}s should be automatically resolved |
| 57 | + * @returns {Observable<RemoteData<SearchObjects<T>>>} Emits a paginated list with all search results found |
| 58 | + */ |
| 59 | + search<T extends DSpaceObject>( |
| 60 | + searchOptions?: PaginatedSearchOptions, |
| 61 | + responseMsToLive?: number, |
| 62 | + useCachedVersionIfAvailable = true, |
| 63 | + reRequestOnStale = true, |
| 64 | + ...linksToFollow: FollowLinkConfig<T>[]): Observable<RemoteData<SearchObjects<T>>> { |
| 65 | + return this.searchService.search(searchOptions, responseMsToLive, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow) |
| 66 | + .pipe(this.completeSearchObjectsWithExtraData()); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + protected completeWithExtraData() { |
| 71 | + return switchMap((itemsRD: RemoteData<PaginatedList<Item>>) => { |
| 72 | + if (itemsRD.isSuccess) { |
| 73 | + return this.fetchExtraData(itemsRD.payload.page).pipe(map(() => { |
| 74 | + return itemsRD; |
| 75 | + })); |
| 76 | + } |
| 77 | + return of(itemsRD); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + protected completeSearchObjectsWithExtraData<T extends DSpaceObject>() { |
| 82 | + return switchMap((searchObjectsRD: RemoteData<SearchObjects<T>>) => { |
| 83 | + if (searchObjectsRD.isSuccess) { |
| 84 | + const items: Item[] = searchObjectsRD.payload.page |
| 85 | + .map((searchResult) => isNotEmpty(searchResult?._embedded?.indexableObject) ? searchResult._embedded.indexableObject : searchResult.indexableObject) as any; |
| 86 | + return this.fetchExtraData(items).pipe(map(() => { |
| 87 | + return searchObjectsRD; |
| 88 | + })); |
| 89 | + } |
| 90 | + return of(searchObjectsRD); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + protected fetchExtraData<T extends DSpaceObject>(objects: T[]): Observable<any> { |
| 95 | + |
| 96 | + const items: Item[] = objects |
| 97 | + .map((object: any) => { |
| 98 | + if (object.type === ITEM.value) { |
| 99 | + return object as Item; |
| 100 | + } else if (object.type === WORKSPACEITEM.value || object.type === WORKFLOWITEM.value) { |
| 101 | + return object?._embedded?.item as Item; |
| 102 | + } else { |
| 103 | + // Handle workflow task here, where the item is embedded in a workflowitem |
| 104 | + return object?._embedded?.workflowitem?._embedded?.item as Item; |
| 105 | + } |
| 106 | + |
| 107 | + }) |
| 108 | + .filter((item) => hasValue(item)); |
| 109 | + |
| 110 | + const uuidList = this.extractUUID(items, environment.searchResult.followAuthorityMetadata, environment.searchResult.followAuthorityMaxItemLimit); |
| 111 | + |
| 112 | + return uuidList.length > 0 ? this.itemService.findAllById(uuidList).pipe( |
| 113 | + getFirstCompletedRemoteData(), |
| 114 | + map(data => { |
| 115 | + if (data.hasSucceeded) { |
| 116 | + return of(data); |
| 117 | + } else { |
| 118 | + of(null); |
| 119 | + } |
| 120 | + }), |
| 121 | + ) : of(null); |
| 122 | + } |
| 123 | + |
| 124 | + protected extractUUID(items: Item[], metadataToFollow: FollowAuthorityMetadata[], numberOfElementsToReturn?: number): string[] { |
| 125 | + const uuidMap = {}; |
| 126 | + |
| 127 | + items.forEach((item) => { |
| 128 | + metadataToFollow.forEach((followMetadata: FollowAuthorityMetadata) => { |
| 129 | + if (item.entityType === followMetadata.type) { |
| 130 | + if (isArray(followMetadata.metadata)) { |
| 131 | + followMetadata.metadata.forEach((metadata) => { |
| 132 | + Metadata.all(item.metadata, metadata, null, null, false, environment.searchResult.followAuthorityMetadataValuesLimit) |
| 133 | + .filter((metadataValue: MetadataValue) => Metadata.hasValidItemAuthority(metadataValue.authority)) |
| 134 | + .forEach((metadataValue: MetadataValue) => uuidMap[metadataValue.authority] = metadataValue); |
| 135 | + }); |
| 136 | + } else { |
| 137 | + Metadata.all(item.metadata, followMetadata.metadata, null, null, false, environment.searchResult.followAuthorityMetadataValuesLimit) |
| 138 | + .filter((metadataValue: MetadataValue) => Metadata.hasValidItemAuthority(metadataValue.authority)) |
| 139 | + .forEach((metadataValue: MetadataValue) => uuidMap[metadataValue.authority] = metadataValue); |
| 140 | + } |
| 141 | + } |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + if (hasValue(numberOfElementsToReturn) && numberOfElementsToReturn > 0) { |
| 146 | + return Object.keys(uuidMap).slice(0, numberOfElementsToReturn); |
| 147 | + } |
| 148 | + |
| 149 | + return Object.keys(uuidMap); |
| 150 | + } |
| 151 | +} |
0 commit comments