|
| 1 | +/** |
| 2 | + * The contents of this file are subject to the license and copyright |
| 3 | + * detailed in the LICENSE and NOTICE files at the root of the source |
| 4 | + * tree and available online at |
| 5 | + * |
| 6 | + * http://www.dspace.org/license/ |
| 7 | + */ |
| 8 | +import { Injectable } from '@angular/core'; |
| 9 | +import { Observable } from 'rxjs'; |
| 10 | +import { map } from 'rxjs/operators'; |
| 11 | + |
| 12 | +import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; |
| 13 | +import { RequestParam } from '../cache/models/request-param.model'; |
| 14 | +import { ObjectCacheService } from '../cache/object-cache.service'; |
| 15 | +import { DeleteDataImpl } from '../data/base/delete-data'; |
| 16 | +import { IdentifiableDataService } from '../data/base/identifiable-data.service'; |
| 17 | +import { SearchDataImpl } from '../data/base/search-data'; |
| 18 | +import { FindListOptions } from '../data/find-list-options.model'; |
| 19 | +import { PaginatedList } from '../data/paginated-list.model'; |
| 20 | +import { RemoteData } from '../data/remote-data'; |
| 21 | +import { RequestService } from '../data/request.service'; |
| 22 | +import { NotificationsService } from '../notification-system/notifications.service'; |
| 23 | +import { HALEndpointService } from '../shared/hal-endpoint.service'; |
| 24 | +import { |
| 25 | + getAllSucceededRemoteDataPayload, |
| 26 | + getPaginatedListPayload, |
| 27 | +} from '../shared/operators'; |
| 28 | +import { EditItem } from './models/edititem.model'; |
| 29 | +import { EditItemMode } from './models/edititem-mode.model'; |
| 30 | + |
| 31 | +/** |
| 32 | + * A service that provides methods to make REST requests with edititems endpoint. |
| 33 | + */ |
| 34 | +@Injectable({ providedIn: 'root' }) |
| 35 | +export class EditItemDataService extends IdentifiableDataService<EditItem> { |
| 36 | + protected linkPath = 'edititems'; |
| 37 | + protected searchById = 'findModesById'; |
| 38 | + private searchData: SearchDataImpl<EditItemMode>; |
| 39 | + private deleteData: DeleteDataImpl<EditItem>; |
| 40 | + |
| 41 | + constructor( |
| 42 | + protected requestService: RequestService, |
| 43 | + protected rdbService: RemoteDataBuildService, |
| 44 | + protected objectCache: ObjectCacheService, |
| 45 | + protected halService: HALEndpointService, |
| 46 | + protected notificationsService: NotificationsService, |
| 47 | + ) { |
| 48 | + super('edititems', requestService, rdbService, objectCache, halService); |
| 49 | + |
| 50 | + this.searchData = new SearchDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, this.responseMsToLive); |
| 51 | + this.deleteData = new DeleteDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, notificationsService, this.responseMsToLive, this.constructIdEndpoint); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Search for editModes from the editItem id |
| 56 | + * |
| 57 | + * @param id string id of edit item |
| 58 | + * @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's |
| 59 | + * no valid cached version. Defaults to true |
| 60 | + * @param reRequestOnStale Whether or not the request should automatically be re- |
| 61 | + * requested after the response becomes stale |
| 62 | + * @return Paginated list of edit item modes |
| 63 | + */ |
| 64 | + searchEditModesById(id: string, useCachedVersionIfAvailable = true, reRequestOnStale = true): Observable<RemoteData<PaginatedList<EditItemMode>>> { |
| 65 | + const options = new FindListOptions(); |
| 66 | + options.searchParams = [ |
| 67 | + new RequestParam('uuid', id, false), |
| 68 | + ]; |
| 69 | + return this.searchData.searchBy(this.searchById, options, useCachedVersionIfAvailable, reRequestOnStale); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Check if editMode with id is part of the edit item with id |
| 74 | + * |
| 75 | + * @param id string id of edit item |
| 76 | + * @param editModeId string id of edit item |
| 77 | + * @return boolean |
| 78 | + */ |
| 79 | + checkEditModeByIdAndType(id: string, editModeId: string) { |
| 80 | + return this.searchEditModesById(id).pipe( |
| 81 | + getAllSucceededRemoteDataPayload(), |
| 82 | + getPaginatedListPayload(), |
| 83 | + map((editModes: EditItemMode[]) => { |
| 84 | + return !!editModes.find(editMode => editMode.uuid === editModeId); |
| 85 | + })); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Invalidate the cache of the editMode |
| 90 | + * @param id |
| 91 | + */ |
| 92 | + invalidateItemCache(id: string) { |
| 93 | + this.requestService.setStaleByHrefSubstring('findModesById?uuid=' + id); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments