1578: primaryBitstream implementation

This commit is contained in:
lotte
2023-04-25 16:12:00 +02:00
parent 4847fc6f7a
commit 7008afd05f
3 changed files with 243 additions and 53 deletions

View File

@@ -24,6 +24,8 @@ import { createPaginatedList } from '../../shared/testing/utils.test';
import { Item } from '../../core/shared/item.model'; import { Item } from '../../core/shared/item.model';
import { MetadataValueFilter } from '../../core/shared/metadata.models'; import { MetadataValueFilter } from '../../core/shared/metadata.models';
import { DSONameService } from '../../core/breadcrumbs/dso-name.service'; import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
import { Bundle } from '../../core/shared/bundle.model';
import { BundleDataService } from '../../core/data/bundle-data.service';
const infoNotification: INotification = new Notification('id', NotificationType.Info, 'info'); const infoNotification: INotification = new Notification('id', NotificationType.Info, 'info');
const warningNotification: INotification = new Notification('id', NotificationType.Warning, 'warning'); const warningNotification: INotification = new Notification('id', NotificationType.Warning, 'warning');
@@ -35,9 +37,14 @@ let bitstreamService: BitstreamDataService;
let bitstreamFormatService: BitstreamFormatDataService; let bitstreamFormatService: BitstreamFormatDataService;
let dsoNameService: DSONameService; let dsoNameService: DSONameService;
let bitstream: Bitstream; let bitstream: Bitstream;
let bitstreamID: string;
let selectedFormat: BitstreamFormat; let selectedFormat: BitstreamFormat;
let allFormats: BitstreamFormat[]; let allFormats: BitstreamFormat[];
let router: Router; let router: Router;
let bundleDataService;
let bundleWithCurrentPrimary: Bundle;
let bundleWithDifferentPrimary: Bundle;
let bundleWithNoPrimary: Bundle;
let comp: EditBitstreamPageComponent; let comp: EditBitstreamPageComponent;
let fixture: ComponentFixture<EditBitstreamPageComponent>; let fixture: ComponentFixture<EditBitstreamPageComponent>;
@@ -45,6 +52,12 @@ let fixture: ComponentFixture<EditBitstreamPageComponent>;
describe('EditBitstreamPageComponent', () => { describe('EditBitstreamPageComponent', () => {
beforeEach(() => { beforeEach(() => {
bitstreamID = 'current-bitstream-id';
bundleWithCurrentPrimary = Object.assign(new Bundle(), { 'primaryBitstreamUUID': bitstreamID });
bundleWithDifferentPrimary = Object.assign(new Bundle(), { 'primaryBitstreamUUID': '12345-abcde-54321-edcba' });
bundleWithNoPrimary = Object.assign(new Bundle(), { 'primaryBitstreamUUID': null });
bundleDataService = jasmine.createSpyObj('BundleDataService', ['patch']);
bundleDataService.patch.and.callFake((a, b) => createSuccessfulRemoteDataObject$(a));
allFormats = [ allFormats = [
Object.assign({ Object.assign({
id: '1', id: '1',
@@ -112,6 +125,8 @@ describe('EditBitstreamPageComponent', () => {
const bundleName = 'ORIGINAL'; const bundleName = 'ORIGINAL';
bitstream = Object.assign(new Bitstream(), { bitstream = Object.assign(new Bitstream(), {
uuid: bitstreamID,
id: bitstreamID,
metadata: { metadata: {
'dc.description': [ 'dc.description': [
{ {
@@ -157,7 +172,8 @@ describe('EditBitstreamPageComponent', () => {
providers: [ providers: [
{ provide: NotificationsService, useValue: notificationsService }, { provide: NotificationsService, useValue: notificationsService },
{ provide: DynamicFormService, useValue: formService }, { provide: DynamicFormService, useValue: formService },
{provide: ActivatedRoute, {
provide: ActivatedRoute,
useValue: { useValue: {
data: observableOf({ bitstream: createSuccessfulRemoteDataObject(bitstream) }), data: observableOf({ bitstream: createSuccessfulRemoteDataObject(bitstream) }),
snapshot: { queryParams: {} } snapshot: { queryParams: {} }
@@ -166,6 +182,7 @@ describe('EditBitstreamPageComponent', () => {
{ provide: BitstreamDataService, useValue: bitstreamService }, { provide: BitstreamDataService, useValue: bitstreamService },
{ provide: DSONameService, useValue: dsoNameService }, { provide: DSONameService, useValue: dsoNameService },
{ provide: BitstreamFormatDataService, useValue: bitstreamFormatService }, { provide: BitstreamFormatDataService, useValue: bitstreamFormatService },
{ provide: BundleDataService, useValue: bundleDataService },
ChangeDetectorRef ChangeDetectorRef
], ],
schemas: [NO_ERRORS_SCHEMA] schemas: [NO_ERRORS_SCHEMA]
@@ -203,6 +220,27 @@ describe('EditBitstreamPageComponent', () => {
it('should put the \"New Format\" input on invisible', () => { it('should put the \"New Format\" input on invisible', () => {
expect(comp.formLayout.newFormat.grid.host).toContain('invisible'); expect(comp.formLayout.newFormat.grid.host).toContain('invisible');
}); });
describe('when the bitstream is the primary bitstream on the bundle', () => {
beforeEach(() => {
(comp as any).bundle = bundleWithCurrentPrimary;
comp.setForm();
rawForm = comp.formGroup.getRawValue();
});
it('should enable the primary bitstream toggle', () => {
expect(rawForm.fileNamePrimaryContainer.primaryBitstream).toEqual(true);
});
});
describe('when the bitstream is not the primary bitstream on the bundle', () => {
beforeEach(() => {
(comp as any).bundle = bundleWithDifferentPrimary;
comp.setForm();
rawForm = comp.formGroup.getRawValue();
});
it('should disable the primary bitstream toggle', () => {
expect(rawForm.fileNamePrimaryContainer.primaryBitstream).toEqual(false);
});
});
}); });
describe('when an unknown format is selected', () => { describe('when an unknown format is selected', () => {
@@ -216,6 +254,95 @@ describe('EditBitstreamPageComponent', () => {
}); });
describe('onSubmit', () => { describe('onSubmit', () => {
describe('when the primaryBitstream changed', () => {
describe('to the current bitstream', () => {
beforeEach(() => {
const rawValue = Object.assign(comp.formGroup.getRawValue(), { fileNamePrimaryContainer: { primaryBitstream: true } });
spyOn(comp.formGroup, 'getRawValue').and.returnValue(rawValue);
});
describe('from a different primary bitstream', () => {
beforeEach(() => {
(comp as any).bundle = bundleWithDifferentPrimary;
comp.onSubmit();
});
it('should call patch with a replace operation', () => {
expect(bundleDataService.patch).toHaveBeenCalledWith(bundleWithDifferentPrimary, [jasmine.objectContaining({
op: 'replace'
})]);
});
it('should call patch with the correct bitstream uuid', () => {
expect(bundleDataService.patch).toHaveBeenCalledWith(bundleWithDifferentPrimary, [jasmine.objectContaining({
value: bitstreamID
})]);
});
});
describe('from no primary bitstream', () => {
beforeEach(() => {
(comp as any).bundle = bundleWithNoPrimary;
comp.onSubmit();
});
it('should call patch with an add operation', () => {
expect(bundleDataService.patch).toHaveBeenCalledWith(bundleWithNoPrimary, [jasmine.objectContaining({
op: 'add'
})]);
});
it('should call patch with the correct bitstream uuid', () => {
expect(bundleDataService.patch).toHaveBeenCalledWith(bundleWithNoPrimary, [jasmine.objectContaining({
value: bitstreamID
})]);
});
});
});
describe('to no primary bitstream', () => {
beforeEach(() => {
const rawValue = Object.assign(comp.formGroup.getRawValue(), { fileNamePrimaryContainer: { primaryBitstream: false } });
spyOn(comp.formGroup, 'getRawValue').and.returnValue(rawValue);
});
describe('from the current bitstream', () => {
beforeEach(() => {
(comp as any).bundle = bundleWithCurrentPrimary;
comp.onSubmit();
});
it('should call patch with a remove operation', () => {
expect(bundleDataService.patch).toHaveBeenCalledWith(bundleWithCurrentPrimary, [jasmine.objectContaining({
op: 'remove'
})]);
});
});
});
});
describe('when the primaryBitstream did not changed', () => {
describe('the current bitstream stayed the primary bitstream', () => {
beforeEach(() => {
const rawValue = Object.assign(comp.formGroup.getRawValue(), { fileNamePrimaryContainer: { primaryBitstream: true } });
spyOn(comp.formGroup, 'getRawValue').and.returnValue(rawValue);
(comp as any).bundle = bundleWithCurrentPrimary;
comp.onSubmit();
});
it('should not call patch on the bundle data service', () => {
expect(bundleDataService.patch).not.toHaveBeenCalled();
});
});
describe('the bitstream was not and did not become the primary bitstream', () => {
beforeEach(() => {
const rawValue = Object.assign(comp.formGroup.getRawValue(), { fileNamePrimaryContainer: { primaryBitstream: false } });
spyOn(comp.formGroup, 'getRawValue').and.returnValue(rawValue);
(comp as any).bundle = bundleWithDifferentPrimary;
comp.onSubmit();
});
it('should not call patch on the bundle data service', () => {
expect(bundleDataService.patch).not.toHaveBeenCalled();
});
});
});
describe('when selected format hasn\'t changed', () => { describe('when selected format hasn\'t changed', () => {
beforeEach(() => { beforeEach(() => {
comp.onSubmit(); comp.onSubmit();
@@ -357,6 +484,7 @@ describe('EditBitstreamPageComponent', () => {
{provide: BitstreamDataService, useValue: bitstreamService}, {provide: BitstreamDataService, useValue: bitstreamService},
{provide: DSONameService, useValue: dsoNameService}, {provide: DSONameService, useValue: dsoNameService},
{provide: BitstreamFormatDataService, useValue: bitstreamFormatService}, {provide: BitstreamFormatDataService, useValue: bitstreamFormatService},
{ provide: BundleDataService, useValue: bundleDataService },
ChangeDetectorRef ChangeDetectorRef
], ],
schemas: [NO_ERRORS_SCHEMA] schemas: [NO_ERRORS_SCHEMA]
@@ -371,7 +499,6 @@ describe('EditBitstreamPageComponent', () => {
spyOn(router, 'navigate'); spyOn(router, 'navigate');
}); });
describe('on startup', () => { describe('on startup', () => {
let rawForm; let rawForm;
@@ -475,6 +602,7 @@ describe('EditBitstreamPageComponent', () => {
{provide: BitstreamDataService, useValue: bitstreamService}, {provide: BitstreamDataService, useValue: bitstreamService},
{provide: DSONameService, useValue: dsoNameService}, {provide: DSONameService, useValue: dsoNameService},
{provide: BitstreamFormatDataService, useValue: bitstreamFormatService}, {provide: BitstreamFormatDataService, useValue: bitstreamFormatService},
{ provide: BundleDataService, useValue: bundleDataService },
ChangeDetectorRef ChangeDetectorRef
], ],
schemas: [NO_ERRORS_SCHEMA] schemas: [NO_ERRORS_SCHEMA]

View File

@@ -52,6 +52,7 @@ import {
DsDynamicInputModel DsDynamicInputModel
} from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model'; } from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model';
import { DsDynamicTextAreaModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-textarea.model'; import { DsDynamicTextAreaModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-textarea.model';
import { BundleDataService } from '../../core/data/bundle-data.service';
@Component({ @Component({
selector: 'ds-edit-bitstream-page', selector: 'ds-edit-bitstream-page',
@@ -380,13 +381,23 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
*/ */
isIIIF = false; isIIIF = false;
/** /**
* Array to track all subscriptions and unsubscribe them onDestroy * Array to track all subscriptions and unsubscribe them onDestroy
* @type {Array} * @type {Array}
*/ */
protected subs: Subscription[] = []; protected subs: Subscription[] = [];
/**
* The parent bundle containing the Bitstream
* @private
*/
private bundle: Bundle;
/**
* Path to patch primary bitstream on the bundle
* @private
*/
private readonly primaryBitstreamPath = '/primarybitstream';
constructor(private route: ActivatedRoute, constructor(private route: ActivatedRoute,
private router: Router, private router: Router,
@@ -397,7 +408,8 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
private bitstreamService: BitstreamDataService, private bitstreamService: BitstreamDataService,
private dsoNameService: DSONameService, private dsoNameService: DSONameService,
private notificationsService: NotificationsService, private notificationsService: NotificationsService,
private bitstreamFormatService: BitstreamFormatDataService) { private bitstreamFormatService: BitstreamFormatDataService,
private bundleService: BundleDataService) {
} }
/** /**
@@ -423,13 +435,20 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
getRemoteDataPayload() getRemoteDataPayload()
); );
const bundle$ = bitstream$.pipe(
switchMap((bitstream: Bitstream) => bitstream.bundle),
getFirstSucceededRemoteDataPayload(),
);
this.subs.push( this.subs.push(
observableCombineLatest( observableCombineLatest(
bitstream$, bitstream$,
allFormats$ allFormats$,
).subscribe(([bitstream, allFormats]) => { bundle$
).subscribe(([bitstream, allFormats, bundle]) => {
this.bitstream = bitstream as Bitstream; this.bitstream = bitstream as Bitstream;
this.formats = allFormats.page; this.formats = allFormats.page;
this.bundle = bundle;
this.setIiifStatus(this.bitstream); this.setIiifStatus(this.bitstream);
}) })
); );
@@ -460,7 +479,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
this.formGroup.patchValue({ this.formGroup.patchValue({
fileNamePrimaryContainer: { fileNamePrimaryContainer: {
fileName: bitstream.name, fileName: bitstream.name,
primaryBitstream: false primaryBitstream: this.bundle.primaryBitstreamUUID === bitstream.uuid
}, },
descriptionContainer: { descriptionContainer: {
description: bitstream.firstMetadataValue('dc.description') description: bitstream.firstMetadataValue('dc.description')
@@ -563,6 +582,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
} }
} }
/** /**
* Check for changes against the bitstream and send update requests to the REST API * Check for changes against the bitstream and send update requests to the REST API
*/ */
@@ -571,9 +591,45 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
const updatedBitstream = this.formToBitstream(updatedValues); const updatedBitstream = this.formToBitstream(updatedValues);
const selectedFormat = this.formats.find((f: BitstreamFormat) => f.id === updatedValues.formatContainer.selectedFormat); const selectedFormat = this.formats.find((f: BitstreamFormat) => f.id === updatedValues.formatContainer.selectedFormat);
const isNewFormat = selectedFormat.id !== this.originalFormat.id; const isNewFormat = selectedFormat.id !== this.originalFormat.id;
const isPrimary = updatedValues.fileNamePrimaryContainer.primaryBitstream;
const wasPrimary = this.bundle.primaryBitstreamUUID === this.bitstream.uuid;
let bitstream$; let bitstream$;
let bundle$: Observable<Bundle>;
if (wasPrimary !== isPrimary) {
let patchOperation;
// No longer primary bitstream: remove
if (wasPrimary) {
patchOperation = {
path: this.primaryBitstreamPath,
op: 'remove'
};
} else {
// Has become primary bitstream
// If it already had a value: replace, otherwise: add
patchOperation = {
path: this.primaryBitstreamPath,
op: hasValue(this.bundle.primaryBitstreamUUID) ? 'replace' : 'add',
value: this.bitstream.uuid
};
}
bundle$ = this.bundleService.patch(this.bundle, [patchOperation]).pipe(
getFirstCompletedRemoteData(),
map((bundleResponse: RemoteData<Bundle>) => {
if (hasValue(bundleResponse) && bundleResponse.hasFailed) {
this.notificationsService.error(
this.translate.instant(this.NOTIFICATIONS_PREFIX + 'error.primaryBitstream.title'),
bundleResponse.errorMessage
);
} else {
return bundleResponse.payload;
}
})
);
} else {
bundle$ = observableOf(this.bundle);
}
if (isNewFormat) { if (isNewFormat) {
bitstream$ = this.bitstreamService.updateFormat(this.bitstream, selectedFormat).pipe( bitstream$ = this.bitstreamService.updateFormat(this.bitstream, selectedFormat).pipe(
getFirstCompletedRemoteData(), getFirstCompletedRemoteData(),
@@ -592,7 +648,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
bitstream$ = observableOf(this.bitstream); bitstream$ = observableOf(this.bitstream);
} }
bitstream$.pipe( combineLatest([bundle$, bitstream$]).pipe(
switchMap(() => { switchMap(() => {
return this.bitstreamService.update(updatedBitstream).pipe( return this.bitstreamService.update(updatedBitstream).pipe(
getFirstSucceededRemoteDataPayload() getFirstSucceededRemoteDataPayload()

View File

@@ -1,4 +1,4 @@
import { deserialize, inheritSerialization } from 'cerialize'; import { autoserializeAs, deserialize, inheritSerialization } from 'cerialize';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
@@ -29,6 +29,12 @@ export class Bundle extends DSpaceObject {
item: HALLink; item: HALLink;
}; };
/**
* The ID of the primaryBitstream of this Bundle
*/
@autoserializeAs('primarybitstream')
primaryBitstreamUUID: string;
/** /**
* The primary Bitstream of this Bundle * The primary Bitstream of this Bundle
* Will be undefined unless the primaryBitstream {@link HALLink} has been resolved. * Will be undefined unless the primaryBitstream {@link HALLink} has been resolved.