import { Injectable } from '@angular/core'; import { Operation } from 'fast-json-patch'; import { Observable } from 'rxjs'; import { map, switchMap, take, } from 'rxjs/operators'; import { hasValue } from '../../shared/empty.util'; import { PaginatedSearchOptions } from '../../shared/search/models/paginated-search-options.model'; import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model'; import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; import { ObjectCacheService } from '../cache/object-cache.service'; import { Bitstream } from '../shared/bitstream.model'; import { Bundle } from '../shared/bundle.model'; import { HALEndpointService } from '../shared/hal-endpoint.service'; import { Item } from '../shared/item.model'; import { IdentifiableDataService } from './base/identifiable-data.service'; import { PatchData, PatchDataImpl, } from './base/patch-data'; import { DSOChangeAnalyzer } from './dso-change-analyzer.service'; import { FindListOptions } from './find-list-options.model'; import { PaginatedList } from './paginated-list.model'; import { RemoteData } from './remote-data'; import { GetRequest } from './request.models'; import { RequestService } from './request.service'; import { RequestEntryState } from './request-entry-state.model'; import { RestRequestMethod } from './rest-request-method'; /** * A service to retrieve {@link Bundle}s from the REST API */ @Injectable({ providedIn: 'root' }) export class BundleDataService extends IdentifiableDataService implements PatchData { private bitstreamsEndpoint = 'bitstreams'; private patchData: PatchDataImpl; constructor( protected requestService: RequestService, protected rdbService: RemoteDataBuildService, protected objectCache: ObjectCacheService, protected halService: HALEndpointService, protected comparator: DSOChangeAnalyzer, ) { super('bundles', requestService, rdbService, objectCache, halService); this.patchData = new PatchDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, comparator, this.responseMsToLive, this.constructIdEndpoint); } /** * Retrieve all {@link Bundle}s in the given {@link Item} * * @param item the {@link Item} the {@link Bundle}s are a part of * @param options the {@link FindListOptions} 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 */ findAllByItem(item: Item, options?: FindListOptions, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig[]): Observable>> { return this.findListByHref(item._links.bundles.href, options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow); } /** * Retrieve a {@link Bundle} in the given {@link Item} by name * * @param item the {@link Item} the {@link Bundle}s are a part of * @param bundleName the name of the {@link Bundle} 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 * @param options the {@link FindListOptions} for the request */ // TODO should be implemented rest side findByItemAndName(item: Item, bundleName: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, options?: FindListOptions, ...linksToFollow: FollowLinkConfig[]): Observable> { //Since we filter by bundleName where the pagination options are not indicated we need to load all the possible bundles. // This is a workaround, in substitution of the previously recursive call with expand const paginationOptions = options ?? { elementsPerPage: 9999 }; return this.findAllByItem(item, paginationOptions, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow).pipe( map((rd: RemoteData>) => { if (hasValue(rd.payload) && hasValue(rd.payload.page)) { const matchingBundle = rd.payload.page.find((bundle: Bundle) => bundle.name === bundleName); if (hasValue(matchingBundle)) { return new RemoteData( rd.timeCompleted, rd.msToLive, rd.lastUpdated, RequestEntryState.Success, null, matchingBundle, 200, ); } else { return new RemoteData( rd.timeCompleted, rd.msToLive, rd.lastUpdated, RequestEntryState.Error, `The bundle with name ${bundleName} was not found.`, null, 404, ); } } else { return rd as any; } }), ); } /** * Get the bitstreams endpoint for a bundle * @param bundleId * @param searchOptions */ getBitstreamsEndpoint(bundleId: string, searchOptions?: PaginatedSearchOptions): Observable { return this.getBrowseEndpoint().pipe( switchMap((href: string) => this.halService.getEndpoint(this.bitstreamsEndpoint, `${href}/${bundleId}`)), map((href) => searchOptions ? searchOptions.toRestUrl(href) : href), ); } /** * Get a bundle's bitstreams using paginated search options * @param bundleId The bundle's ID * @param searchOptions The search options to use * @param linksToFollow The {@link FollowLinkConfig}s for the request */ getBitstreams(bundleId: string, searchOptions?: PaginatedSearchOptions, ...linksToFollow: FollowLinkConfig[]): Observable>> { const hrefObs = this.getBitstreamsEndpoint(bundleId, searchOptions); hrefObs.pipe( take(1), ).subscribe((href) => { const request = new GetRequest(this.requestService.generateRequestId(), href); this.requestService.send(request, true); }); return this.rdbService.buildList(hrefObs, ...linksToFollow); } /** * Commit current object changes to the server * @param method The RestRequestMethod for which de server sync buffer should be committed */ public commitUpdates(method?: RestRequestMethod): void { this.patchData.commitUpdates(method); } /** * Send a patch request for a specified object * @param {T} object The object to send a patch request for * @param {Operation[]} operations The patch operations to be performed */ public patch(object: Bundle, operations: Operation[]): Observable> { return this.patchData.patch(object, operations); } /** * Add a new patch to the object cache * The patch is derived from the differences between the given object and its version in the object cache * @param {DSpaceObject} object The given object */ public update(object: Bundle): Observable> { return this.patchData.update(object); } /** * Return a list of operations representing the difference between an object and its latest value in the cache. * @param object the object to resolve to a list of patch operations */ public createPatchFromCache(object: Bundle): Observable { return this.patchData.createPatchFromCache(object); } }