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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
module Spicey.ViewGeneration where
import AbstractCurry.Types
import AbstractCurry.Build
import Database.ERD
import Database.ERD.Goodies
import Spicey.GenerationHelper
generateViewsForEntity :: String -> [Entity] -> Entity -> [Relationship]
-> CurryProg
generateViewsForEntity erdname allEntities
(Entity ename attrlist) relationships =
let noKeyAttrs = filter (\a -> notKey a && notPKey a) attrlist
noPKeyAttrs = filter notPKey attrlist
in simpleCurryProg
(viewModuleName ename)
[ "WUI", "HTML.Base", "Time", "Sort", bootstrapModule
, spiceyModule, sessionInfoModule
, erdname, entitiesToHtmlModule erdname]
[]
[
wuiSpec erdname (Entity ename noKeyAttrs) relationships allEntities,
tuple2Entity erdname (Entity ename noPKeyAttrs) relationships allEntities,
entity2Tuple erdname (Entity ename noPKeyAttrs) relationships allEntities,
wuiType erdname (Entity ename noKeyAttrs) relationships allEntities,
blankView erdname (Entity ename noKeyAttrs) relationships allEntities,
createView erdname (Entity ename noKeyAttrs) relationships allEntities,
editView erdname (Entity ename noKeyAttrs) relationships allEntities,
showView erdname (Entity ename noKeyAttrs) relationships allEntities,
leqEntity erdname (Entity ename noKeyAttrs) relationships allEntities,
listView erdname (Entity ename noKeyAttrs) relationships allEntities
]
[]
type ViewGenerator = String -> Entity -> [Relationship] -> [Entity] -> CFuncDecl
wuiSpec :: ViewGenerator
wuiSpec erdname (Entity entityName attrlist) relationships allEntities =
let
manyToManyEntities = manyToMany allEntities (Entity entityName attrlist)
manyToOneEntities = manyToOne (Entity entityName attrlist) relationships
argumentCount = length attrlist + length manyToOneEntities
+ length manyToManyEntities
in
stCmtFunc
("The WUI specification for the entity type "++entityName++".\n"++
if null (manyToOneEntities ++ manyToManyEntities)
then ""
else "It also includes fields for associated entities.")
(viewModuleName entityName, "w"++entityName) 2 Public
(foldr CFuncType
(applyTC ("WUI", "WuiSpec")
[entityInterface attrlist manyToOneEntities manyToManyEntities])
(map (\e -> listType (ctvar e))
(manyToOneEntities ++ manyToManyEntities))
)
[simpleRule (map (\e -> CPVar (1, lowerFirst $ e ++ "List"))
(manyToOneEntities ++ manyToManyEntities))
(applyF ("WUI", "withRendering") [
(if (argumentCount == 1) then
head (attrWidgets attrlist)
else
applyF (combinator argumentCount)
( attrWidgets attrlist ++
map (\e -> applyF (wui "wSelect")
[constF (erdname, lowerFirst e++"ToShortView"),
CVar (1, lowerFirst $ e ++ "List")])
manyToOneEntities ++
map (\e ->
applyF (wui "wMultiCheckSelect")
[CLambda [CPVar (1, lowerFirst e)]
(list2ac [
applyF (html "htxt") [
applyF (erdname, lowerFirst e++"ToShortView")
[CVar (1, lowerFirst e)]
]]),
CVar (1, lowerFirst $ e ++ "List")
]) manyToManyEntities
)
),
applyF (spiceyModule, "renderLabels")
[constF (entitiesToHtmlModule erdname,
lowerFirst entityName++"LabelList")]
]
)]
tuple2Entity :: ViewGenerator
tuple2Entity erdname (Entity entityName attrlist) relationships allEntities =
let
manyToManyEntities = manyToMany allEntities (Entity entityName attrlist)
manyToOneEntities = manyToOne (Entity entityName attrlist) relationships
in
stCmtFunc
("Transformation from data of a WUI form to entity type "++entityName++".")
(viewModuleName entityName, "tuple2"++entityName) 2 Public
(
foldr CFuncType
(if null manyToManyEntities
then baseType (erdname, entityName)
else tupleType ([ctvar entityName] ++
map (\name -> listType (ctvar name)) manyToManyEntities)
)
([ctvar entityName] ++
[entityInterface (filter notKey attrlist)
manyToOneEntities manyToManyEntities])
)
[simpleRule
(
[ CPVar (1, lowerFirst entityName ++ "ToUpdate"),
tuplePattern
(
(map (\ ((Attribute name _ _ _), varId) ->
CPVar (varId,lowerFirst name)))
(zip (filter notKey attrlist) [1..]) ++
(map (\ (name, varId) -> CPVar(varId,lowerFirst name))
(zip manyToOneEntities [1..])) ++
(map (\ (name, varId) -> CPVar(varId,lowerFirst $ name++"s"))
(zip manyToManyEntities [1..]))
)
]
)
(tupleExpr $
(foldr (\ (Attribute aname domain _ _) expr ->
case domain of
(KeyDom rel) ->
applyF (erdname, "set"++entityName++aname)
[expr,
applyF (erdname, (lowerFirst rel)++"Key")
[CVar (1, lowerFirst rel)]]
_ -> applyF (erdname, "set"++entityName++aname)
[expr, CVar (1, lowerFirst aname)] )
(CVar (0, lowerFirst $ entityName++"ToUpdate"))
attrlist )
: (map (\e -> cvar (lowerFirst $ e ++ "s")) manyToManyEntities))]
entity2Tuple :: ViewGenerator
entity2Tuple erdname (Entity entityName attrlist) relationships allEntities =
let
manyToManyEntities = manyToMany allEntities (Entity entityName attrlist)
manyToOneEntities = manyToOne (Entity entityName attrlist) relationships
in
stCmtFunc
("Transformation from entity type "++entityName++" to a tuple\n"++
"which can be used in WUI specifications.")
(viewModuleName entityName, (lowerFirst entityName)++"2Tuple") 2 Public
(
foldr (CFuncType)
(entityInterface (filter notKey attrlist)
manyToOneEntities manyToManyEntities)
(
(map ctvar manyToOneEntities) ++
[(
if null manyToManyEntities
then baseType (erdname, entityName)
else
tupleType ([ctvar entityName] ++
map (\name -> listType (ctvar name)) manyToManyEntities)
)]
)
)
[simpleRule
(
(map (\ (name, varId) -> CPVar(varId,(lowerFirst name)))
(zip manyToOneEntities [1..])) ++
[
tuplePattern
(
CPVar (1, lowerFirst entityName) :
(map (\ (name, varId) -> CPVar(varId,(lowerFirst $ name++"s")))
(zip manyToManyEntities [1..]))
)
]
)
(tupleExpr
(map (\ (Attribute a _ _ _) ->
applyF (erdname, (lowerFirst entityName)++a)
[cvar (lowerFirst entityName)])
(filter notKey attrlist) ++
map (\e -> cvar (lowerFirst e)) manyToOneEntities ++
map (\e -> cvar (lowerFirst $ e ++ "s")) manyToManyEntities)
)]
wuiType :: ViewGenerator
wuiType _ (Entity entityName attrlist) relationships allEntities =
let
manyToManyEntities = manyToMany allEntities (Entity entityName attrlist)
manyToOneEntities = manyToOne (Entity entityName attrlist) relationships
in
stCmtFunc
("WUI Type for editing or creating "++entityName++" entities.\n"++
"Includes fields for associated entities.")
(viewModuleName entityName, "w"++entityName++"Type") 2 Public
(
foldr CFuncType
(applyTC ("WUI", "WuiSpec") [
if null manyToManyEntities
then ctvar entityName
else
tupleType ([ctvar entityName] ++
map (\name -> listType (ctvar name)) manyToManyEntities)
])
(
[ctvar entityName] ++
(map (\e -> ctvar e) (manyToOneEntities)) ++
(map (\e -> listType (ctvar e))
(manyToOneEntities ++ manyToManyEntities))
)
)
[simpleRule
(
[CPVar (1, lowerFirst entityName)] ++
(map (\e -> CPVar (1, lowerFirst e)) manyToOneEntities) ++
(map (\e -> CPVar (1, lowerFirst $ e++"List"))
(manyToOneEntities ++ manyToManyEntities))
)
(applyF (wui "transformWSpec") [
tupleExpr
[
applyF (viewModuleName entityName, "tuple2"++entityName)
[cvar (lowerFirst entityName)],
applyF (viewModuleName entityName,lowerFirst entityName++"2Tuple")
(map (\e -> CVar (1, lowerFirst e)) (manyToOneEntities))
],
applyF (viewModuleName entityName, "w"++entityName)
(map (\e -> CVar (1, lowerFirst $ e ++ "List"))
(manyToOneEntities ++ manyToManyEntities))
]
)]
createView :: ViewGenerator
createView _ (Entity entityName attrlist) relationships allEntities =
let
manyToManyEntities = manyToMany allEntities (Entity entityName attrlist)
manyToOneEntities = manyToOne (Entity entityName attrlist) relationships
infovar = (0, "_")
in
viewFunction
("Supplies a WUI form to create a new "++entityName++" entity.\n"++
"Takes default values to be prefilled in the form fields.")
entityName "create" 3
(
userSessionInfoType ~>
(foldr CFuncType viewBlockType (
(map attrType attrlist) ++
(map ctvar manyToOneEntities) ++
(map (\e -> listType (ctvar e)) manyToManyEntities) ++
(map (\e -> listType (ctvar e)) (manyToOneEntities ++ manyToManyEntities)) ++
[entityInterface attrlist manyToOneEntities manyToManyEntities
~> controllerType,
controllerType]))
)
[simpleRule
(
CPVar infovar :
(map (\ ((Attribute name _ _ _), varId) ->
CPVar(varId,("default"++name)))
(zip attrlist [1..])) ++
(map (\ (name, varId) -> CPVar(varId,("default"++name)))
(zip manyToOneEntities [1..])) ++
(map (\ (name, varId) -> CPVar(varId,("default"++name++"s")))
(zip manyToManyEntities [1..])) ++
(map (\ (name, varId) -> CPVar(varId,("possible"++name++"s")))
(zip (manyToOneEntities++manyToManyEntities) [1..])) ++
[CPVar (100, "controller"), CPVar (101, "cancelcontroller")]
)
(applyF (spiceyModule,"renderWuiForm")
[applyF (viewModuleName entityName, "w"++entityName)
(map (\ (name, varId) -> CVar (varId,("possible"++name++"s")))
(zip (manyToOneEntities++manyToManyEntities) [1..])),
tupleExpr (
(map (\ ((Attribute name _ _ _), varId) ->
CVar(varId,("default"++name)))
(zip attrlist [1..])) ++
(map (\ (name, varId) -> CVar(varId,("default"++name)))
(zip manyToOneEntities [1..])) ++
(map (\ (name, varId) -> CVar(varId,"default"++name++"s"))
(zip manyToManyEntities [1..]))),
CVar (100, "controller"),
CVar (101, "cancelcontroller"),
string2ac ("Create new "++entityName),
string2ac "create"]
)]
editView :: ViewGenerator
editView erdname (Entity entityName attrlist) relationships allEntities =
let
manyToManyEntities = manyToMany allEntities (Entity entityName attrlist)
manyToOneEntities = manyToOne (Entity entityName attrlist) relationships
infovar = (0, "_")
in
viewFunction
("Supplies a WUI form to edit the given "++entityName++" entity.\n"++
"Takes also associated entities and a list of possible associations\n"++
"for every associated entity type.")
entityName "edit" 3
(
userSessionInfoType ~>
(foldr CFuncType viewBlockType (
[tupleType ([baseType (erdname, entityName)] ++
map (\name -> listType (ctvar name)) manyToManyEntities)
] ++
(map ctvar manyToOneEntities) ++
(map (\e -> listType (ctvar e))
(manyToOneEntities ++ manyToManyEntities)) ++
[tupleType ([baseType (erdname, entityName)] ++
map (\name -> listType (ctvar name)) manyToManyEntities)
~> controllerType,
controllerType]))
)
[simpleRule
(
CPVar infovar :
[tuplePattern
([CPVar (1, lowerFirst entityName)] ++
(map (\name -> CPVar (1, lowerFirst (name++"s")))
manyToManyEntities)
)] ++
(map (\ (name, varId) -> CPVar(varId,("related"++name)))
(zip manyToOneEntities [2..])) ++
(map (\ (name, varId) -> CPVar(varId,("possible"++name++"s")))
(zip (manyToOneEntities++manyToManyEntities) [2..])) ++
[CPVar (1, "controller"), CPVar (102, "cancelcontroller")]
)
(applyF (spiceyModule,"renderWuiForm")
[applyF (viewModuleName entityName, "w"++entityName++"Type") (
[cvar (lowerFirst entityName)] ++
(map (\ (name, varId) -> CVar(varId,("related"++name)))
(zip manyToOneEntities [2..])) ++
(map (\ (name, varId) -> CVar(varId,("possible"++name++"s")))
(zip (manyToOneEntities++manyToManyEntities) [2..]))
),
tupleExpr ([CVar (1, lowerFirst entityName)] ++
map (\name -> CVar (1, lowerFirst (name++"s")))
manyToManyEntities),
CVar (1, "controller"),
CVar (102, "cancelcontroller"),
string2ac ("Edit "++entityName),
string2ac "change"]
)]
blankView :: ViewGenerator
blankView _ (Entity entityName attrlist) relationships allEntities =
let
manyToManyEntities = manyToMany allEntities (Entity entityName attrlist)
manyToOneEntities = manyToOne (Entity entityName attrlist) relationships
withCTime = hasDateAttribute attrlist
infovar = (0, "sinfo")
in
viewFunction
("Supplies a WUI form to create a new "++entityName++" entity.\n"++
"The fields of the entity have some default values.")
entityName "blank" 3
(
userSessionInfoType ~>
foldr CFuncType viewBlockType (
(if withCTime then [baseType ("Time","ClockTime")] else []) ++
(map (\e -> listType (ctvar e))
(manyToOneEntities ++ manyToManyEntities)) ++
[entityInterface attrlist manyToOneEntities manyToManyEntities
~> controllerType,
controllerType])
)
[simpleRule
(
CPVar infovar :
(if withCTime then [CPVar (0,"ctime")] else []) ++
(map (\ (name, varId) -> CPVar(varId,("possible"++name++"s")))
(zip (manyToOneEntities++manyToManyEntities) [2..])) ++
[CPVar (1, "controller"), CPVar (2, "cancelcontroller")]
)
(applyF (viewFunctionName entityName "create")
(CVar infovar :
(attrDefaultValues (CVar (0,"ctime")) attrlist) ++
(map (\ (name, varId) ->
applyF (pre "head")
[CVar (varId,("possible"++name++"s"))])
(zip manyToOneEntities [2..])) ++
(map (\_ -> list2ac []) (zip manyToManyEntities [2..])) ++
(map (\ (name, varId) -> CVar (varId,("possible"++name++"s")))
(zip (manyToOneEntities++manyToManyEntities) [2..])) ++
[CVar (1, "controller"), CVar (1, "cancelcontroller")]
)
)]
leqEntity :: ViewGenerator
leqEntity erdname (Entity entityName attrlist) _ _ =
stCmtFunc
("Compares two "++entityName++" entities. This order is used in the list view.")
(viewModuleName entityName, "leq" ++ entityName) 2 Private
(baseType (erdname,entityName) ~> baseType (erdname,entityName) ~>boolType)
[let ename = lowerFirst entityName
e1 = (1,"x1")
e2 = (2,"x2")
in simpleRule [CPVar e1,CPVar e2]
(applyF (pre "<=")
[tupleExpr (map (\ (Attribute a _ _ _) ->
applyF (erdname,ename++a) [CVar e1])
(take 5 (filter notKey attrlist))),
tupleExpr (map (\ (Attribute a _ _ _) ->
applyF (erdname,ename++a) [CVar e2])
(take 5 (filter notKey attrlist)))
])]
showView :: ViewGenerator
showView erdname (Entity entityName attrlist) relationships allEntities =
let manyToManyEntities = manyToMany allEntities (Entity entityName attrlist)
manyToOneEntities = manyToOne (Entity entityName attrlist) relationships
infovar = (0, "_")
evar = (1, lowerFirst entityName)
in viewFunction
("Supplies a view to show the details of a "++entityName++".\n")
entityName "show" 2
(userSessionInfoType ~>
foldr CFuncType viewBlockType (
[baseType (erdname,entityName)] ++
(map ctvar manyToOneEntities) ++
(map (\name -> listType (ctvar name)) manyToManyEntities))
)
[simpleRule
(
[CPVar infovar, CPVar evar] ++
(map (\ (name, varId) -> CPVar (varId,"related"++name))
(zip manyToOneEntities [3..])) ++
(map (\ (name, varId) -> CPVar (varId, lowerFirst name ++ "s"))
(zip manyToManyEntities [(length manyToOneEntities + 3)..]))
)
(applyF (pre "++")
[applyF (entitiesToHtmlModule erdname,
lowerFirst entityName++"ToDetailsView")
([CVar evar] ++
map (\ (name, varId) -> CVar (varId,"related"++name))
(zip manyToOneEntities [3..]) ++
map (\ (name, varId) -> CVar (varId, lowerFirst name++"s"))
(zip manyToManyEntities
[(length manyToOneEntities + 3)..])
),
list2ac [applyF hrefButtonName
[string2ac ("?"++entityName++"/list"),
list2ac [applyF (html"htxt")
[string2ac ("back to "++entityName++" list")]]]]
]
)]
listView :: ViewGenerator
listView erdname (Entity entityName attrlist) _ _ =
let infovar = (0, "sinfo")
entsvar = (1, (lowerFirst entityName)++"s")
envar = (2, lowerFirst entityName)
showkey = applyF (erdname,"show"++entityName++"Key") [CVar envar]
in
viewFunction
("Supplies a list view for a given list of "++entityName++" entities.\n"++
"Shows also show/edit/delete buttons if the user is logged in.\n"++
"The arguments are the session info and the list of "++entityName++
" entities.\n")
entityName "list" 1
(userSessionInfoType ~> listType (baseType (erdname,entityName))
~> viewBlockType)
[CRule
[CPVar infovar, CPVar entsvar]
(CSimpleRhs
(applyF (pre ":") [
applyF (html "h1")
[list2ac [applyF (html "htxt")
[string2ac $ entityName ++ " list"]]],
list2ac [
applyF (spiceyModule, "spTable") [
applyF (pre "++") [
list2ac [
applyF (pre "take") [
CLit (CIntc (length attrlist)),
constF (entitiesToHtmlModule erdname,
lowerFirst entityName++"LabelList")
]
],
applyF (pre "map") [
constF (viewModuleName entityName,"list"++entityName),
applyF ("Sort","mergeSortBy") [
constF (viewModuleName entityName,"leq"++entityName),
CVar entsvar
]
]
]
]
]
]
)
[CLocalFunc (stFunc
(viewModuleName entityName, "list"++entityName) 2 Private
(ctvar entityName ~> listType viewBlockType)
[simpleRule [CPVar envar]
(applyF (pre "++") [
applyF (entitiesToHtmlModule erdname,
lowerFirst entityName++"ToListView")
[cvar $ lowerFirst entityName],
applyF (pre "if_then_else")
[applyF (pre "==")
[applyF (sessionInfoModule,"userLoginOfSession")
[CVar infovar],
constF (pre "Nothing")],
list2ac [],
list2ac
[list2ac
[applyF hrefButtonName
[applyF (pre "++")
[string2ac ("?"++entityName++"/show/"),showkey],
list2ac [applyF (html "htxt") [string2ac "show"]]]],
list2ac
[applyF hrefButtonName
[applyF (pre "++")
[string2ac ("?"++entityName++"/edit/"),showkey],
list2ac [applyF (html "htxt") [string2ac "edit"]]]],
list2ac
[applyF hrefButtonName
[applyF (pre "++")
[string2ac ("?"++entityName++"/delete/"),showkey],
list2ac [applyF (html "htxt") [string2ac "delete"]]]]
]]
]
)])
])
]
viewFunction :: String -> String -> String -> Int -> CTypeExpr -> [CRule]
-> CFuncDecl
viewFunction description entityName viewType arity functionType rules =
stCmtFunc description (viewFunctionName entityName viewType) arity
Public functionType rules
entityInterface :: [Attribute] -> [String] -> [String] -> CTypeExpr
entityInterface attrlist manyToOne manyToMany =
tupleType (map attrType attrlist ++
map ctvar manyToOne ++
map (\e -> listType (ctvar e)) manyToMany)
userSessionInfoType :: CTypeExpr
userSessionInfoType = baseType (sessionInfoModule,"UserSessionInfo")
|