refactorting relationship service functions

This commit is contained in:
lotte
2019-08-07 16:21:49 +02:00
parent 848b700f84
commit b6e9c5b840
3 changed files with 63 additions and 84 deletions

View File

@@ -65,7 +65,7 @@ export class ItemRelationshipsComponent extends AbstractItemUpdateComponent impl
*/ */
ngOnInit(): void { ngOnInit(): void {
super.ngOnInit(); super.ngOnInit();
this.relationLabels$ = this.relationshipService.getItemRelationshipLabels(this.item); this.relationLabels$ = this.relationshipService.getRelationshipTypeLabelsByItem(this.item);
this.initializeItemUpdate(); this.initializeItemUpdate();
} }

View File

@@ -3,13 +3,8 @@ import { RequestService } from './request.service';
import { HALEndpointService } from '../shared/hal-endpoint.service'; import { HALEndpointService } from '../shared/hal-endpoint.service';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { hasValue, hasValueOperator, isNotEmptyOperator } from '../../shared/empty.util'; import { hasValue, hasValueOperator, isNotEmptyOperator } from '../../shared/empty.util';
import { distinctUntilChanged, filter, flatMap, map, switchMap, take, tap } from 'rxjs/operators'; import { distinctUntilChanged, filter, flatMap, map, switchMap, take } from 'rxjs/operators';
import { import { configureRequest, getRemoteDataPayload, getResponseFromEntry, getSucceededRemoteData } from '../shared/operators';
configureRequest,
filterSuccessfulResponses,
getRemoteDataPayload, getResponseFromEntry,
getSucceededRemoteData
} from '../shared/operators';
import { DeleteRequest, GetRequest, PostRequest, RestRequest } from './request.models'; import { DeleteRequest, GetRequest, PostRequest, RestRequest } from './request.models';
import { Observable } from 'rxjs/internal/Observable'; import { Observable } from 'rxjs/internal/Observable';
import { RestResponse } from '../cache/response.models'; import { RestResponse } from '../cache/response.models';
@@ -18,17 +13,13 @@ import { Relationship } from '../shared/item-relationships/relationship.model';
import { RelationshipType } from '../shared/item-relationships/relationship-type.model'; import { RelationshipType } from '../shared/item-relationships/relationship-type.model';
import { RemoteData } from './remote-data'; import { RemoteData } from './remote-data';
import { combineLatest as observableCombineLatest } from 'rxjs/internal/observable/combineLatest'; import { combineLatest as observableCombineLatest } from 'rxjs/internal/observable/combineLatest';
import { zip as observableZip } from 'rxjs'; import { zip as observableZip, race as observableRace } from 'rxjs';
import { PaginatedList } from './paginated-list'; import { PaginatedList } from './paginated-list';
import { ItemDataService } from './item-data.service'; import { ItemDataService } from './item-data.service';
import { import { compareArraysUsingIds, relationsToItems } from '../../+item-page/simple/item-types/shared/item-relationships-utils';
compareArraysUsingIds, filterRelationsByTypeLabel,
relationsToItems
} from '../../+item-page/simple/item-types/shared/item-relationships-utils';
import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service'; import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service';
import { HttpHeaders } from '@angular/common/http'; import { HttpHeaders } from '@angular/common/http';
import { DeprecatedCurrencyPipe } from '@angular/common'; import { RequestEntry } from './request.reducer';
import { deprecate } from 'util';
/** /**
* The service handling all relationship requests * The service handling all relationship requests
@@ -85,45 +76,7 @@ export class RelationshipService {
} }
/** /**
* Get a combined observable containing an array of all relationships in an item, as well as an array of the relationships their types * Get an item its relationships in the form of an array
* This is used for easier access of a relationship's type because they exist as observables
* @param item
*/
getItemResolvedRelsAndTypes(item: Item): Observable<[Relationship[], RelationshipType[]]> {
return observableCombineLatest(
this.getItemRelationshipsArray(item),
this.getItemRelationshipTypesArray(item)
);
}
/**
* Get a combined observable containing an array of all the item's relationship's left- and right-side items, as well as an array of the relationships their types
* This is used for easier access of a relationship's type and left and right items because they exist as observables
* @param item
*/
getItemResolvedRelatedItemsAndTypes(item: Item): Observable<[Item[], Item[], RelationshipType[]]> {
return observableCombineLatest(
this.getItemLeftRelatedItemArray(item),
this.getItemRightRelatedItemArray(item),
this.getItemRelationshipTypesArray(item)
);
}
/**
* Get a combined observable containing an array of all the item's relationship's left- and right-side items, as well as an array of the relationships themselves
* This is used for easier access of the relationship and their left and right items because they exist as observables
* @param item
*/
getItemResolvedRelatedItemsAndRelationships(item: Item): Observable<[Item[], Item[], Relationship[]]> {
return observableCombineLatest(
this.getItemLeftRelatedItemArray(item),
this.getItemRightRelatedItemArray(item),
this.getItemRelationshipsArray(item)
);
}
/**
* Get an item their relationships in the form of an array
* @param item * @param item
*/ */
getItemRelationshipsArray(item: Item): Observable<Relationship[]> { getItemRelationshipsArray(item: Item): Observable<Relationship[]> {
@@ -138,29 +91,39 @@ export class RelationshipService {
/** /**
* Get an item their relationships in the form of an array * Get an item its relationships in the form of an array
* @param item * @param item
*/ */
getItemRelationships(item: Item): void { getItemRelationships(item: Item): Observable<RemoteData<PaginatedList<Relationship>>> {
this.halService.getEndpoint(item.self + '/relationships') return this.halService.getEndpoint('items/' + item.uuid + '/relationships')
.pipe( .pipe(
map((endpointURL: string) => new GetRequest(this.requestService.generateRequestId(), endpointURL)), map((endpointURL: string) => new GetRequest(this.requestService.generateRequestId(), endpointURL)),
configureRequest(this.requestService), configureRequest(this.requestService),
switchMap((restRequest: RestRequest) => this.requestService.getByUUID(restRequest.uuid)), switchMap((restRequest: RestRequest) => this.requestService.getByUUID(restRequest.uuid)),
getResponseFromEntry() filter((entry: RequestEntry) => hasValue(entry) && hasValue(entry.response)),
).subscribe(t => console.log(t)); switchMap((entry: RequestEntry) => this.rdbService.buildList(entry.request.href))
);
} }
// /** // /**
// * Get an item their relationships in the form of an array // * Get an item its relationships in the form of an array
// * @param item // * @param item
// */ // */
// getItemRelationshipsByType(item: Item, relationshipTypeLabel: string): Observable<Relationship[]> { getItemRelationshipsByType(item: Item, relationshipTypeLabel: string): Observable<RemoteData<PaginatedList<Relationship>>> {
// this.getItemRelationshipsArray(item: Item) 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 their relationship types in the form of an array * Get an item its relationship types in the form of an array
* @param item * @param item
*/ */
getItemRelationshipTypesArray(item: Item): Observable<RelationshipType[]> { getItemRelationshipTypesArray(item: Item): Observable<RelationshipType[]> {
@@ -176,10 +139,10 @@ export class RelationshipService {
} }
/** /**
* Get an item his relationship's left-side related items in the form of an array * Get an item its relationship's left-side related items in the form of an array
* @param item * @param item
*/ */
getItemLeftRelatedItemArray(item: Item): Observable<Item[]> { getLeft(item: Item): Observable<Item[]> {
return this.getItemRelationshipsArray(item).pipe( return this.getItemRelationshipsArray(item).pipe(
flatMap((rels: Relationship[]) => observableZip(...rels.map((rel: Relationship) => rel.leftItem)).pipe( flatMap((rels: Relationship[]) => observableZip(...rels.map((rel: Relationship) => rel.leftItem)).pipe(
map(([...arr]: Array<RemoteData<Item>>) => arr.map((rd: RemoteData<Item>) => rd.payload).filter((i) => hasValue(i))), map(([...arr]: Array<RemoteData<Item>>) => arr.map((rd: RemoteData<Item>) => rd.payload).filter((i) => hasValue(i))),
@@ -190,7 +153,7 @@ export class RelationshipService {
} }
/** /**
* Get an item his relationship's right-side related items in the form of an array * Get an item its relationship's right-side related items in the form of an array
* @param item * @param item
*/ */
getItemRightRelatedItemArray(item: Item): Observable<Item[]> { getItemRightRelatedItemArray(item: Item): Observable<Item[]> {
@@ -204,31 +167,46 @@ export class RelationshipService {
} }
/** /**
* Get an array of an item their unique relationship type's labels * Get an array of the labels of an items unique relationship types
* The array doesn't contain any duplicate labels * The array doesn't contain any duplicate labels
* @param item * @param item
*/ */
getItemRelationshipLabels(item: Item): Observable<string[]> { getRelationshipTypeLabelsByItem(item: Item): Observable<string[]> {
return this.getItemResolvedRelatedItemsAndTypes(item).pipe( return this.getItemRelationships(item).pipe(
map(([leftItems, rightItems, relTypesCurrentPage]) => { getSucceededRemoteData(),
return relTypesCurrentPage.map((type, index) => { map((relationshipsListRD: RemoteData<PaginatedList<Relationship>>) => relationshipsListRD.payload.page),
if (leftItems[index].uuid === item.uuid) { switchMap((relationships: Relationship[]) => observableCombineLatest(relationships.map((relationship: Relationship) => this.getRelationshipTypeLabelByRelationshipAndItem(relationship, item)))),
return type.leftLabel;
} else {
return type.rightLabel;
}
});
}),
map((labels: string[]) => Array.from(new Set(labels))) map((labels: string[]) => Array.from(new Set(labels)))
) )
} }
private getRelationshipTypeLabelByRelationshipAndItem(relationship: Relationship, item: Item): Observable<string> {
return relationship.leftItem.pipe(
getSucceededRemoteData(),
map((itemRD: RemoteData<Item>) => itemRD.payload),
filter((otherItem: Item) => otherItem.uuid == item.uuid),
switchMap((otherItem: Item) => relationship.relationshipType.pipe(
getSucceededRemoteData(),
map((relationshipTypeRD) => relationshipTypeRD.payload),
map((relationshipType: RelationshipType) => {
if (otherItem.uuid == item.uuid) {
return relationshipType.leftLabel;
} else {
return relationshipType.rightLabel;
}
})
)
))
}
/** /**
* Resolve a given item's relationships into related items and return the items as an array * Resolve a given item's relationships into related items and return the items as an array
* @param item * @param item
*/ */
getRelatedItems(item: Item): Observable<Item[]> { getRelatedItems(item: Item): Observable<Item[]> {
return this.getItemRelationshipsArray(item).pipe( return this.getItemRelationships(item).pipe(
getSucceededRemoteData(),
map((relationshipsListRD: RemoteData<PaginatedList<Relationship>>) => relationshipsListRD.payload.page),
relationsToItems(item.uuid) relationsToItems(item.uuid)
); );
} }
@@ -240,8 +218,9 @@ export class RelationshipService {
* @param label * @param label
*/ */
getRelatedItemsByLabel(item: Item, label: string): Observable<Item[]> { getRelatedItemsByLabel(item: Item, label: string): Observable<Item[]> {
return this.getItemResolvedRelsAndTypes(item).pipe( return this.getItemRelationshipsByType(item, label).pipe(
filterRelationsByTypeLabel(label), getSucceededRemoteData(),
map((relationshipsListRD: RemoteData<PaginatedList<Relationship>>) => relationshipsListRD.payload.page),
relationsToItems(item.uuid) relationsToItems(item.uuid)
); );
} }

View File

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