From 38625d2cd59bdc2e6a736cd754dfc212d8a0d45b Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Fri, 10 May 2019 13:44:58 +0200 Subject: [PATCH] 62339: itemIds to item remotedata in relationships --- .../publication/publication.component.ts | 19 ++++--------- .../shared/item-relationships-utils.ts | 27 +++++++++---------- .../item-types/shared/item.component.ts | 23 +++++++++------- .../items/normalized-relationship.model.ts | 12 +++------ .../item-relationships/relationship.model.ts | 11 +++----- .../journal-issue/journal-issue.component.ts | 14 +++------- .../journal-volume.component.ts | 14 +++------- .../item-pages/journal/journal.component.ts | 12 ++------- .../item-pages/orgunit/orgunit.component.ts | 17 +++--------- .../item-pages/person/person.component.ts | 8 +++--- .../item-pages/project/project.component.ts | 17 +++--------- 11 files changed, 56 insertions(+), 118 deletions(-) diff --git a/src/app/+item-page/simple/item-types/publication/publication.component.ts b/src/app/+item-page/simple/item-types/publication/publication.component.ts index 8798b3c1cf..3647e23367 100644 --- a/src/app/+item-page/simple/item-types/publication/publication.component.ts +++ b/src/app/+item-page/simple/item-types/publication/publication.component.ts @@ -1,12 +1,10 @@ -import { ChangeDetectionStrategy, Component, Inject, OnInit } from '@angular/core'; +import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; import { Observable } from 'rxjs'; -import { ItemDataService } from '../../../../core/data/item-data.service'; import { Item } from '../../../../core/shared/item.model'; import { DEFAULT_ITEM_TYPE, ItemViewMode, rendersItemType } from '../../../../shared/items/item-type-decorator'; -import { ITEM } from '../../../../shared/items/switcher/item-type-switcher.component'; import { ItemComponent } from '../shared/item.component'; import { MetadataRepresentation } from '../../../../core/shared/metadata-representation/metadata-representation.model'; import { filterRelationsByTypeLabel, relationsToItems } from '../shared/item-relationships-utils'; @@ -40,33 +38,26 @@ export class PublicationComponent extends ItemComponent implements OnInit { */ journalIssues$: Observable; - constructor( - @Inject(ITEM) public item: Item, - private ids: ItemDataService - ) { - super(item); - } - ngOnInit(): void { super.ngOnInit(); if (this.resolvedRelsAndTypes$) { - this.authors$ = this.buildRepresentations('Person', 'dc.contributor.author', this.ids); + this.authors$ = this.buildRepresentations('Person', 'dc.contributor.author'); this.projects$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isProjectOfPublication'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); this.orgUnits$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isOrgUnitOfPublication'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); this.journalIssues$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isJournalIssueOfPublication'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); } diff --git a/src/app/+item-page/simple/item-types/shared/item-relationships-utils.ts b/src/app/+item-page/simple/item-types/shared/item-relationships-utils.ts index 93cb196c59..321bbc4751 100644 --- a/src/app/+item-page/simple/item-types/shared/item-relationships-utils.ts +++ b/src/app/+item-page/simple/item-types/shared/item-relationships-utils.ts @@ -3,10 +3,8 @@ import { Observable } from 'rxjs/internal/Observable'; import { Relationship } from '../../../../core/shared/item-relationships/relationship.model'; import { RelationshipType } from '../../../../core/shared/item-relationships/relationship-type.model'; import { distinctUntilChanged, flatMap, map } from 'rxjs/operators'; -import { zip as observableZip } from 'rxjs'; -import { ItemDataService } from '../../../../core/data/item-data.service'; +import { zip as observableZip, combineLatest as observableCombineLatest } from 'rxjs'; import { Item } from '../../../../core/shared/item.model'; -import { RemoteData } from '../../../../core/data/remote-data'; /** * Operator for comparing arrays using a mapping function @@ -55,26 +53,25 @@ export const filterRelationsByTypeLabel = (label: string) => /** * Operator for turning a list of relationships into a list of the relevant items * @param {string} thisId The item's id of which the relations belong to - * @param {ItemDataService} ids The ItemDataService to fetch items from the REST API * @returns {(source: Observable) => Observable} */ -export const relationsToItems = (thisId: string, ids: ItemDataService) => +export const relationsToItems = (thisId: string) => (source: Observable): Observable => source.pipe( flatMap((rels: Relationship[]) => observableZip( - ...rels.map((rel: Relationship) => { - let queryId = rel.leftId; - if (rel.leftId === thisId) { - queryId = rel.rightId; - } - return ids.findById(queryId); - }) + ...rels.map((rel: Relationship) => observableCombineLatest(rel.leftItem, rel.rightItem)) ) ), - map((arr: Array>) => + map((arr) => arr - .filter((d: RemoteData) => d.hasSucceeded) - .map((d: RemoteData) => d.payload)), + .filter(([leftItem, rightItem]) => leftItem.hasSucceeded && rightItem.hasSucceeded) + .map(([leftItem, rightItem]) => { + if (leftItem.payload.id === thisId) { + return rightItem.payload; + } else if (rightItem.payload.id === thisId) { + return leftItem.payload; + } + })), distinctUntilChanged(compareArraysUsingIds()), ); 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 56bd56cc0f..21eb4843ed 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 @@ -23,7 +23,7 @@ import { compareArraysUsingIds } from './item-relationships-utils'; * @param metadata The list of original Metadatum objects * @param ids The ItemDataService to use for fetching Items from the Rest API */ -export const relationsToRepresentations = (thisId: string, itemType: string, metadata: MetadataValue[], ids: ItemDataService) => +export const relationsToRepresentations = (thisId: string, itemType: string, metadata: MetadataValue[]) => (source: Observable): Observable => source.pipe( flatMap((rels: Relationship[]) => @@ -35,13 +35,16 @@ export const relationsToRepresentations = (thisId: string, itemType: string, met const matchingRels = rels.filter((rel: Relationship) => ('' + rel.id) === metadatum.virtualValue); if (matchingRels.length > 0) { const matchingRel = matchingRels[0]; - let queryId = matchingRel.leftId; - if (matchingRel.leftId === thisId) { - queryId = matchingRel.rightId; - } - return ids.findById(queryId).pipe( - getSucceededRemoteData(), - map((d: RemoteData) => Object.assign(new ItemMetadataRepresentation(), d.payload)) + return observableCombineLatest(matchingRel.leftItem, matchingRel.rightItem).pipe( + filter(([leftItem, rightItem]) => leftItem.hasSucceeded && rightItem.hasSucceeded), + map(([leftItem, rightItem]) => { + if (leftItem.payload.id === thisId) { + return rightItem.payload; + } else if (rightItem.payload.id === thisId) { + return leftItem.payload; + } + }), + map((item: Item) => Object.assign(new ItemMetadataRepresentation(), item)) ); } } else { @@ -102,7 +105,7 @@ export class ItemComponent implements OnInit { * @param metadataField The metadata field that resembles the item type. * @param itemDataService ItemDataService to turn relations into items. */ - buildRepresentations(itemType: string, metadataField: string, itemDataService: ItemDataService): Observable { + buildRepresentations(itemType: string, metadataField: string): Observable { const metadata = this.item.findMetadataSortedByPlace(metadataField); const relsCurrentPage$ = this.item.relationships.pipe( getSucceededRemoteData(), @@ -112,7 +115,7 @@ export class ItemComponent implements OnInit { ); return relsCurrentPage$.pipe( - relationsToRepresentations(this.item.id, itemType, metadata, itemDataService) + relationsToRepresentations(this.item.id, itemType, metadata) ); } diff --git a/src/app/core/cache/models/items/normalized-relationship.model.ts b/src/app/core/cache/models/items/normalized-relationship.model.ts index b908426361..4c8b4cdc22 100644 --- a/src/app/core/cache/models/items/normalized-relationship.model.ts +++ b/src/app/core/cache/models/items/normalized-relationship.model.ts @@ -18,17 +18,13 @@ export class NormalizedRelationship extends NormalizedObject { @autoserialize id: string; - /** - * The identifier of the Item to the left side of this Relationship - */ @autoserialize - leftId: string; + @relationship(ResourceType.Item, false) + leftItem: string; - /** - * The identifier of the Item to the right side of this Relationship - */ @autoserialize - rightId: string; + @relationship(ResourceType.Item, false) + rightItem: string; /** * The place of the Item to the left side of this Relationship diff --git a/src/app/core/shared/item-relationships/relationship.model.ts b/src/app/core/shared/item-relationships/relationship.model.ts index df8f04cd8a..a2f612591f 100644 --- a/src/app/core/shared/item-relationships/relationship.model.ts +++ b/src/app/core/shared/item-relationships/relationship.model.ts @@ -3,6 +3,7 @@ import { CacheableObject } from '../../cache/object-cache.reducer'; import { RemoteData } from '../../data/remote-data'; import { ResourceType } from '../resource-type'; import { RelationshipType } from './relationship-type.model'; +import { Item } from '../item.model'; /** * Describes a Relationship between two Items @@ -28,15 +29,9 @@ export class Relationship implements CacheableObject { */ id: string; - /** - * The identifier of the Item to the left side of this Relationship - */ - leftId: string; + leftItem: Observable>; - /** - * The identifier of the Item to the right side of this Relationship - */ - rightId: string; + rightItem: Observable>; /** * The place of the Item to the left side of this Relationship diff --git a/src/app/entity-groups/journal-entities/item-pages/journal-issue/journal-issue.component.ts b/src/app/entity-groups/journal-entities/item-pages/journal-issue/journal-issue.component.ts index 5f3d3b557d..a355431100 100644 --- a/src/app/entity-groups/journal-entities/item-pages/journal-issue/journal-issue.component.ts +++ b/src/app/entity-groups/journal-entities/item-pages/journal-issue/journal-issue.component.ts @@ -1,9 +1,7 @@ -import { Component, Inject } from '@angular/core'; +import { Component } from '@angular/core'; import { Observable } from 'rxjs'; -import { ItemDataService } from '../../../../core/data/item-data.service'; import { Item } from '../../../../core/shared/item.model'; import { ItemViewMode, rendersItemType } from '../../../../shared/items/item-type-decorator'; -import { ITEM } from '../../../../shared/items/switcher/item-type-switcher.component'; import { isNotEmpty } from '../../../../shared/empty.util'; import { ItemComponent } from '../../../../+item-page/simple/item-types/shared/item.component'; import { @@ -31,23 +29,17 @@ export class JournalIssueComponent extends ItemComponent { */ publications$: Observable; - constructor( - @Inject(ITEM) public item: Item, - private ids: ItemDataService - ) { - super(item); - } ngOnInit(): void { super.ngOnInit(); if (isNotEmpty(this.resolvedRelsAndTypes$)) { this.volumes$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isJournalVolumeOfIssue'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); this.publications$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isPublicationOfJournalIssue'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); } } diff --git a/src/app/entity-groups/journal-entities/item-pages/journal-volume/journal-volume.component.ts b/src/app/entity-groups/journal-entities/item-pages/journal-volume/journal-volume.component.ts index cff840e5bd..a851bfebbe 100644 --- a/src/app/entity-groups/journal-entities/item-pages/journal-volume/journal-volume.component.ts +++ b/src/app/entity-groups/journal-entities/item-pages/journal-volume/journal-volume.component.ts @@ -1,9 +1,7 @@ -import { Component, Inject } from '@angular/core'; +import { Component } from '@angular/core'; import { Observable } from 'rxjs'; -import { ItemDataService } from '../../../../core/data/item-data.service'; import { Item } from '../../../../core/shared/item.model'; import { ItemViewMode, rendersItemType } from '../../../../shared/items/item-type-decorator'; -import { ITEM } from '../../../../shared/items/switcher/item-type-switcher.component'; import { isNotEmpty } from '../../../../shared/empty.util'; import { ItemComponent } from '../../../../+item-page/simple/item-types/shared/item.component'; import { @@ -31,23 +29,17 @@ export class JournalVolumeComponent extends ItemComponent { */ issues$: Observable; - constructor( - @Inject(ITEM) public item: Item, - private ids: ItemDataService - ) { - super(item); - } ngOnInit(): void { super.ngOnInit(); if (isNotEmpty(this.resolvedRelsAndTypes$)) { this.journals$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isJournalOfVolume'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); this.issues$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isIssueOfJournalVolume'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); } } diff --git a/src/app/entity-groups/journal-entities/item-pages/journal/journal.component.ts b/src/app/entity-groups/journal-entities/item-pages/journal/journal.component.ts index 71da89cf4c..99183651e1 100644 --- a/src/app/entity-groups/journal-entities/item-pages/journal/journal.component.ts +++ b/src/app/entity-groups/journal-entities/item-pages/journal/journal.component.ts @@ -1,9 +1,7 @@ -import { Component, Inject } from '@angular/core'; +import { Component } from '@angular/core'; import { Observable } from 'rxjs'; -import { ItemDataService } from '../../../../core/data/item-data.service'; import { Item } from '../../../../core/shared/item.model'; import { ItemViewMode, rendersItemType } from '../../../../shared/items/item-type-decorator'; -import { ITEM } from '../../../../shared/items/switcher/item-type-switcher.component'; import { isNotEmpty } from '../../../../shared/empty.util'; import { ItemComponent } from '../../../../+item-page/simple/item-types/shared/item.component'; import { @@ -26,19 +24,13 @@ export class JournalComponent extends ItemComponent { */ volumes$: Observable; - constructor( - @Inject(ITEM) public item: Item, - private ids: ItemDataService - ) { - super(item); - } ngOnInit(): void { super.ngOnInit(); if (isNotEmpty(this.resolvedRelsAndTypes$)) { this.volumes$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isVolumeOfJournal'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); } } diff --git a/src/app/entity-groups/research-entities/item-pages/orgunit/orgunit.component.ts b/src/app/entity-groups/research-entities/item-pages/orgunit/orgunit.component.ts index cae70ce634..7101f05d35 100644 --- a/src/app/entity-groups/research-entities/item-pages/orgunit/orgunit.component.ts +++ b/src/app/entity-groups/research-entities/item-pages/orgunit/orgunit.component.ts @@ -1,9 +1,7 @@ -import { Component, Inject, OnInit } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { Observable } from 'rxjs'; -import { ItemDataService } from '../../../../core/data/item-data.service'; import { Item } from '../../../../core/shared/item.model'; import { ItemViewMode, rendersItemType } from '../../../../shared/items/item-type-decorator'; -import { ITEM } from '../../../../shared/items/switcher/item-type-switcher.component'; import { isNotEmpty } from '../../../../shared/empty.util'; import { ItemComponent } from '../../../../+item-page/simple/item-types/shared/item.component'; import { @@ -36,30 +34,23 @@ export class OrgunitComponent extends ItemComponent implements OnInit { */ publications$: Observable; - constructor( - @Inject(ITEM) public item: Item, - private ids: ItemDataService - ) { - super(item); - } - ngOnInit(): void { super.ngOnInit(); if (isNotEmpty(this.resolvedRelsAndTypes$)) { this.people$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isPersonOfOrgUnit'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); this.projects$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isProjectOfOrgUnit'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); this.publications$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isPublicationOfOrgUnit'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); } }} diff --git a/src/app/entity-groups/research-entities/item-pages/person/person.component.ts b/src/app/entity-groups/research-entities/item-pages/person/person.component.ts index 8ef3ceb57e..ec91561eb9 100644 --- a/src/app/entity-groups/research-entities/item-pages/person/person.component.ts +++ b/src/app/entity-groups/research-entities/item-pages/person/person.component.ts @@ -1,6 +1,5 @@ import { Component, Inject } from '@angular/core'; import { Observable , of as observableOf } from 'rxjs'; -import { ItemDataService } from '../../../../core/data/item-data.service'; import { Item } from '../../../../core/shared/item.model'; import { ItemViewMode, rendersItemType } from '../../../../shared/items/item-type-decorator'; import { ITEM } from '../../../../shared/items/switcher/item-type-switcher.component'; @@ -49,7 +48,6 @@ export class PersonComponent extends ItemComponent { constructor( @Inject(ITEM) public item: Item, - private ids: ItemDataService, private fixedFilterService: SearchFixedFilterService ) { super(item); @@ -60,17 +58,17 @@ export class PersonComponent extends ItemComponent { if (isNotEmpty(this.resolvedRelsAndTypes$)) { this.publications$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isPublicationOfAuthor'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); this.projects$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isProjectOfPerson'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); this.orgUnits$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isOrgUnitOfPerson'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); this.fixedFilterQuery = this.fixedFilterService.getQueryByRelations('isAuthorOfPublication', this.item.id); diff --git a/src/app/entity-groups/research-entities/item-pages/project/project.component.ts b/src/app/entity-groups/research-entities/item-pages/project/project.component.ts index 074bc62363..6a73e187c9 100644 --- a/src/app/entity-groups/research-entities/item-pages/project/project.component.ts +++ b/src/app/entity-groups/research-entities/item-pages/project/project.component.ts @@ -1,9 +1,7 @@ -import { Component, Inject, OnInit } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { Observable } from 'rxjs'; -import { ItemDataService } from '../../../../core/data/item-data.service'; import { Item } from '../../../../core/shared/item.model'; import { ItemViewMode, rendersItemType } from '../../../../shared/items/item-type-decorator'; -import { ITEM } from '../../../../shared/items/switcher/item-type-switcher.component'; import { isNotEmpty } from '../../../../shared/empty.util'; import { ItemComponent } from '../../../../+item-page/simple/item-types/shared/item.component'; import { @@ -36,30 +34,23 @@ export class ProjectComponent extends ItemComponent implements OnInit { */ orgUnits$: Observable; - constructor( - @Inject(ITEM) public item: Item, - private ids: ItemDataService - ) { - super(item); - } - ngOnInit(): void { super.ngOnInit(); if (isNotEmpty(this.resolvedRelsAndTypes$)) { this.people$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isPersonOfProject'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); this.publications$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isPublicationOfProject'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); this.orgUnits$ = this.resolvedRelsAndTypes$.pipe( filterRelationsByTypeLabel('isOrgUnitOfProject'), - relationsToItems(this.item.id, this.ids) + relationsToItems(this.item.id) ); } }