1 {-# LANGUAGE OverloadedStrings, RecordWildCards #-}
    2 module Model.Citation.Types
    3   ( Citation(..)
    4   , makeVolumeCitation
    5   ) where
    6 
    7 import Control.Applicative ((<|>))
    8 import Data.Int (Int16)
    9 import Data.Monoid ((<>))
   10 import qualified Data.Text as T
   11 
   12 import qualified JSON
   13 import Model.URL (URI)
   14 import Model.Volume.Types
   15 
   16 data Citation = Citation
   17   { citationHead :: T.Text
   18   , citationURL :: Maybe URI
   19   , citationYear :: Maybe Int16 -- ^ The year of the published work that this volume is connected to.
   20                                 -- This field doesn't apply to general links added to a volume.
   21   , citationTitle :: Maybe T.Text -- ^ When the Citation is a link to a published version of this volume,
   22                                   -- then this value is the volume's title. This field doesn't apply to general
   23                                   -- links added to a volume
   24   }
   25 
   26 makeVolumeCitation :: Volume -> Maybe (Maybe T.Text -> Citation) -> (Volume, Maybe Citation)
   27 makeVolumeCitation v cf = (v, cf <*> Just (Just (volumeName $ volumeRow v)))
   28 
   29 instance Monoid Citation where
   30   mempty = Citation
   31     { citationHead = T.empty
   32     , citationURL = Nothing
   33     , citationYear = Nothing
   34     , citationTitle = Nothing
   35     }
   36   mappend a b = Citation
   37     { citationHead = if T.null (citationHead a) then citationHead b else citationHead a
   38     , citationURL = citationURL a <|> citationURL b
   39     , citationYear = citationYear a <|> citationYear b
   40     , citationTitle = citationTitle a <|> citationTitle b
   41     }
   42 
   43 instance JSON.ToJSON Citation where
   44     toJSON Citation{..} =
   45         JSON.object
   46             (   "head" JSON..= citationHead
   47              <> "title" `JSON.kvObjectOrEmpty` citationTitle
   48              <> "url" `JSON.kvObjectOrEmpty` citationURL
   49              <> "year" `JSON.kvObjectOrEmpty` citationYear)