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 (
26 import Database.Groundhog.Core ( DefaultKey )
27 import Database.Groundhog.TH (
31 import Test.Tasty ( TestTree, testGroup )
32 import Test.Tasty.HUnit ( (@?=), testCase )
33 import Text.XML.HXT.Core (
49 tsn_db_field_namer ) -- Used in a test
50 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
51 import TSN.XmlImport ( XmlImport(..) )
52 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
56 -- | The database type for teams as they show up in the news.
58 NewsTeam { team_name :: String }
61 instance FromXml NewsTeam where
62 type Db NewsTeam = NewsTeam
65 instance XmlImport NewsTeam
68 -- | Mapping between News records and NewsTeam records in the
69 -- database. We name the fields (even though they're never used) for
70 -- Groundhog's benefit.
73 nnt_news_id :: DefaultKey News,
74 nnt_news_team_id :: DefaultKey NewsTeam }
76 -- | The database type for locations as they show up in the news.
80 state :: Maybe String,
84 instance FromXml NewsLocation where
85 type Db NewsLocation = NewsLocation
88 instance XmlImport NewsLocation
91 -- | Mapping between News records and NewsLocation records in the
92 -- database. We name the fields (even though they're never used) for
93 -- Groundhog's benefit.
94 data News_NewsLocation =
96 nnl_news_id :: DefaultKey News,
97 nnl_news_location_id :: DefaultKey NewsLocation }
100 -- | The msg_id child of <message> contains an event_id attribute; we
101 -- embed it into the 'News' type. We (pointlessly) use the "db_"
102 -- prefix here so that the two names collide on "id" when Groundhog
103 -- is creating its fields using our field namer.
107 db_event_id :: Maybe Int }
108 deriving (Data, Eq, Show, Typeable)
113 xml_xml_file_id :: Int,
114 xml_heading :: String,
116 xml_category :: String,
119 xml_teams :: [NewsTeam],
120 xml_locations :: [NewsLocation],
122 xml_editor :: Maybe String,
123 xml_text :: Maybe String, -- Text and continue seem to show up in pairs,
124 xml_continue :: Maybe String, -- either both present or both missing.
125 xml_time_stamp :: String }
134 db_editor :: Maybe String,
135 db_text :: Maybe String,
136 db_continue :: Maybe String }
137 deriving (Data, Eq, Show, Typeable)
139 instance FromXml Message where
140 type Db Message = News
142 -- We don't need the key argument (from_xml_fk) since the XML type
143 -- contains more information in this case.
144 from_xml (Message _ _ c _ e f _ _ i j k l _) =
147 instance XmlImport Message
149 instance DbImport Message where
152 migrate (undefined :: NewsTeam)
153 migrate (undefined :: NewsLocation)
154 migrate (undefined :: News)
155 migrate (undefined :: News_NewsTeam)
156 migrate (undefined :: News_NewsLocation)
158 dbimport message = do
159 -- Insert the message and acquire its primary key (unique ID)
160 news_id <- insert_xml message
162 -- And insert each one into its own table. We use insert_xml_or_select
163 -- because we know that most teams will already exist, and we
164 -- want to get back the id for the existing team when
165 -- there's a collision.
166 nt_ids <- mapM insert_xml_or_select (xml_teams message)
168 -- Now that the teams have been inserted, create
169 -- news__news_team records mapping beween the two.
170 let news_news_teams = map (News_NewsTeam news_id) nt_ids
171 mapM_ insert_ news_news_teams
173 -- Do all of that over again for the NewsLocations.
174 loc_ids <- mapM insert_xml_or_select (xml_locations message)
175 let news_news_locations = map (News_NewsLocation news_id) loc_ids
176 mapM_ insert_ news_news_locations
178 return ImportSucceeded
181 -- These types don't have special XML representations or field name
182 -- collisions so we use the defaultCodegenConfig and give their fields
183 -- nice simple names.
184 mkPersist defaultCodegenConfig [groundhog|
190 - name: unique_news_team
194 - entity: NewsLocation
195 dbName: news_locations
199 - name: unique_news_location
201 fields: [city, state, country]
205 mkPersist tsn_codegen_config [groundhog|
213 - {name: msg_id, dbName: msg_id}
214 - {name: event_id, dbName: event_id}
223 - entity: News_NewsTeam
224 dbName: news__news_teams
226 - entity: News_NewsLocation
227 dbName: news__news_locations
230 pickle_news_team :: PU NewsTeam
233 xpWrap (from_string, to_string) xpText
235 to_string :: NewsTeam -> String
236 to_string = team_name
238 from_string :: String -> NewsTeam
239 from_string = NewsTeam
241 instance XmlPickler NewsTeam where
242 xpickle = pickle_news_team
244 pickle_msg_id :: PU MsgId
247 xpWrap (from_tuple, to_tuple) $
248 xpPair xpInt (xpAttr "EventId" (xpOption xpInt))
250 from_tuple = uncurryN MsgId
251 to_tuple m = (db_msg_id m, db_event_id m)
253 instance XmlPickler MsgId where
254 xpickle = pickle_msg_id
256 pickle_location :: PU NewsLocation
259 xpWrap (from_tuple, to_tuple) $
260 xpTriple (xpOption (xpElem "city" xpText))
261 (xpOption (xpElem "state" xpText))
262 (xpElem "country" xpText)
265 uncurryN NewsLocation
266 to_tuple l = (city l, state l, country l)
268 instance XmlPickler NewsLocation where
269 xpickle = pickle_location
272 pickle_message :: PU Message
275 xpWrap (from_tuple, to_tuple) $
276 xp13Tuple (xpElem "XML_File_ID" xpInt)
277 (xpElem "heading" xpText)
279 (xpElem "category" xpText)
280 (xpElem "sport" xpText)
281 (xpElem "url" xpText)
282 (xpList pickle_news_team)
283 (xpList pickle_location)
284 (xpElem "SMS" xpText)
285 (xpOption (xpElem "Editor" xpText))
286 (xpOption (xpElem "text" xpText))
288 (xpElem "time_stamp" xpText)
290 from_tuple = uncurryN Message
291 to_tuple m = (xml_xml_file_id m,
305 pickle_continue :: PU (Maybe String)
308 xpWrap (to_string, from_string) $
310 xpList (xpElem "P" xpText)
312 from_string :: String -> [String]
313 from_string = split "\n"
315 to_string :: [String] -> String
316 to_string = join "\n"
318 instance XmlPickler Message where
319 xpickle = pickle_message
324 news_tests :: TestTree
328 [ test_news_fields_have_correct_names,
329 test_pickle_of_unpickle_is_identity,
330 test_unpickle_succeeds ]
333 test_news_fields_have_correct_names :: TestTree
334 test_news_fields_have_correct_names =
335 testCase "news fields get correct database names" $
336 mapM_ check (zip actual expected)
338 -- This is cool, it uses the (derived) Data instance of
339 -- News.News to get its constructor names.
340 field_names :: [String]
342 constrFields . head $ dataTypeConstrs $ dataTypeOf (undefined :: News)
346 map (\x -> tsn_db_field_namer "herp" "derp" 8675309 x 90210) field_names
349 actual = ["mid", "sport", "url", "sms", "editor", "text", "continue"]
351 check (x,y) = (x @?= y)
354 -- | Warning, succeess of this test does not mean that unpickling
356 test_pickle_of_unpickle_is_identity :: TestTree
357 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
358 [ check "pickle composed with unpickle is the identity"
359 "test/xml/newsxml.xml",
361 check "pickle composed with unpickle is the identity (with Editor)"
362 "test/xml/newsxml-with-editor.xml" ]
364 check desc path = testCase desc $ do
365 (expected :: [Message], actual) <- pickle_unpickle "message" path
369 test_unpickle_succeeds :: TestTree
370 test_unpickle_succeeds = testGroup "unpickle tests"
371 [ check "unpickling succeeds"
372 "test/xml/newsxml.xml",
374 check "unpickling succeeds (with Editor)"
375 "test/xml/newsxml-with-editor.xml" ]
377 check desc path = testCase desc $ do
378 actual <- unpickleable path pickle_message