-
Notifications
You must be signed in to change notification settings - Fork 2
/
DialogVisitor.ts
213 lines (181 loc) · 7.53 KB
/
DialogVisitor.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import {Base64} from "../util";
import {DialogProxyTools} from "./DialogProxyTools";
import {JsonObjectVisitor} from "./JsonObjectVisitor";
/**
*
*/
export class DialogVisitor implements JsonObjectVisitor {
private _enclosedJsonObject: any;
constructor(value: string | object) {
if (!value) {
throw new Error('DialogVisitor -- null value exception')
}
if (typeof value === 'string') {
this._enclosedJsonObject = JSON.parse(value as string);
} else {
this._enclosedJsonObject = value;
}
if (!DialogProxyTools.isDialogModel(this._enclosedJsonObject)) {
throw new Error("Object passed to DialogVisitor is not a Dialog");
}
}
// --- State Management Helpers --- //
public static propagateTenantIdAndSessionId(dialog: object, tenantId: string, sessionId: string) {
(new DialogVisitor(dialog)).propagateTenantIdAndSessionId(tenantId, sessionId);
}
public static visitId(dialog: object): string {
return (new DialogVisitor(dialog)).visitId();
}
public static visitAndSetId(dialog: object, id: string) {
(new DialogVisitor(dialog)).visitAndSetId(id);
}
// --- 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 --- //
/**
* The record id targeted by the root dialog (usually a Form) will be used to help derive new dialog ids for the
* root and all of its children. The derived dialog id is a concatenation of the dialogName with a '@' and the
* root record id.
*/
public deriveDialogIdsFromDialogNameAndRecordId() {
let derivedDialogId = this.enclosedJsonObject()['dialogName'];
if (!derivedDialogId) {
throw new Error("Cannot derive dialog ids -- dialog name not found")
}
let rootRecordId = null;
const referringObject = this.visitReferringObject();
if (DialogProxyTools.isReferringDialogModel(referringObject)) {
rootRecordId = this.visitRecordId();
// TODO: Fix the error in Dialog Service that returns null record ids as a string literal of "null"
if (rootRecordId && rootRecordId !== 'null') {
const recordIdEncoded = Base64.encodeUrlSafeString(rootRecordId);
derivedDialogId = `${derivedDialogId}@${recordIdEncoded}`;
}
}
this.visitAndSetId(derivedDialogId);
if (this.enclosedJsonObject()['children']) {
for (const c of this.enclosedJsonObject()['children']) {
const dialogVisitor = new DialogVisitor(c);
dialogVisitor.deriveDialogIdsFromDialogName(rootRecordId);
}
}
}
/**
* Traverse this dialog and its children and derive their dailog ids by concatenating each dialog's
* dialogName with a '$' and the given suffix. Dialog ids ending withs a suffix are synthetic. Its possible
* that a dialog id may have a suffix and a specific record id. In this case the dialog id will contain
* a '$' and an '@' so that each field can be parsed separately.
*
* @param {string} suffix
*/
public deriveDialogIdsFromDialogNameAndSuffix(suffix: string) {
let derivedDialogId = this.enclosedJsonObject()['dialogName'];
if (!derivedDialogId) {
throw new Error("Cannot propagate dialog ids -- dialog name not found")
}
if (suffix) {
derivedDialogId = derivedDialogId + '$' + suffix;
}
this.visitAndSetId(derivedDialogId);
if (this.enclosedJsonObject()['children']) {
for (const c of this.enclosedJsonObject()['children']) {
const dialogVisitor = new DialogVisitor(c);
dialogVisitor.deriveDialogIdsFromDialogNameAndSuffix(suffix);
}
}
}
public propagateTenantIdAndSessionId(tenantId: string, sessionId: string) {
this.enclosedJsonObject()['tenantId'] = tenantId;
this.enclosedJsonObject()['sessionId'] = sessionId;
if (this.enclosedJsonObject()['children']) {
for (const c of this.enclosedJsonObject()['children']) {
const dialogVisitor = new DialogVisitor(c);
dialogVisitor.propagateTenantIdAndSessionId(tenantId, sessionId);
}
}
}
public visitDescription(): string {
return this.enclosedJsonObject().description;
}
public visitAndSetDescription(description: string) {
this.enclosedJsonObject().description = description;
}
public visitDialogName(): string {
return this.enclosedJsonObject().dialogName;
}
public visitId(): string {
return this.enclosedJsonObject().id;
}
public visitAndSetId(id: string) {
this.enclosedJsonObject().id = id;
}
public visitAndSetRootDialogId(rootDialogId: string) {
this.enclosedJsonObject().rootDialogId = rootDialogId;
}
public visitChildAt(index: number): DialogVisitor {
return new DialogVisitor(this.enclosedJsonObject().children[index]);
}
public visitChildAtName(name: string): DialogVisitor {
if (this.enclosedJsonObject()['children']) {
for (const c of this.enclosedJsonObject()['children']) {
const dialogVisitor = new DialogVisitor(c);
if (dialogVisitor.visitDialogName() && dialogVisitor.visitDialogName() === name) {
return dialogVisitor;
}
const childDialogVisitor = dialogVisitor.visitChildAtName(name);
if (childDialogVisitor) {
return childDialogVisitor;
}
}
}
return null;
}
public visitChildAtNameAndSetId(name: string, id: string): boolean {
const childDialogVisitor = this.visitChildAtName(name);
if (childDialogVisitor) {
childDialogVisitor.visitAndSetId(id);
return true;
}
return false;
}
public visitRecordId(): string {
return this.enclosedJsonObject().recordId;
}
public visitAndSetRecordId(recordId: string) {
this.enclosedJsonObject().recordId = recordId;
}
public visitReferringObject(): object {
return this.enclosedJsonObject().referringObject;
}
public visitAndSetReferringDialogId(dialogId: string) {
this.visitReferringObject()['dialogId'] = dialogId;
}
private deriveDialogIdsFromDialogName(rootRecordId: string) {
let dialogName = this.enclosedJsonObject()['dialogName'];
if (!dialogName) {
throw new Error("Cannot propagate dialog name -- dialog name not found")
}
// TODO: Fix the error in Dialog Service that returns null record ids as a string literal of "null"
if (rootRecordId && rootRecordId !== 'null') {
const recordIdEncoded = Base64.encodeUrlSafeString(rootRecordId);
dialogName = `${dialogName}@${recordIdEncoded}`;
}
if (rootRecordId) {
}
this.visitAndSetId(dialogName);
if (this.enclosedJsonObject()['children']) {
for (const c of this.enclosedJsonObject()['children']) {
const dialogVisitor = new DialogVisitor(c);
dialogVisitor.deriveDialogIdsFromDialogName(rootRecordId);
}
}
}
}