+
+instance ToDb EarlyLineGameTeamXml where
+ -- | The database analogue of a 'EarlyLineGameTeamXml' is an
+ -- 'EarlyLineGameTeam', although the DB type is merely embedded
+ -- in another type.
+ --
+ type Db EarlyLineGameTeamXml = EarlyLineGameTeam
+
+
+-- | The 'FromXml' instance for 'EarlyLineGameTeamXml' lets us convert
+-- it to a 'EarlyLineGameTeam' easily.
+--
+instance FromXml EarlyLineGameTeamXml where
+ -- | To convert a 'EarlyLineGameTeamXml' to an 'EarlyLineGameTeam',
+ -- we figure how its fields were represented and choose the ones
+ -- that are populated. For example if the \"line\" attribute was
+ -- there, we'll use it, but if now, we'll use the \<line\>
+ -- element.
+ --
+ from_xml EarlyLineGameTeamXml{..} =
+ EarlyLineGameTeam {
+ db_rotation_number = xml_rotation_number,
+ db_line = merge xml_line_attr xml_line_elem,
+ db_team_name = merge xml_team_name_attr xml_team_name_text,
+ db_pitcher = xml_pitcher }
+ where
+ merge :: Maybe String -> Maybe String -> Maybe String
+ merge Nothing y = y
+ merge x Nothing = x
+ merge _ _ = Nothing
+
+
+
+
+-- | Convert an 'EarlyLineDate' into a list of 'EarlyLineGame's. Each
+-- date has one or more games, and the fields that belong to the date
+-- should really be in the game anyway. So the database
+-- representation of a game has the combined fields of the XML
+-- date/game.
+--
+-- This function gets the games out of a date, and then sticks the
+-- date value inside the games. It also adds the foreign key
+-- reference to the games' parent message, and returns the result.
+--
+-- This would convert a single date to a single game if we only
+-- needed to support earlylineXML.dtd and not MLB_earlylineXML.dtd.
+--
+date_to_games :: (DefaultKey EarlyLine) -> EarlyLineDate -> [EarlyLineGame]
+date_to_games fk date =
+ map convert_game games_only
+ where
+ -- | Get the list of games out of a date (i.e. drop the notes).
+ --
+ games_only :: [EarlyLineGameXml]
+ games_only = (map date_game (date_games_with_notes date))
+
+ -- | Stick the date value into the given game. If our
+ -- 'EarlyLineGameXml' has an 'xml_game_time', then we combine it
+ -- with the day portion of the supplied @date@. If not, then we
+ -- just use @date as-is.
+ --
+ combine_date_time :: Maybe UTCTime -> UTCTime
+ combine_date_time (Just t) =
+ UTCTime (utctDay $ date_value date) (utctDayTime t)
+ combine_date_time Nothing = date_value date
+
+ -- | Convert an XML game to a database one.
+ --
+ convert_game :: EarlyLineGameXml -> EarlyLineGame
+ convert_game EarlyLineGameXml{..} =
+ EarlyLineGame {
+ db_early_lines_id = fk,
+ db_game_time = combine_date_time xml_game_time,
+ db_away_team = from_xml xml_away_team,
+ db_home_team = from_xml xml_home_team,
+ db_over_under = xml_over_under }
+
+