-
Notifications
You must be signed in to change notification settings - Fork 2
/
RecordVisitor.ts
98 lines (82 loc) · 2.99 KB
/
RecordVisitor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {DialogProxyTools} from "./DialogProxyTools";
import {JsonObjectVisitor} from "./JsonObjectVisitor";
import {PropertyVisitor} from "./PropertyVisitor";
/**
*
*/
export class RecordVisitor implements JsonObjectVisitor {
private _enclosedJsonObject: any;
constructor(value: string | object) {
if (!value) {
throw new Error('RecordVisitor -- null value exception')
}
if (typeof value === 'string') {
this._enclosedJsonObject = JSON.parse(value as string);
} else {
this._enclosedJsonObject = value;
}
if (!DialogProxyTools.isRecordModel(this._enclosedJsonObject)) {
throw new Error("Object passed to RecordVisitor is not a Record");
}
if (!this._enclosedJsonObject.properties) {
throw new Error('Invalid record -- missing properties field');
}
if (!Array.isArray(this._enclosedJsonObject.properties)) {
throw new Error('Invalid record set -- properties field is not an array');
}
}
// --- State Management Helpers --- //
public static visitPropertyValueAt(record: object, propertyName: string): any {
return (new RecordVisitor(record)).visitPropertyValueAt(propertyName);
}
public static visitAndSetPropertyValueAt(record: object, propertyName: string, value: any) {
(new RecordVisitor(record)).visitAndSetPropertyValueAt(propertyName, value);
}
// --- State Import/Export --- //
public copyAsJsonObject(): object {
return JSON.parse(this.copyAsJsonString());
}
public copyAsJsonString(): string {
return JSON.stringify(this.enclosedJsonObject());
}
public enclosedJsonObject() {
return this._enclosedJsonObject;
}
// --- State Management --- //
public visitPropertyValueAt(propertyName: string): any {
for (const p of this.enclosedJsonObject().properties) {
if (p.name === propertyName) {
return p.value;
}
}
return undefined;
}
public visitAndSetPropertyValueAt(propertyName: string, value: any) {
let found = false;
for (const p of this.enclosedJsonObject().properties) {
if (p.name === propertyName) {
p.value = value;
found = true;
break;
}
}
if (!found) {
this.enclosedJsonObject().properties.push({
"name": propertyName,
"format": null,
"annotations": [],
"type": "hxgn.api.dialog.Property",
"value": value
});
}
}
public * visitProperties(): IterableIterator<PropertyVisitor> {
let index = 0;
while (index < this.enclosedJsonObject().properties.length) {
yield new PropertyVisitor(this.enclosedJsonObject().properties[index++]);
}
}
public visitRecordId(): string {
return this.enclosedJsonObject().id;
}
}