This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
/
composition.model.ts
274 lines (242 loc) · 7.69 KB
/
composition.model.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* @license
* Copyright 2017 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CompositionType } from "@dataservices/shared/composition-type.enum";
import { CompositionOperator } from "@dataservices/shared/composition-operator.enum";
import { PathUtils } from "@dataservices/shared/path-utils";
/**
* Composition model
*/
export class Composition {
private name: string;
private initialSourcePath: string;
private leftSourcePath: string;
private rightSourcePath: string;
private leftCriteriaColumn: string;
private rightCriteriaColumn: string;
private type: CompositionType = CompositionType.INNER_JOIN;
private operator: CompositionOperator = CompositionOperator.EQ;
/**
* @param {Object} json the JSON representation of a Composition
* @returns {Composition} the new Composition (never null)
*/
public static create( json: object = {} ): Composition {
const composition = new Composition();
composition.setValues( json );
return composition;
}
constructor() {
// nothing to do
}
/**
* @returns {string} the composition name
*/
public getName(): string {
return this.name;
}
/**
* @param {string} name the composition name
*/
public setName( name?: string ): void {
this.name = name ? name : null;
}
/**
* @returns {string} the composition type
*/
public getType(): CompositionType {
return this.type;
}
/**
* @param {string} type the composition type
*/
public setType( type: CompositionType ): void {
this.type = type;
}
/**
* @returns {CompositionOperator} the composition operator
*/
public getOperator(): CompositionOperator {
return this.operator;
}
/**
* @param {CompositionOperator} operator the composition operator
*/
public setOperator(operator: CompositionOperator ): void {
this.operator = operator;
}
/**
* @return {boolean} 'true' if initial source is set and on the left
*/
public get initialSourceOnLeft(): boolean {
if (this.initialSourcePath !== null && this.leftSourcePath !== null && this.initialSourcePath === this.leftSourcePath) {
return true;
}
return false;
}
/**
* @return {boolean} 'true' if initial source is set and on the right
*/
public get initialSourceOnRight(): boolean {
if (this.initialSourcePath !== null && this.rightSourcePath !== null && this.initialSourcePath === this.rightSourcePath) {
return true;
}
return false;
}
/**
* @returns {string} the left source path of the composition
*/
public getLeftSourcePath(): string {
return this.leftSourcePath;
}
/**
* @param {string} sourcePath the left source path of the composition
*/
public setLeftSourcePath(sourcePath: string, isInitialSource = false ): void {
this.leftSourcePath = sourcePath;
if (isInitialSource) {
this.initialSourcePath = this.leftSourcePath;
}
}
/**
* @returns {string} the right source source of the composition
*/
public getRightSourcePath(): string {
return this.rightSourcePath;
}
/**
* @param {string} sourcePath the right source of the composition
*/
public setRightSourcePath(sourcePath: string, isInitialSource = false ): void {
this.rightSourcePath = sourcePath;
if (isInitialSource) {
this.initialSourcePath = this.rightSourcePath;
}
}
/**
* @returns {string} the left criteria column
*/
public getLeftCriteriaColumn(): string {
return this.leftCriteriaColumn;
}
/**
* @param {string} column the left criteria column
*/
public setLeftCriteriaColumn(column: string ): void {
this.leftCriteriaColumn = column;
}
/**
* @returns {string} the right criteria column
*/
public getRightCriteriaColumn(): string {
return this.rightCriteriaColumn;
}
/**
* @param {string} column the right criteria column
*/
public setRightCriteriaColumn(column: string ): void {
this.rightCriteriaColumn = column;
}
/**
* Swap the left and right sources (and corresponding column selections)
*/
public swapTables( ): void {
// Switch the left and right tables
const leftSource = this.getLeftSourcePath();
const rightSource = this.getRightSourcePath();
this.setRightSourcePath(leftSource);
this.setLeftSourcePath(rightSource);
// Switch the column selections
const selectedLeftColumn = this.getLeftCriteriaColumn();
const selectedRightColumn = this.getRightCriteriaColumn();
this.setRightCriteriaColumn(selectedLeftColumn);
this.setLeftCriteriaColumn(selectedRightColumn);
}
/**
* Determine whether the composition is in a complete state
* @returns {boolean} true if complete
*/
public get complete(): boolean {
if ( this.name !== null &&
this.type && this.type !== null &&
this.operator && this.operator !== null &&
this.leftSourcePath && this.leftSourcePath !== null &&
this.rightSourcePath && this.rightSourcePath !== null &&
this.leftCriteriaColumn && this.leftCriteriaColumn !== null &&
this.rightCriteriaColumn && this.rightCriteriaColumn !== null) {
return true;
} else {
return false;
}
}
/**
* Determine if the supplied Composition is equal to this
* @param {Object} values
*/
public isEqual( otherComp: Composition ): boolean {
let equal = false;
if (this.getName() === otherComp.getName() &&
this.getLeftSourcePath() === otherComp.getLeftSourcePath() &&
this.getRightSourcePath() === otherComp.getRightSourcePath() &&
this.getLeftCriteriaColumn() === otherComp.getLeftCriteriaColumn() &&
this.getRightCriteriaColumn() === otherComp.getRightCriteriaColumn() &&
this.getType() === otherComp.getType() &&
this.getOperator() === otherComp.getOperator() ) {
equal = true;
}
return equal;
}
/**
* Set all object values using the supplied View json
* @param {Object} values
*/
public setValues(values: object = {}): void {
Object.assign(this, values);
}
/**
* @returns {{}} a JSON representation of the composition
*/
public toJSON(): {} {
return {
name: this.name,
leftSourcePath: this.leftSourcePath,
rightSourcePath: this.rightSourcePath,
leftCriteriaColumn: this.leftCriteriaColumn,
rightCriteriaColumn: this.rightCriteriaColumn,
type: this.type,
operator: this.operator
};
}
public toString(): string {
return JSON.stringify(this.toJSON());
}
public getLeftSourceDisplay(): string {
const leftSrcPath = this.getLeftSourcePath();
const leftConn = PathUtils.getConnectionName(leftSrcPath);
const leftSrc = PathUtils.getSourceName(leftSrcPath);
return "[" + leftConn + "] " + leftSrc;
}
public getRightSourceDisplay(): string {
const rightSrcPath = this.getRightSourcePath();
const rightConn = PathUtils.getConnectionName(rightSrcPath);
const rightSrc = PathUtils.getSourceName(rightSrcPath);
return "[" + rightConn + "] " + rightSrc;
}
public getCriteriaDisplay(): string {
const leftColumn = this.getLeftCriteriaColumn();
const rightColumn = this.getRightCriteriaColumn();
return leftColumn + " " + CompositionOperator.toSql(this.getOperator()) + " " + rightColumn;
}
}