1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
6 {-# LANGUAGE StandaloneDeriving #-}
7 {-# LANGUAGE TemplateHaskell #-}
8 {-# LANGUAGE TypeFamilies #-}
10 -- | Parse TSN XML for the DTD "injuriesxml.dtd". Each document
11 -- contains a root element \<message\> that in turn contains zero or
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
19 module TSN.XML.Injuries (
23 -- * WARNING: these are private but exported to silence warnings
24 InjuriesConstructor(..),
25 InjuriesListingConstructor(..) )
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 (
37 import Data.Tuple.Curry ( uncurryN )
38 import Test.Tasty ( TestTree, testGroup )
39 import Test.Tasty.HUnit ( (@?=), testCase )
40 import Text.XML.HXT.Core (
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(..) )
73 -- | XML/Database representation of a team as they appear in the
74 -- injuries documents.
78 db_team_name :: String,
79 db_team_league :: Maybe String }
80 deriving (Data, Eq, Show, Typeable)
83 -- * InjuriesListing/InjuriesListingXml
85 -- | XML/Database representation of the injury listings.
87 data InjuriesListingXml =
89 xml_team :: InjuriesTeam,
90 xml_teamno :: Maybe Int,
91 xml_injuries :: String,
92 xml_updated :: Maybe Bool }
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.
99 data 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 }
107 instance ToDb InjuriesListingXml where
108 -- | The DB analogue of a 'InjuriesListingXml' is a 'InjuriesListing'
109 type Db InjuriesListingXml = InjuriesListing
111 instance FromXmlFk InjuriesListingXml where
112 -- | Our foreign key points to an 'Injuries'.
113 type Parent InjuriesListingXml = Injuries
115 -- | To convert between a 'InjuriesListingXml' and a
116 -- 'InjuriesListing', we simply append the foreign key.
117 from_xml_fk fk InjuriesListingXml{..} =
121 db_teamno = xml_teamno,
122 db_injuries = xml_injuries,
123 db_updated = xml_updated }
125 instance XmlImportFk InjuriesListingXml
128 -- * Injuries/Message
130 -- | XML representation of an injuriesxml \<message\>.
134 xml_xml_file_id :: Int,
135 xml_heading :: String,
136 xml_category :: String,
138 xml_listings :: [InjuriesListingXml],
139 xml_time_stamp :: UTCTime }
142 -- | Database representation of a 'Message'. We really only care about
147 db_xml_file_id :: Int,
149 db_time_stamp :: UTCTime }
151 instance ToDb Message where
152 -- | The database analogue of a 'Message' is an 'Injuries'.
153 type Db Message = Injuries
155 instance FromXml Message where
156 -- | To convert from XML to DB, we simply drop the fields we don't
159 from_xml Message{..} =
161 db_xml_file_id = xml_xml_file_id,
162 db_sport = xml_sport,
163 db_time_stamp = xml_time_stamp }
165 instance XmlImport Message
172 instance DbImport Message where
175 migrate (undefined :: Injuries)
176 migrate (undefined :: InjuriesListing)
178 -- | We import a 'Message' by inserting all of its 'listings'.
181 msg_id <- insert_xml msg
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)
187 return ImportSucceeded
190 mkPersist tsn_codegen_config [groundhog|
195 - name: unique_injuries
197 # Prevent multiple imports of the same message.
198 fields: [db_xml_file_id]
200 - entity: InjuriesListing
201 dbName: injuries_listings
203 - name: InjuriesListing
207 - {name: team_name, dbName: team_name}
208 - {name: team_league, dbName: team_league}
209 - name: db_injuries_id
213 - embedded: InjuriesTeam
216 - name: db_team_league
225 -- | A pickler for 'InjuriesTeam's that can convert them to/from XML.
227 pickle_injuries_team :: PU InjuriesTeam
228 pickle_injuries_team =
230 xpWrap (from_tuple, to_tuple) $
231 xpPair xpText (xpAttrImplied "league" xpText)
233 from_tuple = uncurryN InjuriesTeam
234 to_tuple m = (db_team_name m, db_team_league m)
237 -- | A pickler for 'InjuriesListingXml's that can convert them to/from
240 pickle_listing :: PU InjuriesListingXml
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)
249 from_tuple = uncurryN InjuriesListingXml
250 to_tuple l = (xml_team l, xml_teamno l, xml_injuries l, xml_updated l)
253 -- | A pickler for 'Message's that can convert them to/from XML.
255 pickle_message :: PU 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)
266 from_tuple = uncurryN Message
267 to_tuple m = (xml_xml_file_id m,
279 -- | A list of all tests for this module.
281 injuries_tests :: TestTree
285 [ test_pickle_of_unpickle_is_identity,
286 test_unpickle_succeeds ]
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.
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
301 -- | Make sure we can actually unpickle these things.
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