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
module Spicey.EntitiesToHtmlGeneration where

import AbstractCurry.Types
import AbstractCurry.Build
import Database.ERD
import Database.ERD.Goodies

import Spicey.GenerationHelper

-- "main"-function
generateToHtml :: String -> [Entity] -> [Relationship] -> CurryProg
generateToHtml erdname allEntities relationships = simpleCurryProg
  (entitiesToHtmlModule erdname)
  ["Time", "HTML.Base", "HTML.WUI", spiceyModule, erdname] -- imports
  [] -- typedecls
  -- functions
  (
   foldr1 (++)
     (map (\e -> generateToHtmlForEntity erdname allEntities e relationships)
          (filter (not . Spicey.GenerationHelper.isGenerated)
                  allEntities))
  )
  []


generateToHtmlForEntity :: String -> [Entity] -> Entity -> [Relationship]
                        -> [CFuncDecl]
generateToHtmlForEntity erdname allEntities (Entity ename attrlist) relationships =
  [toListView    erdname (Entity ename (filter noKeyAttr attrlist)) relationships allEntities,
   toShortView   erdname (Entity ename (filter noKeyAttr attrlist)) relationships allEntities,
   toDetailsView erdname (Entity ename (filter noKeyAttr attrlist)) relationships allEntities,
   labelList     erdname (Entity ename (filter noKeyAttr attrlist)) relationships allEntities
  ]
 where
  noKeyAttr a = (notKey a) && (notPKey a)


type ToHtmlGenerator = String -> Entity -> [Relationship] -> [Entity] -> CFuncDecl

toListView :: ToHtmlGenerator
toListView erdname (Entity entityName attrlist) _ _ =
 cmtfunc
  ("The list view of a "++entityName++" entity in HTML format.\n"++
   "This view is used in a row of a table of all entities.")
  (thisModuleName erdname, lowerFirst entityName ++ "ToListView") 2 Public
  (withHTMLContext
     (baseType (erdname, entityName) ~> listType (listType htmlTVar)))
  [simpleRule [CPVar (1, lowerFirst entityName)]
     (list2ac (
            (map (\a -> list2ac [
                          applyF (attributeToConverter a) [
                            applyF (erdname, (lowerFirst entityName)++(attributeName a)) [
                              cvar (lowerFirst entityName)
                            ]
                          ]
                        ]
                 )
              attrlist
           )
         )
     )]

toShortView :: ToHtmlGenerator
toShortView erdname (Entity entityName attrlist) _ _ =
  stCmtFunc
  ("The short view of a "++entityName++" entity as a string.\n"++
   "This view is used in menus and comments to refer to a "++entityName++" entity.")
  (thisModuleName erdname, (lowerFirst entityName)++"ToShortView")
  2
  Public
  (baseType (erdname, entityName) ~> stringType)
  [simpleRule [CPVar (1,eName)]
     (case attributeDomain firstKeyAttribute of
         StringDom _ -> accessFirstKeyAttribute
         _           -> applyF (pre "show") [accessFirstKeyAttribute]
     )]
  where
    eName = lowerFirst entityName

    accessFirstKeyAttribute =
      applyF (erdname, eName++attributeName firstKeyAttribute) [cvar eName]

    firstKeyAttribute = findKeyAttribute attrlist attrlist

    findKeyAttribute (attr@(Attribute _ _ key _):attrList) fullList =
      if key== Unique then attr
                      else findKeyAttribute attrList fullList
    findKeyAttribute [] fullList = head fullList

toDetailsView :: ToHtmlGenerator
toDetailsView erdname (Entity entityName attrlist) relationships allEntities =
 let manyToManyEntities = manyToMany allEntities (Entity entityName attrlist)
     manyToOneEntities  = manyToOne (Entity entityName attrlist) relationships
     eName = lowerFirst entityName
     evar  = (1,eName)
  in
 cmtfunc
  ("The detailed view of a " ++ entityName ++ " entity in HTML format.\n" ++
   if null (manyToOneEntities ++ manyToManyEntities) then "" else
   "It also takes associated entities for every associated entity type.")
  (thisModuleName erdname, lowerFirst entityName ++ "ToDetailsView")
  2
  Public
  -- function type
  (withHTMLContext $
     foldr CFuncType (listType htmlTVar)
           ([baseType (erdname, entityName)] ++
            (map ctvar manyToOneEntities) ++ -- defaults for n:1
            (map (\name -> listType (ctvar name)) manyToManyEntities)
           )
  )
  [CRule
    ( -- parameters
      [CPVar evar] ++
       map (\ (name, varId) -> CPVar (varId,"related"++name))
            (zip manyToOneEntities [2..]) ++
       map (\ (name, varId) -> CPVar (varId, lowerFirst name ++ "s"))
           (zip manyToManyEntities [(length manyToOneEntities + 2)..])
    )
    (CSimpleRhs
       (list2ac [
          applyF (spiceyModule, "spTable") [
            applyF (pre "map")
            [
              CLambda [tuplePattern [CPVar(2, "label"), CPVar(3, "value")]]
                      (list2ac [cvar "label", cvar "value"]),
              applyF (pre "zip")
                     [constF (thisModuleName erdname, eName++"LabelList"),
                      CVar (2,"detailedView")]
            ]
          ]
        ]
      )
    [CLocalPat (CPVar (2,"detailedView"))
       (CSimpleRhs
         (list2ac
            (map (\a -> list2ac [
                          applyF (attributeToConverter a)
                                 [applyF (erdname, eName ++ attributeName a)
                                         [CVar evar]]])
                 attrlist ++
             map (\ (name,varId) ->
                    list2ac [applyF (html "htxt")
                                    [applyF (thisModuleName erdname,
                                             lowerFirst name ++ "ToShortView")
                                            [CVar (varId,"related"++name)]]])
                 (zip manyToOneEntities [2..]) ++
             map (\ (name,varId) ->
                    list2ac
                      [applyF (html "htxt")
                              [applyF (pre "unwords")
                                [applyF (pre "map")
                                  [applyF (thisModuleName erdname,
                                           lowerFirst name ++"ToShortView") [],
                                   CVar (varId,lowerFirst name++"s")]]]])
                 (zip manyToManyEntities [(length manyToOneEntities + 2)..])
            )
        )
        [])
    ])
  ]

labelList :: ToHtmlGenerator
labelList erdname (Entity entityName attrlist) relationships allEntities =
  let
    manyToManyEntities = manyToMany allEntities (Entity entityName attrlist)
    manyToOneEntities = manyToOne (Entity entityName attrlist) relationships
  in
   cmtfunc
    ("The labels of a "++entityName++" entity, as used in HTML tables.")
    (thisModuleName erdname, (lowerFirst entityName)++"LabelList") 2 Public
    (withHTMLContext (listType (listType htmlTVar)))
    [simpleRule []
      (list2ac (
            (map (\ (Attribute name domain _ _) ->
                   list2ac
                     [applyF (html "textstyle")
                             [string2ac ("spicey_label spicey_label_for_type_"++
                                         domainToString domain),
                              string2ac name]])
                 attrlist) ++
            (map (\s -> list2ac
                          [applyF (html "textstyle")
                            [string2ac "spicey_label spicey_label_for_type_relation",
                             string2ac s]])
                 (manyToOneEntities++manyToManyEntities))
          )
       )]

thisModuleName :: String -> String
thisModuleName erdname = entitiesToHtmlModule erdname

attributeToConverter :: Attribute -> QName
attributeToConverter (Attribute _ domain _ isnull) =
  (spiceyModule,
   if isnull && not (isStringDom domain)
     then "maybe" ++ upperFirst (domainToString domain) ++ "ToHtml"
     else domainToString domain ++ "ToHtml")

domainToString :: Domain -> String
domainToString domain =
  case domain of
    IntDom    _     -> "int"
    FloatDom  _     -> "float"
    CharDom   _     -> "char"
    StringDom _     -> "string"
    BoolDom   _     -> "bool"
    DateDom   _     -> "date"
    UserDefined _ _ -> "userDefined"
    KeyDom    _     -> "key"