@@ -24,6 +24,7 @@ import {
2424 switchMap ,
2525 take ,
2626} from 'rxjs/operators' ;
27+ import { validate as uuidValidate } from 'uuid' ;
2728
2829import { BrowseService } from '../browse/browse.service' ;
2930import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service' ;
@@ -34,6 +35,7 @@ import { NotificationsService } from '../notification-system/notifications.servi
3435import { Bundle } from '../shared/bundle.model' ;
3536import { Collection } from '../shared/collection.model' ;
3637import { ExternalSourceEntry } from '../shared/external-source-entry.model' ;
38+ import { FollowLinkConfig } from '../shared/follow-link-config.model' ;
3739import { GenericConstructor } from '../shared/generic-constructor' ;
3840import { HALEndpointService } from '../shared/hal-endpoint.service' ;
3941import { Item } from '../shared/item.model' ;
@@ -58,6 +60,7 @@ import {
5860 PatchData ,
5961 PatchDataImpl ,
6062} from './base/patch-data' ;
63+ import { SearchDataImpl } from './base/search-data' ;
6164import { BundleDataService } from './bundle-data.service' ;
6265import { DSOChangeAnalyzer } from './dso-change-analyzer.service' ;
6366import { FindListOptions } from './find-list-options.model' ;
@@ -83,6 +86,7 @@ export abstract class BaseItemDataService extends IdentifiableDataService<Item>
8386 private createData : CreateData < Item > ;
8487 private patchData : PatchData < Item > ;
8588 private deleteData : DeleteData < Item > ;
89+ private searchData : SearchDataImpl < Item > ;
8690
8791 protected constructor (
8892 protected linkPath ,
@@ -101,6 +105,7 @@ export abstract class BaseItemDataService extends IdentifiableDataService<Item>
101105 this . createData = new CreateDataImpl ( this . linkPath , requestService , rdbService , objectCache , halService , notificationsService , this . responseMsToLive ) ;
102106 this . patchData = new PatchDataImpl < Item > ( this . linkPath , requestService , rdbService , objectCache , halService , comparator , this . responseMsToLive , this . constructIdEndpoint ) ;
103107 this . deleteData = new DeleteDataImpl ( this . linkPath , requestService , rdbService , objectCache , halService , notificationsService , this . responseMsToLive , this . constructIdEndpoint ) ;
108+ this . searchData = new SearchDataImpl ( this . linkPath , requestService , rdbService , objectCache , halService , this . responseMsToLive ) ;
104109 }
105110
106111 /**
@@ -425,8 +430,95 @@ export abstract class BaseItemDataService extends IdentifiableDataService<Item>
425430 return this . createData . create ( object , ...params ) ;
426431 }
427432
433+ /**
434+ * Returns an observable of {@link RemoteData} of an object, based on its custom URL, with a list of
435+ * {@link FollowLinkConfig}, to automatically resolve {@link HALLink}s of the object
436+ * @param id custom URL of object we want to retrieve
437+ * @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
438+ * no valid cached version. Defaults to true
439+ * @param reRequestOnStale Whether or not the request should automatically be re-
440+ * requested after the response becomes stale
441+ * @param linksToFollow List of {@link FollowLinkConfig} that indicate which
442+ * {@link HALLink}s should be automatically resolved
443+ * @param projections List of {@link projections} used to pass as parameters
444+ */
445+ public findByCustomUrl ( id : string , useCachedVersionIfAvailable = true , reRequestOnStale = true , linksToFollow : FollowLinkConfig < Item > [ ] , projections : string [ ] = [ ] ) : Observable < RemoteData < Item > > {
446+ const searchHref = 'findByCustomURL' ;
447+
448+ const options = Object . assign ( { } , {
449+ searchParams : [
450+ new RequestParam ( 'q' , id ) ,
451+ ] ,
452+ } ) ;
453+
454+ projections . forEach ( ( projection ) => {
455+ options . searchParams . push ( new RequestParam ( 'projection' , projection ) ) ;
456+ } ) ;
457+
458+ const hrefObs = this . searchData . getSearchByHref ( searchHref , options , ...linksToFollow ) ;
459+
460+ return this . findByHref ( hrefObs , useCachedVersionIfAvailable , reRequestOnStale , ...linksToFollow ) ;
461+ }
462+
463+
464+ /**
465+ * Invalidate cache of request findByCustomURL
466+ *
467+ * @param customUrl
468+ * @param projections
469+ */
470+ public invalidateFindByCustomUrlCache ( customUrl : string , projections : string [ ] = [ ] ) : void {
471+ const options : any = {
472+ searchParams : [ new RequestParam ( 'q' , customUrl ) ] ,
473+ } ;
474+
475+ projections . forEach ( ( p ) => options . searchParams . push ( new RequestParam ( 'projection' , p ) ) ) ;
476+
477+ this . searchData . getSearchByHref ( 'findByCustomURL' , options ) . pipe ( take ( 1 ) ) . subscribe ( ( href : string ) => {
478+ this . requestService . setStaleByHrefSubstring ( href ) ;
479+ this . objectCache . remove ( href ) ;
480+ } ) ;
481+ }
482+
483+ /**
484+ * Returns an observable of {@link RemoteData} of an object, based on its ID, with a list of
485+ * {@link FollowLinkConfig}, to automatically resolve {@link HALLink}s of the object
486+ * @param id ID of object we want to retrieve
487+ * @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
488+ * no valid cached version. Defaults to true
489+ * @param reRequestOnStale Whether or not the request should automatically be re-
490+ * requested after the response becomes stale
491+ * @param linksToFollow List of {@link FollowLinkConfig} that indicate which
492+ * {@link HALLink}s should be automatically resolved
493+ */
494+ public findById ( id : string , useCachedVersionIfAvailable = true , reRequestOnStale = true , ...linksToFollow : FollowLinkConfig < Item > [ ] ) : Observable < RemoteData < Item > > {
495+ const href$ = this . getIDHrefObs ( encodeURIComponent ( id ) , ...linksToFollow ) ;
496+ return this . findByHref ( href$ , useCachedVersionIfAvailable , reRequestOnStale , ...linksToFollow ) ;
497+ }
498+
499+ /**
500+ * Returns an observable of {@link RemoteData} of an object, based on its ID or custom URL if the parameter is not a valid id/uuid, with a list of
501+ * {@link FollowLinkConfig}, to automatically resolve {@link HALLink}s of the object
502+ * @param id ID of object we want to retrieve
503+ * @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
504+ * no valid cached version. Defaults to true
505+ * @param reRequestOnStale Whether or not the request should automatically be re-
506+ * requested after the response becomes stale
507+ * @param linksToFollow List of {@link FollowLinkConfig} that indicate which
508+ * {@link HALLink}s should be automatically resolved
509+ */
510+ public findByIdOrCustomUrl ( id : string , useCachedVersionIfAvailable = true , reRequestOnStale = true , ...linksToFollow : FollowLinkConfig < Item > [ ] ) : Observable < RemoteData < Item > > {
511+ if ( uuidValidate ( id ) ) {
512+ return this . findById ( id , useCachedVersionIfAvailable , reRequestOnStale , ...linksToFollow ) ;
513+ } else {
514+ return this . findByCustomUrl ( id , useCachedVersionIfAvailable , reRequestOnStale , linksToFollow ) ;
515+ }
516+ }
517+
428518}
429519
520+
521+
430522/**
431523 * A service for CRUD operations on Items
432524 */
0 commit comments