fixed relationship service finished

This commit is contained in:
lotte
2019-08-09 12:54:01 +02:00
parent 9907087dc3
commit 827ffc2d79
11 changed files with 97 additions and 216 deletions

View File

@@ -7,7 +7,6 @@ import {
} from '../../../../shared/items/item-type-decorator';
import { ItemComponent } from '../shared/item.component';
import { MetadataRepresentation } from '../../../../core/shared/metadata-representation/metadata-representation.model';
import { getRelatedItemsByTypeLabel } from '../shared/item-relationships-utils';
@rendersItemType('Publication', ItemViewMode.Detail)
@rendersItemType(DEFAULT_ITEM_TYPE, ItemViewMode.Detail)
@@ -41,22 +40,12 @@ export class PublicationComponent extends ItemComponent implements OnInit {
ngOnInit(): void {
super.ngOnInit();
if (this.resolvedRelsAndTypes$) {
this.authors$ = this.buildRepresentations('Person', 'dc.contributor.author');
this.projects$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isProjectOfPublication')
);
this.projects$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isProjectOfPublication');
this.orgUnits$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isOrgUnitOfPublication')
);
this.journalIssues$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isJournalIssueOfPublication')
);
this.orgUnits$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isOrgUnitOfPublication');
}
this.journalIssues$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isJournalIssueOfPublication');
}
}

View File

@@ -46,36 +46,6 @@ export const compareArraysUsing = <T>(mapFn: (t: T) => any) =>
export const compareArraysUsingIds = <T extends { id: string }>() =>
compareArraysUsing((t: T) => hasValue(t) ? t.id : undefined);
/**
* Fetch the relationships which match the type label given
* @param {string} label Type label
* @param thisId The item's id of which the relations belong to
* @returns {(source: Observable<[Relationship[] , RelationshipType[]]>) => Observable<Relationship[]>}
*/
export const filterRelationsByTypeLabel = (label: string, thisId?: string) =>
(source: Observable<[Relationship[], RelationshipType[]]>): Observable<Relationship[]> =>
source.pipe(
switchMap(([relsCurrentPage, relTypesCurrentPage]) => {
const relatedItems$ = observableZip(...relsCurrentPage.map((rel: Relationship) =>
observableCombineLatest(
rel.leftItem.pipe(getSucceededRemoteData(), getRemoteDataPayload()),
rel.rightItem.pipe(getSucceededRemoteData(), getRemoteDataPayload()))
)
);
return relatedItems$.pipe(
map((arr) => relsCurrentPage.filter((rel: Relationship, idx: number) =>
hasValue(relTypesCurrentPage[idx]) && (
(hasNoValue(thisId) && (relTypesCurrentPage[idx].leftLabel === label ||
relTypesCurrentPage[idx].rightLabel === label)) ||
(thisId === arr[idx][0].id && relTypesCurrentPage[idx].leftLabel === label) ||
(thisId === arr[idx][1].id && relTypesCurrentPage[idx].rightLabel === label)
)
))
);
}),
distinctUntilChanged(compareArraysUsingIds())
);
/**
* 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
@@ -104,19 +74,6 @@ export const relationsToItems = (thisId: string) =>
distinctUntilChanged(compareArraysUsingIds()),
);
/**
* Operator for turning a list of relationships and their relationship-types into a list of relevant items by relationship label
* @param thisId The item's id of which the relations belong to
* @param label The label of the relationship-type to filter on
* @param side Filter only on one side of the relationship (for example: child-parent relationships)
*/
export const getRelatedItemsByTypeLabel = (thisId: string, label: string) =>
(source: Observable<[Relationship[], RelationshipType[]]>): Observable<Item[]> =>
source.pipe(
filterRelationsByTypeLabel(label, thisId),
relationsToItems(thisId)
);
/**
* Operator for turning a list of relationships into a list of metadatarepresentations given the original metadata
* @param parentId The id of the parent item

View File

@@ -15,6 +15,7 @@ import { MetadatumRepresentation } from '../../../../core/shared/metadata-repres
import { of } from 'rxjs/internal/observable/of';
import { MetadataValue } from '../../../../core/shared/metadata.models';
import { compareArraysUsingIds } from './item-relationships-utils';
import { RelationshipService } from '../../../../core/data/relationship.service';
/**
* Operator for turning a list of relationships into a list of metadatarepresentations given the original metadata
@@ -62,39 +63,12 @@ export const relationsToRepresentations = (thisId: string, itemType: string, met
* A generic component for displaying metadata and relations of an item
*/
export class ItemComponent implements OnInit {
/**
* Resolved relationships and types together in one observable
*/
resolvedRelsAndTypes$: Observable<[Relationship[], RelationshipType[]]>;
constructor(
@Inject(ITEM) public item: Item
@Inject(ITEM) public item: Item,
protected relationshipService: RelationshipService
) {}
ngOnInit(): void {
const relationships$ = this.item.relationships;
if (relationships$) {
const relsCurrentPage$ = relationships$.pipe(
filter((rd: RemoteData<PaginatedList<Relationship>>) => rd.hasSucceeded),
getRemoteDataPayload(),
map((pl: PaginatedList<Relationship>) => pl.page),
distinctUntilChanged(compareArraysUsingIds())
);
const relTypesCurrentPage$ = relsCurrentPage$.pipe(
flatMap((rels: Relationship[]) =>
observableZip(...rels.map((rel: Relationship) => rel.relationshipType)).pipe(
map(([...arr]: Array<RemoteData<RelationshipType>>) => arr.map((d: RemoteData<RelationshipType>) => d.payload))
)
),
distinctUntilChanged(compareArraysUsingIds())
);
this.resolvedRelsAndTypes$ = observableCombineLatest(
relsCurrentPage$,
relTypesCurrentPage$
);
}
}
/**

View File

@@ -3,7 +3,7 @@ import { RequestService } from './request.service';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { hasValue, hasValueOperator, isNotEmptyOperator } from '../../shared/empty.util';
import { distinctUntilChanged, filter, flatMap, map, startWith, switchMap, tap } from 'rxjs/operators';
import { distinctUntilChanged, filter, flatMap, map, startWith, switchAll, switchMap, tap } from 'rxjs/operators';
import { configureRequest, getRemoteDataPayload, getResponseFromEntry, getSucceededRemoteData } from '../shared/operators';
import { DeleteRequest, GetRequest, PostRequest, RestRequest } from './request.models';
import { Observable } from 'rxjs/internal/Observable';
@@ -18,7 +18,6 @@ import { ItemDataService } from './item-data.service';
import { compareArraysUsingIds, relationsToItems } from '../../+item-page/simple/item-types/shared/item-relationships-utils';
import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service';
import { HttpHeaders } from '@angular/common/http';
import { RequestEntry } from './request.reducer';
/**
* The service handling all relationship requests
@@ -88,36 +87,36 @@ export class RelationshipService {
);
}
/**
* Get an item its relationships in the form of an array
* @param item
*/
getItemRelationships(item: Item): Observable<RemoteData<PaginatedList<Relationship>>> {
return this.halService.getEndpoint('items/' + item.uuid + '/relationships')
.pipe(
map((endpointURL: string) => new GetRequest(this.requestService.generateRequestId(), endpointURL)),
configureRequest(this.requestService),
switchMap((restRequest: RestRequest) => this.requestService.getByUUID(restRequest.uuid)),
filter((entry: RequestEntry) => hasValue(entry) && hasValue(entry.response)),
switchMap((entry: RequestEntry) => this.rdbService.buildList(entry.request.href)),
) as any;
}
/**
* Get an item its relationships in the form of an array
* @param item
*/
getItemRelationshipsByType(item: Item, relationshipTypeLabel: string): Observable<RemoteData<PaginatedList<Relationship>>> {
return this.halService.getEndpoint(this.linkPath + '/search/byLabel')
.pipe(
map((endpointURL: string) => `${endpointURL}?label=${relationshipTypeLabel}&dso=${item.uuid}`),
map((endpointURL: string) => new GetRequest(this.requestService.generateRequestId(), endpointURL)),
configureRequest(this.requestService),
switchMap((restRequest: RestRequest) => this.requestService.getByUUID(restRequest.uuid)),
filter((entry: RequestEntry) => hasValue(entry) && hasValue(entry.response)),
switchMap((entry: RequestEntry) => this.rdbService.buildList(entry.request.href))
);
}
// /**
// * Get an item its relationships in the form of an array
// * @param item
// */
// getItemRelationships(item: Item): Observable<RemoteData<PaginatedList<Relationship>>> {
// return this.halService.getEndpoint('items/' + item.uuid + '/relationships')
// .pipe(
// map((endpointURL: string) => new GetRequest(this.requestService.generateRequestId(), endpointURL)),
// configureRequest(this.requestService),
// switchMap((restRequest: RestRequest) => this.requestService.getByUUID(restRequest.uuid)),
// filter((entry: RequestEntry) => hasValue(entry) && hasValue(entry.response)),
// switchMap((entry: RequestEntry) => this.rdbService.buildList(entry.request.href)),
// ) as any;
// }
//
// /**
// * Get an item its relationships in the form of an array
// * @param item
// */
// getItemRelationshipsByType(item: Item, relationshipTypeLabel: string): Observable<RemoteData<PaginatedList<Relationship>>> {
// return this.halService.getEndpoint(this.linkPath + '/search/byLabel')
// .pipe(
// map((endpointURL: string) => `${endpointURL}?label=${relationshipTypeLabel}&dso=${item.uuid}`),
// map((endpointURL: string) => new GetRequest(this.requestService.generateRequestId(), endpointURL)),
// configureRequest(this.requestService),
// switchMap((restRequest: RestRequest) => this.requestService.getByUUID(restRequest.uuid)),
// filter((entry: RequestEntry) => hasValue(entry) && hasValue(entry.response)),
// switchMap((entry: RequestEntry) => this.rdbService.buildList(entry.request.href))
// );
// }
/**
@@ -142,9 +141,7 @@ export class RelationshipService {
* @param item
*/
getRelationshipTypeLabelsByItem(item: Item): Observable<string[]> {
return this.getItemRelationships(item).pipe(
getSucceededRemoteData(),
map((relationshipsListRD: RemoteData<PaginatedList<Relationship>>) => relationshipsListRD.payload.page),
return this.getItemRelationshipsArray(item).pipe(
switchMap((relationships: Relationship[]) => observableCombineLatest(relationships.map((relationship: Relationship) => this.getRelationshipTypeLabelByRelationshipAndItem(relationship, item)))),
map((labels: string[]) => Array.from(new Set(labels)))
);
@@ -184,12 +181,39 @@ export class RelationshipService {
* @param item
* @param label
*/
//TODO THIS THING IS CAUSING ISSUES (the getbytype thingie)
getRelatedItemsByLabel(item: Item, label: string): Observable<Item[]> {
return this.getItemRelationshipsByType(item, label).pipe(
return this.getItemRelationshipsByLabel(item, label).pipe(relationsToItems(item.uuid));
}
/**
* Resolve a given item's relationships into related items, filtered by a relationship label
* and return the items as an array
* @param item
* @param label
*/
getItemRelationshipsByLabel(item: Item, label: string): Observable<Relationship[]> {
return this.getItemRelationshipsArray(item).pipe(
switchMap((relationships: Relationship[]) => {
return observableCombineLatest(
...relationships.map((relationship: Relationship) => {
return relationship.relationshipType.pipe(
getSucceededRemoteData(),
map((relationshipsListRD: RemoteData<PaginatedList<Relationship>>) => relationshipsListRD.payload.page),
relationsToItems(item.uuid)
getRemoteDataPayload(),
map((relationshipType: RelationshipType) => {
if (label === relationshipType.rightLabel || label === relationshipType.leftLabel) {
return relationship;
} else {
return undefined;
}
}),
)
})
)
}),
map((relationships: Relationship[]) =>
relationships.filter((relationship: Relationship) => hasValue(relationship))
),
);
}
@@ -213,10 +237,7 @@ export class RelationshipService {
);
}))
}),
map((relationships: Relationship[]) => {
const test = relationships.filter((relationship => hasValue(relationship))); console.log(test); return test;
}),
tap((ids) => console.log(ids))
map((relationships: Relationship[]) => relationships.filter((relationship => hasValue(relationship)))),
)
}

View File

@@ -2,9 +2,7 @@ import { Component } from '@angular/core';
import { Observable } from 'rxjs';
import { Item } from '../../../../core/shared/item.model';
import { ItemViewMode, rendersItemType } from '../../../../shared/items/item-type-decorator';
import { isNotEmpty } from '../../../../shared/empty.util';
import { ItemComponent } from '../../../../+item-page/simple/item-types/shared/item.component';
import { getRelatedItemsByTypeLabel } from '../../../../+item-page/simple/item-types/shared/item-relationships-utils';
@rendersItemType('JournalIssue', ItemViewMode.Detail)
@Component({
@@ -28,14 +26,7 @@ export class JournalIssueComponent extends ItemComponent {
ngOnInit(): void {
super.ngOnInit();
if (isNotEmpty(this.resolvedRelsAndTypes$)) {
this.volumes$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isJournalVolumeOfIssue')
);
this.publications$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isPublicationOfJournalIssue')
);
}
this.volumes$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isJournalVolumeOfIssue');
this.publications$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isPublicationOfJournalIssue');
}
}

View File

@@ -2,9 +2,7 @@ import { Component } from '@angular/core';
import { Observable } from 'rxjs';
import { Item } from '../../../../core/shared/item.model';
import { ItemViewMode, rendersItemType } from '../../../../shared/items/item-type-decorator';
import { isNotEmpty } from '../../../../shared/empty.util';
import { ItemComponent } from '../../../../+item-page/simple/item-types/shared/item.component';
import { getRelatedItemsByTypeLabel } from '../../../../+item-page/simple/item-types/shared/item-relationships-utils';
@rendersItemType('JournalVolume', ItemViewMode.Detail)
@Component({
@@ -28,14 +26,7 @@ export class JournalVolumeComponent extends ItemComponent {
ngOnInit(): void {
super.ngOnInit();
if (isNotEmpty(this.resolvedRelsAndTypes$)) {
this.journals$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isJournalOfVolume')
);
this.issues$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isIssueOfJournalVolume')
);
}
this.journals$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isJournalOfVolume');
this.issues$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isIssueOfJournalVolume');
}
}

View File

@@ -2,9 +2,7 @@ import { Component } from '@angular/core';
import { Observable } from 'rxjs';
import { Item } from '../../../../core/shared/item.model';
import { ItemViewMode, rendersItemType } from '../../../../shared/items/item-type-decorator';
import { isNotEmpty } from '../../../../shared/empty.util';
import { ItemComponent } from '../../../../+item-page/simple/item-types/shared/item.component';
import { getRelatedItemsByTypeLabel } from '../../../../+item-page/simple/item-types/shared/item-relationships-utils';
@rendersItemType('Journal', ItemViewMode.Detail)
@Component({
@@ -23,11 +21,6 @@ export class JournalComponent extends ItemComponent {
ngOnInit(): void {
super.ngOnInit();
if (isNotEmpty(this.resolvedRelsAndTypes$)) {
this.volumes$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isVolumeOfJournal')
);
}
this.volumes$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isVolumeOfJournal');
}
}

View File

@@ -2,9 +2,7 @@ import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { Item } from '../../../../core/shared/item.model';
import { ItemViewMode, rendersItemType } from '../../../../shared/items/item-type-decorator';
import { isNotEmpty } from '../../../../shared/empty.util';
import { ItemComponent } from '../../../../+item-page/simple/item-types/shared/item.component';
import { getRelatedItemsByTypeLabel } from '../../../../+item-page/simple/item-types/shared/item-relationships-utils';
@rendersItemType('OrgUnit', ItemViewMode.Detail)
@Component({
@@ -33,18 +31,8 @@ export class OrgunitComponent extends ItemComponent implements OnInit {
ngOnInit(): void {
super.ngOnInit();
if (isNotEmpty(this.resolvedRelsAndTypes$)) {
this.people$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isPersonOfOrgUnit')
);
this.projects$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isProjectOfOrgUnit')
);
this.publications$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isPublicationOfOrgUnit')
);
this.people$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isPersonOfOrgUnit');
this.projects$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isProjectOfOrgUnit');
this.publications$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isPublicationOfOrgUnit');
}
}
}}

View File

@@ -3,10 +3,9 @@ import { Observable, of as observableOf } from 'rxjs';
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 { getRelatedItemsByTypeLabel } from '../../../../+item-page/simple/item-types/shared/item-relationships-utils';
import { getQueryByRelations } from '../../../../shared/utils/relation-query.utils';
import { RelationshipService } from '../../../../core/data/relationship.service';
@rendersItemType('Person', ItemViewMode.Detail)
@Component({
@@ -44,28 +43,19 @@ export class PersonComponent extends ItemComponent {
fixedFilterQuery: string;
constructor(
@Inject(ITEM) public item: Item
@Inject(ITEM) public item: Item,
protected relationshipService: RelationshipService
) {
super(item);
super(item, relationshipService);
}
ngOnInit(): void {
super.ngOnInit();
if (isNotEmpty(this.resolvedRelsAndTypes$)) {
this.publications$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isPublicationOfAuthor')
);
this.projects$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isProjectOfPerson')
);
this.orgUnits$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isOrgUnitOfPerson')
);
this.publications$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isPublicationOfAuthor');
this.projects$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isProjectOfPerson');
this.orgUnits$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isOrgUnitOfPerson');
this.fixedFilterQuery = getQueryByRelations('isAuthorOfPublication', this.item.id);
this.fixedFilter$ = observableOf('publication');
}
}
}

View File

@@ -3,9 +3,7 @@ import { Observable } from 'rxjs';
import { Item } from '../../../../core/shared/item.model';
import { MetadataRepresentation } from '../../../../core/shared/metadata-representation/metadata-representation.model';
import { ItemViewMode, rendersItemType } from '../../../../shared/items/item-type-decorator';
import { isNotEmpty } from '../../../../shared/empty.util';
import { ItemComponent } from '../../../../+item-page/simple/item-types/shared/item.component';
import { getRelatedItemsByTypeLabel } from '../../../../+item-page/simple/item-types/shared/item-relationships-utils';
@rendersItemType('Project', ItemViewMode.Detail)
@Component({
@@ -39,21 +37,10 @@ export class ProjectComponent extends ItemComponent implements OnInit {
ngOnInit(): void {
super.ngOnInit();
if (isNotEmpty(this.resolvedRelsAndTypes$)) {
this.contributors$ = this.buildRepresentations('OrgUnit', 'project.contributor.other');
this.people$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isPersonOfProject')
);
this.publications$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isPublicationOfProject')
);
this.orgUnits$ = this.resolvedRelsAndTypes$.pipe(
getRelatedItemsByTypeLabel(this.item.id, 'isOrgUnitOfProject')
);
}
this.people$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isPersonOfProject');
this.publications$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isPublicationOfProject');
this.orgUnits$ = this.relationshipService.getRelatedItemsByLabel(this.item, 'isOrgUnitOfProject');
}
}

View File

@@ -175,7 +175,7 @@ export class DsDynamicLookupRelationModalComponent implements OnInit, OnDestroy
deselect(selectableObject: SearchResult<Item>) {
this.itemRD$.pipe(
getSucceededRemoteData(),
switchMap((itemRD: RemoteData<Item>) => this.relationshipService.getItemRelationshipsByType(itemRD.payload, this.relationship.relationshipType)),
switchMap((itemRD: RemoteData<Item>) => this.relationshipService.getItemRelationshipsByLabel(itemRD.payload, this.relationship.relationshipType)),
// map((items: Item[]) => items.find((item: Item) => ))
).subscribe();