|
| 1 | +/* eslint-disable max-classes-per-file */ |
| 2 | +import { Observable } from 'rxjs'; |
| 3 | +import { Injectable } from '@angular/core'; |
| 4 | +import { map } from 'rxjs/operators'; |
| 5 | +import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model'; |
| 6 | +import { ResponseParsingService } from './parsing.service'; |
| 7 | +import { RemoteData } from './remote-data'; |
| 8 | +import { GetRequest } from './request.models'; |
| 9 | +import { RequestService } from './request.service'; |
| 10 | +import { GenericConstructor } from '../shared/generic-constructor'; |
| 11 | +import { HALEndpointService } from '../shared/hal-endpoint.service'; |
| 12 | +import { SearchResponseParsingService } from './search-response-parsing.service'; |
| 13 | +import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; |
| 14 | +import { RestRequest } from './rest-request.model'; |
| 15 | +import { BaseDataService } from './base/base-data.service'; |
| 16 | +import { FindListOptions } from './find-list-options.model'; |
| 17 | +import { Duplicate } from '../../shared/object-list/duplicate-data/duplicate.model'; |
| 18 | +import { PaginatedList } from './paginated-list.model'; |
| 19 | +import { RequestParam } from '../cache/models/request-param.model'; |
| 20 | +import { ObjectCacheService } from '../cache/object-cache.service'; |
| 21 | + |
| 22 | + |
| 23 | +/** |
| 24 | + * Service that performs all general actions that have to do with the search page |
| 25 | + */ |
| 26 | +@Injectable() |
| 27 | +export class DuplicateDataService extends BaseDataService<Duplicate> { |
| 28 | + |
| 29 | + /** |
| 30 | + * The ResponseParsingService constructor name |
| 31 | + */ |
| 32 | + private parser: GenericConstructor<ResponseParsingService> = SearchResponseParsingService; |
| 33 | + |
| 34 | + /** |
| 35 | + * The RestRequest constructor name |
| 36 | + */ |
| 37 | + private request: GenericConstructor<RestRequest> = GetRequest; |
| 38 | + |
| 39 | + /** |
| 40 | + * Subscription to unsubscribe from |
| 41 | + */ |
| 42 | + private sub; |
| 43 | + |
| 44 | + constructor( |
| 45 | + protected requestService: RequestService, |
| 46 | + protected rdbService: RemoteDataBuildService, |
| 47 | + protected objectCache: ObjectCacheService, |
| 48 | + protected halService: HALEndpointService, |
| 49 | + ) { |
| 50 | + super('duplicates', requestService, rdbService, objectCache, halService); |
| 51 | + } |
| 52 | + |
| 53 | + protected getEndpoint(): Observable<string> { |
| 54 | + return this.halService.getEndpoint(this.linkPath); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Method to set service options |
| 59 | + * @param {GenericConstructor<ResponseParsingService>} parser The ResponseParsingService constructor name |
| 60 | + * @param {boolean} request The RestRequest constructor name |
| 61 | + */ |
| 62 | + setServiceOptions(parser: GenericConstructor<ResponseParsingService>, request: GenericConstructor<RestRequest>) { |
| 63 | + if (parser) { |
| 64 | + this.parser = parser; |
| 65 | + } |
| 66 | + if (request) { |
| 67 | + this.request = request; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private getSearchUrl(): Observable<string> { |
| 72 | + const href$ = this.getEndpoint(); |
| 73 | + return href$.pipe( |
| 74 | + map((href) => href + '/search') |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + public findDuplicates(uuid: string, options?: FindListOptions, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<Duplicate>[]): Observable<RemoteData<PaginatedList<Duplicate>>> { |
| 79 | + const searchParams = [new RequestParam('uuid', uuid)]; |
| 80 | + let findListOptions = new FindListOptions(); |
| 81 | + if (options) { |
| 82 | + findListOptions = Object.assign(new FindListOptions(), options); |
| 83 | + } |
| 84 | + if (findListOptions.searchParams) { |
| 85 | + findListOptions.searchParams = [...findListOptions.searchParams, ...searchParams]; |
| 86 | + } else { |
| 87 | + findListOptions.searchParams = searchParams; |
| 88 | + } |
| 89 | + |
| 90 | + return this.findListByHref(this.getSearchUrl(), findListOptions, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Unsubscribe from the subscription |
| 95 | + */ |
| 96 | + ngOnDestroy(): void { |
| 97 | + if (this.sub !== undefined) { |
| 98 | + this.sub.unsubscribe(); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments