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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
module Spicey.GenerationHelper where
import AbstractCurry.Types
import AbstractCurry.Build
import Char
import Database.ERD
import Database.ERD.Goodies
import Time
lowerFirst :: String -> String
lowerFirst (y:ys) = (toLower y) : ys
lowerFirst [] = []
upperFirst :: String -> String
upperFirst (y:ys) = (toUpper y) : ys
upperFirst [] = []
db :: String -> QName
db f = ("Database.KeyDatabaseSQLite", f)
html :: String -> QName
html f = ("HTML.Base", f)
wui :: String -> QName
wui f = ("WUI", f)
spiceyModule :: String
spiceyModule = "System.Spicey"
authenticationModule :: String
authenticationModule = "System.Authentication"
authorizationModule :: String
authorizationModule = "System.Authorization"
sessionInfoModule :: String
sessionInfoModule = "System.SessionInfo"
dataModuleName :: String
dataModuleName = "Config.RoutesData"
mappingModuleName :: String
mappingModuleName = "Config.ControllerMapping"
entitiesToHtmlModule :: String -> String
entitiesToHtmlModule erdname = "View." ++ erdname ++ "EntitiesToHtml"
bootstrapModule :: String
bootstrapModule = "HTML.Styles.Bootstrap3"
hrefButtonName :: QName
hrefButtonName = (bootstrapModule,"hrefButton")
relatedRelation :: String -> Relationship -> String
relatedRelation en (Relationship _ [REnd en1 _ _, REnd en2 _ _]) =
if en==en1 then en2 else en1
relationshipsForEntityName :: String -> [Relationship] -> [Relationship]
relationshipsForEntityName ename rels = filter endsIn rels
where
endsIn (Relationship _ ends) = any (\ (REnd n _ _) -> ename == n) ends
isGenerated :: Entity -> Bool
isGenerated (Entity _ attrs) = null (filter (not . isForeignKey) attrs)
notPKey :: Attribute -> Bool
notPKey (Attribute _ _ k _) = k /= PKey
notKey :: Attribute -> Bool
notKey (Attribute _ t _ _) =
case t of
(KeyDom _) -> False
_ -> True
isRelevantForEntity :: Entity -> [Attribute] -> Bool
isRelevantForEntity (Entity ename a) (attr:attrs) =
case attr of
(Attribute _ (KeyDom name) _ _) -> ename == name
_ -> isRelevantForEntity (Entity ename a) attrs
isRelevantForEntity _ [] = False
oneToOne :: Entity -> [Relationship] -> [String]
oneToOne (Entity ename _) rel =
map (relatedRelation ename) (filter isOneToOne rel)
where
isOneToOne :: Relationship -> Bool
isOneToOne relationship =
case relationship of
(Relationship _ [(REnd _ _ (Exactly 1)), (REnd _ _ (Exactly 1))]) -> True
_ -> False
manyToOne :: Entity -> [Relationship] -> [String]
manyToOne (Entity ename _) rel =
map (relatedRelation ename) (filter isManyToOne rel)
where
isManyToOne :: Relationship -> Bool
isManyToOne relationship =
case relationship of
(Relationship _ [REnd _ _ (Exactly 1),
REnd relEName _ (Between _ _)])
-> relEName == ename
_ -> False
manyToMany :: [Entity] -> Entity -> [String]
manyToMany entities forEntity =
map (getOtherREnd forEntity)
(filter (\ (Entity ename attr) -> isGenerated (Entity ename attr) &&
isRelevantForEntity forEntity attr)
entities)
where
getOtherREnd (Entity ename _)
(Entity _ [(Attribute _ (KeyDom name1) _ _),
(Attribute _ (KeyDom name2) _ _)]) =
if (name1 == ename) then name2 else name1
linkTableName :: String -> String -> [Entity] -> String
linkTableName ename1 ename2 entities =
getLinkTableName (filter isGenerated entities)
where
getLinkTableName ((Entity name [(Attribute _ (KeyDom rel1) _ _),
(Attribute _ (KeyDom rel2) _ _)]):erest) =
if (rel1==ename1 && rel2==ename2) || (rel1==ename2 && rel2==ename1)
then name
else getLinkTableName erest
getLinkTableName [] = error "linkTableName: link not found"
controllerType :: CTypeExpr
controllerType = baseType (spiceyModule,"Controller")
controllerModuleName :: String -> String
controllerModuleName entityName = "Controller." ++ entityName
controllerFunctionName :: String -> String -> QName
controllerFunctionName entityName controllerFunction =
(controllerModuleName entityName,
controllerFunction ++ entityName ++ "Controller")
transFunctionName :: String -> String -> QName
transFunctionName entityName controllerFunction =
(controllerModuleName entityName,
controllerFunction ++ entityName ++ "T")
viewModuleName :: String -> String
viewModuleName entityName = "View." ++ entityName
viewFunctionName :: String -> String -> QName
viewFunctionName entityName viewFunction =
(viewModuleName entityName, viewFunction ++ entityName ++ "View")
viewBlockType :: CTypeExpr
viewBlockType = listType (baseType (html "HtmlExp"))
attrType :: Attribute -> CTypeExpr
attrType (Attribute _ t k False) =
case t of (IntDom _) -> if k==PKey
then ctvar "Key"
else ctvar "Int"
(FloatDom _) -> ctvar "Float"
(StringDom _ ) -> ctvar "String"
(BoolDom _) -> ctvar "Bool"
(DateDom _) -> ctvar "CalendarTime"
(UserDefined s _)-> ctvar s
(KeyDom _) -> ctvar "Key"
_ -> ctvar "Int"
attrType (Attribute _ t k True) =
case t of (IntDom _) -> if k==PKey
then maybeType (ctvar "Key")
else maybeType (ctvar "Int")
(FloatDom _) -> maybeType (ctvar "Float")
(StringDom _ ) -> ctvar "String"
(BoolDom _) -> maybeType (ctvar "Bool")
(DateDom _) -> maybeType (ctvar "CalendarTime")
(UserDefined s _)-> maybeType (ctvar s)
(KeyDom _) -> maybeType (ctvar "Key")
_ -> maybeType (ctvar "Int")
attrDefaultValues :: CExpr -> [Attribute] -> [CExpr]
attrDefaultValues defaultctime attrs = map defaultValue attrs
where
defaultValue (Attribute _ domain _ null) = case domain of
IntDom Nothing -> nothingOrDefault
IntDom (Just n) -> addJust (CLit (CIntc n))
FloatDom Nothing -> nothingOrDefault
FloatDom (Just x) -> addJust (CLit (CFloatc x))
CharDom Nothing -> nothingOrDefault
CharDom (Just c) -> addJust (CLit (CCharc c))
StringDom Nothing -> string2ac ""
StringDom (Just s) -> string2ac s
BoolDom Nothing -> nothingOrDefault
BoolDom (Just b) -> addJust (constF (pre (if b then "True" else "False")))
DateDom Nothing -> nothingOrDefault
DateDom (Just (CalendarTime y mo d h m s tz))
-> addJust (applyF ("Time", "CalendarTime")
(map (CLit . CIntc) [y,mo,d,h,m,s,tz]))
UserDefined _ _ -> nothingOrDefault
KeyDom _ -> nothingOrDefault
_ -> error "GenerationHelper.attrDefaultValues: unknown domain for attribute"
where
nothingOrDefault = if null
then constF (pre "Nothing")
else domainDefaultValue defaultctime domain
addJust e = if null then applyF (pre "Just") [e] else e
domainDefaultValue :: CExpr -> Domain -> CExpr
domainDefaultValue defaultctime domain = case domain of
IntDom _ -> CLit (CIntc 0)
FloatDom _ -> CLit (CFloatc 0)
CharDom _ -> CLit (CCharc ' ')
StringDom _ -> string2ac []
BoolDom _ -> constF (pre "False")
DateDom _ -> defaultctime
UserDefined _ _ -> list2ac []
KeyDom _ -> CLit (CIntc 0)
_ -> error "GenerationHelper.domainDefaultValue: unknown domain"
isStringDom :: Domain -> Bool
isStringDom dom = case dom of
StringDom _ -> True
_ -> False
hasCalendarTimeAttribute :: [Attribute] -> Bool
hasCalendarTimeAttribute = any isCalendarTime
where
isCalendarTime (Attribute _ domain _ _) = case domain of
DateDom _ -> True
_ -> False
combinator :: Int -> QName
combinator n =
case n of
0 -> error "GenerationHelper.combinator: empty attribute list"
1 -> error "GenerationHelper.combinator: no combinator for list of length 1"
2 -> (wui "wPair")
3 -> (wui "wTriple")
4 -> (wui "w4Tuple")
5 -> (wui "w5Tuple")
6 -> (wui "w6Tuple")
7 -> (wui "w7Tuple")
8 -> (wui "w8Tuple")
9 -> (wui "w9Tuple")
10 -> (wui "w10Tuple")
11 -> (wui "w11Tuple")
12 -> (wui "w12Tuple")
_ -> error "GenerationHelper.combinator: attribute list too long"
attrWidgets :: [Attribute] -> [CExpr]
attrWidgets ((Attribute _ domain _ null):attrlist) =
(widgetFor domain null) : (attrWidgets attrlist)
attrWidgets [] = []
widgetFor :: Domain -> Bool -> CExpr
widgetFor domain null =
case domain of
IntDom _ -> addMaybe (constF (wui "wInt"))
FloatDom _ -> addMaybe (constF (wui "wFloat"))
CharDom _ -> addMaybe (constF (wui "wString"))
StringDom _ -> if null then constF (spiceyModule,"wString")
else constF (wui "wRequiredString")
BoolDom _ -> addMaybe (constF (wui "wBoolean"))
DateDom _ -> addMaybe (constF (spiceyModule, "wDateType"))
UserDefined _ _ -> addMaybe (applyF (wui "wCheckBool")
[applyF (html "htxt") [string2ac ""]])
KeyDom _ -> addMaybe (constF (wui "wInt"))
_ -> error "widgetFor: unknown domain for attribute"
where
addMaybe e =
if null
then applyF (spiceyModule,"wUncheckMaybe")
[domainDefaultValue
(applyF ("Time", "CalendarTime")
(map (CLit . CIntc) [2016,1,1,0,0,0,0]))
domain, e]
else e
|