63945: Bitstream pagination and removal of listing per bundle

This commit is contained in:
Kristof De Langhe
2019-07-26 17:33:06 +02:00
parent 27ec828142
commit 4f8fe9e611
6 changed files with 109 additions and 108 deletions

View File

@@ -303,7 +303,8 @@
"failed": { "failed": {
"title": "Error deleting bitstream" "title": "Error deleting bitstream"
} }
} },
"empty": "This item doesn't contain any bitstreams. Click the upload button to create one."
} }
} }
}, },

View File

@@ -20,38 +20,41 @@
</div> </div>
</div> </div>
<table *ngIf="updatesMap && updatesMap.size > 0" class="table table-responsive table-striped table-bordered"> <ds-pagination *ngIf="(updates$ | async) && ((updates$ | async) | dsObjectValues).length > 0"
<tbody> [paginationOptions]="(searchOptions$ | async)?.pagination"
<tr> [pageInfoState]="(bitstreams$ | async)?.payload"
<th>{{'item.edit.bitstreams.headers.name' | translate}}</th> [collectionSize]="(bitstreams$ | async)?.payload?.totalElements"
<th>{{'item.edit.bitstreams.headers.description' | translate}}</th> [sortOptions]="(searchOptions$ | async)?.sort"
<th class="text-center">{{'item.edit.bitstreams.headers.format' | translate}}</th> [hideGear]="true"
<th class="text-center">{{'item.edit.bitstreams.headers.actions' | translate}}</th> [hidePagerWhenSinglePage]="true">
</tr> <table class="table table-responsive table-striped table-bordered">
<ng-container *ngFor="let updatesItem of updatesMap | keyvalue"> <tbody>
<tr> <tr>
<th>{{'item.edit.bitstreams.headers.bundle' | translate}}: {{ updatesItem.key }}</th> <th>{{'item.edit.bitstreams.headers.name' | translate}}</th>
<td class="w-100"></td> <th>{{'item.edit.bitstreams.headers.bundle' | translate}}</th>
<td></td> <th>{{'item.edit.bitstreams.headers.description' | translate}}</th>
<td></td> <th class="text-center">{{'item.edit.bitstreams.headers.format' | translate}}</th>
<th class="text-center">{{'item.edit.bitstreams.headers.actions' | translate}}</th>
</tr> </tr>
<ng-container *ngIf="updatesItem.value"> <ng-container *ngVar="((updates$ | async) | dsObjectValues) as updateValues">
<ng-container *ngVar="((updatesItem.value | async) | dsObjectValues) as updateValues"> <tr *ngFor="let updateValue of updateValues"
<tr *ngFor="let updateValue of updateValues" ds-item-edit-bitstream
ds-item-edit-bitstream [fieldUpdate]="updateValue"
[fieldUpdate]="updateValue" [url]="url"
[url]="url" [ngClass]="{
[ngClass]="{ 'table-warning': updateValue.changeType === 0,
'table-warning': updateValue.changeType === 0, 'table-danger': updateValue.changeType === 2,
'table-danger': updateValue.changeType === 2, 'table-success': updateValue.changeType === 1
'table-success': updateValue.changeType === 1 }">
}"> </tr>
</tr>
</ng-container>
</ng-container> </ng-container>
</ng-container> </tbody>
</tbody> </table>
</table> </ds-pagination>
<div *ngIf="!(updates$ | async) || (updates$ | async) && ((updates$ | async) | dsObjectValues).length === 0" class="alert alert-info w-100 d-inline-block" role="alert">
{{'item.edit.bitstreams.empty' | translate}}
</div>
<div class="button-row bottom"> <div class="button-row bottom">
<div class="my-2 float-right"> <div class="my-2 float-right">
<button class="btn btn-danger" *ngIf="!(isReinstatable() | async)" <button class="btn btn-danger" *ngIf="!(isReinstatable() | async)"

View File

@@ -1,8 +1,8 @@
import { ChangeDetectorRef, Component, Inject, OnDestroy } from '@angular/core'; import { ChangeDetectorRef, Component, Inject, OnDestroy } from '@angular/core';
import { AbstractItemUpdateComponent } from '../abstract-item-update/abstract-item-update.component'; import { AbstractItemUpdateComponent } from '../abstract-item-update/abstract-item-update.component';
import { filter, map, switchMap, take } from 'rxjs/operators'; import { filter, map, switchMap, take, tap } from 'rxjs/operators';
import { Bitstream } from '../../../core/shared/bitstream.model'; import { Bitstream } from '../../../core/shared/bitstream.model';
import { toBitstreamsArray, toBundleMap } from '../../../core/shared/item-bitstreams-utils'; import { toBitstreamsArray } from '../../../core/shared/item-bitstreams-utils';
import { Observable } from 'rxjs/internal/Observable'; import { Observable } from 'rxjs/internal/Observable';
import { FieldUpdate, FieldUpdates } from '../../../core/data/object-updates/object-updates.reducer'; import { FieldUpdate, FieldUpdates } from '../../../core/data/object-updates/object-updates.reducer';
import { Subscription } from 'rxjs/internal/Subscription'; import { Subscription } from 'rxjs/internal/Subscription';
@@ -22,8 +22,10 @@ import { RequestService } from '../../../core/data/request.service';
import { getSucceededRemoteData } from '../../../core/shared/operators'; import { getSucceededRemoteData } from '../../../core/shared/operators';
import { Item } from '../../../core/shared/item.model'; import { Item } from '../../../core/shared/item.model';
import { RemoteData } from '../../../core/data/remote-data'; import { RemoteData } from '../../../core/data/remote-data';
import { PaginatedList } from '../../../core/data/paginated-list';
export const ORIGINAL_BUNDLE = 'ORIGINAL'; import { SearchConfigurationService } from '../../../+search-page/search-service/search-configuration.service';
import { PaginatedSearchOptions } from '../../../+search-page/paginated-search-options.model';
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
@Component({ @Component({
selector: 'ds-item-bitstreams', selector: 'ds-item-bitstreams',
@@ -36,16 +38,14 @@ export const ORIGINAL_BUNDLE = 'ORIGINAL';
export class ItemBitstreamsComponent extends AbstractItemUpdateComponent implements OnDestroy { export class ItemBitstreamsComponent extends AbstractItemUpdateComponent implements OnDestroy {
/** /**
* A Map of the current item's bundles and respective FieldUpdates * The currently listed bitstreams
* key: Bundle Name
* value: FieldUpdates about the bundle's bitstreams
*/ */
updatesMap: Map<string, Observable<FieldUpdates>>; bitstreams$: BehaviorSubject<RemoteData<PaginatedList<Bitstream>>> = new BehaviorSubject<RemoteData<PaginatedList<Bitstream>>>(null);
/** /**
* A subscription keeping the updatesMap up-to-date * The current paginated search options
*/ */
updatesMapSub: Subscription; searchOptions$: Observable<PaginatedSearchOptions>;
/** /**
* A subscription that checks when the item is deleted in cache and reloads the item by sending a new request * A subscription that checks when the item is deleted in cache and reloads the item by sending a new request
@@ -53,6 +53,11 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
*/ */
itemUpdateSubscription: Subscription; itemUpdateSubscription: Subscription;
/**
* A subscription keeping track of the current search options and applying them to the bitstreams$ observable
*/
bitstreamsSubscription: Subscription;
constructor( constructor(
public itemService: ItemDataService, public itemService: ItemDataService,
public objectUpdatesService: ObjectUpdatesService, public objectUpdatesService: ObjectUpdatesService,
@@ -64,7 +69,8 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
public bitstreamService: BitstreamDataService, public bitstreamService: BitstreamDataService,
public objectCache: ObjectCacheService, public objectCache: ObjectCacheService,
public requestService: RequestService, public requestService: RequestService,
public cdRef: ChangeDetectorRef public cdRef: ChangeDetectorRef,
public searchConfig: SearchConfigurationService
) { ) {
super(itemService, objectUpdatesService, router, notificationsService, translateService, EnvConfig, route); super(itemService, objectUpdatesService, router, notificationsService, translateService, EnvConfig, route);
} }
@@ -74,6 +80,8 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
*/ */
ngOnInit(): void { ngOnInit(): void {
super.ngOnInit(); super.ngOnInit();
this.searchOptions$ = this.searchConfig.paginatedSearchOptions;
this.initializeBitstreamsUpdate();
this.initializeItemUpdate(); this.initializeItemUpdate();
} }
@@ -88,35 +96,17 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
* Initialize the original fields for the object-updates-service * Initialize the original fields for the object-updates-service
*/ */
initializeOriginalFields(): void { initializeOriginalFields(): void {
this.item.bitstreams.pipe( this.objectUpdatesService.initialize(this.url, [], this.item.lastModified);
toBitstreamsArray(),
take(1)
).subscribe((bitstreams: Bitstream[]) => {
this.objectUpdatesService.initialize(this.url, bitstreams, this.item.lastModified);
});
} }
/** /**
* Initialize field updates * Initialize field updates
*/ */
initializeUpdates(): void { initializeUpdates(): void {
this.updates$ = this.item.bitstreams.pipe( this.updates$ = this.bitstreams$.pipe(
toBitstreamsArray(), toBitstreamsArray(),
switchMap((bitstreams: Bitstream[]) => this.objectUpdatesService.getFieldUpdates(this.url, bitstreams)) switchMap((bitstreams: Bitstream[]) => this.objectUpdatesService.getFieldUpdatesExclusive(this.url, bitstreams))
); );
this.updatesMapSub = this.item.bitstreams.pipe(
toBundleMap()
).subscribe((bundleMap: Map<string, Bitstream[]>) => {
const updatesMap = new Map();
bundleMap.forEach((bitstreams: Bitstream[], bundleName: string) => {
updatesMap.set(bundleName, this.objectUpdatesService.getFieldUpdatesExclusive(this.url, bitstreams));
});
if (updatesMap.size === 0) {
// Add an ORIGINAL bundle by default if the item doesn't contain any bitstreams
updatesMap.set(ORIGINAL_BUNDLE, undefined);
}
this.updatesMap = updatesMap;
});
} }
/** /**
@@ -132,17 +122,31 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
this.item = itemRD.payload; this.item = itemRD.payload;
this.initializeOriginalFields(); this.initializeOriginalFields();
this.initializeUpdates(); this.initializeUpdates();
// Navigate back to the first page to force a reload of the bitstream page
this.router.navigate([this.url], { queryParamsHandling: 'merge', queryParams: { page: 0 } });
this.cdRef.detectChanges(); this.cdRef.detectChanges();
}); });
} }
/**
* Initialize the bitstream update subscription, which keeps track of the current search options and applies
* them to the bitstreams$ observable by sending out a REST request
*/
initializeBitstreamsUpdate(): void {
this.bitstreamsSubscription = this.searchOptions$.pipe(
switchMap((searchOptions) => this.itemService.getBitstreams(this.item.id, searchOptions))
).subscribe((bitsreams: RemoteData<PaginatedList<Bitstream>>) => {
this.bitstreams$.next(bitsreams);
});
}
/** /**
* Submit the current changes * Submit the current changes
* Bitstreams marked as deleted send out a delete request to the rest API * Bitstreams marked as deleted send out a delete request to the rest API
* Display notifications and reset the current item/updates * Display notifications and reset the current item/updates
*/ */
submit() { submit() {
const removedBitstreams$ = this.item.bitstreams.pipe( const removedBitstreams$ = this.bitstreams$.pipe(
toBitstreamsArray(), toBitstreamsArray(),
switchMap((bitstreams: Bitstream[]) => this.objectUpdatesService.getFieldUpdatesExclusive(this.url, bitstreams) as Observable<FieldUpdates>), switchMap((bitstreams: Bitstream[]) => this.objectUpdatesService.getFieldUpdatesExclusive(this.url, bitstreams) as Observable<FieldUpdates>),
map((fieldUpdates: FieldUpdates) => Object.values(fieldUpdates).filter((fieldUpdate: FieldUpdate) => fieldUpdate.changeType === FieldChangeType.REMOVE)), map((fieldUpdates: FieldUpdates) => Object.values(fieldUpdates).filter((fieldUpdate: FieldUpdate) => fieldUpdate.changeType === FieldChangeType.REMOVE)),
@@ -180,7 +184,6 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
* De-cache the current item (it should automatically reload due to itemUpdateSubscription) * De-cache the current item (it should automatically reload due to itemUpdateSubscription)
*/ */
reset() { reset() {
this.updatesMap = undefined;
this.refreshItemCache(); this.refreshItemCache();
this.initializeItemUpdate(); this.initializeItemUpdate();
} }
@@ -197,7 +200,7 @@ export class ItemBitstreamsComponent extends AbstractItemUpdateComponent impleme
* Unsubscribe from open subscriptions whenever the component gets destroyed * Unsubscribe from open subscriptions whenever the component gets destroyed
*/ */
ngOnDestroy(): void { ngOnDestroy(): void {
this.updatesMapSub.unsubscribe();
this.itemUpdateSubscription.unsubscribe(); this.itemUpdateSubscription.unsubscribe();
this.bitstreamsSubscription.unsubscribe();
} }
} }

View File

@@ -1,7 +1,10 @@
<td> <td class="w-100">
{{ bitstream.name }} {{ bitstream.name }}
</td> </td>
<td class="w-100"> <td>
{{ bitstream.bundleName }}
</td>
<td>
{{ bitstream.description }} {{ bitstream.description }}
</td> </td>
<td class="text-center"> <td class="text-center">

View File

@@ -1,4 +1,4 @@
import { distinctUntilChanged, filter, map } from 'rxjs/operators'; import { distinctUntilChanged, filter, map, take } from 'rxjs/operators';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
@@ -12,7 +12,7 @@ import { URLCombiner } from '../url-combiner/url-combiner';
import { DataService } from './data.service'; import { DataService } from './data.service';
import { RequestService } from './request.service'; import { RequestService } from './request.service';
import { HALEndpointService } from '../shared/hal-endpoint.service'; import { HALEndpointService } from '../shared/hal-endpoint.service';
import { FindAllOptions, PatchRequest, RestRequest } from './request.models'; import { FindAllOptions, GetRequest, PatchRequest, RestRequest } from './request.models';
import { ObjectCacheService } from '../cache/object-cache.service'; import { ObjectCacheService } from '../cache/object-cache.service';
import { NotificationsService } from '../../shared/notifications/notifications.service'; import { NotificationsService } from '../../shared/notifications/notifications.service';
import { DSOChangeAnalyzer } from './dso-change-analyzer.service'; import { DSOChangeAnalyzer } from './dso-change-analyzer.service';
@@ -20,6 +20,10 @@ import { HttpClient } from '@angular/common/http';
import { NormalizedObjectBuildService } from '../cache/builders/normalized-object-build.service'; import { NormalizedObjectBuildService } from '../cache/builders/normalized-object-build.service';
import { configureRequest, getRequestFromRequestHref } from '../shared/operators'; import { configureRequest, getRequestFromRequestHref } from '../shared/operators';
import { RequestEntry } from './request.reducer'; import { RequestEntry } from './request.reducer';
import { PaginatedSearchOptions } from '../../+search-page/paginated-search-options.model';
import { RemoteData } from './remote-data';
import { PaginatedList } from './paginated-list';
import { Bitstream } from '../shared/bitstream.model';
@Injectable() @Injectable()
export class ItemDataService extends DataService<Item> { export class ItemDataService extends DataService<Item> {
@@ -118,4 +122,25 @@ export class ItemDataService extends DataService<Item> {
map((requestEntry: RequestEntry) => requestEntry.response) map((requestEntry: RequestEntry) => requestEntry.response)
); );
} }
/**
* Get an item's bitstreams using paginated search options
* @param itemId The item's ID
* @param searchOptions The search options to use
*/
public getBitstreams(itemId: string, searchOptions?: PaginatedSearchOptions): Observable<RemoteData<PaginatedList<Bitstream>>> {
const hrefObs = this.getItemWithdrawEndpoint(itemId).pipe(
map((href) => `${href}/bitstreams`),
map((href) => searchOptions ? searchOptions.toRestUrl(href) : href)
);
hrefObs.pipe(
take(1)
).subscribe((href) => {
const request = new GetRequest(this.requestService.generateRequestId(), href);
this.requestService.configure(request);
});
return this.rdbService.buildList<Bitstream>(hrefObs);
}
} }

View File

@@ -3,48 +3,14 @@ import { RemoteData } from '../data/remote-data';
import { PaginatedList } from '../data/paginated-list'; import { PaginatedList } from '../data/paginated-list';
import { Bitstream } from './bitstream.model'; import { Bitstream } from './bitstream.model';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { combineLatest as observableCombineLatest } from 'rxjs'; import { hasValueOperator } from '../../shared/empty.util';
import { getSucceededRemoteData } from './operators';
/**
* Operator for turning the current page of bitstreams into an array
*/
export const toBitstreamsArray = () => export const toBitstreamsArray = () =>
(source: Observable<RemoteData<PaginatedList<Bitstream>>>): Observable<Bitstream[]> => (source: Observable<RemoteData<PaginatedList<Bitstream>>>): Observable<Bitstream[]> =>
source.pipe( source.pipe(
getSucceededRemoteData(), hasValueOperator(),
map((bitstreamRD: RemoteData<PaginatedList<Bitstream>>) => bitstreamRD.payload.page) map((bitstreamRD: RemoteData<PaginatedList<Bitstream>>) => bitstreamRD.payload.page)
); );
export const getBundleNames = () =>
(source: Observable<RemoteData<PaginatedList<Bitstream>>>): Observable<string[]> =>
source.pipe(
toBitstreamsArray(),
map((bitstreams: Bitstream[]) => {
const result = [];
bitstreams.forEach((bitstream: Bitstream) => {
if (result.indexOf(bitstream.bundleName) < 0) {
result.push(bitstream.bundleName);
}
});
return result;
})
);
export const filterByBundleName = (bundleName: string) =>
(source: Observable<RemoteData<PaginatedList<Bitstream>>>): Observable<Bitstream[]> =>
source.pipe(
toBitstreamsArray(),
map((bitstreams: Bitstream[]) =>
bitstreams.filter((bitstream: Bitstream) => bitstream.bundleName === bundleName)
)
);
export const toBundleMap = () =>
(source: Observable<RemoteData<PaginatedList<Bitstream>>>): Observable<Map<string, Bitstream[]>> =>
observableCombineLatest(source.pipe(toBitstreamsArray()), source.pipe(getBundleNames())).pipe(
map(([bitstreams, bundleNames]) => {
const bundleMap = new Map();
bundleNames.forEach((bundleName: string) => {
bundleMap.set(bundleName, bitstreams.filter((bitstream: Bitstream) => bitstream.bundleName === bundleName));
});
return bundleMap;
})
);