]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/XML/Injuries.hs
6cc802f9752d2dcbc13d59e3415cd38f7d9fb963
[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/Database 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 instance XmlImportFk InjuriesListingXml
126
127
128 -- * Injuries/Message
129
130 -- | XML representation of an injuriesxml \<message\>.
131 --
132 data Message =
133 Message {
134 xml_xml_file_id :: Int,
135 xml_heading :: String,
136 xml_category :: String,
137 xml_sport :: String,
138 xml_listings :: [InjuriesListingXml],
139 xml_time_stamp :: UTCTime }
140 deriving (Eq, Show)
141
142 -- | Database representation of a 'Message'. We really only care about
143 -- the time stamp.
144 --
145 data Injuries =
146 Injuries {
147 db_xml_file_id :: Int,
148 db_sport :: String,
149 db_time_stamp :: UTCTime }
150
151 instance ToDb Message where
152 -- | The database analogue of a 'Message' is an 'Injuries'.
153 type Db Message = Injuries
154
155 instance FromXml Message where
156 -- | To convert from XML to DB, we simply drop the fields we don't
157 -- care about.
158 --
159 from_xml Message{..} =
160 Injuries {
161 db_xml_file_id = xml_xml_file_id,
162 db_sport = xml_sport,
163 db_time_stamp = xml_time_stamp }
164
165 instance XmlImport Message
166
167
168 --
169 -- Database code
170 --
171
172 instance DbImport Message where
173 dbmigrate _ =
174 run_dbmigrate $ do
175 migrate (undefined :: Injuries)
176 migrate (undefined :: InjuriesListing)
177
178 -- | We import a 'Message' by inserting all of its 'listings'.
179 --
180 dbimport msg = do
181 msg_id <- insert_xml msg
182
183 -- Convert each XML listing to a DB one using the message id and
184 -- insert it (disregarding the result).
185 mapM_ (insert_xml_fk_ msg_id) (xml_listings msg)
186
187 return ImportSucceeded
188
189
190 mkPersist tsn_codegen_config [groundhog|
191 - entity: Injuries
192 constructors:
193 - name: Injuries
194 uniques:
195 - name: unique_injuries
196 type: constraint
197 # Prevent multiple imports of the same message.
198 fields: [db_xml_file_id]
199
200 - entity: InjuriesListing
201 dbName: injuries_listings
202 constructors:
203 - name: InjuriesListing
204 fields:
205 - name: db_team
206 embeddedType:
207 - {name: team_name, dbName: team_name}
208 - {name: team_league, dbName: team_league}
209 - name: db_injuries_id
210 reference:
211 onDelete: cascade
212
213 - embedded: InjuriesTeam
214 fields:
215 - name: db_team_name
216 - name: db_team_league
217 |]
218
219
220 --
221 -- XML Picklers
222 --
223
224
225 -- | A pickler for 'InjuriesTeam's that can convert them to/from XML.
226 --
227 pickle_injuries_team :: PU InjuriesTeam
228 pickle_injuries_team =
229 xpElem "team" $
230 xpWrap (from_tuple, to_tuple) $
231 xpPair xpText (xpAttrImplied "league" xpText)
232 where
233 from_tuple = uncurryN InjuriesTeam
234 to_tuple m = (db_team_name m, db_team_league m)
235
236
237 -- | A pickler for 'InjuriesListingXml's that can convert them to/from
238 -- XML.
239 --
240 pickle_listing :: PU InjuriesListingXml
241 pickle_listing =
242 xpElem "listing" $
243 xpWrap (from_tuple, to_tuple) $
244 xp4Tuple pickle_injuries_team
245 (xpOption $ xpElem "teamno" xpInt)
246 (xpElem "injuries" xpText)
247 (xpOption $ xpElem "updated" xpPrim)
248 where
249 from_tuple = uncurryN InjuriesListingXml
250 to_tuple l = (xml_team l, xml_teamno l, xml_injuries l, xml_updated l)
251
252
253 -- | A pickler for 'Message's that can convert them to/from XML.
254 --
255 pickle_message :: PU Message
256 pickle_message =
257 xpElem "message" $
258 xpWrap (from_tuple, to_tuple) $
259 xp6Tuple (xpElem "XML_File_ID" xpInt)
260 (xpElem "heading" xpText)
261 (xpElem "category" xpText)
262 (xpElem "sport" xpText)
263 (xpList pickle_listing)
264 (xpElem "time_stamp" xp_time_stamp)
265 where
266 from_tuple = uncurryN Message
267 to_tuple m = (xml_xml_file_id m,
268 xml_heading m,
269 xml_category m,
270 xml_sport m,
271 xml_listings m,
272 xml_time_stamp m)
273
274
275 --
276 -- Tasty Tests
277 --
278
279 -- | A list of all tests for this module.
280 --
281 injuries_tests :: TestTree
282 injuries_tests =
283 testGroup
284 "Injuries tests"
285 [ test_pickle_of_unpickle_is_identity,
286 test_unpickle_succeeds ]
287
288
289 -- | If we unpickle something and then pickle it, we should wind up
290 -- with the same thing we started with. WARNING: success of this
291 -- test does not mean that unpickling succeeded.
292 --
293 test_pickle_of_unpickle_is_identity :: TestTree
294 test_pickle_of_unpickle_is_identity =
295 testCase "pickle composed with unpickle is the identity" $ do
296 let path = "test/xml/injuriesxml.xml"
297 (expected, actual) <- pickle_unpickle pickle_message path
298 actual @?= expected
299
300
301 -- | Make sure we can actually unpickle these things.
302 --
303 test_unpickle_succeeds :: TestTree
304 test_unpickle_succeeds =
305 testCase "unpickling succeeds" $ do
306 let path = "test/xml/injuriesxml.xml"
307 actual <- unpickleable path pickle_message
308 let expected = True
309 actual @?= expected