2 Module : Gargantext.Database.Tree
3 Description : Tree of Resource Nodes built from Database
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Let a Root Node, return the Tree of the Node as a directed acyclic graph
15 {-# LANGUAGE FlexibleContexts #-}
16 {-# LANGUAGE NoImplicitPrelude #-}
17 {-# LANGUAGE OverloadedStrings #-}
18 {-# LANGUAGE QuasiQuotes #-}
19 {-# LANGUAGE RankNTypes #-}
21 module Gargantext.Database.Action.Query.Tree
24 import Control.Lens (Prism', (#), (^..), at, each, _Just, to)
25 import Control.Monad.Error.Class (MonadError(throwError))
26 import Data.Map (Map, fromListWith, lookup)
27 import Data.Text (Text)
28 import Database.PostgreSQL.Simple
29 import Database.PostgreSQL.Simple.SqlQQ
30 import Gargantext.Core.Types.Main (NodeTree(..), Tree(..))
31 import Gargantext.Database.Admin.Types.Node -- (pgNodeId, NodeType(..))
32 import Gargantext.Database.Admin.Config (fromNodeTypeId, nodeTypeId)
33 import Gargantext.Database.Admin.Types.Node (NodeId, NodeType, DocId, allNodeTypes)
34 import Gargantext.Database.Admin.Utils (Cmd, runPGSQuery)
35 import Gargantext.Prelude
37 ------------------------------------------------------------------------
39 findCorpus :: RootId -> Cmd err (Maybe CorpusId)
41 _mapNodes <- toTreeParent <$> dbTree r []
47 ------------------------------------------------------------------------
48 data TreeError = NoRoot | EmptyRoot | TooManyRoots
51 class HasTreeError e where
52 _TreeError :: Prism' e TreeError
54 treeError :: ( MonadError e m
58 treeError te = throwError $ _TreeError # te
60 -- | Returns the Tree of Nodes in Database
61 treeDB :: HasTreeError err
64 -> Cmd err (Tree NodeTree)
65 treeDB r nodeTypes = toTree =<< (toTreeParent <$> dbTree r nodeTypes)
67 ------------------------------------------------------------------------
68 toTree :: ( MonadError e m
70 => Map (Maybe ParentId) [DbTreeNode]
73 case lookup Nothing m of
74 Just [n] -> pure $ toTree' m n
75 Nothing -> treeError NoRoot
76 Just [] -> treeError EmptyRoot
77 Just _ -> treeError TooManyRoots
79 toTree' :: Map (Maybe ParentId) [DbTreeNode]
83 TreeN (toNodeTree n) $
84 m ^.. at (Just $ dt_nodeId n) . _Just . each . to (toTree' m)
86 ------------------------------------------------------------------------
87 toNodeTree :: DbTreeNode
89 toNodeTree (DbTreeNode nId tId _ n) = NodeTree n nodeType nId
91 nodeType = fromNodeTypeId tId
92 ------------------------------------------------------------------------
93 toTreeParent :: [DbTreeNode]
94 -> Map (Maybe ParentId) [DbTreeNode]
95 toTreeParent = fromListWith (<>) . map (\n -> (dt_parentId n, [n]))
96 ------------------------------------------------------------------------
97 data DbTreeNode = DbTreeNode { dt_nodeId :: NodeId
99 , dt_parentId :: Maybe NodeId
103 -- | Main DB Tree function
104 -- TODO add typenames as parameters
107 -> Cmd err [DbTreeNode]
108 dbTree rootId nodeTypes = map (\(nId, tId, pId, n) -> DbTreeNode nId tId pId n)
109 <$> runPGSQuery [sql|
111 tree (id, typename, parent_id, name) AS
113 SELECT p.id, p.typename, p.parent_id, p.name
119 SELECT c.id, c.typename, c.parent_id, c.name
122 INNER JOIN tree AS s ON c.parent_id = s.id
123 WHERE c.typename IN ?
126 |] (rootId, In typename)
128 typename = map nodeTypeId ns
129 ns = case nodeTypes of
131 -- [2, 20, 21, 22, 3, 5, 30, 31, 40, 7, 9, 90, 71]
134 isDescendantOf :: NodeId -> RootId -> Cmd err Bool
135 isDescendantOf childId rootId = (== [Only True])
136 <$> runPGSQuery [sql|
138 SET TRANSACTION READ ONLY;
142 tree (id, parent_id) AS
144 SELECT c.id, c.parent_id
150 SELECT p.id, p.parent_id
152 INNER JOIN tree AS t ON t.parent_id = p.id
155 SELECT COUNT(*) = 1 from tree AS t
159 -- TODO should we check the category?
160 isIn :: NodeId -> DocId -> Cmd err Bool
161 isIn cId docId = ( == [Only True])
162 <$> runPGSQuery [sql| SELECT COUNT(*) = 1
164 WHERE nn.node1_id = ?