From 11b09a22697b119d38e34f15f3d8515abbc5c11f Mon Sep 17 00:00:00 2001 From: Art Lowel Date: Tue, 29 Jan 2019 18:23:01 +0100 Subject: [PATCH] rename getPrimaryValue to getValue, improve doccomments and move virtual metadata code to the Metadatum model --- .../item-types/shared/item.component.spec.ts | 8 +-- .../item-types/shared/item.component.ts | 9 +-- ...item-metadata-representation.model.spec.ts | 14 ++-- .../item-metadata-representation.model.ts | 14 ++-- .../metadata-representation.model.ts | 4 +- .../metadatum-representation.model.spec.ts | 6 +- .../metadatum-representation.model.ts | 2 +- src/app/core/shared/metadatum.model.spec.ts | 67 +++++++++++++++++++ src/app/core/shared/metadatum.model.ts | 28 ++++++++ ...-text-metadata-list-element.component.html | 2 +- ...in-text-metadata-list-element.component.ts | 2 +- 11 files changed, 126 insertions(+), 30 deletions(-) create mode 100644 src/app/core/shared/metadatum.model.spec.ts diff --git a/src/app/+item-page/simple/item-types/shared/item.component.spec.ts b/src/app/+item-page/simple/item-types/shared/item.component.spec.ts index 8c6cf15ee9..28bd8e33e3 100644 --- a/src/app/+item-page/simple/item-types/shared/item.component.spec.ts +++ b/src/app/+item-page/simple/item-types/shared/item.component.spec.ts @@ -411,10 +411,10 @@ describe('ItemComponent', () => { it('should have all the representations in the correct order', () => { representations.subscribe((reps: MetadataRepresentation[]) => { - expect(reps[0].getPrimaryValue()).toEqual('First value'); - expect(reps[1].getPrimaryValue()).toEqual('Second value'); - expect(reps[2].getPrimaryValue()).toEqual('related item'); - expect(reps[3].getPrimaryValue()).toEqual('Fourth value'); + expect(reps[0].getValue()).toEqual('First value'); + expect(reps[1].getValue()).toEqual('Second value'); + expect(reps[2].getValue()).toEqual('related item'); + expect(reps[3].getValue()).toEqual('Fourth value'); }); }); diff --git a/src/app/+item-page/simple/item-types/shared/item.component.ts b/src/app/+item-page/simple/item-types/shared/item.component.ts index c5aae65f13..3cea15c251 100644 --- a/src/app/+item-page/simple/item-types/shared/item.component.ts +++ b/src/app/+item-page/simple/item-types/shared/item.component.ts @@ -99,10 +99,11 @@ export const relationsToRepresentations = (thisId: string, itemType: string, met source.pipe( flatMap((rels: Relationship[]) => observableZip( - ...metadata.map((metadatum: Metadatum) => { - const prefix = 'virtual::'; - if (hasValue(metadatum.authority) && metadatum.authority.startsWith(prefix)) { - const matchingRels = rels.filter((rel: Relationship) => ('' + rel.id) === metadatum.authority.substring(metadatum.authority.indexOf(prefix) + prefix.length)); + ...metadata + .map((metadatum: any) => Object.assign(new Metadatum(), metadatum)) + .map((metadatum: Metadatum) => { + if (metadatum.isVirtual) { + const matchingRels = rels.filter((rel: Relationship) => ('' + rel.id) === metadatum.virtualValue); if (matchingRels.length > 0) { const matchingRel = matchingRels[0]; let queryId = matchingRel.leftId; diff --git a/src/app/core/shared/metadata-representation/item/item-metadata-representation.model.spec.ts b/src/app/core/shared/metadata-representation/item/item-metadata-representation.model.spec.ts index 4afe5a783f..cd153153de 100644 --- a/src/app/core/shared/metadata-representation/item/item-metadata-representation.model.spec.ts +++ b/src/app/core/shared/metadata-representation/item/item-metadata-representation.model.spec.ts @@ -1,5 +1,5 @@ import { MetadataRepresentationType } from '../metadata-representation.model'; -import { ItemMetadataRepresentation, ItemTypeToPrimaryValue } from './item-metadata-representation.model'; +import { ItemMetadataRepresentation, ItemTypeToValue } from './item-metadata-representation.model'; import { Item } from '../../item.model'; import { Metadatum } from '../../metadatum.model'; @@ -7,14 +7,14 @@ describe('ItemMetadataRepresentation', () => { const valuePrefix = 'Test value for '; const item = new Item(); let itemMetadataRepresentation: ItemMetadataRepresentation; - item.metadata = Object.keys(ItemTypeToPrimaryValue).map((key: string) => { + item.metadata = Object.keys(ItemTypeToValue).map((key: string) => { return Object.assign(new Metadatum(), { - key: ItemTypeToPrimaryValue[key], - value: `${valuePrefix}${ItemTypeToPrimaryValue[key]}` + key: ItemTypeToValue[key], + value: `${valuePrefix}${ItemTypeToValue[key]}` }); }); - for (const itemType of Object.keys(ItemTypeToPrimaryValue)) { + for (const itemType of Object.keys(ItemTypeToValue)) { describe(`when creating an ItemMetadataRepresentation with item-type "${itemType}"`, () => { beforeEach(() => { itemMetadataRepresentation = Object.assign(new ItemMetadataRepresentation(itemType), item); @@ -24,8 +24,8 @@ describe('ItemMetadataRepresentation', () => { expect(itemMetadataRepresentation.representationType).toEqual(MetadataRepresentationType.Item); }); - it('should return the correct value when calling getPrimaryValue', () => { - expect(itemMetadataRepresentation.getPrimaryValue()).toEqual(`${valuePrefix}${ItemTypeToPrimaryValue[itemType]}`); + it('should return the correct value when calling getValue', () => { + expect(itemMetadataRepresentation.getValue()).toEqual(`${valuePrefix}${ItemTypeToValue[itemType]}`); }); it('should return the correct item type', () => { diff --git a/src/app/core/shared/metadata-representation/item/item-metadata-representation.model.ts b/src/app/core/shared/metadata-representation/item/item-metadata-representation.model.ts index 9ff72d5cf6..2d4f3fdf75 100644 --- a/src/app/core/shared/metadata-representation/item/item-metadata-representation.model.ts +++ b/src/app/core/shared/metadata-representation/item/item-metadata-representation.model.ts @@ -3,15 +3,15 @@ import { MetadataRepresentation, MetadataRepresentationType } from '../metadata- import { hasValue } from '../../../../shared/empty.util'; /** - * An object to convert item types into the metadata field it should render for the item's primary value + * An object to convert item types into the metadata field it should render for the item's value */ -export const ItemTypeToPrimaryValue = { +export const ItemTypeToValue = { Default: 'dc.title', Person: 'dc.contributor.author' }; /** - * This class defines the way the item it extends should be represented as metadata + * This class determines which fields to use when rendering an Item as a metadata value. */ export class ItemMetadataRepresentation extends Item implements MetadataRepresentation { @@ -35,12 +35,12 @@ export class ItemMetadataRepresentation extends Item implements MetadataRepresen /** * Get the value to display, depending on the itemType */ - getPrimaryValue(): string { + getValue(): string { let metadata; - if (hasValue(ItemTypeToPrimaryValue[this.itemType])) { - metadata = ItemTypeToPrimaryValue[this.itemType]; + if (hasValue(ItemTypeToValue[this.itemType])) { + metadata = ItemTypeToValue[this.itemType]; } else { - metadata = ItemTypeToPrimaryValue.Default; + metadata = ItemTypeToValue.Default; } return this.findMetadata(metadata); } diff --git a/src/app/core/shared/metadata-representation/metadata-representation.model.ts b/src/app/core/shared/metadata-representation/metadata-representation.model.ts index 770c462e8d..58e5bf906f 100644 --- a/src/app/core/shared/metadata-representation/metadata-representation.model.ts +++ b/src/app/core/shared/metadata-representation/metadata-representation.model.ts @@ -25,7 +25,7 @@ export interface MetadataRepresentation { representationType: MetadataRepresentationType, /** - * Fetches the primary value to be displayed + * Fetches the value to be displayed */ - getPrimaryValue(): string + getValue(): string } diff --git a/src/app/core/shared/metadata-representation/metadatum/metadatum-representation.model.spec.ts b/src/app/core/shared/metadata-representation/metadatum/metadatum-representation.model.spec.ts index 7356a79bbd..c55ff7f9f3 100644 --- a/src/app/core/shared/metadata-representation/metadatum/metadatum-representation.model.spec.ts +++ b/src/app/core/shared/metadata-representation/metadatum/metadatum-representation.model.spec.ts @@ -26,7 +26,7 @@ describe('MetadatumRepresentation', () => { }); it('should return the correct value when calling getPrimaryValue', () => { - expect(metadatumRepresentation.getPrimaryValue()).toEqual(normalMetadatum.value); + expect(metadatumRepresentation.getValue()).toEqual(normalMetadatum.value); }); it('should return the correct item type', () => { @@ -43,8 +43,8 @@ describe('MetadatumRepresentation', () => { expect(metadatumRepresentation.representationType).toEqual(MetadataRepresentationType.AuthorityControlled); }); - it('should return the correct value when calling getPrimaryValue', () => { - expect(metadatumRepresentation.getPrimaryValue()).toEqual(authorityMetadatum.value); + it('should return the correct value when calling getValue', () => { + expect(metadatumRepresentation.getValue()).toEqual(authorityMetadatum.value); }); it('should return the correct item type', () => { diff --git a/src/app/core/shared/metadata-representation/metadatum/metadatum-representation.model.ts b/src/app/core/shared/metadata-representation/metadatum/metadatum-representation.model.ts index 6a8de97733..08ae041706 100644 --- a/src/app/core/shared/metadata-representation/metadatum/metadatum-representation.model.ts +++ b/src/app/core/shared/metadata-representation/metadatum/metadatum-representation.model.ts @@ -31,7 +31,7 @@ export class MetadatumRepresentation extends Metadatum implements MetadataRepres /** * Get the value to display */ - getPrimaryValue(): string { + getValue(): string { return this.value; } diff --git a/src/app/core/shared/metadatum.model.spec.ts b/src/app/core/shared/metadatum.model.spec.ts new file mode 100644 index 0000000000..0b552eed56 --- /dev/null +++ b/src/app/core/shared/metadatum.model.spec.ts @@ -0,0 +1,67 @@ +import { Metadatum } from './metadatum.model'; + +describe('Metadatum', () => { + let metadatum: Metadatum ; + + beforeEach(() => { + metadatum = new Metadatum(); + }); + + describe('isVirtual', () => { + describe('when the metadatum has no authority key', () => { + beforeEach(() => { + metadatum.authority = undefined; + }); + + it('should return false', () => { + expect(metadatum.isVirtual).toBe(false); + }); + }); + + describe('when the metadatum has an authority key', () => { + describe('but it doesn\'t start with the virtual prefix', () => { + beforeEach(() => { + metadatum.authority = 'value'; + }); + + it('should return false', () => { + expect(metadatum.isVirtual).toBe(false); + }); + }); + + describe('and it starts with the virtual prefix', () => { + beforeEach(() => { + metadatum.authority = 'virtual::value'; + }); + + it('should return true', () => { + expect(metadatum.isVirtual).toBe(true); + }); + }); + + }); + + }); + + describe('virtualValue', () => { + describe('when the metadatum isn\'t virtual', () => { + beforeEach(() => { + metadatum.authority = 'value'; + }); + + it('should return undefined', () => { + expect(metadatum.virtualValue).toBeUndefined(); + }); + }); + + describe('when the metadatum is virtual', () => { + beforeEach(() => { + metadatum.authority = 'virtual::value'; + }); + + it('should return everything in the authority key after virtual::', () => { + expect(metadatum.virtualValue).toBe('value'); + }); + }); + }); +}); diff --git a/src/app/core/shared/metadatum.model.ts b/src/app/core/shared/metadatum.model.ts index c4ce8c3101..f8cac4fd51 100644 --- a/src/app/core/shared/metadatum.model.ts +++ b/src/app/core/shared/metadatum.model.ts @@ -1,4 +1,7 @@ import { autoserialize } from 'cerialize'; +import { hasValue } from '../../shared/empty.util'; + +const VIRTUAL_METADATA_PREFIX = 'virtual::'; export class Metadatum { @@ -33,4 +36,29 @@ export class Metadatum { @autoserialize authority: string; + /** + * The authority confidence value + */ + @autoserialize + confidence: number; + + /** + * Returns true if this Metadatum's authority key starts with 'virtual::' + */ + get isVirtual(): boolean { + return hasValue(this.authority) && this.authority.startsWith(VIRTUAL_METADATA_PREFIX); + } + + /** + * If this is a virtual Metadatum, it returns everything in the authority key after 'virtual::'. + * Returns undefined otherwise. + */ + get virtualValue(): string { + if (this.isVirtual) { + return this.authority.substring(this.authority.indexOf(VIRTUAL_METADATA_PREFIX) + VIRTUAL_METADATA_PREFIX.length); + } else { + return undefined; + } + } + } diff --git a/src/app/shared/object-list/metadata-representation-list-element/plain-text/plain-text-metadata-list-element.component.html b/src/app/shared/object-list/metadata-representation-list-element/plain-text/plain-text-metadata-list-element.component.html index 2533cf834b..3e017e1ae8 100644 --- a/src/app/shared/object-list/metadata-representation-list-element/plain-text/plain-text-metadata-list-element.component.html +++ b/src/app/shared/object-list/metadata-representation-list-element/plain-text/plain-text-metadata-list-element.component.html @@ -1,3 +1,3 @@
- {{metadataRepresentation.getPrimaryValue()}} + {{metadataRepresentation.getValue()}}
diff --git a/src/app/shared/object-list/metadata-representation-list-element/plain-text/plain-text-metadata-list-element.component.ts b/src/app/shared/object-list/metadata-representation-list-element/plain-text/plain-text-metadata-list-element.component.ts index e7cf235c75..e86210a961 100644 --- a/src/app/shared/object-list/metadata-representation-list-element/plain-text/plain-text-metadata-list-element.component.ts +++ b/src/app/shared/object-list/metadata-representation-list-element/plain-text/plain-text-metadata-list-element.component.ts @@ -13,7 +13,7 @@ import { VIEW_MODE_METADATA } from '../../../../+item-page/simple/metadata-repre }) /** * A component for displaying MetadataRepresentation objects in the form of plain text - * It will simply use the value retrieved from MetadataRepresentation.getPrimaryValue() to display as plain text + * It will simply use the value retrieved from MetadataRepresentation.getValue() to display as plain text */ export class PlainTextMetadataListElementComponent extends MetadataRepresentationListElementComponent { }