1 {-# LANGUAGE DeriveGeneric #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
6 {-# LANGUAGE TemplateHaskell #-}
7 {-# LANGUAGE TypeFamilies #-}
9 -- | Parse TSN XML for the DTD \"Schedule_Changes_XML.dtd\". Each
10 -- \<message\> element contains zero or more \<Schedule_Change\>
11 -- which are just a wrapper around zero or more \<SC_Listing\>s.
13 -- The teams appear to use the shared "TSN.Team" representation.
15 module TSN.XML.ScheduleChanges (
19 schedule_changes_tests,
20 -- * WARNING: these are private but exported to silence warnings
21 ScheduleChangesConstructor(..),
22 ScheduleChangesListingConstructor(..) )
26 import Control.Monad ( forM_ )
27 import Data.Time ( UTCTime(..) )
28 import Data.Tuple.Curry ( uncurryN )
29 import Database.Groundhog (
35 silentMigrationLogger )
36 import Database.Groundhog.Core ( DefaultKey )
37 import Database.Groundhog.Generic ( runDbConn )
38 import Database.Groundhog.Sqlite ( withSqliteConn )
39 import Database.Groundhog.TH (
42 import qualified GHC.Generics as GHC ( Generic )
43 import Test.Tasty ( TestTree, testGroup )
44 import Test.Tasty.HUnit ( (@?=), testCase )
45 import Text.XML.HXT.Core (
59 import Generics ( Generic(..), to_tuple )
60 import TSN.Codegen ( tsn_codegen_config )
61 import TSN.Database ( insert_or_select )
62 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
63 import TSN.Picklers ( xp_date_padded, xp_tba_time, xp_time_stamp )
64 import TSN.Team ( Team(..), HTeam(..), VTeam(..) )
65 import TSN.XmlImport ( XmlImport(..) )
75 -- | The DTD to which this module corresponds. Used to invoke
79 dtd = "Schedule_Changes_XML.dtd"
86 -- * ScheduleChanges/Message
88 -- | Database representation of a 'Message'. Comparatively, it lacks
89 -- the listings since they are linked via a foreign key.
91 data ScheduleChanges =
93 db_xml_file_id :: Int,
95 db_category :: String,
97 db_time_stamp :: UTCTime }
101 -- | XML representation of a \<Schedule_Change\> within a
102 -- \<message\>. These are wrappers around a bunch of
103 -- \<SC_Listing\>s, but they also contain the sport name for all of
104 -- the contained listings.
106 data ScheduleChangeXml =
108 xml_sc_sport :: String,
109 xml_sc_listings :: [ScheduleChangesListingXml] }
110 deriving (Eq, GHC.Generic, Show)
112 -- | For 'Generics.to_tuple'.
114 instance Generic ScheduleChangeXml
117 -- | XML representation of a 'ScheduleChanges'. It has the same
118 -- fields, but in addition contains the 'xml_listings'.
122 xml_xml_file_id :: Int,
123 xml_heading :: String,
124 xml_category :: String,
126 xml_schedule_changes :: [ScheduleChangeXml],
127 xml_time_stamp :: UTCTime }
128 deriving (Eq, GHC.Generic, Show)
131 -- | For 'Generics.to_tuple'.
133 instance Generic Message
136 instance ToDb Message where
137 -- | The database analogue of a 'Message' is a 'ScheduleChanges'.
139 type Db Message = ScheduleChanges
142 -- | The 'FromXml' instance for 'Message' is required for the
143 -- 'XmlImport' instance.
145 instance FromXml Message where
146 -- | To convert a 'Message' to an 'ScheduleChanges', we just drop
147 -- the 'xml_schedule_changes'.
149 from_xml Message{..} =
151 db_xml_file_id = xml_xml_file_id,
152 db_heading = xml_heading,
153 db_category = xml_category,
154 db_sport = xml_sport,
155 db_time_stamp = xml_time_stamp }
158 -- | This allows us to insert the XML representation 'Message'
161 instance XmlImport Message
165 -- * ScheduleChangesListing/ScheduleChangesListingXml
167 -- | An embedded type within 'ScheduleChangesListing'. These look
168 -- like, \<status numeral=\"4\"\>FINAL\</status\> within the XML,
169 -- but they're in one-to-one correspondence with the listings.
171 data ScheduleChangesListingStatus =
172 ScheduleChangesListingStatus {
173 db_status_numeral :: Int,
174 db_status :: Maybe String } -- Yes, they can be empty.
179 -- | Database representation of a \<SC_Listing\> contained within a
180 -- \<Schedule_Change\>, within a \<message\>. During the transition
181 -- to the database, we drop the intermediate \<Schedule_Change\>
182 -- leaving the listing keyed to the 'ScheduleChanges' itself.
184 -- The home/away teams reuse the 'Team' representation.
186 -- The sport name (sc_sport) is pulled out of the containing
187 -- \<Schedule_Change\> and embedded into the listings themselves.
189 data ScheduleChangesListing =
190 ScheduleChangesListing {
191 db_schedule_changes_id :: DefaultKey ScheduleChanges,
192 db_away_team_id :: DefaultKey Team,
193 db_home_team_id ::DefaultKey Team,
195 db_sc_sport :: String,
196 db_schedule_id :: Int,
197 db_game_time :: UTCTime,
198 db_location :: Maybe String,
201 db_listing_status :: ScheduleChangesListingStatus,
202 db_notes :: Maybe String }
205 -- | XML representation of a \<SC_Listing\> contained within a
206 -- \<Schedule_Change\>, within a \<message\>.
208 data ScheduleChangesListingXml =
209 ScheduleChangesListingXml {
211 xml_schedule_id :: Int,
212 xml_game_date :: UTCTime,
213 xml_game_time :: Maybe UTCTime,
214 xml_location :: Maybe String,
215 xml_away_team :: VTeam,
216 xml_home_team :: HTeam,
219 xml_listing_status :: ScheduleChangesListingStatus,
220 xml_notes :: Maybe String }
221 deriving (Eq, GHC.Generic, Show)
224 -- | For 'Generics.to_tuple'.
226 instance Generic ScheduleChangesListingXml
229 instance ToDb ScheduleChangesListingXml where
230 -- | The database analogue of an 'ScheduleChangesListingXml' is
231 -- an 'ScheduleChangesListing'.
233 type Db ScheduleChangesListingXml = ScheduleChangesListing
237 -- | We don't make 'ScheduleChangesListingXml' an instance of
238 -- 'FromXmlFkTeams' because it needs some additional information,
239 -- namely the sport name from its containing \<Schedule_Change\>.
240 -- But essentially we'll need to do the same thing as
241 -- 'from_xml_fk_teams'. This function accomplishes the same thing,
242 -- with the addition of the sport that's passed in.
244 -- The parameter order is for convenience later (see dbimport).
246 from_xml_fk_sport :: (DefaultKey ScheduleChanges)
247 -> String -- ^ The sport from our containing schedule change
248 -> (DefaultKey Team) -- ^ Away team FK
249 -> (DefaultKey Team) -- ^ Home team FK
250 -> ScheduleChangesListingXml
251 -> ScheduleChangesListing
252 from_xml_fk_sport fk sport fk_away fk_home ScheduleChangesListingXml{..} =
253 ScheduleChangesListing {
254 db_schedule_changes_id = fk,
255 db_away_team_id = fk_away,
256 db_home_team_id = fk_home,
259 db_schedule_id = xml_schedule_id,
260 db_game_time = make_game_time xml_game_date xml_game_time,
261 db_location = xml_location,
262 db_vscore = xml_vscore,
263 db_hscore = xml_hscore,
264 db_listing_status = xml_listing_status,
265 db_notes = xml_notes }
267 -- | Make the database \"game time\" from the XML
268 -- date/time. Simply take the day part from one and the time
271 make_game_time d Nothing = d
272 make_game_time d (Just t) = UTCTime (utctDay d) (utctDayTime t)
280 instance DbImport Message where
283 migrate (undefined :: Team)
284 migrate (undefined :: ScheduleChanges)
285 migrate (undefined :: ScheduleChangesListing)
288 -- Insert the top-level message
289 msg_id <- insert_xml m
291 -- Now loop through the message's schedule changes
292 forM_ (xml_schedule_changes m) $ \sc -> do
293 -- Construct the function that will turn an XML listing into a DB one.
294 -- This is only partially applied without the away/home team IDs.
295 let listing_xml_to_db = from_xml_fk_sport msg_id (xml_sc_sport sc)
297 -- Now loop through the listings so that we can handle the teams
298 -- one listing at a time.
299 forM_ (xml_sc_listings sc) $ \listing -> do
300 away_team_id <- insert_or_select (vteam $ xml_away_team listing)
301 home_team_id <- insert_or_select (hteam $ xml_home_team listing)
303 -- Finish constructing the xml -> db function.
304 let listing_xml_to_db' = listing_xml_to_db away_team_id home_team_id
305 let db_listing = listing_xml_to_db' listing
309 return ImportSucceeded
312 mkPersist tsn_codegen_config [groundhog|
313 - entity: ScheduleChanges
314 dbName: schedule_changes
316 - name: ScheduleChanges
318 - name: unique_schedule_changes
320 # Prevent multiple imports of the same message.
321 fields: [db_xml_file_id]
323 # Note: we drop the "sc" prefix from the db_sc_sport field.
324 - entity: ScheduleChangesListing
325 dbName: schedule_changes_listings
327 - name: ScheduleChangesListing
329 - name: db_schedule_changes_id
332 - name: db_away_team_id
335 - name: db_home_team_id
340 - name: db_listing_status
342 - {name: status_numeral, dbName: status_numeral}
343 - {name: status, dbName: status}
345 - embedded: ScheduleChangesListingStatus
347 - name: db_status_numeral
348 dbName: status_numeral
360 -- | An (un)pickler for the \<Away_Team\> elements.
362 pickle_away_team :: PU VTeam
365 xpWrap (from_tuple, to_tuple') $
366 xpPair (xpAttr "AT_ID" xpText)
369 from_tuple (x,y) = VTeam (Team x Nothing y)
370 to_tuple' (VTeam t) = (team_id t, name t)
373 -- | An (un)pickler for the \<Away_Team\> elements.
375 pickle_home_team :: PU HTeam
378 xpWrap (from_tuple, to_tuple') $
379 xpPair (xpAttr "HT_ID" xpText)
382 from_tuple (x,y) = HTeam (Team x Nothing y)
383 to_tuple' (HTeam t) = (team_id t, name t)
386 -- | An (un)pickler for the \<status\> elements.
388 pickle_status :: PU ScheduleChangesListingStatus
391 xpWrap (from_tuple, to_tuple') $
392 xpPair (xpAttr "numeral" xpInt)
395 from_tuple = uncurry ScheduleChangesListingStatus
397 -- Avouid unused field warnings.
398 to_tuple' ScheduleChangesListingStatus{..} =
399 (db_status_numeral, db_status)
401 -- | An (un)pickler for the \<SC_Listing\> elements.
403 pickle_listing :: PU ScheduleChangesListingXml
405 xpElem "SC_Listing" $
406 xpWrap (from_tuple, to_tuple) $
407 xp11Tuple (xpAttr "type" xpText)
408 (xpElem "Schedule_ID" xpInt)
409 (xpElem "Game_Date" xp_date_padded)
410 (xpElem "Game_Time" xp_tba_time)
411 (xpElem "Location" (xpOption xpText))
414 (xpElem "vscore" xpInt)
415 (xpElem "hscore" xpInt)
417 (xpElem "notes" (xpOption xpText))
419 from_tuple = uncurryN ScheduleChangesListingXml
422 -- | An (un)pickler for the \<Schedule_Change\> elements.
424 pickle_schedule_change :: PU ScheduleChangeXml
425 pickle_schedule_change =
426 xpElem "Schedule_Change" $
427 xpWrap (from_tuple, to_tuple) $
428 xpPair (xpAttr "Sport" xpText)
429 (xpList pickle_listing)
431 from_tuple = uncurry ScheduleChangeXml
434 -- | Pickler for the top-level 'Message'.
436 pickle_message :: PU Message
439 xpWrap (from_tuple, to_tuple) $
440 xp6Tuple (xpElem "XML_File_ID" xpInt)
441 (xpElem "heading" xpText)
442 (xpElem "category" xpText)
443 (xpElem "sport" xpText)
444 (xpList pickle_schedule_change)
445 (xpElem "time_stamp" xp_time_stamp)
447 from_tuple = uncurryN Message
454 -- | A list of all tests for this module.
456 schedule_changes_tests :: TestTree
457 schedule_changes_tests =
459 "ScheduleChanges tests"
460 [ test_on_delete_cascade,
461 test_pickle_of_unpickle_is_identity,
462 test_unpickle_succeeds ]
464 -- | If we unpickle something and then pickle it, we should wind up
465 -- with the same thing we started with. WARNING: success of this
466 -- test does not mean that unpickling succeeded.
468 test_pickle_of_unpickle_is_identity :: TestTree
469 test_pickle_of_unpickle_is_identity =
470 testCase "pickle composed with unpickle is the identity" $ do
471 let path = "test/xml/Schedule_Changes_XML.xml"
472 (expected, actual) <- pickle_unpickle pickle_message path
477 -- | Make sure we can actually unpickle these things.
479 test_unpickle_succeeds :: TestTree
480 test_unpickle_succeeds =
481 testCase "unpickling succeeds" $ do
482 let path = "test/xml/Schedule_Changes_XML.xml"
483 actual <- unpickleable path pickle_message
490 -- | Make sure everything gets deleted when we delete the top-level
493 test_on_delete_cascade :: TestTree
494 test_on_delete_cascade =
495 testCase "deleting auto_racing_results deletes its children" $ do
496 let path = "test/xml/Schedule_Changes_XML.xml"
497 results <- unsafe_unpickle path pickle_message
498 let a = undefined :: Team
499 let b = undefined :: ScheduleChanges
500 let c = undefined :: ScheduleChangesListing
502 actual <- withSqliteConn ":memory:" $ runDbConn $ do
503 runMigration silentMigrationLogger $ do
507 _ <- dbimport results
509 count_a <- countAll a
510 count_b <- countAll b
511 count_c <- countAll c
512 return $ sum [count_a, count_b, count_c]
513 let expected = 12 -- There are 16 team elements, but 4 are dupes,
514 -- so 12 unique teams should be left over.