module Database.Drasil.Dump where

import Language.Drasil (UID, HasUID(..))
import Database.Drasil.ChunkDB (refTable, labelledcontentTable, sectionTable, 
  conceptinsTable, theoryModelTable, gendefTable, insmodelTable, dataDefnTable,
  unitTable, UMap, ChunkDB(termTable, symbolTable))

import Data.Map.Strict (Map, insert)
import qualified Data.Map.Strict as SM

import Control.Lens ((^.))

type ChunkType = String
type DumpedChunkDB = Map ChunkType [UID]

umapDump :: HasUID a => UMap a -> [UID]
umapDump :: forall a. HasUID a => UMap a -> [UID]
umapDump = ((a, Int) -> UID) -> [(a, Int)] -> [UID]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> Getting UID a UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID a UID
forall c. HasUID c => Getter c UID
Getter a UID
uid) (a -> UID) -> ((a, Int) -> a) -> (a, Int) -> UID
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, Int) -> a
forall a b. (a, b) -> a
fst) ([(a, Int)] -> [UID]) -> (UMap a -> [(a, Int)]) -> UMap a -> [UID]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UMap a -> [(a, Int)]
forall k a. Map k a -> [a]
SM.elems

dumpChunkDB :: ChunkDB -> DumpedChunkDB
dumpChunkDB :: ChunkDB -> Map ChunkType [UID]
dumpChunkDB ChunkDB
cdb = 
      ChunkType -> [UID] -> Map ChunkType [UID] -> Map ChunkType [UID]
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"symbols" (UMap QuantityDict -> [UID]
forall a. HasUID a => UMap a -> [UID]
umapDump (UMap QuantityDict -> [UID]) -> UMap QuantityDict -> [UID]
forall a b. (a -> b) -> a -> b
$ ChunkDB -> UMap QuantityDict
symbolTable ChunkDB
cdb)
    (Map ChunkType [UID] -> Map ChunkType [UID])
-> Map ChunkType [UID] -> Map ChunkType [UID]
forall a b. (a -> b) -> a -> b
$ ChunkType -> [UID] -> Map ChunkType [UID] -> Map ChunkType [UID]
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"terms" (UMap IdeaDict -> [UID]
forall a. HasUID a => UMap a -> [UID]
umapDump (UMap IdeaDict -> [UID]) -> UMap IdeaDict -> [UID]
forall a b. (a -> b) -> a -> b
$ ChunkDB -> UMap IdeaDict
termTable ChunkDB
cdb)
    (Map ChunkType [UID] -> Map ChunkType [UID])
-> Map ChunkType [UID] -> Map ChunkType [UID]
forall a b. (a -> b) -> a -> b
$ ChunkType -> [UID] -> Map ChunkType [UID] -> Map ChunkType [UID]
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"concepts" (UMap ConceptInstance -> [UID]
forall a. HasUID a => UMap a -> [UID]
umapDump (UMap ConceptInstance -> [UID]) -> UMap ConceptInstance -> [UID]
forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb ChunkDB
-> Getting (UMap ConceptInstance) ChunkDB (UMap ConceptInstance)
-> UMap ConceptInstance
forall s a. s -> Getting a s a -> a
^. Getting (UMap ConceptInstance) ChunkDB (UMap ConceptInstance)
Lens' ChunkDB (UMap ConceptInstance)
conceptinsTable)
    (Map ChunkType [UID] -> Map ChunkType [UID])
-> Map ChunkType [UID] -> Map ChunkType [UID]
forall a b. (a -> b) -> a -> b
$ ChunkType -> [UID] -> Map ChunkType [UID] -> Map ChunkType [UID]
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"units" (UMap UnitDefn -> [UID]
forall a. HasUID a => UMap a -> [UID]
umapDump (UMap UnitDefn -> [UID]) -> UMap UnitDefn -> [UID]
forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb ChunkDB
-> Getting (UMap UnitDefn) ChunkDB (UMap UnitDefn) -> UMap UnitDefn
forall s a. s -> Getting a s a -> a
^. Getting (UMap UnitDefn) ChunkDB (UMap UnitDefn)
Lens' ChunkDB (UMap UnitDefn)
unitTable)
    (Map ChunkType [UID] -> Map ChunkType [UID])
-> Map ChunkType [UID] -> Map ChunkType [UID]
forall a b. (a -> b) -> a -> b
$ ChunkType -> [UID] -> Map ChunkType [UID] -> Map ChunkType [UID]
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"dataDefinitions" (UMap DataDefinition -> [UID]
forall a. HasUID a => UMap a -> [UID]
umapDump (UMap DataDefinition -> [UID]) -> UMap DataDefinition -> [UID]
forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb ChunkDB
-> Getting (UMap DataDefinition) ChunkDB (UMap DataDefinition)
-> UMap DataDefinition
forall s a. s -> Getting a s a -> a
^. Getting (UMap DataDefinition) ChunkDB (UMap DataDefinition)
Lens' ChunkDB (UMap DataDefinition)
dataDefnTable)
    (Map ChunkType [UID] -> Map ChunkType [UID])
-> Map ChunkType [UID] -> Map ChunkType [UID]
forall a b. (a -> b) -> a -> b
$ ChunkType -> [UID] -> Map ChunkType [UID] -> Map ChunkType [UID]
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"instanceModels" (UMap InstanceModel -> [UID]
forall a. HasUID a => UMap a -> [UID]
umapDump (UMap InstanceModel -> [UID]) -> UMap InstanceModel -> [UID]
forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb ChunkDB
-> Getting (UMap InstanceModel) ChunkDB (UMap InstanceModel)
-> UMap InstanceModel
forall s a. s -> Getting a s a -> a
^. Getting (UMap InstanceModel) ChunkDB (UMap InstanceModel)
Lens' ChunkDB (UMap InstanceModel)
insmodelTable)
    (Map ChunkType [UID] -> Map ChunkType [UID])
-> Map ChunkType [UID] -> Map ChunkType [UID]
forall a b. (a -> b) -> a -> b
$ ChunkType -> [UID] -> Map ChunkType [UID] -> Map ChunkType [UID]
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"generalDefinitions" (UMap GenDefn -> [UID]
forall a. HasUID a => UMap a -> [UID]
umapDump (UMap GenDefn -> [UID]) -> UMap GenDefn -> [UID]
forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb ChunkDB
-> Getting (UMap GenDefn) ChunkDB (UMap GenDefn) -> UMap GenDefn
forall s a. s -> Getting a s a -> a
^. Getting (UMap GenDefn) ChunkDB (UMap GenDefn)
Lens' ChunkDB (UMap GenDefn)
gendefTable)
    (Map ChunkType [UID] -> Map ChunkType [UID])
-> Map ChunkType [UID] -> Map ChunkType [UID]
forall a b. (a -> b) -> a -> b
$ ChunkType -> [UID] -> Map ChunkType [UID] -> Map ChunkType [UID]
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"theoryModels" (UMap TheoryModel -> [UID]
forall a. HasUID a => UMap a -> [UID]
umapDump (UMap TheoryModel -> [UID]) -> UMap TheoryModel -> [UID]
forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb ChunkDB
-> Getting (UMap TheoryModel) ChunkDB (UMap TheoryModel)
-> UMap TheoryModel
forall s a. s -> Getting a s a -> a
^. Getting (UMap TheoryModel) ChunkDB (UMap TheoryModel)
Lens' ChunkDB (UMap TheoryModel)
theoryModelTable)
    (Map ChunkType [UID] -> Map ChunkType [UID])
-> Map ChunkType [UID] -> Map ChunkType [UID]
forall a b. (a -> b) -> a -> b
$ ChunkType -> [UID] -> Map ChunkType [UID] -> Map ChunkType [UID]
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"conceptInstances" (UMap ConceptInstance -> [UID]
forall a. HasUID a => UMap a -> [UID]
umapDump (UMap ConceptInstance -> [UID]) -> UMap ConceptInstance -> [UID]
forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb ChunkDB
-> Getting (UMap ConceptInstance) ChunkDB (UMap ConceptInstance)
-> UMap ConceptInstance
forall s a. s -> Getting a s a -> a
^. Getting (UMap ConceptInstance) ChunkDB (UMap ConceptInstance)
Lens' ChunkDB (UMap ConceptInstance)
conceptinsTable)
    (Map ChunkType [UID] -> Map ChunkType [UID])
-> Map ChunkType [UID] -> Map ChunkType [UID]
forall a b. (a -> b) -> a -> b
$ ChunkType -> [UID] -> Map ChunkType [UID] -> Map ChunkType [UID]
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"sections" (UMap Section -> [UID]
forall a. HasUID a => UMap a -> [UID]
umapDump (UMap Section -> [UID]) -> UMap Section -> [UID]
forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb ChunkDB
-> Getting (UMap Section) ChunkDB (UMap Section) -> UMap Section
forall s a. s -> Getting a s a -> a
^. Getting (UMap Section) ChunkDB (UMap Section)
Lens' ChunkDB (UMap Section)
sectionTable)
    (Map ChunkType [UID] -> Map ChunkType [UID])
-> Map ChunkType [UID] -> Map ChunkType [UID]
forall a b. (a -> b) -> a -> b
$ ChunkType -> [UID] -> Map ChunkType [UID] -> Map ChunkType [UID]
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"labelledContent" (UMap LabelledContent -> [UID]
forall a. HasUID a => UMap a -> [UID]
umapDump (UMap LabelledContent -> [UID]) -> UMap LabelledContent -> [UID]
forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb ChunkDB
-> Getting (UMap LabelledContent) ChunkDB (UMap LabelledContent)
-> UMap LabelledContent
forall s a. s -> Getting a s a -> a
^. Getting (UMap LabelledContent) ChunkDB (UMap LabelledContent)
Lens' ChunkDB (UMap LabelledContent)
labelledcontentTable)
    (Map ChunkType [UID] -> Map ChunkType [UID])
-> Map ChunkType [UID] -> Map ChunkType [UID]
forall a b. (a -> b) -> a -> b
$ ChunkType -> [UID] -> Map ChunkType [UID] -> Map ChunkType [UID]
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert ChunkType
"references" (UMap Reference -> [UID]
forall a. HasUID a => UMap a -> [UID]
umapDump (UMap Reference -> [UID]) -> UMap Reference -> [UID]
forall a b. (a -> b) -> a -> b
$ ChunkDB
cdb ChunkDB
-> Getting (UMap Reference) ChunkDB (UMap Reference)
-> UMap Reference
forall s a. s -> Getting a s a -> a
^. Getting (UMap Reference) ChunkDB (UMap Reference)
Lens' ChunkDB (UMap Reference)
refTable)
      Map ChunkType [UID]
forall a. Monoid a => a
mempty