rename getPrimaryValue to getValue, improve doccomments and move virtual metadata code to the Metadatum model

This commit is contained in:
Art Lowel
2019-01-29 18:23:01 +01:00
parent 9982254ca2
commit 11b09a2269
11 changed files with 126 additions and 30 deletions

View File

@@ -411,10 +411,10 @@ describe('ItemComponent', () => {
it('should have all the representations in the correct order', () => { it('should have all the representations in the correct order', () => {
representations.subscribe((reps: MetadataRepresentation[]) => { representations.subscribe((reps: MetadataRepresentation[]) => {
expect(reps[0].getPrimaryValue()).toEqual('First value'); expect(reps[0].getValue()).toEqual('First value');
expect(reps[1].getPrimaryValue()).toEqual('Second value'); expect(reps[1].getValue()).toEqual('Second value');
expect(reps[2].getPrimaryValue()).toEqual('related item'); expect(reps[2].getValue()).toEqual('related item');
expect(reps[3].getPrimaryValue()).toEqual('Fourth value'); expect(reps[3].getValue()).toEqual('Fourth value');
}); });
}); });

View File

@@ -99,10 +99,11 @@ export const relationsToRepresentations = (thisId: string, itemType: string, met
source.pipe( source.pipe(
flatMap((rels: Relationship[]) => flatMap((rels: Relationship[]) =>
observableZip( observableZip(
...metadata.map((metadatum: Metadatum) => { ...metadata
const prefix = 'virtual::'; .map((metadatum: any) => Object.assign(new Metadatum(), metadatum))
if (hasValue(metadatum.authority) && metadatum.authority.startsWith(prefix)) { .map((metadatum: Metadatum) => {
const matchingRels = rels.filter((rel: Relationship) => ('' + rel.id) === metadatum.authority.substring(metadatum.authority.indexOf(prefix) + prefix.length)); if (metadatum.isVirtual) {
const matchingRels = rels.filter((rel: Relationship) => ('' + rel.id) === metadatum.virtualValue);
if (matchingRels.length > 0) { if (matchingRels.length > 0) {
const matchingRel = matchingRels[0]; const matchingRel = matchingRels[0];
let queryId = matchingRel.leftId; let queryId = matchingRel.leftId;

View File

@@ -1,5 +1,5 @@
import { MetadataRepresentationType } from '../metadata-representation.model'; 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 { Item } from '../../item.model';
import { Metadatum } from '../../metadatum.model'; import { Metadatum } from '../../metadatum.model';
@@ -7,14 +7,14 @@ describe('ItemMetadataRepresentation', () => {
const valuePrefix = 'Test value for '; const valuePrefix = 'Test value for ';
const item = new Item(); const item = new Item();
let itemMetadataRepresentation: ItemMetadataRepresentation; let itemMetadataRepresentation: ItemMetadataRepresentation;
item.metadata = Object.keys(ItemTypeToPrimaryValue).map((key: string) => { item.metadata = Object.keys(ItemTypeToValue).map((key: string) => {
return Object.assign(new Metadatum(), { return Object.assign(new Metadatum(), {
key: ItemTypeToPrimaryValue[key], key: ItemTypeToValue[key],
value: `${valuePrefix}${ItemTypeToPrimaryValue[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}"`, () => { describe(`when creating an ItemMetadataRepresentation with item-type "${itemType}"`, () => {
beforeEach(() => { beforeEach(() => {
itemMetadataRepresentation = Object.assign(new ItemMetadataRepresentation(itemType), item); itemMetadataRepresentation = Object.assign(new ItemMetadataRepresentation(itemType), item);
@@ -24,8 +24,8 @@ describe('ItemMetadataRepresentation', () => {
expect(itemMetadataRepresentation.representationType).toEqual(MetadataRepresentationType.Item); expect(itemMetadataRepresentation.representationType).toEqual(MetadataRepresentationType.Item);
}); });
it('should return the correct value when calling getPrimaryValue', () => { it('should return the correct value when calling getValue', () => {
expect(itemMetadataRepresentation.getPrimaryValue()).toEqual(`${valuePrefix}${ItemTypeToPrimaryValue[itemType]}`); expect(itemMetadataRepresentation.getValue()).toEqual(`${valuePrefix}${ItemTypeToValue[itemType]}`);
}); });
it('should return the correct item type', () => { it('should return the correct item type', () => {

View File

@@ -3,15 +3,15 @@ import { MetadataRepresentation, MetadataRepresentationType } from '../metadata-
import { hasValue } from '../../../../shared/empty.util'; 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', Default: 'dc.title',
Person: 'dc.contributor.author' 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 { 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 * Get the value to display, depending on the itemType
*/ */
getPrimaryValue(): string { getValue(): string {
let metadata; let metadata;
if (hasValue(ItemTypeToPrimaryValue[this.itemType])) { if (hasValue(ItemTypeToValue[this.itemType])) {
metadata = ItemTypeToPrimaryValue[this.itemType]; metadata = ItemTypeToValue[this.itemType];
} else { } else {
metadata = ItemTypeToPrimaryValue.Default; metadata = ItemTypeToValue.Default;
} }
return this.findMetadata(metadata); return this.findMetadata(metadata);
} }

View File

@@ -25,7 +25,7 @@ export interface MetadataRepresentation {
representationType: MetadataRepresentationType, representationType: MetadataRepresentationType,
/** /**
* Fetches the primary value to be displayed * Fetches the value to be displayed
*/ */
getPrimaryValue(): string getValue(): string
} }

View File

@@ -26,7 +26,7 @@ describe('MetadatumRepresentation', () => {
}); });
it('should return the correct value when calling getPrimaryValue', () => { 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', () => { it('should return the correct item type', () => {
@@ -43,8 +43,8 @@ describe('MetadatumRepresentation', () => {
expect(metadatumRepresentation.representationType).toEqual(MetadataRepresentationType.AuthorityControlled); expect(metadatumRepresentation.representationType).toEqual(MetadataRepresentationType.AuthorityControlled);
}); });
it('should return the correct value when calling getPrimaryValue', () => { it('should return the correct value when calling getValue', () => {
expect(metadatumRepresentation.getPrimaryValue()).toEqual(authorityMetadatum.value); expect(metadatumRepresentation.getValue()).toEqual(authorityMetadatum.value);
}); });
it('should return the correct item type', () => { it('should return the correct item type', () => {

View File

@@ -31,7 +31,7 @@ export class MetadatumRepresentation extends Metadatum implements MetadataRepres
/** /**
* Get the value to display * Get the value to display
*/ */
getPrimaryValue(): string { getValue(): string {
return this.value; return this.value;
} }

View File

@@ -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');
});
});
});
});

View File

@@ -1,4 +1,7 @@
import { autoserialize } from 'cerialize'; import { autoserialize } from 'cerialize';
import { hasValue } from '../../shared/empty.util';
const VIRTUAL_METADATA_PREFIX = 'virtual::';
export class Metadatum { export class Metadatum {
@@ -33,4 +36,29 @@ export class Metadatum {
@autoserialize @autoserialize
authority: string; 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;
}
}
} }

View File

@@ -1,3 +1,3 @@
<div> <div>
<span>{{metadataRepresentation.getPrimaryValue()}}</span> <span>{{metadataRepresentation.getValue()}}</span>
</div> </div>

View File

@@ -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 * 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 { export class PlainTextMetadataListElementComponent extends MetadataRepresentationListElementComponent {
} }