]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/XML/News.hs
Add the insert_xml_or_select function to TSN.XmlImport and use it in TSN.XML.News.
[dead/htsn-import.git] / src / TSN / XML / News.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE GADTs #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE StandaloneDeriving #-}
8 {-# LANGUAGE TemplateHaskell #-}
9 {-# LANGUAGE TypeFamilies #-}
10
11 -- | Parse TSN XML for the DTD "newsxml.dtd". Each document contains a
12 -- root element \<message\> that contains an entire news item.
13 --
14 module TSN.XML.News (
15 Message,
16 news_tests )
17 where
18
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,
25 insert_,
26 migrate,
27 runMigration )
28 import Database.Groundhog.Core ( DefaultKey )
29 import Database.Groundhog.TH (
30 defaultCodegenConfig,
31 groundhog,
32 mkPersist )
33 import Test.Tasty ( TestTree, testGroup )
34 import Test.Tasty.HUnit ( (@?=), testCase )
35 import Text.XML.HXT.Core (
36 PU,
37 XmlPickler(..),
38 xp13Tuple,
39 xpAttr,
40 xpElem,
41 xpInt,
42 xpList,
43 xpOption,
44 xpPair,
45 xpText,
46 xpTriple,
47 xpWrap )
48
49 import TSN.Codegen (
50 tsn_codegen_config,
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 )
55
56
57
58 -- | The database type for teams as they show up in the news.
59 data NewsTeam =
60 NewsTeam { team_name :: String }
61 deriving (Eq, Show)
62
63 instance FromXml NewsTeam where
64 type Db NewsTeam = NewsTeam
65 from_xml = id
66
67 instance XmlImport NewsTeam
68
69
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.
73 data News_NewsTeam =
74 News_NewsTeam {
75 nnt_news_id :: DefaultKey News,
76 nnt_news_team_id :: DefaultKey NewsTeam }
77
78 -- | The database type for locations as they show up in the news.
79 data NewsLocation =
80 NewsLocation {
81 city :: Maybe String,
82 state :: Maybe String,
83 country :: String }
84 deriving (Eq, Show)
85
86 instance FromXml NewsLocation where
87 type Db NewsLocation = NewsLocation
88 from_xml = id
89
90 instance XmlImport NewsLocation
91
92
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 =
97 News_NewsLocation {
98 nnl_news_id :: DefaultKey News,
99 nnl_news_location_id :: DefaultKey NewsLocation }
100
101
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.
106 data MsgId =
107 MsgId {
108 db_msg_id :: Int,
109 db_event_id :: Maybe Int }
110 deriving (Data, Eq, Show, Typeable)
111
112
113 data Message =
114 Message {
115 xml_xml_file_id :: Int,
116 xml_heading :: String,
117 xml_mid :: MsgId,
118 xml_category :: String,
119 xml_sport :: String,
120 xml_url :: String,
121 xml_teams :: [NewsTeam],
122 xml_locations :: [NewsLocation],
123 xml_sms :: String,
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 }
128 deriving (Eq, Show)
129
130 data News =
131 News {
132 db_mid :: MsgId,
133 db_sport :: String,
134 db_url :: String,
135 db_sms :: String,
136 db_editor :: Maybe String,
137 db_text :: Maybe String,
138 db_continue :: Maybe String }
139 deriving (Data, Eq, Show, Typeable)
140
141 instance FromXml Message where
142 type Db Message = News
143
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 _) =
147 News c e f i j k l
148
149 instance XmlImport Message
150
151 instance DbImport Message where
152 dbmigrate _ =
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)
159
160 dbimport message = do
161 -- Insert the message and acquire its primary key (unique ID)
162 news_id <- insert_xml message
163
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)
169
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
174
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
179
180 return ImportSucceeded
181
182
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|
187 - entity: NewsTeam
188 dbName: news_teams
189 constructors:
190 - name: NewsTeam
191 uniques:
192 - name: unique_news_team
193 type: constraint
194 fields: [team_name]
195
196 - entity: NewsLocation
197 dbName: news_locations
198 constructors:
199 - name: NewsLocation
200 uniques:
201 - name: unique_news_location
202 type: constraint
203 fields: [city, state, country]
204
205 |]
206
207 mkPersist tsn_codegen_config [groundhog|
208 - entity: News
209 dbName: news
210 constructors:
211 - name: News
212 fields:
213 - name: db_mid
214 embeddedType:
215 - {name: msg_id, dbName: msg_id}
216 - {name: event_id, dbName: event_id}
217 - embedded: MsgId
218 fields:
219 - name: db_msg_id
220 dbName: msg_id
221 - name: db_event_id
222 dbName: event_id
223
224
225 - entity: News_NewsTeam
226 dbName: news__news_teams
227
228 - entity: News_NewsLocation
229 dbName: news__news_locations
230 |]
231
232 pickle_news_team :: PU NewsTeam
233 pickle_news_team =
234 xpElem "team" $
235 xpWrap (from_string, to_string) xpText
236 where
237 to_string :: NewsTeam -> String
238 to_string = team_name
239
240 from_string :: String -> NewsTeam
241 from_string = NewsTeam
242
243 instance XmlPickler NewsTeam where
244 xpickle = pickle_news_team
245
246 pickle_msg_id :: PU MsgId
247 pickle_msg_id =
248 xpElem "msg_id" $
249 xpWrap (from_tuple, to_tuple) $
250 xpPair xpInt (xpAttr "EventId" (xpOption xpInt))
251 where
252 from_tuple = uncurryN MsgId
253 to_tuple m = (db_msg_id m, db_event_id m)
254
255 instance XmlPickler MsgId where
256 xpickle = pickle_msg_id
257
258 pickle_location :: PU NewsLocation
259 pickle_location =
260 xpElem "location" $
261 xpWrap (from_tuple, to_tuple) $
262 xpTriple (xpOption (xpElem "city" xpText))
263 (xpOption (xpElem "state" xpText))
264 (xpElem "country" xpText)
265 where
266 from_tuple =
267 uncurryN NewsLocation
268 to_tuple l = (city l, state l, country l)
269
270 instance XmlPickler NewsLocation where
271 xpickle = pickle_location
272
273
274 pickle_message :: PU Message
275 pickle_message =
276 xpElem "message" $
277 xpWrap (from_tuple, to_tuple) $
278 xp13Tuple (xpElem "XML_File_ID" xpInt)
279 (xpElem "heading" xpText)
280 pickle_msg_id
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))
289 pickle_continue
290 (xpElem "time_stamp" xpText)
291 where
292 from_tuple = uncurryN Message
293 to_tuple m = (xml_xml_file_id m,
294 xml_heading m,
295 xml_mid m,
296 xml_category m,
297 xml_sport m,
298 xml_url m,
299 xml_teams m,
300 xml_locations m,
301 xml_sms m,
302 xml_editor m,
303 xml_text m,
304 xml_continue m,
305 xml_time_stamp m)
306
307 pickle_continue :: PU (Maybe String)
308 pickle_continue =
309 xpOption $
310 xpWrap (to_string, from_string) $
311 xpElem "continue" $
312 xpList (xpElem "P" xpText)
313 where
314 from_string :: String -> [String]
315 from_string = split "\n"
316
317 to_string :: [String] -> String
318 to_string = join "\n"
319
320 instance XmlPickler Message where
321 xpickle = pickle_message
322
323
324
325 -- * Tasty Tests
326 news_tests :: TestTree
327 news_tests =
328 testGroup
329 "News tests"
330 [ test_news_fields_have_correct_names,
331 test_pickle_of_unpickle_is_identity,
332 test_unpickle_succeeds ]
333
334
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)
339 where
340 -- This is cool, it uses the (derived) Data instance of
341 -- News.News to get its constructor names.
342 field_names :: [String]
343 field_names =
344 constrFields . head $ dataTypeConstrs $ dataTypeOf (undefined :: News)
345
346 expected :: [String]
347 expected =
348 map (\x -> tsn_db_field_namer "herp" "derp" 8675309 x 90210) field_names
349
350 actual :: [String]
351 actual = ["mid", "sport", "url", "sms", "editor", "text", "continue"]
352
353 check (x,y) = (x @?= y)
354
355
356 -- | Warning, succeess of this test does not mean that unpickling
357 -- succeeded.
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",
362
363 check "pickle composed with unpickle is the identity (with Editor)"
364 "test/xml/newsxml-with-editor.xml" ]
365 where
366 check desc path = testCase desc $ do
367 (expected :: [Message], actual) <- pickle_unpickle "message" path
368 actual @?= expected
369
370
371 test_unpickle_succeeds :: TestTree
372 test_unpickle_succeeds = testGroup "unpickle tests"
373 [ check "unpickling succeeds"
374 "test/xml/newsxml.xml",
375
376 check "unpickling succeeds (with Editor)"
377 "test/xml/newsxml-with-editor.xml" ]
378 where
379 check desc path = testCase desc $ do
380 actual <- unpickleable path pickle_message
381 let expected = True
382 actual @?= expected