1 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE RecordWildCards #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
8 -- | GameInfo represents a collection of DTDs that we don't really
9 -- handle but want to make available. The raw XML gets stored in the
10 -- database along with the XML_File_ID, but we don't parse any of it.
12 -- See also: TSN.XML.SportInfo
14 module TSN.XML.GameInfo (
17 -- * WARNING: these are private but exported to silence warnings
18 GameInfoConstructor(..) )
22 import Data.Time.Clock ( UTCTime )
23 import Database.Groundhog ( migrate )
24 import Database.Groundhog.TH (
27 import Text.XML.HXT.Core ( XmlTree )
28 import Text.XML.HXT.DOM.ShowXml ( xshow )
31 import TSN.Codegen ( tsn_codegen_config )
32 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
36 parse_xml_time_stamp )
37 import TSN.XmlImport ( XmlImport(..) )
42 -- | The DTDs for everything that we consider "Game Info."
44 -- TODO: This is the list from the old implementation. We need to
45 -- make sure that we are really receiving XML for these DTDs
46 -- (i.e. the names are correct).
50 [ "CBASK_Lineup_XML.dtd", -- missing DTD
51 "cbaskpreviewxml.dtd", -- missing DTD
52 "cflpreviewxml.dtd", -- missing DTD
53 "Matchup_NBA_NHL_XML.dtd", -- missing DTD
55 "MLB_Gaming_Matchup_XML.dtd",
56 "MLB.dtd", -- missing DTD
58 "MLB_Matchup_XML.dtd",
59 "MLS_Preview_XML.dtd",
60 "NBA_Gaming_Matchup_XML.dtd",
61 "NBA.dtd", -- missing DTD
62 "NBA_Playoff_Matchup_XML.dtd",
65 "NCAA_FB_Preview_XML.dtd", -- missing DTD
66 "nflpreviewxml.dtd", -- missing DTD
67 "NFL_NCAA_FB_Matchup_XML.dtd", -- missing DTD
70 "WorldBaseballPreviewXML.dtd" -- missing DTD
74 -- | XML representation of a GameInfo \<message\>.
79 xml_xml_file_id :: Int,
80 xml_time_stamp :: UTCTime,
85 -- | Attempt to parse a 'Message' from an 'XmlTree'. If we cannot,
86 -- we fail with an error message.
88 parse_xml :: String -> XmlTree -> Either String Message
89 parse_xml dtdname xmltree = do
90 xmlfid <- parse_xmlfid xmltree
91 timestamp <- parse_xml_time_stamp xmltree
92 message <- parse_message xmltree
93 return $ Message dtdname (fromInteger xmlfid) timestamp (xshow [message])
96 -- | Database representation of a 'Message'.
101 db_xml_file_id :: Int,
102 db_time_stamp :: UTCTime,
106 instance ToDb Message where
107 -- | The database analogue of a 'Message' is an 'GameInfo'.
108 type Db Message = GameInfo
110 instance FromXml Message where
111 -- | The XML to DB conversion is trivial here.
113 from_xml Message{..} = GameInfo {
115 db_xml_file_id = xml_xml_file_id,
116 db_time_stamp = xml_time_stamp,
120 -- | This allows us to insert the XML representation 'Message'
123 instance XmlImport Message
130 instance DbImport Message where
132 run_dbmigrate $ migrate (undefined :: GameInfo)
134 -- | We import a 'Message' by inserting the whole thing at
135 -- once. Nothing fancy going on here.
138 return ImportSucceeded
140 mkPersist tsn_codegen_config [groundhog|
145 - name: unique_game_info
147 # Prevent multiple imports of the same message.
148 fields: [db_xml_file_id]