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 insert_xml_or_select
165 -- because we know that most teams will already exist, and we
166 -- want to get back the id for the existing team when
167 -- there's a collision.
168 nt_ids <- mapM insert_xml_or_select (xml_teams message)
170 -- Now that the teams have been inserted, create
171 -- news__news_team records mapping beween the two.
172 let news_news_teams = map (News_NewsTeam news_id) nt_ids
173 mapM_ insert_ news_news_teams
175 -- Do all of that over again for the NewsLocations.
176 loc_ids <- mapM insert_xml_or_select (xml_locations message)
177 let news_news_locations = map (News_NewsLocation news_id) loc_ids
178 mapM_ insert_ news_news_locations
180 return ImportSucceeded
183 -- These types don't have special XML representations or field name
184 -- collisions so we use the defaultCodegenConfig and give their fields
185 -- nice simple names.
186 mkPersist defaultCodegenConfig [groundhog|
192 - name: unique_news_team
196 - entity: NewsLocation
197 dbName: news_locations
201 - name: unique_news_location
203 fields: [city, state, country]
207 mkPersist tsn_codegen_config [groundhog|
215 - {name: msg_id, dbName: msg_id}
216 - {name: event_id, dbName: event_id}
225 - entity: News_NewsTeam
226 dbName: news__news_teams
228 - entity: News_NewsLocation
229 dbName: news__news_locations
232 pickle_news_team :: PU NewsTeam
235 xpWrap (from_string, to_string) xpText
237 to_string :: NewsTeam -> String
238 to_string = team_name
240 from_string :: String -> NewsTeam
241 from_string = NewsTeam
243 instance XmlPickler NewsTeam where
244 xpickle = pickle_news_team
246 pickle_msg_id :: PU MsgId
249 xpWrap (from_tuple, to_tuple) $
250 xpPair xpInt (xpAttr "EventId" (xpOption xpInt))
252 from_tuple = uncurryN MsgId
253 to_tuple m = (db_msg_id m, db_event_id m)
255 instance XmlPickler MsgId where
256 xpickle = pickle_msg_id
258 pickle_location :: PU NewsLocation
261 xpWrap (from_tuple, to_tuple) $
262 xpTriple (xpOption (xpElem "city" xpText))
263 (xpOption (xpElem "state" xpText))
264 (xpElem "country" xpText)
267 uncurryN NewsLocation
268 to_tuple l = (city l, state l, country l)
270 instance XmlPickler NewsLocation where
271 xpickle = pickle_location
274 pickle_message :: PU Message
277 xpWrap (from_tuple, to_tuple) $
278 xp13Tuple (xpElem "XML_File_ID" xpInt)
279 (xpElem "heading" xpText)
281 (xpElem "category" xpText)
282 (xpElem "sport" xpText)
283 (xpElem "url" xpText)
284 (xpList pickle_news_team)
285 (xpList pickle_location)
286 (xpElem "SMS" xpText)
287 (xpOption (xpElem "Editor" xpText))
288 (xpOption (xpElem "text" xpText))
290 (xpElem "time_stamp" xpText)
292 from_tuple = uncurryN Message
293 to_tuple m = (xml_xml_file_id m,
307 pickle_continue :: PU (Maybe String)
310 xpWrap (to_string, from_string) $
312 xpList (xpElem "P" xpText)
314 from_string :: String -> [String]
315 from_string = split "\n"
317 to_string :: [String] -> String
318 to_string = join "\n"
320 instance XmlPickler Message where
321 xpickle = pickle_message
326 news_tests :: TestTree
330 [ test_news_fields_have_correct_names,
331 test_pickle_of_unpickle_is_identity,
332 test_unpickle_succeeds ]
335 test_news_fields_have_correct_names :: TestTree
336 test_news_fields_have_correct_names =
337 testCase "news fields get correct database names" $
338 mapM_ check (zip actual expected)
340 -- This is cool, it uses the (derived) Data instance of
341 -- News.News to get its constructor names.
342 field_names :: [String]
344 constrFields . head $ dataTypeConstrs $ dataTypeOf (undefined :: News)
348 map (\x -> tsn_db_field_namer "herp" "derp" 8675309 x 90210) field_names
351 actual = ["mid", "sport", "url", "sms", "editor", "text", "continue"]
353 check (x,y) = (x @?= y)
356 -- | Warning, succeess of this test does not mean that unpickling
358 test_pickle_of_unpickle_is_identity :: TestTree
359 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
360 [ check "pickle composed with unpickle is the identity"
361 "test/xml/newsxml.xml",
363 check "pickle composed with unpickle is the identity (with Editor)"
364 "test/xml/newsxml-with-editor.xml" ]
366 check desc path = testCase desc $ do
367 (expected :: [Message], actual) <- pickle_unpickle "message" path
371 test_unpickle_succeeds :: TestTree
372 test_unpickle_succeeds = testGroup "unpickle tests"
373 [ check "unpickling succeeds"
374 "test/xml/newsxml.xml",
376 check "unpickling succeeds (with Editor)"
377 "test/xml/newsxml-with-editor.xml" ]
379 check desc path = testCase desc $ do
380 actual <- unpickleable path pickle_message