]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Types/Node.hs
[CORS] Adding CORS for test with frontend/Purescript.
[gargantext.git] / src / Gargantext / Types / Node.hs
1 {-|
2 Module : Gargantext.Types.Nodes
3 Description : Main Types of Nodes
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -}
11
12 {-# LANGUAGE DeriveGeneric #-}
13 {-# LANGUAGE TemplateHaskell #-}
14 {-# LANGUAGE OverloadedStrings #-}
15 {-# LANGUAGE FlexibleInstances #-}
16 -- {-# LANGUAGE DuplicateRecordFields #-}
17
18 module Gargantext.Types.Node where
19
20 import GHC.Generics (Generic)
21
22 import Control.Lens hiding (elements)
23 import qualified Control.Lens as L
24 import Data.Aeson
25 import Data.Aeson (Value(),toJSON)
26 import Data.Aeson.TH (deriveJSON)
27 import Data.ByteString.Lazy (ByteString)
28 import Data.Either
29 import Data.Eq (Eq)
30 import Data.Text (Text, unpack)
31 import Data.Time (UTCTime)
32 import Data.Time.Segment (jour)
33 import Data.Swagger
34
35 import Text.Read (read)
36 import Text.Show (Show())
37
38 import Servant
39
40 import Test.QuickCheck.Arbitrary
41 import Test.QuickCheck (elements)
42
43 import Gargantext.Prelude
44 import Gargantext.Utils.Prefix (unPrefix)
45
46 ------------------------------------------------------------------------
47 data Status = Status { status_date :: Maybe UTCTime
48 , status_error :: Maybe Text
49 , status_action :: Maybe Text
50 , status_complete :: Maybe Bool
51 , status_progress :: Maybe Int
52 } deriving (Show, Generic)
53 $(deriveJSON (unPrefix "status_") ''Status)
54
55 instance Arbitrary Status where
56 arbitrary = elements [Status Nothing Nothing Nothing Nothing Nothing]
57
58
59 ------------------------------------------------------------------------
60 data HyperdataDocument = HyperdataDocument { hyperdataDocument_bdd :: Maybe Text
61 , hyperdataDocument_doi :: Maybe Text
62 , hyperdataDocument_url :: Maybe Text
63 , hyperdataDocument_page :: Maybe Int
64 , hyperdataDocument_title :: Maybe Text
65 , hyperdataDocument_authors :: Maybe Text
66 , hyperdataDocument_abstract :: Maybe Text
67 , hyperdataDocument_statuses :: Maybe [Status]
68 , hyperdataDocument_publication_date :: Maybe Text
69 , hyperdataDocument_publication_year :: Maybe Double
70 , hyperdataDocument_publication_month :: Maybe Double
71 , hyperdataDocument_publication_hour :: Maybe Double
72 , hyperdataDocument_publication_minute :: Maybe Double
73 , hyperdataDocument_publication_second :: Maybe Double
74 , hyperdataDocument_languageIso2 :: Maybe Text
75 } deriving (Show, Generic)
76 $(deriveJSON (unPrefix "hyperdataDocument_") ''HyperdataDocument)
77
78 hyperdataDocuments :: [HyperdataDocument]
79 hyperdataDocuments = [HyperdataDocument Nothing Nothing Nothing Nothing (Just "Title")
80 Nothing (Just "Abstract") Nothing Nothing
81 Nothing Nothing Nothing Nothing Nothing Nothing
82 ]
83
84
85 instance Arbitrary HyperdataDocument where
86 arbitrary = elements hyperdataDocuments
87
88 ------------------------------------------------------------------------
89 data LanguageNodes = LanguageNodes { languageNodes___unknown__ :: [Int]}
90 deriving (Show, Generic)
91 $(deriveJSON (unPrefix "languageNodes_") ''LanguageNodes)
92
93
94 ------------------------------------------------------------------------
95
96 data Resource = Resource { resource_url :: Maybe Text
97 , resource_path :: Maybe Text
98 , resource_type :: Maybe Int
99 , resource_extracted :: Maybe Bool
100 } deriving (Show, Generic)
101 $(deriveJSON (unPrefix "resource_") ''Resource)
102
103 instance Arbitrary Resource where
104 arbitrary = elements [Resource Nothing Nothing Nothing Nothing]
105
106 data HyperdataCorpus = HyperdataCorpus { hyperdataCorpus_action :: Maybe Text
107 , hyperdataCorpus_statuses :: Maybe [Status]
108 , hyperdataCorpus_languages :: Maybe LanguageNodes
109 , hyperdataCorpus_resources :: Maybe [Resource]
110 , hyperdataCorpus_language_id :: Maybe Text
111 , hyperdataCorpus_skipped_docs :: Maybe [Int]
112 } deriving (Show, Generic)
113 $(deriveJSON (unPrefix "hyperdataCorpus_") ''HyperdataCorpus)
114
115
116 data HyperdataUser = HyperdataUser { hyperdataUser_language :: Maybe Text
117 } deriving (Show, Generic)
118 $(deriveJSON (unPrefix "hyperdataUser_") ''HyperdataUser)
119
120 -- Preferences ?
121
122 data HyperdataFolder = HyperdataFolder { hyperdataFolder_preferences :: Maybe Text
123 } deriving (Show, Generic)
124 $(deriveJSON (unPrefix "hyperdataFolder_") ''HyperdataFolder)
125
126
127 data HyperdataProject = HyperdataProject { hyperdataProject_preferences :: Maybe Text
128 } deriving (Show, Generic)
129 $(deriveJSON (unPrefix "hyperdataProject_") ''HyperdataProject)
130
131
132
133 data HyperdataList = HyperdataList { hyperdataList_preferences :: Maybe Text
134 } deriving (Show, Generic)
135 $(deriveJSON (unPrefix "hyperdataList_") ''HyperdataList)
136
137 data HyperdataScore = HyperdataScore { hyperdataScore_preferences :: Maybe Text
138 } deriving (Show, Generic)
139 $(deriveJSON (unPrefix "hyperdataScore_") ''HyperdataScore)
140
141
142
143 data HyperdataFavorites = HyperdataFavorites { hyperdataFavorites_preferences :: Maybe Text
144 } deriving (Show, Generic)
145 $(deriveJSON (unPrefix "hyperdataFavorites_") ''HyperdataFavorites)
146
147 data HyperdataResource = HyperdataResource { hyperdataResource_preferences :: Maybe Text
148 } deriving (Show, Generic)
149 $(deriveJSON (unPrefix "hyperdataResource_") ''HyperdataResource)
150
151
152
153 -- TODO add the Graph Structure here
154 data HyperdataGraph = HyperdataGraph { hyperdataGraph_preferences :: Maybe Text
155 } deriving (Show, Generic)
156 $(deriveJSON (unPrefix "hyperdataGraph_") ''HyperdataGraph)
157
158
159 -- TODO add the Graph Structure here
160 data HyperdataPhylo = HyperdataPhylo { hyperdataPhylo_preferences :: Maybe Text
161 } deriving (Show, Generic)
162 $(deriveJSON (unPrefix "hyperdataPhylo_") ''HyperdataPhylo)
163
164 -- | TODO FEATURE: Notebook saved in the node (to work with Python or Haskell)
165 data HyperdataNotebook = HyperdataNotebook { hyperdataNotebook_preferences :: Maybe Text
166 } deriving (Show, Generic)
167 $(deriveJSON (unPrefix "hyperdataNotebook_") ''HyperdataNotebook)
168
169
170
171 -- | NodePoly indicates that Node has a Polymorphism Type
172 type Node json = NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime json -- NodeVector
173
174 -- type Node json = NodePoly NodeId NodeTypeId UserId ParentId NodeName UTCTime json
175 type NodeTypeId = Int
176 type NodeId = Int
177 type NodeParentId = Int
178 type NodeUserId = Int
179 type NodeName = Text
180 --type NodeVector = Vector
181
182 --type NodeUser = Node HyperdataUser
183
184 -- | Then a Node can be either a Folder or a Corpus or a Document
185 type NodeUser = Node HyperdataUser
186 type Folder = Node HyperdataFolder
187 type Project = Folder -- NP Node HyperdataProject ?
188 type Corpus = Node HyperdataCorpus
189 type Document = Node HyperdataDocument
190
191 ------------------------------------------------------------------------
192 data NodeType = NodeUser | Project | Corpus | Document | DocumentCopy
193 | Classification
194 | Lists
195 | Metrics | Occurrences
196 deriving (Show, Read, Eq, Generic)
197
198 instance FromJSON NodeType
199 instance ToJSON NodeType
200
201 instance FromHttpApiData NodeType
202 where
203 parseUrlPiece = Right . read . unpack
204
205 instance ToParamSchema NodeType
206 instance ToSchema NodeType
207
208 ------------------------------------------------------------------------
209 data NodePoly id typename userId parentId name date hyperdata = Node { node_id :: id
210 , node_typename :: typename
211 , node_userId :: userId
212 -- , nodeHashId :: hashId
213 , node_parentId :: parentId
214 , node_name :: name
215 , node_date :: date
216 , node_hyperdata :: hyperdata
217 -- , node_titleAbstract :: titleAbstract
218 } deriving (Show, Generic)
219 $(deriveJSON (unPrefix "node_") ''NodePoly)
220
221 instance Arbitrary (NodePoly NodeId NodeTypeId (Maybe NodeUserId) NodeParentId NodeName UTCTime Value) where
222 arbitrary = elements [Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) (toJSON ("{}"::Text))]
223
224
225 instance Arbitrary (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime Value) where
226 arbitrary = elements [Node 1 1 1 (Just 1) "name" (jour 2018 01 01) (toJSON ("{}"::Text))]
227
228 instance Arbitrary (NodePoly NodeId NodeTypeId (Maybe NodeUserId) NodeParentId NodeName UTCTime HyperdataDocument) where
229 arbitrary = elements [Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) ((hyperdataDocument))]
230
231
232 instance Arbitrary (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime HyperdataDocument) where
233 arbitrary = elements [Node 1 1 1 (Just 1) "name" (jour 2018 01 01) hyperdataDocument]
234
235
236 ------------------------------------------------------------------------
237 hyperdataDocument :: HyperdataDocument
238 hyperdataDocument = case decode docExample of
239 Just hp -> hp
240 Nothing -> HyperdataDocument Nothing Nothing Nothing Nothing
241 Nothing Nothing Nothing Nothing
242 Nothing Nothing Nothing Nothing
243 Nothing Nothing Nothing
244 docExample :: ByteString
245 docExample = "{\"publication_day\":6,\"language_iso2\":\"en\",\"publication_minute\":0,\"publication_month\":7,\"language_iso3\":\"eng\",\"publication_second\":0,\"authors\":\"Nils Hovdenak, Kjell Haram\",\"publication_year\":2012,\"publication_date\":\"2012-07-06 00:00:00+00:00\",\"language_name\":\"English\",\"statuses\":[],\"realdate_full_\":\"2012 01 12\",\"source\":\"European journal of obstetrics, gynecology, and reproductive biology\",\"abstract\":\"The literature was searched for publications on minerals and vitamins during pregnancy and the possible influence of supplements on pregnancy outcome.\",\"title\":\"Influence of mineral and vitamin supplements on pregnancy outcome.\",\"publication_hour\":0}"
246
247
248 instance ToSchema HyperdataDocument where
249 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
250 L.& mapped.schema.description ?~ "a document"
251 L.& mapped.schema.example ?~ toJSON hyperdataDocument
252
253
254 instance ToSchema (NodePoly NodeId NodeTypeId NodeUserId
255 (Maybe NodeParentId) NodeName
256 UTCTime HyperdataDocument
257 )
258
259 instance ToSchema (NodePoly NodeId NodeTypeId
260 (Maybe NodeUserId)
261 NodeParentId NodeName
262 UTCTime HyperdataDocument
263 )
264
265 instance ToSchema Status
266
267