@@ -4,8 +4,10 @@ import {
44 NgIf ,
55} from '@angular/common' ;
66import {
7+ ChangeDetectorRef ,
78 Component ,
89 Inject ,
10+ InjectionToken ,
911 Injector ,
1012 Input ,
1113 OnDestroy ,
@@ -23,25 +25,33 @@ import {
2325import {
2426 BehaviorSubject ,
2527 combineLatest as observableCombineLatest ,
28+ EMPTY ,
2629 Observable ,
2730 Subscription ,
2831} from 'rxjs' ;
29- import { map } from 'rxjs/operators' ;
32+ import {
33+ map ,
34+ mergeMap ,
35+ tap ,
36+ } from 'rxjs/operators' ;
3037
38+ import {
39+ APP_DATA_SERVICES_MAP ,
40+ LazyDataServicesMap ,
41+ } from '../../../config/app-config.interface' ;
3142import { ArrayMoveChangeAnalyzer } from '../../core/data/array-move-change-analyzer.service' ;
32- import { DATA_SERVICE_FACTORY } from '../../core/data/base/data-service.decorator' ;
33- import { HALDataService } from '../../core/data/base/hal-data-service.interface' ;
3443import { RemoteData } from '../../core/data/remote-data' ;
3544import { UpdateDataService } from '../../core/data/update-data.service' ;
45+ import { lazyService } from '../../core/lazy-service' ;
3646import { DSpaceObject } from '../../core/shared/dspace-object.model' ;
37- import { GenericConstructor } from '../../core/shared/generic-constructor' ;
3847import { getFirstCompletedRemoteData } from '../../core/shared/operators' ;
3948import { ResourceType } from '../../core/shared/resource-type' ;
4049import { AlertComponent } from '../../shared/alert/alert.component' ;
4150import { AlertType } from '../../shared/alert/alert-type' ;
4251import {
4352 hasNoValue ,
4453 hasValue ,
54+ isNotEmpty ,
4555} from '../../shared/empty.util' ;
4656import { LoadingComponent } from '../../shared/loading/loading.component' ;
4757import { NotificationsService } from '../../shared/notifications/notifications.service' ;
@@ -141,7 +151,8 @@ export class DsoEditMetadataComponent implements OnInit, OnDestroy {
141151 protected translateService : TranslateService ,
142152 protected parentInjector : Injector ,
143153 protected arrayMoveChangeAnalyser : ArrayMoveChangeAnalyzer < number > ,
144- @Inject ( DATA_SERVICE_FACTORY ) protected getDataServiceFor : ( resourceType : ResourceType ) => GenericConstructor < HALDataService < any > > ) {
154+ protected cdr : ChangeDetectorRef ,
155+ @Inject ( APP_DATA_SERVICES_MAP ) private dataServiceMap : InjectionToken < LazyDataServicesMap > ) {
145156 }
146157
147158 /**
@@ -152,48 +163,74 @@ export class DsoEditMetadataComponent implements OnInit, OnDestroy {
152163 if ( hasNoValue ( this . dso ) ) {
153164 this . dsoUpdateSubscription = observableCombineLatest ( [ this . route . data , this . route . parent . data ] ) . pipe (
154165 map ( ( [ data , parentData ] : [ Data , Data ] ) => Object . assign ( { } , data , parentData ) ) ,
155- map ( ( data : any ) => data . dso ) ,
156- ) . subscribe ( ( rd : RemoteData < DSpaceObject > ) => {
157- this . dso = rd . payload ;
158- this . initDataService ( ) ;
166+ tap ( ( data : any ) => this . initDSO ( data . dso . payload ) ) ,
167+ mergeMap ( ( ) => this . retrieveDataService ( ) ) ,
168+ ) . subscribe ( ( dataService : UpdateDataService < DSpaceObject > ) => {
169+ this . initDataService ( dataService ) ;
159170 this . initForm ( ) ;
160171 } ) ;
161172 } else {
162- this . initDataService ( ) ;
163- this . initForm ( ) ;
173+ this . initDSOType ( this . dso ) ;
174+ this . retrieveDataService ( ) . subscribe ( ( dataService : UpdateDataService < DSpaceObject > ) => {
175+ this . initDataService ( dataService ) ;
176+ this . initForm ( ) ;
177+ } ) ;
164178 }
165179 this . savingOrLoadingFieldValidation$ = observableCombineLatest ( [ this . saving$ , this . loadingFieldValidation$ ] ) . pipe (
166180 map ( ( [ saving , loading ] : [ boolean , boolean ] ) => saving || loading ) ,
167181 ) ;
168182 }
169183
170184 /**
171- * Initialise (resolve) the data-service for the current DSpaceObject
185+ * Resolve the data-service for the current DSpaceObject and retrieve its instance
172186 */
173- initDataService ( ) : void {
174- let type : ResourceType ;
175- if ( typeof this . dso . type === 'string' ) {
176- type = new ResourceType ( this . dso . type ) ;
187+ retrieveDataService ( ) : Observable < UpdateDataService < DSpaceObject > > {
188+ if ( hasNoValue ( this . updateDataService ) ) {
189+ const lazyProvider$ : Observable < UpdateDataService < DSpaceObject > > = lazyService ( this . dataServiceMap [ this . dsoType ] , this . parentInjector ) ;
190+ return lazyProvider$ ;
177191 } else {
178- type = this . dso . type ;
192+ return EMPTY ;
179193 }
180- if ( hasNoValue ( this . updateDataService ) ) {
181- const provider = this . getDataServiceFor ( type ) ;
182- this . updateDataService = Injector . create ( {
183- providers : [ ] ,
184- parent : this . parentInjector ,
185- } ) . get ( provider ) ;
194+ }
195+
196+ /**
197+ * Initialise the current DSpaceObject
198+ */
199+ initDSO ( object : DSpaceObject ) {
200+ this . dso = object ;
201+ this . initDSOType ( object ) ;
202+ }
203+
204+ /**
205+ * Initialise the current DSpaceObject's type
206+ */
207+ initDSOType ( object : DSpaceObject ) {
208+ let type : ResourceType ;
209+ if ( typeof object . type === 'string' ) {
210+ type = new ResourceType ( object . type ) ;
211+ } else {
212+ type = object . type ;
186213 }
187214 this . dsoType = type . value ;
188215 }
189216
217+ /**
218+ * Initialise the data-service for the current DSpaceObject
219+ */
220+ initDataService ( dataService : UpdateDataService < DSpaceObject > ) : void {
221+ if ( isNotEmpty ( dataService ) ) {
222+ this . updateDataService = dataService ;
223+ }
224+ }
225+
190226 /**
191227 * Initialise the dynamic form object by passing the DSpaceObject's metadata
192228 * Call onValueSaved() to update the form's state properties
193229 */
194230 initForm ( ) : void {
195231 this . form = new DsoEditMetadataForm ( this . dso . metadata ) ;
196232 this . onValueSaved ( ) ;
233+ this . cdr . detectChanges ( ) ;
197234 }
198235
199236 /**
0 commit comments