diff --git a/src/app/core/browse/search-manager.ts b/src/app/core/browse/search-manager.ts index 2f2a4acce5f..8ac1e00dd63 100644 --- a/src/app/core/browse/search-manager.ts +++ b/src/app/core/browse/search-manager.ts @@ -111,7 +111,8 @@ export class SearchManager { }) .filter((item) => hasValue(item)); - const uuidList = this.extractUUID(items, environment.followAuthorityMetadata); + const uuidList = this.extractUUID(items, environment.followAuthorityMetadata, 100); + return uuidList.length > 0 ? this.itemService.findAllById(uuidList).pipe( getFirstCompletedRemoteData(), map(data => { @@ -124,20 +125,20 @@ export class SearchManager { ) : of(null); } - protected extractUUID(items: Item[], metadataToFollow: FollowAuthorityMetadata[]): string[] { + protected extractUUID(items: Item[], metadataToFollow: FollowAuthorityMetadata[], numberOfElementsToReturn?: number): string[] { const uuidMap = {}; items.forEach((item) => { metadataToFollow.forEach((followMetadata: FollowAuthorityMetadata) => { if (item.entityType === followMetadata.type) { if (isArray(followMetadata.metadata)) { - followMetadata.metadata.forEach((metadata) => { - Metadata.all(item.metadata, metadata) + followMetadata.metadata.forEach((metadata) => { + Metadata.all(item.metadata, metadata, null, environment.followAuthorityMetadataValuesLimit) .filter((metadataValue: MetadataValue) => Metadata.hasValidItemAuthority(metadataValue.authority)) .forEach((metadataValue: MetadataValue) => uuidMap[metadataValue.authority] = metadataValue); }); } else { - Metadata.all(item.metadata, followMetadata.metadata) + Metadata.all(item.metadata, followMetadata.metadata, null, environment.followAuthorityMetadataValuesLimit) .filter((metadataValue: MetadataValue) => Metadata.hasValidItemAuthority(metadataValue.authority)) .forEach((metadataValue: MetadataValue) => uuidMap[metadataValue.authority] = metadataValue); } @@ -145,6 +146,10 @@ export class SearchManager { }); }); + if (hasValue(numberOfElementsToReturn) && numberOfElementsToReturn > 0) { + return Object.keys(uuidMap).slice(0, numberOfElementsToReturn); + } + return Object.keys(uuidMap); } } diff --git a/src/app/core/browse/search.manager.spec.ts b/src/app/core/browse/search.manager.spec.ts index 9d28786b4c0..89da1320ce5 100644 --- a/src/app/core/browse/search.manager.spec.ts +++ b/src/app/core/browse/search.manager.spec.ts @@ -182,6 +182,11 @@ describe('SearchManager', () => { const uuidList = (service as any).extractUUID([firstPublication, firstPublication], [{type: 'Publication', metadata: ['dc.contributor.author']}]); expect(uuidList).toEqual([validAuthority]); }); + + it('should limit the number of extracted uuids', () => { + const uuidList = (service as any).extractUUID([firstPublication, secondPublication, invalidAuthorityPublication], [{type: 'Publication', metadata: ['dc.contributor.author']}], 2); + expect(uuidList.length).toBe(2); + }); }); }); diff --git a/src/app/core/shared/dspace-object.model.ts b/src/app/core/shared/dspace-object.model.ts index c0cc8d9c214..717988b4aae 100644 --- a/src/app/core/shared/dspace-object.model.ts +++ b/src/app/core/shared/dspace-object.model.ts @@ -115,6 +115,18 @@ export class DSpaceObject extends ListableObject implements CacheableObject { return Metadata.all(this.metadata, keyOrKeys, valueFilter); } + /** + * Gets all matching metadata in this DSpaceObject, up to a limit. + * + * @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see [[Metadata]]. + * @param {number} limit The maximum number of results to return. + * @param {MetadataValueFilter} valueFilter The value filter to use. If unspecified, no filtering will be done. + * @returns {MetadataValue[]} the matching values or an empty array. + */ + limitedMetadata(keyOrKeys: string | string[], limit: number, valueFilter?: MetadataValueFilter): MetadataValue[] { + return Metadata.all(this.metadata, keyOrKeys, valueFilter, limit); + } + /** * Like [[allMetadata]], but only returns string values. * diff --git a/src/app/core/shared/metadata.utils.spec.ts b/src/app/core/shared/metadata.utils.spec.ts index a80e482f24f..f4348723001 100644 --- a/src/app/core/shared/metadata.utils.spec.ts +++ b/src/app/core/shared/metadata.utils.spec.ts @@ -44,11 +44,11 @@ const multiViewModelList = [ { key: 'foo', ...bar, order: 0 } ]; -const testMethod = (fn, resultKind, mapOrMaps, keyOrKeys, expected, filter?) => { +const testMethod = (fn, resultKind, mapOrMaps, keyOrKeys, expected, filter?, limit?: number) => { const keys = keyOrKeys instanceof Array ? keyOrKeys : [keyOrKeys]; describe('and key' + (keys.length === 1 ? (' ' + keys[0]) : ('s ' + JSON.stringify(keys))) + ' with ' + (isUndefined(filter) ? 'no filter' : 'filter ' + JSON.stringify(filter)), () => { - const result = fn(mapOrMaps, keys, filter); + const result = fn(mapOrMaps, keys, filter, limit); let shouldReturn; if (resultKind === 'boolean') { shouldReturn = expected; @@ -56,7 +56,8 @@ const testMethod = (fn, resultKind, mapOrMaps, keyOrKeys, expected, filter?) => shouldReturn = 'undefined'; } else if (expected instanceof Array) { shouldReturn = 'an array with ' + expected.length + ' ' + (expected.length > 1 ? 'ordered ' : '') - + resultKind + (expected.length !== 1 ? 's' : ''); + + resultKind + (expected.length !== 1 ? 's' : '') + + (isUndefined(limit) ? '' : ' (limited to ' + limit + ')'); } else { shouldReturn = 'a ' + resultKind; } @@ -297,4 +298,12 @@ describe('Metadata', () => { }); + describe('all method with limit', () => { + const testAllWithLimit = (mapOrMaps, keyOrKeys, expected, limit) => + testMethod(Metadata.all, 'value', mapOrMaps, keyOrKeys, expected, undefined, limit); + + describe('with multiMap and limit', () => { + testAllWithLimit(multiMap, 'dc.title', [dcTitle1], 1); + }); + }); }); diff --git a/src/app/core/shared/metadata.utils.ts b/src/app/core/shared/metadata.utils.ts index f6a7fb4fd00..ae5f85f582a 100644 --- a/src/app/core/shared/metadata.utils.ts +++ b/src/app/core/shared/metadata.utils.ts @@ -37,10 +37,11 @@ export class Metadata { * checked in order, and only values from the first with at least one match will be returned. * @param {string|string[]} keyOrKeys The metadata key(s) in scope. Wildcards are supported; see above. * @param {MetadataValueFilter} filter The value filter to use. If unspecified, no filtering will be done. + * @param {number} limit The maximum number of values to return. If unspecified, all matching values will be returned. * @returns {MetadataValue[]} the matching values or an empty array. */ public static all(mapOrMaps: MetadataMapInterface | MetadataMapInterface[], keyOrKeys: string | string[], - filter?: MetadataValueFilter): MetadataValue[] { + filter?: MetadataValueFilter, limit?: number): MetadataValue[] { const mdMaps: MetadataMapInterface[] = mapOrMaps instanceof Array ? mapOrMaps : [mapOrMaps]; const matches: MetadataValue[] = []; for (const mdMap of mdMaps) { @@ -50,6 +51,9 @@ export class Metadata { for (const candidate of candidates) { if (Metadata.valueMatches(candidate as MetadataValue, filter)) { matches.push(candidate as MetadataValue); + if (hasValue(limit) && matches.length >= limit) { + return matches; + } } } } diff --git a/src/app/shared/object-list/my-dspace-result-list-element/item-list-preview/item-list-preview.component.html b/src/app/shared/object-list/my-dspace-result-list-element/item-list-preview/item-list-preview.component.html index ba376d2f64e..4c4cd3513fb 100644 --- a/src/app/shared/object-list/my-dspace-result-list-element/item-list-preview/item-list-preview.component.html +++ b/src/app/shared/object-list/my-dspace-result-list-element/item-list-preview/item-list-preview.component.html @@ -18,7 +18,7 @@