diff --git a/src/app/+bitstream-page/edit-bitstream-page/edit-bitstream-page.component.ts b/src/app/+bitstream-page/edit-bitstream-page/edit-bitstream-page.component.ts index f502ffffb4..8d29a8111b 100644 --- a/src/app/+bitstream-page/edit-bitstream-page/edit-bitstream-page.component.ts +++ b/src/app/+bitstream-page/edit-bitstream-page/edit-bitstream-page.component.ts @@ -41,6 +41,11 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy { */ bitstream: Bitstream; + /** + * The ID of the originally selected format + */ + originalFormatID: string; + /** * @type {string} Key prefix used to generate form messages */ @@ -298,6 +303,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy { getRemoteDataPayload(), take(1) ).subscribe((format: BitstreamFormat) => { + this.originalFormatID = format.id; this.formGroup.patchValue({ formatContainer: { selectedFormat: format.id @@ -364,6 +370,10 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy { } } + /** + * Fired whenever the form receives an update and changes the layout of the "Other Format" input, depending on the selected format + * @param event + */ onChange(event) { const model = event.model; if (model.id === this.selectedFormatModel.id) { @@ -377,10 +387,16 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy { onSubmit() { const updatedValues = this.formGroup.getRawValue(); const newBitstream = this.formToBitstream(updatedValues); - this.bitstreamService.update(newBitstream).pipe( + const selectedFormat = updatedValues.formatContainer.selectedFormat; + + const updatedBitstream$ = this.bitstreamService.update(newBitstream).pipe( tap(() => this.bitstreamService.commitUpdates()), getSucceededRemoteData() - ).subscribe((bitstreamRD: RemoteData) => { + ); + const updatedFormatResponse$ = this.bitstreamService.updateFormat(newBitstream, selectedFormat); + + observableCombineLatest(updatedBitstream$, updatedFormatResponse$).subscribe(([bitstreamRD, formatResponse]) => { + console.log(formatResponse); this.bitstream = bitstreamRD.payload; this.updateForm(this.bitstream); this.notificationsService.success( diff --git a/src/app/core/cache/server-sync-buffer.effects.ts b/src/app/core/cache/server-sync-buffer.effects.ts index 19629ec7de..f25923e1f5 100644 --- a/src/app/core/cache/server-sync-buffer.effects.ts +++ b/src/app/core/cache/server-sync-buffer.effects.ts @@ -107,7 +107,6 @@ export class ServerSyncBufferEffects { this.requestService.configure(new PatchRequest(this.requestService.generateRequestId(), href, flatPatch)); return new ApplyPatchObjectCacheAction(href); } - // this.requestService.configure(new PutRequest(this.requestService.generateRequestId(), href, serializedObject)); }) ) } diff --git a/src/app/core/data/bitstream-data.service.ts b/src/app/core/data/bitstream-data.service.ts index 679ea0035b..896dd49ab2 100644 --- a/src/app/core/data/bitstream-data.service.ts +++ b/src/app/core/data/bitstream-data.service.ts @@ -10,11 +10,16 @@ import { BrowseService } from '../browse/browse.service'; import { ObjectCacheService } from '../cache/object-cache.service'; import { HALEndpointService } from '../shared/hal-endpoint.service'; import { NotificationsService } from '../../shared/notifications/notifications.service'; -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpHeaders } from '@angular/common/http'; import { DSOChangeAnalyzer } from './dso-change-analyzer.service'; -import { FindAllOptions } from './request.models'; +import { FindAllOptions, PutRequest } from './request.models'; import { Observable } from 'rxjs/internal/Observable'; import { RestResponse } from '../cache/response.models'; +import { BitstreamFormatDataService } from './bitstream-format-data.service'; +import { map, switchMap } from 'rxjs/operators'; +import { combineLatest as observableCombineLatest } from 'rxjs'; +import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service'; +import { configureRequest, getResponseFromEntry } from '../shared/operators'; /** * A service responsible for fetching/sending data from/to the REST API on the bitstreams endpoint @@ -34,7 +39,8 @@ export class BitstreamDataService extends DataService { protected halService: HALEndpointService, protected notificationsService: NotificationsService, protected http: HttpClient, - protected comparator: DSOChangeAnalyzer) { + protected comparator: DSOChangeAnalyzer, + protected bitstreamFormatService: BitstreamFormatDataService) { super(); } @@ -60,4 +66,34 @@ export class BitstreamDataService extends DataService { this.requestService.removeByHrefSubstring(bitstream.self); return response$; } + + /** + * Set the format of a bitstream by ID + * @param bitstream + * @param formatId + */ + updateFormat(bitstream: Bitstream, formatId: string): Observable { + const requestId = this.requestService.generateRequestId(); + const bitstreamHref$ = this.getBrowseEndpoint().pipe( + map((href: string) => `${href}/${bitstream.id}`), + switchMap((href: string) => this.halService.getEndpoint('format', href)) + ); + const formatHref$ = this.bitstreamFormatService.getBrowseEndpoint().pipe( + map((href: string) => `${href}/${formatId}`) + ); + observableCombineLatest(bitstreamHref$, formatHref$).pipe( + map(([bitstreamHref, formatHref]) => { + const options: HttpOptions = Object.create({}); + let headers = new HttpHeaders(); + headers = headers.append('Content-Type', 'text/uri-list'); + options.headers = headers; + return new PutRequest(requestId, bitstreamHref, formatHref, options); + }), + configureRequest(this.requestService) + ).subscribe(); + + return this.requestService.getByUUID(requestId).pipe( + getResponseFromEntry() + ); + } }