]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/XML/Injuries.hs
eb369e03eb8d1ecae646a383a4d0a95e9e2b8659
[dead/htsn-import.git] / src / TSN / XML / Injuries.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE GADTs #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
6 {-# LANGUAGE StandaloneDeriving #-}
7 {-# LANGUAGE TemplateHaskell #-}
8 {-# LANGUAGE TypeFamilies #-}
9
10 -- | Parse TSN XML for the DTD "injuriesxml.dtd". Each document
11 -- contains a root element \<message\> that in turn contains zero or
12 -- more \<listing\>s.
13 --
14 -- The listings will be mapped to a database table called
15 -- \"injuries_listings\" automatically. The root message is retained
16 -- so that we can easily delete its associated listings based on its
17 -- time_stamp.
18 --
19 module TSN.XML.Injuries (
20 pickle_message,
21 -- * Tests
22 injuries_tests,
23 -- * WARNING: these are private but exported to silence warnings
24 InjuriesConstructor(..),
25 InjuriesListingConstructor(..) )
26 where
27
28 -- System imports.
29 import Data.Data ( Data )
30 import Data.Time ( UTCTime )
31 import Data.Typeable ( Typeable )
32 import Database.Groundhog ( migrate )
33 import Database.Groundhog.Core ( DefaultKey )
34 import Database.Groundhog.TH (
35 groundhog,
36 mkPersist )
37 import Data.Tuple.Curry ( uncurryN )
38 import Test.Tasty ( TestTree, testGroup )
39 import Test.Tasty.HUnit ( (@?=), testCase )
40 import Text.XML.HXT.Core (
41 PU,
42 xp4Tuple,
43 xp6Tuple,
44 xpAttrImplied,
45 xpElem,
46 xpInt,
47 xpList,
48 xpOption,
49 xpPair,
50 xpPrim,
51 xpText,
52 xpWrap )
53
54 -- Local imports.
55 import TSN.Codegen ( tsn_codegen_config )
56 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
57 import TSN.Picklers ( xp_time_stamp )
58 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
59 import Xml (
60 FromXml(..),
61 FromXmlFk(..),
62 ToDb(..),
63 pickle_unpickle,
64 unpickleable )
65
66
67 --
68 -- DB/XML Data types
69 --
70
71 -- * InjuriesTeam
72
73 -- | XML/Database representation of a team as they appear in the
74 -- injuries documents.
75 --
76 data InjuriesTeam =
77 InjuriesTeam {
78 db_team_name :: String,
79 db_team_league :: Maybe String }
80 deriving (Data, Eq, Show, Typeable)
81
82
83 -- * InjuriesListing/InjuriesListingXml
84
85 -- | XML representation of the injury listings.
86 --
87 data InjuriesListingXml =
88 InjuriesListingXml {
89 xml_team :: InjuriesTeam,
90 xml_teamno :: Maybe Int,
91 xml_injuries :: String,
92 xml_updated :: Maybe Bool }
93 deriving (Eq, Show)
94
95 -- | Database representation of a 'InjuriesListing'. It possesses a
96 -- foreign key to an 'Injuries' object so that we can easily delete
97 -- 'InjuriesListing's based on the parent message's time_stamp.
98 --
99 data InjuriesListing =
100 InjuriesListing {
101 db_injuries_id :: DefaultKey Injuries,
102 db_team :: InjuriesTeam,
103 db_teamno :: Maybe Int,
104 db_injuries :: String,
105 db_updated :: Maybe Bool }
106
107 instance ToDb InjuriesListingXml where
108 -- | The DB analogue of a 'InjuriesListingXml' is a 'InjuriesListing'
109 type Db InjuriesListingXml = InjuriesListing
110
111 instance FromXmlFk InjuriesListingXml where
112 -- | Our foreign key points to an 'Injuries'.
113 type Parent InjuriesListingXml = Injuries
114
115 -- | To convert between a 'InjuriesListingXml' and a
116 -- 'InjuriesListing', we simply append the foreign key.
117 from_xml_fk fk InjuriesListingXml{..} =
118 InjuriesListing {
119 db_injuries_id = fk,
120 db_team = xml_team,
121 db_teamno = xml_teamno,
122 db_injuries = xml_injuries,
123 db_updated = xml_updated }
124
125 -- | This allows us to insert the XML representation
126 -- 'InjuriesListingXml' directly.
127 --
128 instance XmlImportFk InjuriesListingXml
129
130
131 -- * Injuries/Message
132
133 -- | XML representation of an injuriesxml \<message\>.
134 --
135 data Message =
136 Message {
137 xml_xml_file_id :: Int,
138 xml_heading :: String,
139 xml_category :: String,
140 xml_sport :: String,
141 xml_listings :: [InjuriesListingXml],
142 xml_time_stamp :: UTCTime }
143 deriving (Eq, Show)
144
145 -- | Database representation of a 'Message'.
146 --
147 data Injuries =
148 Injuries {
149 db_xml_file_id :: Int,
150 db_sport :: String,
151 db_time_stamp :: UTCTime }
152
153 instance ToDb Message where
154 -- | The database analogue of a 'Message' is an 'Injuries'.
155 type Db Message = Injuries
156
157 instance FromXml Message where
158 -- | To convert from XML to DB, we simply drop the fields we don't
159 -- care about.
160 --
161 from_xml Message{..} =
162 Injuries {
163 db_xml_file_id = xml_xml_file_id,
164 db_sport = xml_sport,
165 db_time_stamp = xml_time_stamp }
166
167 -- | This allows us to insert the XML representation 'Message'
168 -- directly.
169 --
170 instance XmlImport Message
171
172
173 --
174 -- Database code
175 --
176
177 instance DbImport Message where
178 dbmigrate _ =
179 run_dbmigrate $ do
180 migrate (undefined :: Injuries)
181 migrate (undefined :: InjuriesListing)
182
183 -- | We import a 'Message' by inserting all of its 'listings', but
184 -- the listings require a foreign key to the parent 'Message'.
185 --
186 dbimport msg = do
187 msg_id <- insert_xml msg
188
189 -- Convert each XML listing to a DB one using the message id and
190 -- insert it (disregarding the result).
191 mapM_ (insert_xml_fk_ msg_id) (xml_listings msg)
192
193 return ImportSucceeded
194
195
196 mkPersist tsn_codegen_config [groundhog|
197 - entity: Injuries
198 constructors:
199 - name: Injuries
200 uniques:
201 - name: unique_injuries
202 type: constraint
203 # Prevent multiple imports of the same message.
204 fields: [db_xml_file_id]
205
206 - entity: InjuriesListing
207 dbName: injuries_listings
208 constructors:
209 - name: InjuriesListing
210 fields:
211 - name: db_team
212 embeddedType:
213 - {name: team_name, dbName: team_name}
214 - {name: team_league, dbName: team_league}
215 - name: db_injuries_id
216 reference:
217 onDelete: cascade
218
219 - embedded: InjuriesTeam
220 fields:
221 - name: db_team_name
222 - name: db_team_league
223 |]
224
225
226 --
227 -- XML Picklers
228 --
229
230
231 -- | A pickler for 'InjuriesTeam's that can convert them to/from XML.
232 --
233 pickle_injuries_team :: PU InjuriesTeam
234 pickle_injuries_team =
235 xpElem "team" $
236 xpWrap (from_tuple, to_tuple) $
237 xpPair xpText (xpAttrImplied "league" xpText)
238 where
239 from_tuple = uncurryN InjuriesTeam
240 to_tuple m = (db_team_name m, db_team_league m)
241
242
243 -- | A pickler for 'InjuriesListingXml's that can convert them to/from
244 -- XML.
245 --
246 pickle_listing :: PU InjuriesListingXml
247 pickle_listing =
248 xpElem "listing" $
249 xpWrap (from_tuple, to_tuple) $
250 xp4Tuple pickle_injuries_team
251 (xpOption $ xpElem "teamno" xpInt)
252 (xpElem "injuries" xpText)
253 (xpOption $ xpElem "updated" xpPrim)
254 where
255 from_tuple = uncurryN InjuriesListingXml
256 to_tuple l = (xml_team l, xml_teamno l, xml_injuries l, xml_updated l)
257
258
259 -- | A pickler for 'Message's that can convert them to/from XML.
260 --
261 pickle_message :: PU Message
262 pickle_message =
263 xpElem "message" $
264 xpWrap (from_tuple, to_tuple) $
265 xp6Tuple (xpElem "XML_File_ID" xpInt)
266 (xpElem "heading" xpText)
267 (xpElem "category" xpText)
268 (xpElem "sport" xpText)
269 (xpList pickle_listing)
270 (xpElem "time_stamp" xp_time_stamp)
271 where
272 from_tuple = uncurryN Message
273 to_tuple m = (xml_xml_file_id m,
274 xml_heading m,
275 xml_category m,
276 xml_sport m,
277 xml_listings m,
278 xml_time_stamp m)
279
280
281 --
282 -- Tasty Tests
283 --
284
285 -- | A list of all tests for this module.
286 --
287 injuries_tests :: TestTree
288 injuries_tests =
289 testGroup
290 "Injuries tests"
291 [ test_pickle_of_unpickle_is_identity,
292 test_unpickle_succeeds ]
293
294
295 -- | If we unpickle something and then pickle it, we should wind up
296 -- with the same thing we started with. WARNING: success of this
297 -- test does not mean that unpickling succeeded.
298 --
299 test_pickle_of_unpickle_is_identity :: TestTree
300 test_pickle_of_unpickle_is_identity =
301 testCase "pickle composed with unpickle is the identity" $ do
302 let path = "test/xml/injuriesxml.xml"
303 (expected, actual) <- pickle_unpickle pickle_message path
304 actual @?= expected
305
306
307 -- | Make sure we can actually unpickle these things.
308 --
309 test_unpickle_succeeds :: TestTree
310 test_unpickle_succeeds =
311 testCase "unpickling succeeds" $ do
312 let path = "test/xml/injuriesxml.xml"
313 actual <- unpickleable path pickle_message
314 let expected = True
315 actual @?= expected