]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/XML/Injuries.hs
Update TSN.XML.Injuries for the new typeclass hierarchy.
[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 -- | XML/Database representation of a team as they appear in the
67 -- injuries documents.
68 --
69 data InjuriesTeam =
70 InjuriesTeam {
71 db_team_name :: String,
72 db_team_league :: Maybe String }
73 deriving (Data, Eq, Show, Typeable)
74
75
76 -- | XML/Database representation of the injury listings.
77 --
78 data InjuriesListingXml =
79 InjuriesListingXml {
80 xml_team :: InjuriesTeam,
81 xml_teamno :: Maybe Int,
82 xml_injuries :: String,
83 xml_updated :: Maybe Bool }
84 deriving (Eq, Show)
85
86 -- | Database representation of a 'InjuriesListing'. It possesses a
87 -- foreign key to an 'Injuries' object so that we can easily delete
88 -- 'InjuriesListing's based on the parent message's time_stamp.
89 --
90 data InjuriesListing =
91 InjuriesListing {
92 db_injuries_id :: DefaultKey Injuries,
93 db_team :: InjuriesTeam,
94 db_teamno :: Maybe Int,
95 db_injuries :: String,
96 db_updated :: Maybe Bool }
97
98 instance ToDb InjuriesListingXml where
99 -- | The DB analogue of a 'InjuriesListingXml' is a 'InjuriesListing'
100 type Db InjuriesListingXml = InjuriesListing
101
102 instance FromXmlFk InjuriesListingXml where
103 -- | Our foreign key points to an 'Injuries'.
104 type Parent InjuriesListingXml = Injuries
105
106 -- | To convert between a 'InjuriesListingXml' and a
107 -- 'InjuriesListing', we simply append the foreign key.
108 from_xml_fk fk InjuriesListingXml{..} =
109 InjuriesListing {
110 db_injuries_id = fk,
111 db_team = xml_team,
112 db_teamno = xml_teamno,
113 db_injuries = xml_injuries,
114 db_updated = xml_updated }
115
116 instance XmlImportFk InjuriesListingXml
117
118 -- | XML representation of an injuriesxml \<message\>.
119 --
120 data Message =
121 Message {
122 xml_xml_file_id :: Int,
123 xml_heading :: String,
124 xml_category :: String,
125 xml_sport :: String,
126 xml_listings :: [InjuriesListingXml],
127 xml_time_stamp :: UTCTime }
128 deriving (Eq, Show)
129
130 -- | Database representation of a 'Message'. We really only care about
131 -- the time stamp.
132 --
133 data Injuries =
134 Injuries {
135 db_sport :: String,
136 db_time_stamp :: UTCTime }
137
138 instance ToDb Message where
139 -- | The database analogue of a 'Message' is an 'Injuries'.
140 type Db Message = Injuries
141
142 instance FromXml Message where
143 -- | To convert from XML to DB, we simply drop the fields we don't
144 -- care about.
145 --
146 from_xml Message{..} =
147 Injuries {
148 db_sport = xml_sport,
149 db_time_stamp = xml_time_stamp }
150
151 instance XmlImport Message
152
153
154 instance DbImport Message where
155 dbmigrate _ =
156 run_dbmigrate $ do
157 migrate (undefined :: Injuries)
158 migrate (undefined :: InjuriesListing)
159
160 -- | We import a 'Message' by inserting all of its 'listings'.
161 --
162 dbimport msg = do
163 msg_id <- insert_xml msg
164
165 -- Convert each XML listing to a DB one using the message id and
166 -- insert it (disregarding the result).
167 mapM_ (insert_xml_fk_ msg_id) (xml_listings msg)
168
169 return ImportSucceeded
170
171
172 mkPersist tsn_codegen_config [groundhog|
173 - entity: Injuries
174
175 - entity: InjuriesListing
176 dbName: injuries_listings
177 constructors:
178 - name: InjuriesListing
179 fields:
180 - name: db_team
181 embeddedType:
182 - {name: team_name, dbName: team_name}
183 - {name: team_league, dbName: team_league}
184 - name: db_injuries_id
185 reference:
186 onDelete: cascade
187
188 - embedded: InjuriesTeam
189 fields:
190 - name: db_team_name
191 - name: db_team_league
192 |]
193
194
195 -- | A pickler for 'InjuriesTeam's that can convert them to/from XML.
196 --
197 pickle_injuries_team :: PU InjuriesTeam
198 pickle_injuries_team =
199 xpElem "team" $
200 xpWrap (from_tuple, to_tuple) $
201 xpPair xpText (xpAttrImplied "league" xpText)
202 where
203 from_tuple = uncurryN InjuriesTeam
204 to_tuple m = (db_team_name m, db_team_league m)
205
206
207 -- | A pickler for 'InjuriesListingXml's that can convert them to/from XML.
208 --
209 pickle_listing :: PU InjuriesListingXml
210 pickle_listing =
211 xpElem "listing" $
212 xpWrap (from_tuple, to_tuple) $
213 xp4Tuple pickle_injuries_team
214 (xpOption $ xpElem "teamno" xpInt)
215 (xpElem "injuries" xpText)
216 (xpOption $ xpElem "updated" xpPrim)
217 where
218 from_tuple = uncurryN InjuriesListingXml
219 to_tuple l = (xml_team l, xml_teamno l, xml_injuries l, xml_updated l)
220
221
222 -- | A pickler for 'Message's that can convert them to/from XML.
223 --
224 pickle_message :: PU Message
225 pickle_message =
226 xpElem "message" $
227 xpWrap (from_tuple, to_tuple) $
228 xp6Tuple (xpElem "XML_File_ID" xpInt)
229 (xpElem "heading" xpText)
230 (xpElem "category" xpText)
231 (xpElem "sport" xpText)
232 (xpList pickle_listing)
233 (xpElem "time_stamp" xp_time_stamp)
234 where
235 from_tuple = uncurryN Message
236 to_tuple m = (xml_xml_file_id m,
237 xml_heading m,
238 xml_category m,
239 xml_sport m,
240 xml_listings m,
241 xml_time_stamp m)
242
243
244 --
245 -- Tasty Tests
246 --
247
248 -- | A list of all tests for this module.
249 --
250 injuries_tests :: TestTree
251 injuries_tests =
252 testGroup
253 "Injuries tests"
254 [ test_pickle_of_unpickle_is_identity,
255 test_unpickle_succeeds ]
256
257
258 -- | If we unpickle something and then pickle it, we should wind up
259 -- with the same thing we started with. WARNING: success of this
260 -- test does not mean that unpickling succeeded.
261 --
262 test_pickle_of_unpickle_is_identity :: TestTree
263 test_pickle_of_unpickle_is_identity =
264 testCase "pickle composed with unpickle is the identity" $ do
265 let path = "test/xml/injuriesxml.xml"
266 (expected, actual) <- pickle_unpickle pickle_message path
267 actual @?= expected
268
269
270 -- | Make sure we can actually unpickle these things.
271 --
272 test_unpickle_succeeds :: TestTree
273 test_unpickle_succeeds =
274 testCase "unpickling succeeds" $ do
275 let path = "test/xml/injuriesxml.xml"
276 actual <- unpickleable path pickle_message
277 let expected = True
278 actual @?= expected