1
0
Files
yel-dspace-angular/src/app/core/data/item-data.service.ts
lotte 6dba02ca67 Merge branch 'master' into community-and-collection-forms
Conflicts:
	src/app/core/data/item-data.service.spec.ts
	src/app/core/data/item-data.service.ts
	src/app/core/shared/operators.spec.ts
	src/app/core/shared/operators.ts
	src/app/shared/shared.module.ts
2019-01-22 08:34:51 +01:00

151 lines
5.4 KiB
TypeScript

import {distinctUntilChanged, map, filter} from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { isNotEmpty } from '../../shared/empty.util';
import { BrowseService } from '../browse/browse.service';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { NormalizedItem } from '../cache/models/normalized-item.model';
import { CoreState } from '../core.reducers';
import { Item } from '../shared/item.model';
import { URLCombiner } from '../url-combiner/url-combiner';
import { DataService } from './data.service';
import { RequestService } from './request.service';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { DeleteRequest, FindAllOptions, PatchRequest, RestRequest } from './request.models';
import { ObjectCacheService } from '../cache/object-cache.service';
import { NotificationsService } from '../../shared/notifications/notifications.service';
import { HttpClient } from '@angular/common/http';
import { NormalizedObjectBuildService } from '../cache/builders/normalized-object-build.service';
import { DSOChangeAnalyzer } from './dso-change-analyzer.service';
import { configureRequest, getRequestFromRequestHref } from '../shared/operators';
import { RequestEntry } from './request.reducer';
@Injectable()
export class ItemDataService extends DataService<NormalizedItem, Item> {
protected linkPath = 'items';
constructor(
protected requestService: RequestService,
protected rdbService: RemoteDataBuildService,
protected dataBuildService: NormalizedObjectBuildService,
protected store: Store<CoreState>,
private bs: BrowseService,
protected objectCache: ObjectCacheService,
protected halService: HALEndpointService,
protected notificationsService: NotificationsService,
protected http: HttpClient,
protected comparator: DSOChangeAnalyzer) {
super();
}
/**
* Get the endpoint for browsing items
* (When options.sort.field is empty, the default field to browse by will be 'dc.date.issued')
* @param {FindAllOptions} options
* @returns {Observable<string>}
*/
public getBrowseEndpoint(options: FindAllOptions = {}, linkPath: string = this.linkPath): Observable<string> {
let field = 'dc.date.issued';
if (options.sort && options.sort.field) {
field = options.sort.field;
}
return this.bs.getBrowseURLFor(field, linkPath).pipe(
filter((href: string) => isNotEmpty(href)),
map((href: string) => new URLCombiner(href, `?scope=${options.scopeID}`).toString()),
distinctUntilChanged(),);
}
/**
* Get the endpoint for item withdrawal and reinstatement
* @param itemId
*/
public getItemWithdrawEndpoint(itemId: string): Observable<string> {
return this.halService.getEndpoint(this.linkPath).pipe(
map((endpoint: string) => this.getFindByIDHref(endpoint, itemId))
);
}
/**
* Get the endpoint to make item private and public
* @param itemId
*/
public getItemDiscoverableEndpoint(itemId: string): Observable<string> {
return this.halService.getEndpoint(this.linkPath).pipe(
map((endpoint: string) => this.getFindByIDHref(endpoint, itemId))
);
}
/**
* Get the endpoint to delete the item
* @param itemId
*/
public getItemDeleteEndpoint(itemId: string): Observable<string> {
return this.halService.getEndpoint(this.linkPath).pipe(
map((endpoint: string) => this.getFindByIDHref(endpoint, itemId))
);
}
/**
* Set the isWithdrawn state of an item to a specified state
* @param itemId
* @param withdrawn
*/
public setWithDrawn(itemId: string, withdrawn: boolean) {
const patchOperation = [{
op: 'replace', path: '/withdrawn', value: withdrawn
}];
return this.getItemWithdrawEndpoint(itemId).pipe(
distinctUntilChanged(),
map((endpointURL: string) =>
new PatchRequest(this.requestService.generateRequestId(), endpointURL, patchOperation)
),
configureRequest(this.requestService),
map((request: RestRequest) => request.href),
getRequestFromRequestHref(this.requestService),
map((requestEntry: RequestEntry) => requestEntry.response)
);
}
/**
* Set the isDiscoverable state of an item to a specified state
* @param itemId
* @param discoverable
*/
public setDiscoverable(itemId: string, discoverable: boolean) {
const patchOperation = [{
op: 'replace', path: '/discoverable', value: discoverable
}];
return this.getItemDiscoverableEndpoint(itemId).pipe(
distinctUntilChanged(),
map((endpointURL: string) =>
new PatchRequest(this.requestService.generateRequestId(), endpointURL, patchOperation)
),
configureRequest(this.requestService),
map((request: RestRequest) => request.href),
getRequestFromRequestHref(this.requestService),
map((requestEntry: RequestEntry) => requestEntry.response)
);
}
/**
* Delete the item
* @param itemId
*/
public delete(itemId: string) {
return this.getItemDeleteEndpoint(itemId).pipe(
distinctUntilChanged(),
map((endpointURL: string) =>
new DeleteRequest(this.requestService.generateRequestId(), endpointURL)
),
configureRequest(this.requestService),
map((request: RestRequest) => request.href),
getRequestFromRequestHref(this.requestService),
map((requestEntry: RequestEntry) => requestEntry.response)
);
}
}