]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/TSN/Injuries.hs
Comment out the DTD in Injuries_Detail_XML.xml.
[dead/htsn-import.git] / src / TSN / Injuries.hs
index 956aa9d896e0ad8a12fc1f19a843c4f15526c88e..4809786b2906523ff06850e2e20b45a55c9fffb5 100644 (file)
@@ -1,22 +1,24 @@
-{-# LANGUAGE EmptyDataDecls #-}
-{-# LANGUAGE FlexibleContexts #-}
 {-# LANGUAGE FlexibleInstances #-}
 {-# LANGUAGE GADTs #-}
-{-# LANGUAGE MultiParamTypeClasses #-}
-{-# LANGUAGE OverloadedStrings #-}
 {-# LANGUAGE QuasiQuotes #-}
+{-# LANGUAGE StandaloneDeriving #-}
 {-# LANGUAGE TemplateHaskell #-}
 {-# LANGUAGE TypeFamilies #-}
-module TSN.Injuries
+
+-- | Parse TSN XML for the DTD "injuriesxml.dtd". Each document
+--   contains a root element \<message\> that in turn contains zero or
+--   more \<listing\>s.
+--
+--   The listings will be mapped to a database table called "injuries"
+--   automatically. The root message is not retained.
+--
+module TSN.Injuries (
+  Message )
 where
 
-import Database.Persist.TH (
-  mkDeleteCascade,
-  mkMigrate,
-  mkPersist,
-  persistLowerCase,
-  share,
-  sqlOnlySettings )
+import Data.Tuple.Curry ( uncurryN )
+import Database.Groundhog()
+import Database.Groundhog.TH
 import Text.XML.HXT.Core (
   PU,
   XmlPickler(..),
@@ -28,62 +30,67 @@ import Text.XML.HXT.Core (
   xpText,
   xpWrap )
 
-import Uncurry (uncurry4, uncurry5, uncurry6)
 
-share [mkPersist sqlOnlySettings,
-       mkDeleteCascade sqlOnlySettings,
-       mkMigrate "migrate_injuries"] [persistLowerCase|
-InjuriesListing
-  team String
-  teamno Int
-  injuries String
-  updated Bool
-  deriving Show
 
-InjuriesMessage
-  xml_file_id Int
-  heading String
-  category String
-  sport String
-  listings [InjuriesListing]
-  time_stamp String
-  deriving Show
+data Listing =
+  Listing {
+    team :: String,
+    teamno :: Int,
+    injuries :: String,
+    updated :: Bool }
+  deriving (Show)
+
+data Message =
+  Message {
+    xml_file_id :: Int,
+    heading :: String,
+    category :: String,
+    sport :: String,
+    listings :: [Listing],
+    time_stamp :: String }
+  deriving (Show)
+
+
+mkPersist defaultCodegenConfig [groundhog|
+- entity: Listing
+  dbName: injuries
 |]
 
 
-pickle_injurieslisting :: PU InjuriesListing
-pickle_injurieslisting =
+pickle_listing :: PU Listing
+pickle_listing =
   xpElem "listing" $
-    xpWrap (uncurry4 InjuriesListing,
-            \l -> (injuriesListingTeam l,
-                   injuriesListingTeamno l,
-                   injuriesListingInjuries l,
-                   injuriesListingUpdated l)) $
+    xpWrap (from_tuple, to_tuple) $
     xp4Tuple (xpElem "team" xpText)
              (xpElem "teamno" xpPrim)
              (xpElem "injuries" xpText)
              (xpElem "updated" xpPrim)
+  where
+    from_tuple = uncurryN Listing
+    to_tuple l = (team l, teamno l, injuries l, updated l)
 
-instance XmlPickler InjuriesListing where
-  xpickle = pickle_injurieslisting
+instance XmlPickler Listing where
+  xpickle = pickle_listing
 
 
-pickle_injuriesmessage :: PU InjuriesMessage
-pickle_injuriesmessage =
+pickle_message :: PU Message
+pickle_message =
   xpElem "message" $
-    xpWrap (uncurry6 InjuriesMessage,
-            \m -> (injuriesMessageXml_file_id m,
-                   injuriesMessageHeading m,
-                   injuriesMessageCategory m,
-                   injuriesMessageSport m,
-                   injuriesMessageListings m,
-                   injuriesMessageTime_stamp m)) $
+    xpWrap (from_tuple, to_tuple) $
     xp6Tuple (xpElem "XML_File_ID" xpPrim)
              (xpElem "heading" xpText)
              (xpElem "category" xpText)
              (xpElem "sport" xpText)
-             (xpList pickle_injurieslisting)
+             (xpList pickle_listing)
              (xpElem "time_stamp" xpText)
+  where
+    from_tuple = uncurryN Message
+    to_tuple m = (xml_file_id m,
+                  heading m,
+                  category m,
+                  sport m,
+                  listings m,
+                  time_stamp m)
 
-instance XmlPickler InjuriesMessage where
-  xpickle = pickle_injuriesmessage
+instance XmlPickler Message where
+  xpickle = pickle_message