|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { DataService } from '../data.service'; |
| 3 | +import { RequestService } from '../request.service'; |
| 4 | +import { RemoteDataBuildService } from '../../cache/builders/remote-data-build.service'; |
| 5 | +import { Store } from '@ngrx/store'; |
| 6 | +import { CoreState } from '../../core.reducers'; |
| 7 | +import { ObjectCacheService } from '../../cache/object-cache.service'; |
| 8 | +import { HALEndpointService } from '../../shared/hal-endpoint.service'; |
| 9 | +import { NotificationsService } from '../../../shared/notifications/notifications.service'; |
| 10 | +import { HttpClient } from '@angular/common/http'; |
| 11 | +import { DefaultChangeAnalyzer } from '../default-change-analyzer.service'; |
| 12 | +import { Process } from '../../../process-page/processes/process.model'; |
| 13 | +import { dataService } from '../../cache/builders/build-decorators'; |
| 14 | +import { PROCESS } from '../../../process-page/processes/process.resource-type'; |
| 15 | +import { Observable } from 'rxjs/internal/Observable'; |
| 16 | +import { map, switchMap } from 'rxjs/operators'; |
| 17 | +import { ProcessFilesRequest, RestRequest } from '../request.models'; |
| 18 | +import { configureRequest, filterSuccessfulResponses } from '../../shared/operators'; |
| 19 | +import { GenericSuccessResponse } from '../../cache/response.models'; |
| 20 | +import { PaginatedList } from '../paginated-list'; |
| 21 | +import { Bitstream } from '../../shared/bitstream.model'; |
| 22 | +import { RemoteData } from '../remote-data'; |
| 23 | + |
| 24 | +@Injectable() |
| 25 | +@dataService(PROCESS) |
| 26 | +export class ProcessDataService extends DataService<Process> { |
| 27 | + protected linkPath = 'processes'; |
| 28 | + |
| 29 | + constructor( |
| 30 | + protected requestService: RequestService, |
| 31 | + protected rdbService: RemoteDataBuildService, |
| 32 | + protected store: Store<CoreState>, |
| 33 | + protected objectCache: ObjectCacheService, |
| 34 | + protected halService: HALEndpointService, |
| 35 | + protected notificationsService: NotificationsService, |
| 36 | + protected http: HttpClient, |
| 37 | + protected comparator: DefaultChangeAnalyzer<Process>) { |
| 38 | + super(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Get the endpoint for a process his files |
| 43 | + * @param processId The ID of the process |
| 44 | + */ |
| 45 | + getFilesEndpoint(processId: string): Observable<string> { |
| 46 | + return this.getBrowseEndpoint().pipe( |
| 47 | + switchMap((href) => this.halService.getEndpoint('files', `${href}/${processId}`)) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Get a process his output files |
| 53 | + * @param processId The ID of the process |
| 54 | + */ |
| 55 | + getFiles(processId: string): Observable<RemoteData<PaginatedList<Bitstream>>> { |
| 56 | + const request$ = this.getFilesEndpoint(processId).pipe( |
| 57 | + map((href) => new ProcessFilesRequest(this.requestService.generateRequestId(), href)), |
| 58 | + configureRequest(this.requestService) |
| 59 | + ); |
| 60 | + const requestEntry$ = request$.pipe( |
| 61 | + switchMap((request: RestRequest) => this.requestService.getByHref(request.href)) |
| 62 | + ); |
| 63 | + const payload$ = requestEntry$.pipe( |
| 64 | + filterSuccessfulResponses(), |
| 65 | + map((response: GenericSuccessResponse<PaginatedList<Bitstream>>) => response.payload) |
| 66 | + ); |
| 67 | + |
| 68 | + return this.rdbService.toRemoteDataObservable(requestEntry$, payload$); |
| 69 | + } |
| 70 | +} |
0 commit comments