1 {-# LANGUAGE NoMonomorphismRestriction #-}
5 import Control.Arrow ( (&&&), arr, returnA )
6 import Control.Monad ( when )
7 import Control.Monad.IO.Class ( liftIO )
8 import Database.Groundhog (
9 defaultMigrationLogger,
13 import Database.Groundhog.Core ( PersistEntity )
14 import Database.Groundhog.Generic ( runDbConn )
15 import Database.Groundhog.Sqlite (
17 import Database.Groundhog.Postgresql (
19 import Data.Monoid ( (<>) )
20 import System.Console.CmdArgs ( def )
21 import System.Exit ( exitWith, ExitCode (ExitFailure) )
22 import System.IO.Error ( catchIOError )
23 import Text.XML.HXT.Core (
38 import Backend ( Backend(..) )
39 import CommandLine ( get_args )
40 import Configuration ( Configuration(..), merge_optional )
41 import ConnectionString ( ConnectionString(..) )
42 import ExitCodes ( exit_no_xml_files )
43 import Network.Services.TSN.Logging ( init_logging )
44 import qualified OptionalConfiguration as OC (
45 OptionalConfiguration ( xml_files ),
47 import Network.Services.TSN.Report (
50 import qualified TSN.Injuries as Injuries (
52 Message ( listings ) )
53 import qualified TSN.InjuriesDetail as InjuriesDetail (
54 Listing ( player_listings ),
57 import qualified TSN.News as News ( Message )
58 import Xml ( parse_opts )
61 -- | We put the 'Configuration' and 'XmlTree' arguments last so that
62 -- it's easy to eta reduce all of the import_foo functions that call
65 import_generic :: (XmlPickler a, PersistEntity b)
66 => b -- ^ Dummy Listing instance needed for 'migrate'
67 -> (a -> [b]) -- ^ listings getter
70 -> IO (Maybe Int) -- ^ Return the number of records inserted.
71 import_generic dummy g cfg xml
72 | backend cfg == Postgres = withPostgresqlConn cs go
73 | otherwise = withSqliteConn cs go
75 -- | Pull the real connection String out of the configuration.
77 cs = get_connection_string $ connection_string cfg
79 -- Needs NoMonomorphismRestriction to be allowed to return
80 -- different types in the two cases above.
82 runMigration defaultMigrationLogger $ migrate dummy
83 let root_element = unpickleDoc xpickle xml
86 let msg = "Could not unpickle document in import_generic."
87 liftIO $ report_error msg
90 ids <- mapM insert (g elt)
91 return $ Just (length ids)
95 -- | Import TSN.News from an 'XmlTree'.
96 import_news :: Configuration -> XmlTree -> IO (Maybe Int)
99 (undefined :: News.Message)
102 -- | Import TSN.Injuries from an 'XmlTree'.
103 import_injuries :: Configuration -> XmlTree -> IO (Maybe Int)
106 (undefined :: Injuries.Listing)
109 -- | Import TSN.InjuriesDetail from an 'XmlTree'.
110 import_injuries_detail :: Configuration -> XmlTree -> IO (Maybe Int)
111 import_injuries_detail =
113 (undefined :: InjuriesDetail.PlayerListing)
114 ( (concatMap InjuriesDetail.player_listings) . InjuriesDetail.listings)
116 import_file :: Configuration -> FilePath -> IO ()
117 import_file cfg path = do
118 results <- catchIOError
121 report_error (show e)
122 report_error $ "Failed to import file " ++ path ++ "."
123 -- Return a nonempty list so we don't claim incorrectly that
124 -- we couldn't parse the DTD.
128 -- If results' is empty, one of the arrows return "nothing."
129 [] -> report_error $ "Unable to determine DTD for file " ++ path ++ "."
133 Just cnt -> report_info $ "Successfully imported " ++
135 " records from " ++ path ++ "."
137 -- | An arrow that reads a document into an 'XmlTree'.
138 readA :: IOStateArrow s a XmlTree
139 readA = readDocument parse_opts path
141 -- | An arrow which parses the doctype "SYSTEM" of an 'XmlTree'.
142 -- We use these to determine the parser to use.
143 doctypeA :: ArrowXml a => a XmlTree String
144 doctypeA = getAttrl >>> hasName "doctype-SYSTEM" /> getText
146 -- | Combine the arrows above as well as the function below
147 -- (arrowized with 'arr') into an IO action that does everything
148 -- (parses and then runs the import on what was parsed).
150 -- The result of runX has type IO [IO (Maybe Int)]. We thus use
151 -- bind (>>=) and sequence to combine all of the IOs into one
152 -- big one outside of the list.
153 parse_and_import :: IO [Maybe Int]
155 runX (readA >>> (doctypeA &&& returnA) >>> (arr import_with_dtd))
159 -- | Takes a 'Doctype', 'XmlTree' pair and uses the 'Doctype' to
160 -- determine which function to call on the 'XmlTree'.
161 import_with_dtd :: (String, XmlTree) -> IO (Maybe Int)
162 import_with_dtd (dtd,xml)
163 | dtd == "injuriesxml.dtd" = import_injuries cfg xml
164 | dtd == "Injuries_Detail_XML.dtd" = import_injuries_detail cfg xml
165 | dtd == "newsxml.dtd" = import_news cfg xml
167 report_info $ "Unrecognized DTD in " ++ path ++ ": " ++ dtd ++ "."
175 -- Merge the config file options with the command-line ones,
176 -- prefering the command-line ones.
177 let opt_config = rc_cfg <> cmd_cfg
179 -- Update a default config with any options that have been set in
180 -- either the config file or on the command-line. We initialize
181 -- logging before the missing parameter checks below so that we can
183 let cfg = (def :: Configuration) `merge_optional` opt_config
184 init_logging (log_file cfg) (log_level cfg) (syslog cfg)
186 -- Check the optional config for missing required options.
187 when (null $ OC.xml_files opt_config) $ do
188 report_error "No XML files given."
189 exitWith (ExitFailure exit_no_xml_files)
191 -- We don't do this in parallel (for now?) to keep the error
192 -- messages nice and linear.
193 mapM_ (import_file cfg) (OC.xml_files opt_config)