1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE StandaloneDeriving #-}
8 {-# LANGUAGE TemplateHaskell #-}
9 {-# LANGUAGE TypeFamilies #-}
11 -- | Parse TSN XML for the DTD "newsxml.dtd". Each document contains a
12 -- root element \<message\> that contains an entire news item.
19 import Data.Data ( Data, constrFields, dataTypeConstrs, dataTypeOf )
20 import Data.List.Utils ( join, split )
21 import Data.Tuple.Curry ( uncurryN )
22 import Data.Typeable ( Typeable )
23 import Database.Groundhog (
24 defaultMigrationLogger,
28 import Database.Groundhog.Core ( DefaultKey )
29 import Database.Groundhog.TH (
33 import Test.Tasty ( TestTree, testGroup )
34 import Test.Tasty.HUnit ( (@?=), testCase )
35 import Text.XML.HXT.Core (
51 tsn_db_field_namer ) -- Used in a test
52 import TSN.DbImport ( DbImport(..), ImportResult(..) )
53 import TSN.XmlImport ( XmlImport(..) )
54 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
58 -- | The database type for teams as they show up in the news.
60 NewsTeam { team_name :: String }
63 instance FromXml NewsTeam where
64 type Db NewsTeam = NewsTeam
67 instance XmlImport NewsTeam
70 -- | Mapping between News records and NewsTeam records in the
71 -- database. We name the fields (even though they're never used) for
72 -- Groundhog's benefit.
75 nnt_news_id :: DefaultKey News,
76 nnt_news_team_id :: DefaultKey NewsTeam }
78 -- | The database type for locations as they show up in the news.
82 state :: Maybe String,
86 instance FromXml NewsLocation where
87 type Db NewsLocation = NewsLocation
90 instance XmlImport NewsLocation
93 -- | Mapping between News records and NewsLocation records in the
94 -- database. We name the fields (even though they're never used) for
95 -- Groundhog's benefit.
96 data News_NewsLocation =
98 nnl_news_id :: DefaultKey News,
99 nnl_news_location_id :: DefaultKey NewsLocation }
102 -- | The msg_id child of <message> contains an event_id attribute; we
103 -- embed it into the 'News' type. We (pointlessly) use the "db_"
104 -- prefix here so that the two names collide on "id" when Groundhog
105 -- is creating its fields using our field namer.
109 db_event_id :: Maybe Int }
110 deriving (Data, Eq, Show, Typeable)
115 xml_xml_file_id :: Int,
116 xml_heading :: String,
118 xml_category :: String,
121 xml_teams :: [NewsTeam],
122 xml_locations :: [NewsLocation],
124 xml_editor :: Maybe String,
125 xml_text :: Maybe String, -- Text and continue seem to show up in pairs,
126 xml_continue :: Maybe String, -- either both present or both missing.
127 xml_time_stamp :: String }
136 db_editor :: Maybe String,
137 db_text :: Maybe String,
138 db_continue :: Maybe String }
139 deriving (Data, Eq, Show, Typeable)
141 instance FromXml Message where
142 type Db Message = News
144 -- We don't need the key argument (from_xml_fk) since the XML type
145 -- contains more information in this case.
146 from_xml (Message _ _ c _ e f _ _ i j k l _) =
149 instance XmlImport Message
151 instance DbImport Message where
153 runMigration defaultMigrationLogger $ do
154 migrate (undefined :: NewsTeam)
155 migrate (undefined :: NewsLocation)
156 migrate (undefined :: News)
157 migrate (undefined :: News_NewsTeam)
158 migrate (undefined :: News_NewsLocation)
160 dbimport message = do
161 -- Insert the message and acquire its primary key (unique ID)
162 news_id <- insert_xml message
164 -- And insert each one into its own table. We use insertByAll_xml
165 -- because we know that most teams will already exist, and we
166 -- want to get back a Left (id) for the existing team when
167 -- there's a collision. In fact, if the insert succeeds, we'll
168 -- get a Right (id) back, so we can disregard the Either
169 -- constructor entirely. That's what the (either id id) does.
170 either_nt_ids <- mapM insertByAll_xml (xml_teams message)
171 let nt_ids = map (either id id) either_nt_ids
173 -- Now that the teams have been inserted, create
174 -- news__news_team records mapping beween the two.
175 let news_news_teams = map (News_NewsTeam news_id) nt_ids
176 mapM_ insert_ news_news_teams
178 -- Do all of that over again for the NewsLocations.
179 either_loc_ids <- mapM insertByAll_xml (xml_locations message)
180 let loc_ids = map (either id id) either_loc_ids
181 let news_news_locations = map (News_NewsLocation news_id) loc_ids
182 mapM_ insert_ news_news_locations
184 return ImportSucceeded
187 -- These types don't have special XML representations or field name
188 -- collisions so we use the defaultCodegenConfig and give their fields
189 -- nice simple names.
190 mkPersist defaultCodegenConfig [groundhog|
196 - name: unique_news_team
200 - entity: NewsLocation
201 dbName: news_locations
205 - name: unique_news_location
207 fields: [city, state, country]
211 mkPersist tsn_codegen_config [groundhog|
219 - {name: msg_id, dbName: msg_id}
220 - {name: event_id, dbName: event_id}
229 - entity: News_NewsTeam
230 dbName: news__news_teams
232 - entity: News_NewsLocation
233 dbName: news__news_locations
236 pickle_news_team :: PU NewsTeam
239 xpWrap (from_string, to_string) xpText
241 to_string :: NewsTeam -> String
242 to_string = team_name
244 from_string :: String -> NewsTeam
245 from_string = NewsTeam
247 instance XmlPickler NewsTeam where
248 xpickle = pickle_news_team
250 pickle_msg_id :: PU MsgId
253 xpWrap (from_tuple, to_tuple) $
254 xpPair xpInt (xpAttr "EventId" (xpOption xpInt))
256 from_tuple = uncurryN MsgId
257 to_tuple m = (db_msg_id m, db_event_id m)
259 instance XmlPickler MsgId where
260 xpickle = pickle_msg_id
262 pickle_location :: PU NewsLocation
265 xpWrap (from_tuple, to_tuple) $
266 xpTriple (xpOption (xpElem "city" xpText))
267 (xpOption (xpElem "state" xpText))
268 (xpElem "country" xpText)
271 uncurryN NewsLocation
272 to_tuple l = (city l, state l, country l)
274 instance XmlPickler NewsLocation where
275 xpickle = pickle_location
278 pickle_message :: PU Message
281 xpWrap (from_tuple, to_tuple) $
282 xp13Tuple (xpElem "XML_File_ID" xpInt)
283 (xpElem "heading" xpText)
285 (xpElem "category" xpText)
286 (xpElem "sport" xpText)
287 (xpElem "url" xpText)
288 (xpList pickle_news_team)
289 (xpList pickle_location)
290 (xpElem "SMS" xpText)
291 (xpOption (xpElem "Editor" xpText))
292 (xpOption (xpElem "text" xpText))
294 (xpElem "time_stamp" xpText)
296 from_tuple = uncurryN Message
297 to_tuple m = (xml_xml_file_id m,
311 pickle_continue :: PU (Maybe String)
314 xpWrap (to_string, from_string) $
316 xpList (xpElem "P" xpText)
318 from_string :: String -> [String]
319 from_string = split "\n"
321 to_string :: [String] -> String
322 to_string = join "\n"
324 instance XmlPickler Message where
325 xpickle = pickle_message
330 news_tests :: TestTree
334 [ test_news_fields_have_correct_names,
335 test_pickle_of_unpickle_is_identity,
336 test_unpickle_succeeds ]
339 test_news_fields_have_correct_names :: TestTree
340 test_news_fields_have_correct_names =
341 testCase "news fields get correct database names" $
342 mapM_ check (zip actual expected)
344 -- This is cool, it uses the (derived) Data instance of
345 -- News.News to get its constructor names.
346 field_names :: [String]
348 constrFields . head $ dataTypeConstrs $ dataTypeOf (undefined :: News)
352 map (\x -> tsn_db_field_namer "herp" "derp" 8675309 x 90210) field_names
355 actual = ["mid", "sport", "url", "sms", "editor", "text", "continue"]
357 check (x,y) = (x @?= y)
360 -- | Warning, succeess of this test does not mean that unpickling
362 test_pickle_of_unpickle_is_identity :: TestTree
363 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
364 [ check "pickle composed with unpickle is the identity"
365 "test/xml/newsxml.xml",
367 check "pickle composed with unpickle is the identity (with Editor)"
368 "test/xml/newsxml-with-editor.xml" ]
370 check desc path = testCase desc $ do
371 (expected :: [Message], actual) <- pickle_unpickle "message" path
375 test_unpickle_succeeds :: TestTree
376 test_unpickle_succeeds = testGroup "unpickle tests"
377 [ check "unpickling succeeds"
378 "test/xml/newsxml.xml",
380 check "unpickling succeeds (with Editor)"
381 "test/xml/newsxml-with-editor.xml" ]
383 check desc path = testCase desc $ do
384 actual <- unpickleable path pickle_message