]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/DbImport.hs
Create an ImportResult type and refactor things around it.
[dead/htsn-import.git] / src / TSN / DbImport.hs
1 -- | Definition of the DbImport typeclass.
2 module TSN.DbImport
3 where
4
5 import Control.Monad.IO.Class ( MonadIO )
6 import Database.Groundhog (
7 defaultMigrationLogger,
8 insert,
9 migrate,
10 runMigration )
11 import Database.Groundhog.Core ( PersistBackend, PersistEntity )
12 import Text.XML.HXT.Core (
13 XmlPickler,
14 XmlTree,
15 unpickleDoc,
16 xpickle )
17
18
19 -- | The type that will be returned from every file import attempt. If
20 -- there was an error, its description will be wrapped in an Err. If
21 -- we successfully imported records, the number of records imported
22 -- will be wrapped in a Succ.
23 --
24 -- Anything else will be wrapped in a "Info" constructor;
25 -- i.e. somewhere between success and failure. This is like an
26 -- 'Either' with three choices. A "Info" return value means that
27 -- the XML document *was* processed, so it should be removed.
28 --
29 data ImportResult = Err String | Info String | Succ Int
30
31 -- | Instances of this type know how to insert themselves into a
32 -- Groundhog database.
33 class DbImport a where
34 dbimport :: (MonadIO m, PersistBackend m)
35 => a
36 -> XmlTree
37 -> m ImportResult
38
39
40 -- | We put the 'Configuration' and 'XmlTree' arguments last so that
41 -- it's easy to eta reduce all of the import_foo functions that call
42 -- this.
43 --
44 import_generic :: (XmlPickler a, MonadIO m, PersistEntity b, PersistBackend m)
45 => (a -> [b]) -- ^ listings getter
46 -> b -- ^ Dummy Listing instance needed for 'migrate'
47 -> XmlTree
48 -> m ImportResult -- ^ Return the number of records inserted.
49 import_generic g dummy xml = do
50 runMigration defaultMigrationLogger $ migrate dummy
51 let root_element = unpickleDoc xpickle xml
52 case root_element of
53 Nothing -> return $ Err "Could not unpickle document in import_generic."
54 Just elt -> do
55 ids <- mapM insert (g elt)
56 return $ Succ (length ids)