Skip to content

Commit

Permalink
chore(models): expand resource conversion (DSP-1795) (#354)
Browse files Browse the repository at this point in the history
* chore(models): expand resource conversion (DSP-1795)

* refactor(models): format resource conversion util
  • Loading branch information
kilchenmann authored Jul 15, 2021
1 parent bc57089 commit f5bbe8f
Showing 1 changed file with 188 additions and 156 deletions.
344 changes: 188 additions & 156 deletions src/models/v2/resources/ResourcesConversionUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import { ReadBooleanValue } from "./values/read/read-boolean-value";
import { ReadColorValue } from "./values/read/read-color-value";
import { ParseReadDateValue, ReadDateValue } from "./values/read/read-date-value";
import { ReadDecimalValue } from "./values/read/read-decimal-value";
import { ReadStillImageFileValue } from "./values/read/read-file-value";
import {
ReadAudioFileValue,
ReadDocumentFileValue,
ReadMovingImageFileValue,
ReadStillImageFileValue
} from "./values/read/read-file-value";
import { ParseReadGeomValue, ReadGeomValue } from "./values/read/read-geom-value";
import { ReadGeonameValue } from "./values/read/read-geoname-value";
import { ReadIntValue } from "./values/read/read-int-value";
Expand Down Expand Up @@ -60,7 +65,7 @@ export namespace ResourcesConversionUtil {
return new ReadResourceSequence(resources);
}
})
);
);
} else {
// one or no resource
if (Object.keys(resourcesJsonld).length === 0) {
Expand Down Expand Up @@ -265,184 +270,211 @@ export namespace ResourcesConversionUtil {
*/
const createValueValue = (propIri: string, valueJsonld: any, entitiyDefs: ResourceClassAndPropertyDefinitions, ontologyCache: OntologyCache, listNodeCache: ListNodeV2Cache, jsonConvert: JsonConvert): Observable<ReadValue> => {

if (Array.isArray(valueJsonld)) throw new Error("value is expected to be a single object");
if (Array.isArray(valueJsonld)) throw new Error("value is expected to be a single object");

const type = valueJsonld["@type"];
const type = valueJsonld["@type"];

let value: Observable<ReadValue>;
let value: Observable<ReadValue>;

switch (type) {
switch (type) {

case Constants.BooleanValue: {
const boolVal = handleSimpleValue(valueJsonld, ReadBooleanValue, jsonConvert);
value = boolVal.pipe(map((val: ReadBooleanValue) => {
val.strval = val.bool ? "TRUE" : "FALSE";
return val;
}));
break;
}

case Constants.ColorValue: {
const colorVal = handleSimpleValue(valueJsonld, ReadColorValue, jsonConvert);
value = colorVal.pipe(map((val: ReadColorValue) => {
val.strval = val.color;
return val;
}));
break;
}

case Constants.DateValue: {
const dateVal = handleSimpleValue(valueJsonld, ParseReadDateValue, jsonConvert);
value = dateVal.pipe(map(
(val: ParseReadDateValue) => {
return new ReadDateValue(val);
}
));
break;
}
case Constants.BooleanValue: {
const boolVal = handleSimpleValue(valueJsonld, ReadBooleanValue, jsonConvert);
value = boolVal.pipe(map((val: ReadBooleanValue) => {
val.strval = val.bool ? "TRUE" : "FALSE";
return val;
}));
break;
}

case Constants.IntValue: {
const intVal = handleSimpleValue(valueJsonld, ReadIntValue, jsonConvert);
value = intVal.pipe(map((val: ReadIntValue) => {
val.strval = val.int.toString();
return val;
}));
break;
}
case Constants.ColorValue: {
const colorVal = handleSimpleValue(valueJsonld, ReadColorValue, jsonConvert);
value = colorVal.pipe(map((val: ReadColorValue) => {
val.strval = val.color;
return val;
}));
break;
}

case Constants.DecimalValue: {
const decimalVal = handleSimpleValue(valueJsonld, ReadDecimalValue, jsonConvert);
value = decimalVal.pipe(map((val: ReadDecimalValue) => {
val.strval = val.decimal.toString();
return val;
}));
break;
}
case Constants.DateValue: {
const dateVal = handleSimpleValue(valueJsonld, ParseReadDateValue, jsonConvert);
value = dateVal.pipe(map(
(val: ParseReadDateValue) => {
return new ReadDateValue(val);
}
));
break;
}

case Constants.IntervalValue: {
const intervalVal = handleSimpleValue(valueJsonld, ReadIntervalValue, jsonConvert);
value = intervalVal.pipe(map((val: ReadIntervalValue) => {
val.strval = val.start.toString() + " - " + val.end.toString();
return val;
}));
break;
}
case Constants.IntValue: {
const intVal = handleSimpleValue(valueJsonld, ReadIntValue, jsonConvert);
value = intVal.pipe(map((val: ReadIntValue) => {
val.strval = val.int.toString();
return val;
}));
break;
}

case Constants.ListValue: {
const listValue = value = handleSimpleValue(valueJsonld, ReadListValue, jsonConvert);
value = listValue.pipe(
mergeMap(
(listVal: ReadListValue) => {
// get referred list node's label
return listNodeCache.getNode(listVal.listNode).pipe(
map(
listNode => {
listVal.listNodeLabel = listNode.label;
listVal.strval = listNode.label;
return listVal;
})
);
}
)
);
break;
}
case Constants.DecimalValue: {
const decimalVal = handleSimpleValue(valueJsonld, ReadDecimalValue, jsonConvert);
value = decimalVal.pipe(map((val: ReadDecimalValue) => {
val.strval = val.decimal.toString();
return val;
}));
break;
}

case Constants.UriValue: {
const uriVal = handleSimpleValue(valueJsonld, ReadUriValue, jsonConvert);
value = uriVal.pipe(map((val: ReadUriValue) => {
val.strval = val.uri;
return val;
}));
break;
}
case Constants.IntervalValue: {
const intervalVal = handleSimpleValue(valueJsonld, ReadIntervalValue, jsonConvert);
value = intervalVal.pipe(map((val: ReadIntervalValue) => {
val.strval = val.start.toString() + " - " + val.end.toString();
return val;
}));
break;
}

case Constants.TextValue: {
const textVal = handleTextValue(valueJsonld, jsonConvert);
value = textVal.pipe(map((val: ReadTextValue) => {
if (val instanceof ReadTextValueAsString) {
val.strval = val.text;
} else if (val instanceof ReadTextValueAsXml) {
val.strval = val.xml;
} else if (val instanceof ReadTextValueAsHtml) {
val.strval = val.html;
} else {
console.error("String representation for a ReadTextValue could not be constructed for: ", type);
case Constants.ListValue: {
const listValue = value = handleSimpleValue(valueJsonld, ReadListValue, jsonConvert);
value = listValue.pipe(
mergeMap(
(listVal: ReadListValue) => {
// get referred list node's label
return listNodeCache.getNode(listVal.listNode).pipe(
map(
listNode => {
listVal.listNodeLabel = listNode.label;
listVal.strval = listNode.label;
return listVal;
})
);
}
return val;
}));
break;
}
)
);
break;
}

case Constants.LinkValue: {
const linkVal = handleLinkValue(valueJsonld, ontologyCache, listNodeCache, jsonConvert);
value = linkVal.pipe(map((val: ReadLinkValue) => {
if (val.linkedResource !== undefined) {
val.strval = val.linkedResource.label;
} else {
val.strval = val.linkedResourceIri;
}
return val;
}));
break;
}
case Constants.UriValue: {
const uriVal = handleSimpleValue(valueJsonld, ReadUriValue, jsonConvert);
value = uriVal.pipe(map((val: ReadUriValue) => {
val.strval = val.uri;
return val;
}));
break;
}

case Constants.GeomValue: {
const geomVal = handleSimpleValue(valueJsonld, ParseReadGeomValue, jsonConvert);
value = geomVal.pipe(map(
(geom: ParseReadGeomValue) => {
return new ReadGeomValue(geom);
}
));
break;
}
case Constants.TextValue: {
const textVal = handleTextValue(valueJsonld, jsonConvert);
value = textVal.pipe(map((val: ReadTextValue) => {
if (val instanceof ReadTextValueAsString) {
val.strval = val.text;
} else if (val instanceof ReadTextValueAsXml) {
val.strval = val.xml;
} else if (val instanceof ReadTextValueAsHtml) {
val.strval = val.html;
} else {
console.error("String representation for a ReadTextValue could not be constructed for: ", type);
}
return val;
}));
break;
}

case Constants.StillImageFileValue: {
const stillImageVal = handleSimpleValue(valueJsonld, ReadStillImageFileValue, jsonConvert);
value = stillImageVal.pipe(map((val: ReadStillImageFileValue) => {
val.strval = val.fileUrl;
return val;
}));
break;
}
case Constants.LinkValue: {
const linkVal = handleLinkValue(valueJsonld, ontologyCache, listNodeCache, jsonConvert);
value = linkVal.pipe(map((val: ReadLinkValue) => {
if (val.linkedResource !== undefined) {
val.strval = val.linkedResource.label;
} else {
val.strval = val.linkedResourceIri;
}
return val;
}));
break;
}

case Constants.TimeValue: {
const timeVal = handleSimpleValue(valueJsonld, ReadTimeValue, jsonConvert);
value = timeVal.pipe(map((val: ReadTimeValue) => {
val.strval = val.time;
return val;
}));
break;
}
case Constants.GeomValue: {
const geomVal = handleSimpleValue(valueJsonld, ParseReadGeomValue, jsonConvert);
value = geomVal.pipe(map(
(geom: ParseReadGeomValue) => {
return new ReadGeomValue(geom);
}
));
break;
}

case Constants.GeonameValue: {
const geonameVal = handleSimpleValue(valueJsonld, ReadGeonameValue, jsonConvert);
value = geonameVal.pipe(map((val: ReadGeonameValue) => {
val.strval = val.geoname;
return val;
}));
break;
}
case Constants.AudioFileValue: {
const audioVal = handleSimpleValue(valueJsonld, ReadAudioFileValue, jsonConvert);
value = audioVal.pipe(map((val: ReadAudioFileValue) => {
val.strval = val.fileUrl;
return val;
}));
break;
}

default: {
console.error("Unknown value type: ", type);
value = of(jsonConvert.deserialize(valueJsonld, ReadValue) as ReadValue);
}
case Constants.DocumentFileValue: {
const documentVal = handleSimpleValue(valueJsonld, ReadDocumentFileValue, jsonConvert);
value = documentVal.pipe(map((val: ReadDocumentFileValue) => {
val.strval = val.fileUrl;
return val;
}));
break;
}

case Constants.MovingImageFileValue: {
const movingImageVal = handleSimpleValue(valueJsonld, ReadMovingImageFileValue, jsonConvert);
value = movingImageVal.pipe(map((val: ReadMovingImageFileValue) => {
val.strval = val.fileUrl;
return val;
}));
break;
}

case Constants.StillImageFileValue: {
const stillImageVal = handleSimpleValue(valueJsonld, ReadStillImageFileValue, jsonConvert);
value = stillImageVal.pipe(map((val: ReadStillImageFileValue) => {
val.strval = val.fileUrl;
return val;
}));
break;
}

return value.pipe(map(
val => {
val.property = propIri;
val.propertyLabel = entitiyDefs.properties[propIri].label;
val.propertyComment = entitiyDefs.properties[propIri].comment;
case Constants.TimeValue: {
const timeVal = handleSimpleValue(valueJsonld, ReadTimeValue, jsonConvert);
value = timeVal.pipe(map((val: ReadTimeValue) => {
val.strval = val.time;
return val;
}));
break;
}

case Constants.GeonameValue: {
const geonameVal = handleSimpleValue(valueJsonld, ReadGeonameValue, jsonConvert);
value = geonameVal.pipe(map((val: ReadGeonameValue) => {
val.strval = val.geoname;
return val;
}
));
}));
break;
}

default: {
console.error("Unknown value type: ", type);
value = of(jsonConvert.deserialize(valueJsonld, ReadValue) as ReadValue);
}

}
;

return value.pipe(map(
val => {
val.property = propIri;
val.propertyLabel = entitiyDefs.properties[propIri].label;
val.propertyComment = entitiyDefs.properties[propIri].comment;

return val;
}
));

}
;

/**
* Creates a response to a count query.
Expand Down

0 comments on commit f5bbe8f

Please sign in to comment.