-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathputallHandler.ts
89 lines (86 loc) · 2.61 KB
/
putallHandler.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
import { Injectable, Injector, Inject } from "@angular/core";
import { Http } from "@angular/http";
import { Observable } from "rxjs/Observable";
import {
WiContrib,
WiServiceHandlerContribution,
IContributionTypes,
ActionResult,
IActionResult,
WiContribModelService,
IFieldDefinition,
WiContributionUtils,
IActivityContribution,
IConnectorContribution,
} from "wi-studio/app/contrib/wi-contrib";
import {
IValidationResult,
ValidationResult,
} from "wi-studio/common/models/validation";
@WiContrib({})
@Injectable()
export class putallHandler extends WiServiceHandlerContribution {
constructor(private injector: Injector, private http: Http) {
super(injector, http);
}
value = (
fieldName: string,
context: IActivityContribution
): Observable<any> | any => {
if (fieldName === "data") {
let dataField: IFieldDefinition = context.getField("data");
if (dataField.value) {
let sch = JSON.parse(dataField.value);
// check schema object type, and convert to key-value type
if (sch["type"] == "object") {
let data = {};
data["type"] = "object";
data["properties"] = sch["properties"];
let keyval = {};
keyval["key"] = { type: "string" };
keyval["value"] = data;
let item = {};
item["type"] = "object";
item["properties"] = keyval;
let dataschema = {};
dataschema["$schema"] = sch["$schema"];
dataschema["type"] = "array";
dataschema["items"] = item;
return JSON.stringify(dataschema, null, 2);
}
}
} else if (fieldName === "result") {
// set it the same as data if not using shared data defs
let dataField: IFieldDefinition = context.getField("data");
if (dataField.value) {
return dataField.value;
}
}
return null;
};
validate = (
fieldName: string,
context: IActivityContribution
): Observable<IValidationResult> | IValidationResult => {
if (fieldName === "data") {
let vresult: IValidationResult = ValidationResult.newValidationResult();
let dataField: IFieldDefinition = context.getField("data");
if (dataField.value) {
try {
let valRes;
valRes = JSON.parse(dataField.value);
valRes = JSON.stringify(valRes);
} catch (e) {
vresult.setError(
"FABRIC-PUTALL-1000",
"Invalid JSON: " + e.toString()
);
}
} else {
vresult.setError("FABRIC-PUTALL-1010", "Data schema must not be empty");
}
return vresult;
}
return null;
};
}