generated from intersystems-community/intersystems-iris-dev-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ServerAppGenerator.cls
130 lines (93 loc) · 3.98 KB
/
ServerAppGenerator.cls
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
Include dc.openapi.common.macro
Class dc.openapi.server.ServerAppGenerator
{
Parameter XDataName = "OpenAPI";
ClassMethod Generate(
application As %String,
specification,
webApplicationname As %String = "",
ByRef features As %Binary,
externals As %DynamicObject = "") As %Status
{
$$$zlog("Generate server-side application " _ application)
Do ##class(community.utils.SSLClient).CreateSSLConfiguration("DefaultSSL")
Set spec = ##class(dc.openapi.common.SpecificationLoader).AbstractSpecLoader(specification, .sc, '$Get(features("noExtRef"),0))
If $$$ISERR(sc) Do $SYSTEM.Status.DisplayError(sc) Quit sc
Set sc = ..%CreateServerApplication(application, spec, .features, .newApplication, externals)
If $$$ISERR(sc),$Get(%zverbose,1) Do $SYSTEM.Status.DisplayError(sc)
If webApplicationname '= "" Set sc = $$$ADDSC(sc, ..CreateWebApplication(webApplicationname, application_".disp"))
Return sc
}
ClassMethod %CreateServerApplication(
applicationName As %String,
swagger As %DynamicObject,
ByRef features As %Binary,
Output newApplication As %Boolean,
externals As %DynamicObject = "") As %Status
{
Set sc = $$$OK
Try {
Set specClass = applicationName _ ".Spec"
$$$zlog("* Create a class with the specification in a XData block " _ specClass)
Set cDef=##class(%Dictionary.ClassDefinition).%OpenId(specClass,4,.sc)
If $Isobject(cDef) {
Do ##class(dc.openapi.common.Utils).RemoveXDataBlock(cDef, ..#XDataName)
Set newApplication = $$$NO
} Else {
Set cDef=##class(%Dictionary.ClassDefinition).%New(specClass)
Set newApplication = $$$YES
}
; Security to avoid overwrite an existing package on the server ...
If newApplication && ##class(dc.openapi.common.Utils).packageExists(applicationName) {
Set sc = $$$ERROR(5001, "Overwritting code protection. This package already exist on the server, please choose an other appName "_applicationName)
$$$ThrowStatus(sc)
}
Set cDef.Super = "%RegisteredObject"
Set cDef.ProcedureBlock = $$$YES
Set xData = ##class(%Dictionary.XDataDefinition).%New()
Set xData.Name = ..#XDataName
Set xData.MimeType = "application/json"
$$$ThrowOnError(##class(%JSON.Formatter).%New().FormatToStream(swagger, xData.Data))
$$$ThrowOnError(cDef.XDatas.Insert(xData))
$$$ThrowOnError(cDef.%Save())
$$$ThrowOnError($system.OBJ.Compile(specClass,"ck"))
set swagger.externals = externals
Set generator = ##class(dc.openapi.server.RESTGenerator).%New(swagger, applicationName)
Set sc = generator.Generate(.features)
} Catch (ex) {
Set sc = ex.AsStatus()
}
Do ##class(dc.openapi.common.Utils).KillTempData()
If $$$ISOK(sc), $Get(features("compile"), 1) {
; no error -> compile
Set sc = $SYSTEM.OBJ.CompilePackage(applicationName)
}
Return sc
}
ClassMethod CreateWebApplication(
webAppName As %String,
dispatchClassName As %String) As %Status
{
Set ns = $NameSpace, sc = $$$OK
New $NameSpace
Set $NameSpace = "%SYS"
If ##class(Security.Applications).Exists(webAppName) {
Set $NameSpace = ns
$$$zlog("= Web application " _ webAppName _ " already exists, skip ...")
Quit sc
}
set webProperties("NameSpace") = ns
set webProperties("Enabled") = 1
set webProperties("CSPZENEnabled") = 1
set webProperties("AutheEnabled") = 64
set webProperties("iKnowEnabled") = 1
set webProperties("DeepSeeEnabled") = 1
set webProperties("DeepSeeEnabled") = 1
Set webProperties("DispatchClass") = dispatchClassName
Set webProperties("MatchRoles")=":%DB_" _ ns
set sc = ##class(Security.Applications).Create(webAppName, .webProperties)
Set $NameSpace = ns
$$$zlog("= Web application " _ webAppName _ "status : " _ $SYSTEM.Status.GetOneErrorText(sc) )
Return sc
}
}