From f8d6e96e44b656d5e496590ac1ade44bab55a841 Mon Sep 17 00:00:00 2001 From: Art Lowel Date: Mon, 24 Apr 2017 13:43:54 +0200 Subject: [PATCH] added builders for bundles and bitstreams --- .../core/cache/models/bitstream-builder.ts | 65 +++++++++++++ src/app/core/cache/models/bundle-builder.ts | 93 +++++++++++++++++++ .../core/cache/models/collection-builder.ts | 15 ++- src/app/core/cache/models/item-builder.ts | 34 ++++++- .../cache/models/normalized-bundle.model.ts | 5 +- .../cache/models/normalized-item.model.ts | 2 + src/app/core/shared/bundle.model.ts | 5 +- src/app/core/shared/item.model.ts | 4 + src/backend/bitstreams.ts | 4 +- src/backend/bundles.ts | 8 +- src/backend/items.ts | 8 +- src/platform/modules/node.module.ts | 2 - 12 files changed, 220 insertions(+), 25 deletions(-) create mode 100644 src/app/core/cache/models/bitstream-builder.ts create mode 100644 src/app/core/cache/models/bundle-builder.ts diff --git a/src/app/core/cache/models/bitstream-builder.ts b/src/app/core/cache/models/bitstream-builder.ts new file mode 100644 index 0000000000..2cb3f30eb9 --- /dev/null +++ b/src/app/core/cache/models/bitstream-builder.ts @@ -0,0 +1,65 @@ +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, + 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 { + + constructor( + objectCache: ObjectCacheService, + responseCache: ResponseCacheService, + requestService: RequestService, + store: Store, + 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 { + constructor( + objectCache: ObjectCacheService, + responseCache: ResponseCacheService, + requestService: RequestService, + store: Store, + 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(); + } + +} diff --git a/src/app/core/cache/models/bundle-builder.ts b/src/app/core/cache/models/bundle-builder.ts new file mode 100644 index 0000000000..b360c28f58 --- /dev/null +++ b/src/app/core/cache/models/bundle-builder.ts @@ -0,0 +1,93 @@ +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, + 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 { + + constructor( + objectCache: ObjectCacheService, + responseCache: ResponseCacheService, + requestService: RequestService, + store: Store, + 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 { + constructor( + objectCache: ObjectCacheService, + responseCache: ResponseCacheService, + requestService: RequestService, + store: Store, + 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(); + } + +} diff --git a/src/app/core/cache/models/collection-builder.ts b/src/app/core/cache/models/collection-builder.ts index 8dbb960c19..3291d10518 100644 --- a/src/app/core/cache/models/collection-builder.ts +++ b/src/app/core/cache/models/collection-builder.ts @@ -11,6 +11,7 @@ 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 { @@ -20,28 +21,26 @@ export class CollectionBuilder { protected requestService: RequestService, protected store: Store, protected href: string, - protected nc: NormalizedCollection + protected normalized: NormalizedCollection ) { } build(): Collection { let links: any = {}; - if (hasValue(this.nc.items)) { - this.nc.items.forEach((href: string) => { + if (hasValue(this.normalized.items)) { + this.normalized.items.forEach((href: string) => { const isCached = this.objectCache.hasBySelfLink(href); const isPending = this.requestService.isPending(href); - console.log('href', href, 'isCached', isCached, "isPending", isPending); - if (!(isCached || isPending)) { - const request = new Request(href, Item); + const request = new Request(href, NormalizedItem); this.store.dispatch(new RequestConfigureAction(request)); this.store.dispatch(new RequestExecuteAction(href)); } }); - links.items = this.nc.items.map((href: string) => { + links.items = this.normalized.items.map((href: string) => { return new ItemRDBuilder( this.objectCache, this.responseCache, @@ -52,7 +51,7 @@ export class CollectionBuilder { }); } - return Object.assign(new Collection(), this.nc, links); + return Object.assign(new Collection(), this.normalized, links); } } diff --git a/src/app/core/cache/models/item-builder.ts b/src/app/core/cache/models/item-builder.ts index b812ef0430..6935741e38 100644 --- a/src/app/core/cache/models/item-builder.ts +++ b/src/app/core/cache/models/item-builder.ts @@ -6,6 +6,11 @@ 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 { @@ -15,13 +20,36 @@ export class ItemBuilder { protected requestService: RequestService, protected store: Store, protected href: string, - protected nc: NormalizedItem + protected normalized: NormalizedItem ) { } build(): Item { - //TODO - return Object.assign(new Item(), this.nc); + 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); } } diff --git a/src/app/core/cache/models/normalized-bundle.model.ts b/src/app/core/cache/models/normalized-bundle.model.ts index 73c31990d0..9eed7cf033 100644 --- a/src/app/core/cache/models/normalized-bundle.model.ts +++ b/src/app/core/cache/models/normalized-bundle.model.ts @@ -1,4 +1,4 @@ -import { inheritSerialization } from "cerialize"; +import { autoserialize, inheritSerialization } from "cerialize"; import { NormalizedDSpaceObject } from "./normalized-dspace-object.model"; @inheritSerialization(NormalizedDSpaceObject) @@ -6,6 +6,7 @@ export class NormalizedBundle extends NormalizedDSpaceObject { /** * The primary bitstream of this Bundle */ + @autoserialize primaryBitstream: string; /** @@ -18,4 +19,6 @@ export class NormalizedBundle extends NormalizedDSpaceObject { */ owner: string; + @autoserialize + bitstreams: Array; } diff --git a/src/app/core/cache/models/normalized-item.model.ts b/src/app/core/cache/models/normalized-item.model.ts index 69fb83afcc..53f44e2eb3 100644 --- a/src/app/core/cache/models/normalized-item.model.ts +++ b/src/app/core/cache/models/normalized-item.model.ts @@ -35,4 +35,6 @@ export class NormalizedItem extends NormalizedDSpaceObject { */ owner: string; + @autoserialize + bundles: Array; } diff --git a/src/app/core/shared/bundle.model.ts b/src/app/core/shared/bundle.model.ts index b990c8617e..f6fd8f3820 100644 --- a/src/app/core/shared/bundle.model.ts +++ b/src/app/core/shared/bundle.model.ts @@ -2,13 +2,14 @@ import { inheritSerialization } from "cerialize"; import { DSpaceObject } from "./dspace-object.model"; import { Bitstream } from "./bitstream.model"; import { Item } from "./item.model"; +import { RemoteData } from "../data/remote-data"; @inheritSerialization(DSpaceObject) export class Bundle extends DSpaceObject { /** * The primary bitstream of this Bundle */ - primaryBitstream: Bitstream; + primaryBitstream: RemoteData; /** * An array of Items that are direct parents of this Bundle @@ -20,4 +21,6 @@ export class Bundle extends DSpaceObject { */ owner: Item; + bitstreams: Array> + } diff --git a/src/app/core/shared/item.model.ts b/src/app/core/shared/item.model.ts index 478d94f814..fe77068628 100644 --- a/src/app/core/shared/item.model.ts +++ b/src/app/core/shared/item.model.ts @@ -1,6 +1,8 @@ import { inheritSerialization, autoserialize } from "cerialize"; import { DSpaceObject } from "./dspace-object.model"; import { Collection } from "./collection.model"; +import { RemoteData } from "../data/remote-data"; +import { Bundle } from "./bundle.model"; @inheritSerialization(DSpaceObject) export class Item extends DSpaceObject { @@ -36,4 +38,6 @@ export class Item extends DSpaceObject { */ owner: Collection; + bundles: Array> + } diff --git a/src/backend/bitstreams.ts b/src/backend/bitstreams.ts index 480a0b4b55..ed47d0d94a 100644 --- a/src/backend/bitstreams.ts +++ b/src/backend/bitstreams.ts @@ -1,7 +1,7 @@ export const BITSTREAMS = [ { "_links": { - "self": { "href": "/bitstreams/43c57c2b-206f-4645-8c8f-5f10c84b09fa" }, + "self": { "href": "/bitstreams/3678" }, "bundle": { "href": "/bundles/35e0606d-5e18-4f9c-aa61-74fc751cc3f9" }, "retrieve": { "href": "/bitstreams/43c57c2b-206f-4645-8c8f-5f10c84b09fa/retrieve" } }, @@ -22,7 +22,7 @@ export const BITSTREAMS = [ }, { "_links": { - "self": { "href": "/bitstreams/1a013ecc-fb25-4689-a44f-f1383ad26632" }, + "self": { "href": "/bitstreams/8842" }, "bundle": { "href": "/bundles/a469c57a-abcf-45c3-83e4-b187ebd708fd" }, "retrieve": { "href": "/rest/bitstreams/1a013ecc-fb25-4689-a44f-f1383ad26632/retrieve" } }, diff --git a/src/backend/bundles.ts b/src/backend/bundles.ts index 01e8f07002..fde6409635 100644 --- a/src/backend/bundles.ts +++ b/src/backend/bundles.ts @@ -1,12 +1,12 @@ export const BUNDLES = [ { "_links": { - "self": { "href": "/bundles/35e0606d-5e18-4f9c-aa61-74fc751cc3f9" }, + "self": { "href": "/bundles/2355" }, "items": [ { "href": "/items/8871" } ], "bitstreams": [ - { "href": "/bitstreams/43c57c2b-206f-4645-8c8f-5f10c84b09fa" }, + { "href": "/bitstreams/3678" }, ], "primaryBitstream": { "href": "/bitstreams/43c57c2b-206f-4645-8c8f-5f10c84b09fa" } }, @@ -19,12 +19,12 @@ export const BUNDLES = [ }, { "_links": { - "self": { "href": "/bundles/a469c57a-abcf-45c3-83e4-b187ebd708fd" }, + "self": { "href": "/bundles/5687" }, "items": [ { "href": "/items/8871" } ], "bitstreams": [ - { "href": "/bitstreams/1a013ecc-fb25-4689-a44f-f1383ad26632" }, + { "href": "/bitstreams/8842" }, ], "primaryBitstream": { "href": "/bitstreams/1a013ecc-fb25-4689-a44f-f1383ad26632" } }, diff --git a/src/backend/items.ts b/src/backend/items.ts index 290e2b96aa..5578691215 100644 --- a/src/backend/items.ts +++ b/src/backend/items.ts @@ -14,10 +14,10 @@ export const ITEMS = [ ], "bundles": [ { - "href": "/bundles/35e0606d-5e18-4f9c-aa61-74fc751cc3f9" + "href": "/bundles/2355" }, { - "href": "/bundles/a469c57a-abcf-45c3-83e4-b187ebd708fd" + "href": "/bundles/5687" } ] }, @@ -106,10 +106,10 @@ export const ITEMS = [ ], "bundles": [ { - "href": "/bundles/b0176baa-d52e-4c20-a8e6-d586f2c70c76" + "href": "/bundles/2355" }, { - "href": "/bundles/40b1cd3f-07ad-4ca6-9716-132671f93a15" + "href": "/bundles/5687" } ] }, diff --git a/src/platform/modules/node.module.ts b/src/platform/modules/node.module.ts index 7148855d4f..f8772ad3a3 100755 --- a/src/platform/modules/node.module.ts +++ b/src/platform/modules/node.module.ts @@ -13,7 +13,6 @@ import { CoreModule } from "../../app/core/core.module"; import { StoreModule, Store } from "@ngrx/store"; import { RouterStoreModule } from "@ngrx/router-store"; -import { StoreDevtoolsModule } from "@ngrx/store-devtools"; import { rootReducer, AppState, NGRX_CACHE_KEY } from '../../app/app.reducers'; import { effects } from '../../app/app.effects'; @@ -59,7 +58,6 @@ export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE'; AppModule, StoreModule.provideStore(rootReducer), RouterStoreModule.connectRouter(), - StoreDevtoolsModule.instrumentOnlyWithExtension(), effects ], providers: [