]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/XML/ScheduleChanges.hs
151b615357ba7e024feb064c8ad841bfc2067b83
[dead/htsn-import.git] / src / TSN / XML / ScheduleChanges.hs
1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE GADTs #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE RecordWildCards #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
7
8 -- | Parse TSN XML for the DTD \"Schedule_Changes_XML.dtd\". Each
9 -- \<message\> element contains zero or more \<Schedule_Change\>
10 -- which are just a wrapper around zero or more \<SC_Listing\>s.
11 --
12 -- The teams appear to use the shared "TSN.Team" representation.
13 --
14 module TSN.XML.ScheduleChanges (
15 dtd,
16 pickle_message,
17 -- * Tests
18 schedule_changes_tests,
19 -- * WARNING: these are private but exported to silence warnings
20 ScheduleChangesConstructor(..),
21 ScheduleChangesListingConstructor(..) )
22 where
23
24 -- System imports.
25 import Control.Monad ( forM_ )
26 import Data.Time ( UTCTime(..) )
27 import Data.Tuple.Curry ( uncurryN )
28 import Database.Groundhog (
29 countAll,
30 deleteAll,
31 insert_,
32 migrate,
33 runMigration,
34 silentMigrationLogger )
35 import Database.Groundhog.Core ( DefaultKey )
36 import Database.Groundhog.Generic ( runDbConn )
37 import Database.Groundhog.Sqlite ( withSqliteConn )
38 import Database.Groundhog.TH (
39 groundhog,
40 mkPersist )
41 import Test.Tasty ( TestTree, testGroup )
42 import Test.Tasty.HUnit ( (@?=), testCase )
43 import Text.XML.HXT.Core (
44 PU,
45 xp6Tuple,
46 xp11Tuple,
47 xpAttr,
48 xpElem,
49 xpInt,
50 xpList,
51 xpOption,
52 xpPair,
53 xpText,
54 xpWrap )
55
56 -- Local imports.
57 import TSN.Codegen ( tsn_codegen_config )
58 import TSN.Database ( insert_or_select )
59 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
60 import TSN.Picklers ( xp_date_padded, xp_tba_time, xp_time_stamp )
61 import TSN.Team ( Team(..), HTeam(..), VTeam(..) )
62 import TSN.XmlImport ( XmlImport(..) )
63 import Xml (
64 FromXml(..),
65 ToDb(..),
66 pickle_unpickle,
67 unpickleable,
68 unsafe_unpickle )
69
70
71
72 -- | The DTD to which this module corresponds. Used to invoke
73 -- 'dbimport'.
74 --
75 dtd :: String
76 dtd = "Schedule_Changes_XML.dtd"
77
78
79 --
80 -- DB/XML data types
81 --
82
83 -- * ScheduleChanges/Message
84
85 -- | Database representation of a 'Message'. Comparatively, it lacks
86 -- the listings since they are linked via a foreign key.
87 --
88 data ScheduleChanges =
89 ScheduleChanges {
90 db_xml_file_id :: Int,
91 db_heading :: String,
92 db_category :: String,
93 db_sport :: String,
94 db_time_stamp :: UTCTime }
95 deriving (Eq, Show)
96
97
98 -- | XML representation of a \<Schedule_Change\> within a
99 -- \<message\>. These are wrappers around a bunch of
100 -- \<SC_Listing\>s, but they also contain the sport name for all of
101 -- the contained listings.
102 --
103 data ScheduleChangeXml =
104 ScheduleChangeXml {
105 xml_sc_sport :: String,
106 xml_sc_listings :: [ScheduleChangesListingXml] }
107 deriving (Eq, Show)
108
109
110 -- | XML representation of a 'ScheduleChanges'. It has the same
111 -- fields, but in addition contains the 'xml_listings'.
112 --
113 data Message =
114 Message {
115 xml_xml_file_id :: Int,
116 xml_heading :: String,
117 xml_category :: String,
118 xml_sport :: String,
119 xml_schedule_changes :: [ScheduleChangeXml],
120 xml_time_stamp :: UTCTime }
121 deriving (Eq, Show)
122
123
124
125 instance ToDb Message where
126 -- | The database analogue of a 'Message' is a 'ScheduleChanges'.
127 --
128 type Db Message = ScheduleChanges
129
130
131 -- | The 'FromXml' instance for 'Message' is required for the
132 -- 'XmlImport' instance.
133 --
134 instance FromXml Message where
135 -- | To convert a 'Message' to an 'ScheduleChanges', we just drop
136 -- the 'xml_schedule_changes'.
137 --
138 from_xml Message{..} =
139 ScheduleChanges {
140 db_xml_file_id = xml_xml_file_id,
141 db_heading = xml_heading,
142 db_category = xml_category,
143 db_sport = xml_sport,
144 db_time_stamp = xml_time_stamp }
145
146
147 -- | This allows us to insert the XML representation 'Message'
148 -- directly.
149 --
150 instance XmlImport Message
151
152
153
154 -- * ScheduleChangesListing/ScheduleChangesListingXml
155
156 -- | An embedded type within 'ScheduleChangesListing'. These look
157 -- like, \<status numeral=\"4\"\>FINAL\</status\> within the XML,
158 -- but they're in one-to-one correspondence with the listings.
159 --
160 data ScheduleChangesListingStatus =
161 ScheduleChangesListingStatus {
162 db_status_numeral :: Int,
163 db_status :: Maybe String } -- Yes, they can be empty.
164 deriving (Eq, Show)
165
166
167 -- | Database representation of a \<SC_Listing\> contained within a
168 -- \<Schedule_Change\>, within a \<message\>. During the transition
169 -- to the database, we drop the intermediate \<Schedule_Change\>
170 -- leaving the listing keyed to the 'ScheduleChanges' itself.
171 --
172 -- The home/away teams reuse the 'Team' representation.
173 --
174 -- The sport name (sc_sport) is pulled out of the containing
175 -- \<Schedule_Change\> and embedded into the listings themselves.
176 --
177 data ScheduleChangesListing =
178 ScheduleChangesListing {
179 db_schedule_changes_id :: DefaultKey ScheduleChanges,
180 db_away_team_id :: DefaultKey Team,
181 db_home_team_id ::DefaultKey Team,
182 db_type :: String,
183 db_sc_sport :: String,
184 db_schedule_id :: Int,
185 db_game_time :: UTCTime,
186 db_location :: Maybe String,
187 db_vscore :: Int,
188 db_hscore :: Int,
189 db_listing_status :: ScheduleChangesListingStatus,
190 db_notes :: Maybe String }
191
192
193 -- | XML representation of a \<SC_Listing\> contained within a
194 -- \<Schedule_Change\>, within a \<message\>.
195 --
196 data ScheduleChangesListingXml =
197 ScheduleChangesListingXml {
198 xml_type :: String,
199 xml_schedule_id :: Int,
200 xml_game_date :: UTCTime,
201 xml_game_time :: Maybe UTCTime,
202 xml_location :: Maybe String,
203 xml_away_team :: VTeam,
204 xml_home_team :: HTeam,
205 xml_vscore :: Int,
206 xml_hscore :: Int,
207 xml_listing_status :: ScheduleChangesListingStatus,
208 xml_notes :: Maybe String }
209 deriving (Eq, Show)
210
211
212 instance ToDb ScheduleChangesListingXml where
213 -- | The database analogue of an 'ScheduleChangesListingXml' is
214 -- an 'ScheduleChangesListing'.
215 --
216 type Db ScheduleChangesListingXml = ScheduleChangesListing
217
218
219
220 -- | We don't make 'ScheduleChangesListingXml' an instance of
221 -- 'FromXmlFkTeams' because it needs some additional information,
222 -- namely the sport name from its containing \<Schedule_Change\>.
223 -- But essentially we'll need to do the same thing as
224 -- 'from_xml_fk_teams'. This function accomplishes the same thing,
225 -- with the addition of the sport that's passed in.
226 --
227 -- The parameter order is for convenience later (see dbimport).
228 --
229 from_xml_fk_sport :: (DefaultKey ScheduleChanges)
230 -> String -- ^ The sport from our containing schedule change
231 -> (DefaultKey Team) -- ^ Away team FK
232 -> (DefaultKey Team) -- ^ Home team FK
233 -> ScheduleChangesListingXml
234 -> ScheduleChangesListing
235 from_xml_fk_sport fk sport fk_away fk_home ScheduleChangesListingXml{..} =
236 ScheduleChangesListing {
237 db_schedule_changes_id = fk,
238 db_away_team_id = fk_away,
239 db_home_team_id = fk_home,
240 db_type = xml_type,
241 db_sc_sport = sport,
242 db_schedule_id = xml_schedule_id,
243 db_game_time = make_game_time xml_game_date xml_game_time,
244 db_location = xml_location,
245 db_vscore = xml_vscore,
246 db_hscore = xml_hscore,
247 db_listing_status = xml_listing_status,
248 db_notes = xml_notes }
249 where
250 -- | Make the database \"game time\" from the XML
251 -- date/time. Simply take the day part from one and the time
252 -- from the other.
253 --
254 make_game_time d Nothing = d
255 make_game_time d (Just t) = UTCTime (utctDay d) (utctDayTime t)
256
257
258
259 --
260 -- * Database stuff.
261 --
262
263 instance DbImport Message where
264 dbmigrate _ =
265 run_dbmigrate $ do
266 migrate (undefined :: Team)
267 migrate (undefined :: ScheduleChanges)
268 migrate (undefined :: ScheduleChangesListing)
269
270 dbimport m = do
271 -- Insert the top-level message
272 msg_id <- insert_xml m
273
274 -- Now loop through the message's schedule changes
275 forM_ (xml_schedule_changes m) $ \sc -> do
276 -- Construct the function that will turn an XML listing into a DB one.
277 -- This is only partially applied without the away/home team IDs.
278 let listing_xml_to_db = from_xml_fk_sport msg_id (xml_sc_sport sc)
279
280 -- Now loop through the listings so that we can handle the teams
281 -- one listing at a time.
282 forM_ (xml_sc_listings sc) $ \listing -> do
283 away_team_id <- insert_or_select (vteam $ xml_away_team listing)
284 home_team_id <- insert_or_select (hteam $ xml_home_team listing)
285
286 -- Finish constructing the xml -> db function.
287 let listing_xml_to_db' = listing_xml_to_db away_team_id home_team_id
288 let db_listing = listing_xml_to_db' listing
289
290 insert_ db_listing
291
292 return ImportSucceeded
293
294
295 mkPersist tsn_codegen_config [groundhog|
296 - entity: ScheduleChanges
297 dbName: schedule_changes
298 constructors:
299 - name: ScheduleChanges
300 uniques:
301 - name: unique_schedule_changes
302 type: constraint
303 # Prevent multiple imports of the same message.
304 fields: [db_xml_file_id]
305
306 # Note: we drop the "sc" prefix from the db_sc_sport field.
307 - entity: ScheduleChangesListing
308 dbName: schedule_changes_listings
309 constructors:
310 - name: ScheduleChangesListing
311 fields:
312 - name: db_schedule_changes_id
313 reference:
314 onDelete: cascade
315 - name: db_away_team_id
316 reference:
317 onDelete: cascade
318 - name: db_home_team_id
319 reference:
320 onDelete: cascade
321 - name: db_sc_sport
322 dbName: sport
323 - name: db_listing_status
324 embeddedType:
325 - {name: status_numeral, dbName: status_numeral}
326 - {name: status, dbName: status}
327
328 - embedded: ScheduleChangesListingStatus
329 fields:
330 - name: db_status_numeral
331 dbName: status_numeral
332 - name: db_status
333 dbName: status
334
335 |]
336
337
338
339 --
340 -- * Pickling
341 --
342
343 -- | An (un)pickler for the \<Away_Team\> elements.
344 --
345 pickle_away_team :: PU VTeam
346 pickle_away_team =
347 xpElem "Away_Team" $
348 xpWrap (from_tuple, to_tuple) $
349 xpPair (xpAttr "AT_ID" xpText)
350 (xpOption xpText)
351 where
352 from_tuple (x,y) = VTeam (Team x Nothing y)
353 to_tuple (VTeam t) = (team_id t, name t)
354
355
356 -- | An (un)pickler for the \<Away_Team\> elements.
357 --
358 pickle_home_team :: PU HTeam
359 pickle_home_team =
360 xpElem "Home_Team" $
361 xpWrap (from_tuple, to_tuple) $
362 xpPair (xpAttr "HT_ID" xpText)
363 (xpOption xpText)
364 where
365 from_tuple (x,y) = HTeam (Team x Nothing y)
366 to_tuple (HTeam t) = (team_id t, name t)
367
368
369 -- | An (un)pickler for the \<status\> elements.
370 --
371 pickle_status :: PU ScheduleChangesListingStatus
372 pickle_status =
373 xpElem "status" $
374 xpWrap (from_tuple, to_tuple) $
375 xpPair (xpAttr "numeral" xpInt)
376 (xpOption xpText)
377 where
378 from_tuple = uncurry ScheduleChangesListingStatus
379 to_tuple s = (db_status_numeral s,
380 db_status s)
381
382
383 -- | An (un)pickler for the \<SC_Listing\> elements.
384 --
385 pickle_listing :: PU ScheduleChangesListingXml
386 pickle_listing =
387 xpElem "SC_Listing" $
388 xpWrap (from_tuple, to_tuple) $
389 xp11Tuple (xpAttr "type" xpText)
390 (xpElem "Schedule_ID" xpInt)
391 (xpElem "Game_Date" xp_date_padded)
392 (xpElem "Game_Time" xp_tba_time)
393 (xpElem "Location" (xpOption xpText))
394 pickle_away_team
395 pickle_home_team
396 (xpElem "vscore" xpInt)
397 (xpElem "hscore" xpInt)
398 pickle_status
399 (xpElem "notes" (xpOption xpText))
400 where
401 from_tuple = uncurryN ScheduleChangesListingXml
402 to_tuple l = (xml_type l,
403 xml_schedule_id l,
404 xml_game_date l,
405 xml_game_time l,
406 xml_location l,
407 xml_away_team l,
408 xml_home_team l,
409 xml_vscore l,
410 xml_hscore l,
411 xml_listing_status l,
412 xml_notes l)
413
414
415 -- | An (un)pickler for the \<Schedule_Change\> elements.
416 --
417 pickle_schedule_change :: PU ScheduleChangeXml
418 pickle_schedule_change =
419 xpElem "Schedule_Change" $
420 xpWrap (from_tuple, to_tuple) $
421 xpPair (xpAttr "Sport" xpText)
422 (xpList pickle_listing)
423 where
424 from_tuple = uncurry ScheduleChangeXml
425 to_tuple sc = (xml_sc_sport sc,
426 xml_sc_listings sc)
427
428
429 -- | Pickler for the top-level 'Message'.
430 --
431 pickle_message :: PU Message
432 pickle_message =
433 xpElem "message" $
434 xpWrap (from_tuple, to_tuple) $
435 xp6Tuple (xpElem "XML_File_ID" xpInt)
436 (xpElem "heading" xpText)
437 (xpElem "category" xpText)
438 (xpElem "sport" xpText)
439 (xpList pickle_schedule_change)
440 (xpElem "time_stamp" xp_time_stamp)
441 where
442 from_tuple = uncurryN Message
443 to_tuple m = (xml_xml_file_id m,
444 xml_heading m,
445 xml_category m,
446 xml_sport m,
447 xml_schedule_changes m,
448 xml_time_stamp m)
449
450
451
452 --
453 -- * Tests
454 --
455 -- | A list of all tests for this module.
456 --
457 schedule_changes_tests :: TestTree
458 schedule_changes_tests =
459 testGroup
460 "ScheduleChanges tests"
461 [ test_on_delete_cascade,
462 test_pickle_of_unpickle_is_identity,
463 test_unpickle_succeeds ]
464
465 -- | If we unpickle something and then pickle it, we should wind up
466 -- with the same thing we started with. WARNING: success of this
467 -- test does not mean that unpickling succeeded.
468 --
469 test_pickle_of_unpickle_is_identity :: TestTree
470 test_pickle_of_unpickle_is_identity =
471 testCase "pickle composed with unpickle is the identity" $ do
472 let path = "test/xml/Schedule_Changes_XML.xml"
473 (expected, actual) <- pickle_unpickle pickle_message path
474 actual @?= expected
475
476
477
478 -- | Make sure we can actually unpickle these things.
479 --
480 test_unpickle_succeeds :: TestTree
481 test_unpickle_succeeds =
482 testCase "unpickling succeeds" $ do
483 let path = "test/xml/Schedule_Changes_XML.xml"
484 actual <- unpickleable path pickle_message
485
486 let expected = True
487 actual @?= expected
488
489
490
491 -- | Make sure everything gets deleted when we delete the top-level
492 -- record.
493 --
494 test_on_delete_cascade :: TestTree
495 test_on_delete_cascade =
496 testCase "deleting auto_racing_results deletes its children" $ do
497 let path = "test/xml/Schedule_Changes_XML.xml"
498 results <- unsafe_unpickle path pickle_message
499 let a = undefined :: Team
500 let b = undefined :: ScheduleChanges
501 let c = undefined :: ScheduleChangesListing
502
503 actual <- withSqliteConn ":memory:" $ runDbConn $ do
504 runMigration silentMigrationLogger $ do
505 migrate a
506 migrate b
507 migrate c
508 _ <- dbimport results
509 deleteAll b
510 count_a <- countAll a
511 count_b <- countAll b
512 count_c <- countAll c
513 return $ sum [count_a, count_b, count_c]
514 let expected = 12 -- There are 16 team elements, but 4 are dupes,
515 -- so 12 unique teams should be left over.
516 actual @?= expected