-
Notifications
You must be signed in to change notification settings - Fork 2
/
RecordSetVisitor.ts
136 lines (115 loc) · 3.93 KB
/
RecordSetVisitor.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {DialogProxyTools} from "./DialogProxyTools";
import {JsonObjectVisitor} from "./JsonObjectVisitor";
import {RecordVisitor} from "./RecordVisitor";
/**
*
*/
export class RecordSetVisitor implements JsonObjectVisitor {
private _enclosedJsonObject: any;
constructor(value: string | object) {
if (!value) {
throw new Error('RecordSetVisitor -- null value exception')
}
if (typeof value === 'string') {
this._enclosedJsonObject = JSON.parse(value as string);
} else {
this._enclosedJsonObject = value;
}
if (!DialogProxyTools.isRecordSetModel(this._enclosedJsonObject)) {
throw new Error("Object passed to RecordSetVisitor is not a RecordSet");
}
if (!this._enclosedJsonObject.records) {
throw new Error('Invalid record set -- missing records field');
}
if (!Array.isArray(this._enclosedJsonObject.records)) {
throw new Error('Invalid record set -- records field is not an array');
}
}
// --- State Management Helpers --- //
public static addOrUpdateRecord(jsonObject: object, recordState: RecordVisitor) {
(new RecordSetVisitor(jsonObject)).addOrUpdateRecord(recordState);
}
public static emptyRecordSetVisitor(): RecordSetVisitor {
return new RecordSetVisitor({
defaultActionId: null,
records: [],
hasMore: false,
type: "hxgn.api.dialog.RecordSet"
});
}
// --- 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 addOrUpdateAllRecords(recordSetState: RecordSetVisitor) {
for (const r of recordSetState.enclosedJsonObject().records) {
this.addOrUpdateRecord(new RecordVisitor(r));
}
}
public addOrUpdateRecord(recordState: RecordVisitor) {
let found = false;
const recordCopy = JSON.parse(JSON.stringify(recordState.enclosedJsonObject()));
for (const r of this.enclosedJsonObject().records) {
if (r.id === recordCopy.id) {
for (const k of Object.keys(r)) {
r[k] = recordCopy[k];
}
found = true;
break;
}
}
if (!found) {
this.enclosedJsonObject().records.push(recordCopy);
}
}
public fromRecordId(recordId: string) {
if (!recordId) {
return;
}
const records = [];
let found = false;
for (const r of this.visitRecords()) {
if (found) {
records.push(r);
} else {
if (r.visitRecordId() === recordId) {
found = true;
}
}
}
return this.enclosedJsonObject().records = records;
}
public recordCount(): number {
return this.enclosedJsonObject().records.length;
}
public visitAndClearRecords() {
this.enclosedJsonObject().records = [];
}
public visitHasMore(): boolean {
return this.enclosedJsonObject().hasMore;
}
public visitAndSetHasMore(hasMore: boolean) {
this.enclosedJsonObject().hasMore = hasMore;
}
public visitRecordAtId(id: string): RecordVisitor {
for (const r of this.visitRecords()) {
if (r.visitRecordId() === id) {
return r;
}
}
return null;
}
public *visitRecords(): IterableIterator<RecordVisitor> {
let index = 0;
while (index < this.enclosedJsonObject().records.length) {
yield new RecordVisitor(this.enclosedJsonObject().records[index++]);
}
}
}