intermediate commit

This commit is contained in:
Lotte Hofstede
2018-05-17 17:10:38 +02:00
parent f21b3e390e
commit ca8c1830aa
21 changed files with 293 additions and 158 deletions

View File

@@ -4,8 +4,9 @@ import { CacheableObject } from '../cache/object-cache.reducer';
import { PageInfo } from '../shared/page-info.model';
import { ObjectCacheService } from '../cache/object-cache.service';
import { GlobalConfig } from '../../../config/global-config.interface';
import { NormalizedObject } from '../cache/models/normalized-object.model';
import { GenericConstructor } from '../shared/generic-constructor';
import { PaginatedList } from './paginated-list';
import { NormalizedObject } from '../cache/models/normalized-object.model';
function isObjectLevel(halObj: any) {
return isNotEmpty(halObj._links) && hasValue(halObj._links.self);
@@ -17,96 +18,93 @@ function isPaginatedResponse(halObj: any) {
/* tslint:disable:max-classes-per-file */
class ProcessRequestDTO<ObjectDomain> {
[key: string]: ObjectDomain[]
}
export abstract class BaseResponseParsingService {
protected abstract EnvConfig: GlobalConfig;
protected abstract objectCache: ObjectCacheService;
protected abstract objectFactory: any;
protected abstract toCache: boolean;
protected process<ObjectDomain,ObjectType>(data: any, requestHref: string): ProcessRequestDTO<ObjectDomain> {
protected process<ObjectDomain, ObjectType>(data: any, requestHref: string): any {
if (isNotEmpty(data)) {
if (isPaginatedResponse(data)) {
return this.process(data._embedded, requestHref);
if (hasNoValue(data) || (typeof data !== 'object')) {
return data;
} else if (isPaginatedResponse(data)) {
return this.processPaginatedList(data, requestHref);
} else if (Array.isArray(data)) {
return this.processArray(data, requestHref);
} else if (isObjectLevel(data)) {
return { topLevel: this.deserializeAndCache(data, requestHref) };
} else {
const result = new ProcessRequestDTO<ObjectDomain>();
if (Array.isArray(data)) {
result.topLevel = [];
data.forEach((datum) => {
if (isPaginatedResponse(datum)) {
const obj = this.process(datum, requestHref);
result.topLevel = [...result.topLevel, ...this.flattenSingleKeyObject(obj)];
} else {
result.topLevel = [...result.topLevel, ...this.deserializeAndCache<ObjectDomain,ObjectType>(datum, requestHref)];
}
});
} else {
Object.keys(data)
.filter((property) => data.hasOwnProperty(property))
.filter((property) => hasValue(data[property]))
const object = this.deserialize(data, requestHref);
this.cache(object, requestHref);
if (isNotEmpty(data._embedded)) {
const list = {};
Object
.keys(data._embedded)
.filter((property) => data._embedded.hasOwnProperty(property))
.forEach((property) => {
if (isPaginatedResponse(data[property])) {
const obj = this.process(data[property], requestHref);
result[property] = this.flattenSingleKeyObject(obj);
} else {
result[property] = this.deserializeAndCache(data[property], requestHref);
}
console.log(data._embedded[property]);
const parsedObj = this.process<ObjectDomain, ObjectType>(data._embedded[property], requestHref);
list[property] = parsedObj;
});
console.log(list);
Object.assign(object, list);
}
return result;
return object;
}
const result = {};
Object.keys(data)
.filter((property) => data.hasOwnProperty(property))
.filter((property) => hasValue(data[property]))
.forEach((property) => {
const obj = this.process(data[property], requestHref);
result[property] = obj;
});
return result;
}
}
protected deserializeAndCache<ObjectDomain,ObjectType>(obj, requestHref: string): ObjectDomain[] {
if (Array.isArray(obj)) {
let result = [];
obj.forEach((o) => result = [...result, ...this.deserializeAndCache<ObjectDomain,ObjectType>(o, requestHref)]);
return result;
}
protected processPaginatedList<ObjectDomain, ObjectType>(data: any, requestHref: string): PaginatedList<ObjectDomain> {
const pageInfo: PageInfo = this.processPageInfo(data);
const list = this.flattenSingleKeyObject(data._embedded);
const page: ObjectDomain[] = this.processArray(list, requestHref);
return new PaginatedList<ObjectDomain>(pageInfo, page);
}
protected processArray<ObjectDomain, ObjectType>(data: any, requestHref: string): ObjectDomain[] {
let array: ObjectDomain[] = [];
data.forEach((datum) => {
array = [...array, this.process(datum, requestHref)];
}
);
return array;
}
protected deserialize<ObjectDomain, ObjectType>(obj, requestHref) {
const type: ObjectType = obj.type;
if (hasValue(type)) {
const normObjConstructor = this.objectFactory.getConstructor(type) as GenericConstructor<ObjectDomain>;
if (hasValue(normObjConstructor)) {
const serializer = new DSpaceRESTv2Serializer(normObjConstructor);
let processed;
if (isNotEmpty(obj._embedded)) {
processed = this.process<ObjectDomain,ObjectType>(obj._embedded, requestHref);
}
const normalizedObj: any = serializer.deserialize(obj);
if (isNotEmpty(processed)) {
const processedList = {};
Object.keys(processed).forEach((key) => {
processedList[key] = processed[key].map((no: NormalizedObject) => (this.toCache) ? no.self : no);
});
Object.assign(normalizedObj, processedList);
}
if (this.toCache) {
this.addToObjectCache(normalizedObj, requestHref);
}
return [normalizedObj] as any;
const res = serializer.deserialize(obj);
return res;
} else {
// TODO: move check to Validator?
// throw new Error(`The server returned an object with an unknown a known type: ${type}`);
return [];
return null;
}
} else {
// TODO: move check to Validator
// throw new Error(`The server returned an object without a type: ${JSON.stringify(obj)}`);
return [];
return null;
}
}
protected cache<ObjectDomain, ObjectType>(obj, requestHref) {
if (this.toCache) {
this.addToObjectCache(obj, requestHref);
}
}
@@ -119,7 +117,7 @@ export abstract class BaseResponseParsingService {
processPageInfo(payload: any): PageInfo {
if (isNotEmpty(payload.page)) {
const pageObj = Object.assign({}, payload.page, {_links: payload._links});
const pageObj = Object.assign({}, payload.page, { _links: payload._links });
const pageInfoObject = new DSpaceRESTv2Serializer(PageInfo).deserialize(pageObj);
if (pageInfoObject.currentPage >= 0) {
Object.assign(pageInfoObject, { currentPage: pageInfoObject.currentPage + 1 });