refactoring

This commit is contained in:
Art Lowel
2017-05-03 11:05:18 +02:00
parent f8d6e96e44
commit 2a2bb36692
15 changed files with 236 additions and 443 deletions

View File

@@ -0,0 +1,21 @@
import { Bitstream } from "../../shared/bitstream.model";
import { RequestService } from "../../data/request.service";
import { hasValue } from "../../../shared/empty.util";
import { NormalizedBitstream } from "../models/normalized-bitstream.model";
import { RemoteDataBuildService } from "./remote-data-build.service";
import { DomainModelBuilder } from "./domain-model-builder";
export class BitstreamBuilder extends DomainModelBuilder<NormalizedBitstream, Bitstream> {
constructor(
private requestService: RequestService,
private rdbService: RemoteDataBuildService,
) {
super();
}
build(): Bitstream {
let links: any = {};
//TODO
return Object.assign(new Bitstream(), this.normalized, links);
}
}

View File

@@ -0,0 +1,34 @@
import { Bundle } from "../../shared/bundle.model";
import { RequestService } from "../../data/request.service";
import { NormalizedBundle } from "../models/normalized-bundle.model";
import { hasValue } from "../../../shared/empty.util";
import { NormalizedBitstream } from "../models/normalized-bitstream.model";
import { RemoteDataBuildService } from "./remote-data-build.service";
import { DomainModelBuilder } from "./domain-model-builder";
import { BitstreamBuilder } from "./bitstream-builder";
export class BundleBuilder extends DomainModelBuilder<NormalizedBundle, Bundle> {
constructor(
private requestService: RequestService,
private rdbService: RemoteDataBuildService,
) {
super();
}
build(): Bundle {
let links: any = {};
if (hasValue(this.normalized.bitstreams)) {
this.normalized.bitstreams.forEach((href: string) => {
setTimeout(() => {
this.requestService.configure(href, NormalizedBitstream);
},0);
});
links.bitstreams = this.normalized.bitstreams.map((href: string) => {
return this.rdbService.buildSingle(href, NormalizedBitstream, new BitstreamBuilder(this.requestService, this.rdbService));
});
}
return Object.assign(new Bundle(), this.normalized, links);
}
}

View File

@@ -0,0 +1,34 @@
import { Collection } from "../../shared/collection.model";
import { RequestService } from "../../data/request.service";
import { NormalizedCollection } from "../models/normalized-collection.model";
import { hasValue } from "../../../shared/empty.util";
import { NormalizedItem } from "../models/normalized-item.model";
import { RemoteDataBuildService } from "./remote-data-build.service";
import { DomainModelBuilder } from "./domain-model-builder";
import { ItemBuilder } from "./item-builder";
export class CollectionBuilder extends DomainModelBuilder<NormalizedCollection, Collection> {
constructor(
private requestService: RequestService,
private rdbService: RemoteDataBuildService,
) {
super();
}
build(): Collection {
let links: any = {};
if (hasValue(this.normalized.items)) {
this.normalized.items.forEach((href: string) => {
setTimeout(() => {
this.requestService.configure(href, NormalizedItem)
},0);
});
links.items = this.normalized.items.map((href: string) => {
return this.rdbService.buildSingle(href, NormalizedItem, new ItemBuilder(this.requestService, this.rdbService));
});
}
return Object.assign(new Collection(), this.normalized, links);
}
}

View File

@@ -0,0 +1,21 @@
import { CacheableObject } from "../object-cache.reducer";
export abstract class DomainModelBuilder<TNormalized extends CacheableObject, TDomain> {
protected href: string;
protected normalized: TNormalized;
constructor() {
}
setHref(href: string): DomainModelBuilder<TNormalized, TDomain> {
this.href = href;
return this;
}
setNormalized(normalized: TNormalized): DomainModelBuilder<TNormalized, TDomain> {
this.normalized = normalized;
return this;
}
abstract build(): TDomain;
}

View File

@@ -0,0 +1,34 @@
import { Item } from "../../shared/item.model";
import { RequestService } from "../../data/request.service";
import { NormalizedItem } from "../models/normalized-item.model";
import { hasValue } from "../../../shared/empty.util";
import { NormalizedBundle } from "../models/normalized-bundle.model";
import { RemoteDataBuildService } from "./remote-data-build.service";
import { DomainModelBuilder } from "./domain-model-builder";
import { BundleBuilder } from "./bundle-builder";
export class ItemBuilder extends DomainModelBuilder<NormalizedItem, Item> {
constructor(
private requestService: RequestService,
private rdbService: RemoteDataBuildService,
) {
super();
}
build(): Item {
let links: any = {};
if (hasValue(this.normalized.bundles)) {
this.normalized.bundles.forEach((href: string) => {
setTimeout(() => {
this.requestService.configure(href, NormalizedBundle)
},0);
});
links.bundles = this.normalized.bundles.map((href: string) => {
return this.rdbService.buildSingle(href, NormalizedBundle, new BundleBuilder(this.requestService, this.rdbService));
});
}
return Object.assign(new Item(), this.normalized, links);
}
}

View File

@@ -1,38 +1,36 @@
import { RemoteData } from "../../data/remote-data";
import { Observable } from "rxjs/Observable";
import { RequestEntry } from "../../data/request.reducer";
import { ResponseCacheEntry } from "../response-cache.reducer";
import { ErrorResponse, SuccessResponse } from "../response-cache.models";
import { Store } from "@ngrx/store";
import { CoreState } from "../../core.reducers";
import { ResponseCacheService } from "../response-cache.service";
import { Injectable } from "@angular/core";
import { GenericConstructor } from "../../shared/generic-constructor";
import { CacheableObject } from "../object-cache.reducer";
import { ObjectCacheService } from "../object-cache.service";
import { RequestService } from "../../data/request.service";
import { CacheableObject } from "../object-cache.reducer";
import { GenericConstructor } from "../../shared/generic-constructor";
import { ResponseCacheService } from "../response-cache.service";
import { Store } from "@ngrx/store";
import { CoreState } from "../../core.reducers";
import { RequestEntry } from "../../data/request.reducer";
import { hasValue, isNotEmpty } from "../../../shared/empty.util";
import { ResponseCacheEntry } from "../response-cache.reducer";
import { ErrorResponse, SuccessResponse } from "../response-cache.models";
import { Observable } from "rxjs/Observable";
import { RemoteData } from "../../data/remote-data";
import { DomainModelBuilder } from "./domain-model-builder";
export interface RemoteDataBuilder<T> {
build(): RemoteData<T>
}
export abstract class SingleRemoteDataBuilder<TDomain, TNormalized extends CacheableObject> implements RemoteDataBuilder<TDomain> {
@Injectable()
export class RemoteDataBuildService {
constructor(
protected objectCache: ObjectCacheService,
protected responseCache: ResponseCacheService,
protected requestService: RequestService,
protected store: Store<CoreState>,
protected href: string,
protected normalizedType: GenericConstructor<TNormalized>
) {
}
protected abstract normalizedToDomain(normalized: TNormalized): TDomain;
build(): RemoteData<TDomain> {
const requestObs = this.store.select<RequestEntry>('core', 'data', 'request', this.href);
const responseCacheObs = this.responseCache.get(this.href);
buildSingle<TNormalized extends CacheableObject, TDomain>(
href: string,
normalizedType: GenericConstructor<TNormalized>,
builder: DomainModelBuilder<TNormalized, TDomain>
): RemoteData<TDomain> {
const requestObs = this.store.select<RequestEntry>('core', 'data', 'request', href);
const responseCacheObs = this.responseCache.get(href);
const requestPending = requestObs.map((entry: RequestEntry) => hasValue(entry) && entry.requestPending).distinctUntilChanged();
@@ -48,23 +46,27 @@ export abstract class SingleRemoteDataBuilder<TDomain, TNormalized extends Cache
const payload =
Observable.race(
this.objectCache.getBySelfLink<TNormalized>(this.href, this.normalizedType),
this.objectCache.getBySelfLink<TNormalized>(href, normalizedType),
responseCacheObs
.filter((entry: ResponseCacheEntry) => hasValue(entry) && entry.response.isSuccessful)
.map((entry: ResponseCacheEntry) => (<SuccessResponse> entry.response).resourceUUIDs)
.flatMap((resourceUUIDs: Array<string>) => {
if (isNotEmpty(resourceUUIDs)) {
return this.objectCache.get(resourceUUIDs[0], this.normalizedType);
return this.objectCache.get(resourceUUIDs[0], normalizedType);
}
else {
return Observable.of(undefined);
}
})
.distinctUntilChanged()
).map((normalized: TNormalized) => this.normalizedToDomain(normalized));
).map((normalized: TNormalized) => builder
.setHref(href)
.setNormalized(normalized)
.build()
);
return new RemoteData(
this.href,
href,
requestPending,
responsePending,
isSuccessFul,
@@ -73,25 +75,13 @@ export abstract class SingleRemoteDataBuilder<TDomain, TNormalized extends Cache
);
}
}
export abstract class ListRemoteDataBuilder<TDomain, TNormalized extends CacheableObject> implements RemoteDataBuilder<TDomain[]> {
constructor(
protected objectCache: ObjectCacheService,
protected responseCache: ResponseCacheService,
protected requestService: RequestService,
protected store: Store<CoreState>,
protected href: string,
protected normalizedType: GenericConstructor<TNormalized>
) {
}
protected abstract normalizedToDomain(normalized: TNormalized): TDomain;
build(): RemoteData<TDomain[]> {
const requestObs = this.store.select<RequestEntry>('core', 'data', 'request', this.href);
const responseCacheObs = this.responseCache.get(this.href);
buildList<TNormalized extends CacheableObject, TDomain>(
href: string,
normalizedType: GenericConstructor<TNormalized>,
builder: DomainModelBuilder<TNormalized, TDomain>
): RemoteData<TDomain[]> {
const requestObs = this.store.select<RequestEntry>('core', 'data', 'request', href);
const responseCacheObs = this.responseCache.get(href);
const requestPending = requestObs.map((entry: RequestEntry) => hasValue(entry) && entry.requestPending).distinctUntilChanged();
@@ -109,17 +99,20 @@ export abstract class ListRemoteDataBuilder<TDomain, TNormalized extends Cacheab
.filter((entry: ResponseCacheEntry) => hasValue(entry) && entry.response.isSuccessful)
.map((entry: ResponseCacheEntry) => (<SuccessResponse> entry.response).resourceUUIDs)
.flatMap((resourceUUIDs: Array<string>) => {
return this.objectCache.getList(resourceUUIDs, this.normalizedType)
return this.objectCache.getList(resourceUUIDs, normalizedType)
.map((normList: TNormalized[]) => {
return normList.map((normalized: TNormalized) => {
return this.normalizedToDomain(normalized);
return builder
.setHref(href)
.setNormalized(normalized)
.build();
});
});
})
.distinctUntilChanged();
return new RemoteData(
this.href,
href,
requestPending,
responsePending,
isSuccessFul,
@@ -127,5 +120,4 @@ export abstract class ListRemoteDataBuilder<TDomain, TNormalized extends Cacheab
payload
);
}
}

View File

@@ -1,65 +0,0 @@
import { Bitstream } from "../../shared/bitstream.model";
import { ObjectCacheService } from "../object-cache.service";
import { ResponseCacheService } from "../response-cache.service";
import { RequestService } from "../../data/request.service";
import { Store } from "@ngrx/store";
import { CoreState } from "../../core.reducers";
import { NormalizedBitstream } from "./normalized-bitstream.model";
import { ListRemoteDataBuilder, SingleRemoteDataBuilder } from "./remote-data-builder";
import { Request } from "../../data/request.models";
import { hasValue } from "../../../shared/empty.util";
import { RequestConfigureAction, RequestExecuteAction } from "../../data/request.actions";
export class BitstreamBuilder {
constructor(
protected objectCache: ObjectCacheService,
protected responseCache: ResponseCacheService,
protected requestService: RequestService,
protected store: Store<CoreState>,
protected href: string,
protected normalized: NormalizedBitstream
) {
}
build(): Bitstream {
let links: any = {};
//TODO
return Object.assign(new Bitstream(), this.normalized, links);
}
}
export class BitstreamRDBuilder extends SingleRemoteDataBuilder<Bitstream, NormalizedBitstream> {
constructor(
objectCache: ObjectCacheService,
responseCache: ResponseCacheService,
requestService: RequestService,
store: Store<CoreState>,
href: string
) {
super(objectCache, responseCache, requestService, store, href, NormalizedBitstream);
}
protected normalizedToDomain(normalized: NormalizedBitstream): Bitstream {
return new BitstreamBuilder(this.objectCache, this.responseCache, this.requestService, this.store, this.href, normalized).build();
}
}
export class BitstreamListRDBuilder extends ListRemoteDataBuilder<Bitstream, NormalizedBitstream> {
constructor(
objectCache: ObjectCacheService,
responseCache: ResponseCacheService,
requestService: RequestService,
store: Store<CoreState>,
href: string
) {
super(objectCache, responseCache, requestService, store, href, NormalizedBitstream);
}
protected normalizedToDomain(normalized: NormalizedBitstream): Bitstream {
return new BitstreamBuilder(this.objectCache, this.responseCache, this.requestService, this.store, this.href, normalized).build();
}
}

View File

@@ -1,93 +0,0 @@
import { Bundle } from "../../shared/bundle.model";
import { ObjectCacheService } from "../object-cache.service";
import { ResponseCacheService } from "../response-cache.service";
import { RequestService } from "../../data/request.service";
import { Store } from "@ngrx/store";
import { CoreState } from "../../core.reducers";
import { NormalizedBundle } from "./normalized-bundle.model";
import { ListRemoteDataBuilder, SingleRemoteDataBuilder } from "./remote-data-builder";
import { Request } from "../../data/request.models";
import { hasValue } from "../../../shared/empty.util";
import { RequestConfigureAction, RequestExecuteAction } from "../../data/request.actions";
import { BitstreamRDBuilder } from "./bitstream-builder";
import { NormalizedBitstream } from "./normalized-bitstream.model";
export class BundleBuilder {
constructor(
protected objectCache: ObjectCacheService,
protected responseCache: ResponseCacheService,
protected requestService: RequestService,
protected store: Store<CoreState>,
protected href: string,
protected normalized: NormalizedBundle
) {
}
build(): Bundle {
let links: any = {};
if (hasValue(this.normalized.bitstreams)) {
//for some reason the dispatches in the forEach don't
//fire without this timeout. A zone issue?
setTimeout(() => {
this.normalized.bitstreams.forEach((href: string) => {
const isCached = this.objectCache.hasBySelfLink(href);
const isPending = this.requestService.isPending(href);
if (!(isCached || isPending)) {
const request = new Request(href, NormalizedBitstream);
this.store.dispatch(new RequestConfigureAction(request));
this.store.dispatch(new RequestExecuteAction(href));
}
});
}, 0);
links.bitstreams = this.normalized.bitstreams.map((href: string) => {
return new BitstreamRDBuilder(
this.objectCache,
this.responseCache,
this.requestService,
this.store,
href
).build();
});
}
return Object.assign(new Bundle(), this.normalized, links);
}
}
export class BundleRDBuilder extends SingleRemoteDataBuilder<Bundle, NormalizedBundle> {
constructor(
objectCache: ObjectCacheService,
responseCache: ResponseCacheService,
requestService: RequestService,
store: Store<CoreState>,
href: string
) {
super(objectCache, responseCache, requestService, store, href, NormalizedBundle);
}
protected normalizedToDomain(normalized: NormalizedBundle): Bundle {
return new BundleBuilder(this.objectCache, this.responseCache, this.requestService, this.store, this.href, normalized).build();
}
}
export class BundleListRDBuilder extends ListRemoteDataBuilder<Bundle, NormalizedBundle> {
constructor(
objectCache: ObjectCacheService,
responseCache: ResponseCacheService,
requestService: RequestService,
store: Store<CoreState>,
href: string
) {
super(objectCache, responseCache, requestService, store, href, NormalizedBundle);
}
protected normalizedToDomain(normalized: NormalizedBundle): Bundle {
return new BundleBuilder(this.objectCache, this.responseCache, this.requestService, this.store, this.href, normalized).build();
}
}

View File

@@ -1,92 +0,0 @@
import { Collection } from "../../shared/collection.model";
import { hasValue } from "../../../shared/empty.util";
import { Item } from "../../shared/item.model";
import { RequestConfigureAction, RequestExecuteAction } from "../../data/request.actions";
import { ObjectCacheService } from "../object-cache.service";
import { ResponseCacheService } from "../response-cache.service";
import { RequestService } from "../../data/request.service";
import { Store } from "@ngrx/store";
import { CoreState } from "../../core.reducers";
import { NormalizedCollection } from "./normalized-collection.model";
import { Request } from "../../data/request.models";
import { ListRemoteDataBuilder, SingleRemoteDataBuilder } from "./remote-data-builder";
import { ItemRDBuilder } from "./item-builder";
import { NormalizedItem } from "./normalized-item.model";
export class CollectionBuilder {
constructor(
protected objectCache: ObjectCacheService,
protected responseCache: ResponseCacheService,
protected requestService: RequestService,
protected store: Store<CoreState>,
protected href: string,
protected normalized: NormalizedCollection
) {
}
build(): Collection {
let links: any = {};
if (hasValue(this.normalized.items)) {
this.normalized.items.forEach((href: string) => {
const isCached = this.objectCache.hasBySelfLink(href);
const isPending = this.requestService.isPending(href);
if (!(isCached || isPending)) {
const request = new Request(href, NormalizedItem);
this.store.dispatch(new RequestConfigureAction(request));
this.store.dispatch(new RequestExecuteAction(href));
}
});
links.items = this.normalized.items.map((href: string) => {
return new ItemRDBuilder(
this.objectCache,
this.responseCache,
this.requestService,
this.store,
href
).build();
});
}
return Object.assign(new Collection(), this.normalized, links);
}
}
export class CollectionRDBuilder extends SingleRemoteDataBuilder<Collection, NormalizedCollection> {
constructor(
objectCache: ObjectCacheService,
responseCache: ResponseCacheService,
requestService: RequestService,
store: Store<CoreState>,
href: string
) {
super(objectCache, responseCache, requestService, store, href, NormalizedCollection);
}
protected normalizedToDomain(normalized: NormalizedCollection): Collection {
return new CollectionBuilder(this.objectCache, this.responseCache, this.requestService, this.store, this.href, normalized).build();
}
}
export class CollectionListRDBuilder extends ListRemoteDataBuilder<Collection, NormalizedCollection> {
constructor(
objectCache: ObjectCacheService,
responseCache: ResponseCacheService,
requestService: RequestService,
store: Store<CoreState>,
href: string
) {
super(objectCache, responseCache, requestService, store, href, NormalizedCollection);
}
protected normalizedToDomain(normalized: NormalizedCollection): Collection {
return new CollectionBuilder(this.objectCache, this.responseCache, this.requestService, this.store, this.href, normalized).build();
}
}

View File

@@ -1,89 +0,0 @@
import { Item } from "../../shared/item.model";
import { ObjectCacheService } from "../object-cache.service";
import { ResponseCacheService } from "../response-cache.service";
import { RequestService } from "../../data/request.service";
import { Store } from "@ngrx/store";
import { CoreState } from "../../core.reducers";
import { NormalizedItem } from "./normalized-item.model";
import { ListRemoteDataBuilder, SingleRemoteDataBuilder } from "./remote-data-builder";
import { Request } from "../../data/request.models";
import { hasValue } from "../../../shared/empty.util";
import { RequestConfigureAction, RequestExecuteAction } from "../../data/request.actions";
import { BundleRDBuilder } from "./bundle-builder";
import { NormalizedBundle } from "./normalized-bundle.model";
export class ItemBuilder {
constructor(
protected objectCache: ObjectCacheService,
protected responseCache: ResponseCacheService,
protected requestService: RequestService,
protected store: Store<CoreState>,
protected href: string,
protected normalized: NormalizedItem
) {
}
build(): Item {
let links: any = {};
if (hasValue(this.normalized.bundles)) {
this.normalized.bundles.forEach((href: string) => {
const isCached = this.objectCache.hasBySelfLink(href);
const isPending = this.requestService.isPending(href);
if (!(isCached || isPending)) {
const request = new Request(href, NormalizedBundle);
this.store.dispatch(new RequestConfigureAction(request));
this.store.dispatch(new RequestExecuteAction(href));
}
});
links.bundles = this.normalized.bundles.map((href: string) => {
return new BundleRDBuilder(
this.objectCache,
this.responseCache,
this.requestService,
this.store,
href
).build();
});
}
return Object.assign(new Item(), this.normalized, links);
}
}
export class ItemRDBuilder extends SingleRemoteDataBuilder<Item, NormalizedItem> {
constructor(
objectCache: ObjectCacheService,
responseCache: ResponseCacheService,
requestService: RequestService,
store: Store<CoreState>,
href: string
) {
super(objectCache, responseCache, requestService, store, href, NormalizedItem);
}
protected normalizedToDomain(normalized: NormalizedItem): Item {
return new ItemBuilder(this.objectCache, this.responseCache, this.requestService, this.store, this.href, normalized).build();
}
}
export class ItemListRDBuilder extends ListRemoteDataBuilder<Item, NormalizedItem> {
constructor(
objectCache: ObjectCacheService,
responseCache: ResponseCacheService,
requestService: RequestService,
store: Store<CoreState>,
href: string
) {
super(objectCache, responseCache, requestService, store, href, NormalizedItem);
}
protected normalizedToDomain(normalized: NormalizedItem): Item {
return new ItemBuilder(this.objectCache, this.responseCache, this.requestService, this.store, this.href, normalized).build();
}
}

View File

@@ -9,6 +9,7 @@ import { ResponseCacheService } from "./cache/response-cache.service";
import { CollectionDataService } from "./data/collection-data.service";
import { ItemDataService } from "./data/item-data.service";
import { RequestService } from "./data/request.service";
import { RemoteDataBuildService } from "./cache/builders/remote-data-build.service";
const IMPORTS = [
CommonModule,
@@ -29,7 +30,8 @@ const PROVIDERS = [
DSpaceRESTv2Service,
ObjectCacheService,
ResponseCacheService,
RequestService
RequestService,
RemoteDataBuildService
];
@NgModule({

View File

@@ -7,39 +7,25 @@ import { Store } from "@ngrx/store";
import { NormalizedCollection } from "../cache/models/normalized-collection.model";
import { CoreState } from "../core.reducers";
import { RequestService } from "./request.service";
import { CollectionListRDBuilder, CollectionRDBuilder } from "../cache/models/collection-builder";
import { RemoteDataBuildService } from "../cache/builders/remote-data-build.service";
import { DomainModelBuilder } from "../cache/builders/domain-model-builder";
import { CollectionBuilder } from "../cache/builders/collection-builder";
@Injectable()
export class CollectionDataService extends DataService<Collection, NormalizedCollection> {
export class CollectionDataService extends DataService<NormalizedCollection, Collection> {
protected endpoint = '/collections';
constructor(
protected objectCache: ObjectCacheService,
protected responseCache: ResponseCacheService,
protected requestService: RequestService,
protected rdbService: RemoteDataBuildService,
protected store: Store<CoreState>
) {
super(NormalizedCollection);
}
protected getListDataBuilder(href: string): CollectionListRDBuilder {
return new CollectionListRDBuilder (
this.objectCache,
this.responseCache,
this.requestService,
this.store,
href,
);
protected getDomainModelBuilder(): CollectionBuilder {
return new CollectionBuilder(this.requestService, this.rdbService);
}
protected getSingleDataBuilder(href: string): CollectionRDBuilder {
return new CollectionRDBuilder (
this.objectCache,
this.responseCache,
this.requestService,
this.store,
href,
);
}
}

View File

@@ -8,22 +8,23 @@ import { FindAllRequest, FindByIDRequest, Request } from "./request.models";
import { Store } from "@ngrx/store";
import { RequestConfigureAction, RequestExecuteAction } from "./request.actions";
import { CoreState } from "../core.reducers";
import { RemoteDataBuilder } from "../cache/models/remote-data-builder";
import { DomainModelBuilder } from "../cache/builders/domain-model-builder";
import { RequestService } from "./request.service";
import { RemoteDataBuildService } from "../cache/builders/remote-data-build.service";
export abstract class DataService<T, U extends CacheableObject> {
export abstract class DataService<TNormalized extends CacheableObject, TDomain> {
protected abstract objectCache: ObjectCacheService;
protected abstract responseCache: ResponseCacheService;
protected abstract requestService: RequestService;
protected abstract rdbService: RemoteDataBuildService;
protected abstract store: Store<CoreState>;
protected abstract endpoint: string;
constructor(private normalizedResourceType: GenericConstructor<U>) {
constructor(private normalizedResourceType: GenericConstructor<TNormalized>) {
}
protected abstract getListDataBuilder(href: string): RemoteDataBuilder<T[]>;
protected abstract getSingleDataBuilder(href: string): RemoteDataBuilder<T>;
protected abstract getDomainModelBuilder(): DomainModelBuilder<TNormalized, TDomain>;
protected getFindAllHref(scopeID?): string {
let result = this.endpoint;
@@ -33,37 +34,37 @@ export abstract class DataService<T, U extends CacheableObject> {
return result;
}
findAll(scopeID?: string): RemoteData<Array<T>> {
findAll(scopeID?: string): RemoteData<Array<TDomain>> {
const href = this.getFindAllHref(scopeID);
if (!this.responseCache.has(href) && !this.requestService.isPending(href)) {
const request = new FindAllRequest(href, this.normalizedResourceType, scopeID);
this.store.dispatch(new RequestConfigureAction(request));
this.store.dispatch(new RequestExecuteAction(href));
}
return this.getListDataBuilder(href).build();
return this.rdbService.buildList(href, this.normalizedResourceType, this.getDomainModelBuilder())
}
protected getFindByIDHref(resourceID): string {
return `${this.endpoint}/${resourceID}`;
}
findById(id: string): RemoteData<T> {
findById(id: string): RemoteData<TDomain> {
const href = this.getFindByIDHref(id);
if (!this.objectCache.hasBySelfLink(href) && !this.requestService.isPending(href)) {
const request = new FindByIDRequest(href, this.normalizedResourceType, id);
this.store.dispatch(new RequestConfigureAction(request));
this.store.dispatch(new RequestExecuteAction(href));
}
return this.getSingleDataBuilder(href).build();
return this.rdbService.buildSingle(href, this.normalizedResourceType, this.getDomainModelBuilder())
}
findByHref(href: string): RemoteData<T> {
findByHref(href: string): RemoteData<TDomain> {
if (!this.objectCache.hasBySelfLink(href) && !this.requestService.isPending(href)) {
const request = new Request(href, this.normalizedResourceType);
this.store.dispatch(new RequestConfigureAction(request));
this.store.dispatch(new RequestExecuteAction(href));
}
return this.getSingleDataBuilder(href).build();
return this.rdbService.buildSingle(href, this.normalizedResourceType, this.getDomainModelBuilder())
}
}

View File

@@ -7,38 +7,24 @@ import { Store } from "@ngrx/store";
import { CoreState } from "../core.reducers";
import { NormalizedItem } from "../cache/models/normalized-item.model";
import { RequestService } from "./request.service";
import { ItemListRDBuilder, ItemRDBuilder } from "../cache/models/item-builder";
import { RemoteDataBuildService } from "../cache/builders/remote-data-build.service";
import { ItemBuilder } from "../cache/builders/item-builder";
@Injectable()
export class ItemDataService extends DataService<Item, NormalizedItem> {
export class ItemDataService extends DataService<NormalizedItem, Item> {
protected endpoint = '/items';
constructor(
protected objectCache: ObjectCacheService,
protected responseCache: ResponseCacheService,
protected requestService: RequestService,
protected rdbService: RemoteDataBuildService,
protected store: Store<CoreState>
) {
super(NormalizedItem);
}
protected getListDataBuilder(href: string): ItemListRDBuilder {
return new ItemListRDBuilder(
this.objectCache,
this.responseCache,
this.requestService,
this.store,
href,
);
}
protected getSingleDataBuilder(href: string): ItemRDBuilder {
return new ItemRDBuilder(
this.objectCache,
this.responseCache,
this.requestService,
this.store,
href,
);
protected getDomainModelBuilder(): ItemBuilder {
return new ItemBuilder(this.requestService, this.rdbService);
}
}

View File

@@ -1,13 +1,23 @@
import { Injectable } from "@angular/core";
import { RequestEntry, RequestState } from "./request.reducer";
import { Store } from "@ngrx/store";
import { Request } from "./request.models";
import { hasValue } from "../../shared/empty.util";
import { Observable } from "rxjs/Observable";
import { RequestConfigureAction, RequestExecuteAction } from "./request.actions";
import { GenericConstructor } from "../shared/generic-constructor";
import { ResponseCacheService } from "../cache/response-cache.service";
import { ObjectCacheService } from "../cache/object-cache.service";
import { CacheableObject } from "../cache/object-cache.reducer";
@Injectable()
export class RequestService {
constructor(private store: Store<RequestState>) {
constructor(
private objectCache: ObjectCacheService,
private responseCache: ResponseCacheService,
private store: Store<RequestState>
) {
}
isPending(href: string): boolean {
@@ -24,4 +34,15 @@ export class RequestService {
get(href: string): Observable<RequestEntry> {
return this.store.select<RequestEntry>('core', 'data', 'request', href);
}
configure<T extends CacheableObject>(href: string, normalizedType: GenericConstructor<T>): void {
const isCached = this.objectCache.hasBySelfLink(href);
const isPending = this.isPending(href);
if (!(isCached || isPending)) {
const request = new Request(href, normalizedType);
this.store.dispatch(new RequestConfigureAction(request));
this.store.dispatch(new RequestExecuteAction(href));
}
}
}