server/src/app/Models/Document.php
changeset 19 eadaf0b8f02e
parent 18 f2a40bbc27f6
child 20 a9b98b16b053
--- a/server/src/app/Models/Document.php	Tue Nov 17 13:11:55 2015 +0100
+++ b/server/src/app/Models/Document.php	Fri Nov 27 17:59:36 2015 +0100
@@ -25,86 +25,152 @@
 
     private $id = null;
 
+    // memoization
+    private $providedCHO = null;
+    private $title = false;
+    private $publishers = null;
+    private $mediaArray = null;
+    private $issued = null;
+    private $modified = null;
+
+    public function getProvidedCHO() {
+        if(is_null($this->providedCHO)) {
+            $this->providedCHO = $this->get("<http://www.europeana.eu/schemas/edm/aggregatedCHO>");
+        }
+        return $this->providedCHO;
+    }
+
+    private function clearMemoizationCache() {
+        $this->providedCHO = null;
+        $this->title = false;
+        $this->publishers = null;
+        $this->mediaArray = null;
+        $this->issued = null;
+        $this->modified = null;
+    }
+
     public function getId() {
         if(is_null($this->id)) {
-            $this->id = CocoonUtils::getIdFromUri($this->uri);
+            $this->id = CocoonUtils::getIdFromCorpusUri($this->uri);
         }
         return $this->id;
     }
 
     public function getTitle() {
-        try {
-            return $this->getLiteral('<http://purl.org/dc/elements/1.1/title>');
-        } catch(\Exception $e) {
-            return null;
+        if($this->title === false) {
+            try {
+                $this->title = $this->getProvidedCHO()->getLiteral('<http://purl.org/dc/elements/1.1/title>');
+            } catch(\Exception $e) {
+                $this->title = null;
+            }
         }
+        return $this->title;
     }
 
     public function getPublishers() {
-        try {
-            return $this->allLiterals('dc11:publisher');
-        } catch(\Exception $e) {
-           return [];
-       }
+        if(is_null($this->publishers)) {
+            try {
+                $this->publishers = $this->getProvidedCHO()->all('dc11:publisher');
+            } catch(\Exception $e) {
+               $this->publishers = [];
+            }
+        }
+        return $this->publishers;
+    }
+
+    public function getIssued() {
+        if(is_null($this->issued)) {
+            try {
+                $this->issued = $this->getProvidedCHO()->getLiteral("<http://purl.org/dc/terms/issued>");
+            } catch(\Exception $e) {
+                $this->issued = null;
+            }
+        }
+        return $this->issued;
+    }
+
+    public function getModified() {
+        if(is_null($this->modified)) {
+            try {
+                $this->modified = $this->getProvidedCHO()->getLiteral("<http://purl.org/dc/terms/modified>");
+                if(is_null($this->modified)) {
+                    $this->modified = $this->getIssued();
+                }
+                else {
+                    $this->modified = $this->modified->getValue();
+                }
+            } catch(\Exception $e) {
+                $this->modified = null;
+            }
+        }
+        return $this->modified;
     }
 
     public function getMediaArray() {
 
-        //TODO: add media type
-        $res = [];
-        $formats = [];
-        try {
-            $formats = $this->allResources("dc:isFormatOf");
-        } catch(\Exception $e) {
-            // do nothing
+        if(is_null($this->mediaArray)) {
+            //TODO: add media type
+            $this->mediaArray = [];
+
+            $master = $this->get('<http://www.europeana.eu/schemas/edm/isShownBy>');
+            $masterUrl = is_null($master)?null:$master->getUri();
+
+            foreach($this->graph->allOfType("<http://www.europeana.eu/schemas/edm/WebResources>") as $webResource) {
+                $extent = $webResource->getLiteral("dc:extent");
+                $extent = is_null($extent)?null:$extent->getValue();
+                $extent_ms = Utils::iso8601IntervalToMillis($extent);
+                $format = $webResource->getLiteral("dc11:format");
+
+                $this->mediaArray[$webResource->getUri()] = [
+                    'url' => $webResource->getUri(),
+                    'format' => is_null($format)?null:$format->getValue(),
+                    'extent' => $extent,
+                    'extent_ms' => $extent_ms,
+                    'master' => (($webResource->getUri() === $masterUrl)?true:false)
+                ];
+            }
         }
-        foreach ($formats as $f) {
-            $uri = $f->getUri();
-            $mimetype = Utils::getMimetype($uri);
-            array_push($res, ["url" => $uri, "format" => $mimetype]);
-        }
-
-        $format = null;
-        try {
-            $format = $this->getLiteral('dc11:format');
-        } catch(\Exception $e) {
-            // do nothing
-        }
-        array_push($res, ["url" => $this->getUri(), "format" => $format]);
-        return $res;
+        return $this->mediaArray;
     }
 
     public function getTypes() {
-        return $this->all('dc11:type');
+        return $this->getProvidedCHO()->all('dc11:type');
     }
 
     public function getDiscourseTypes() {
         return array_values(array_filter($this->getTypes(), function($v) {
-            return $v instanceof Literal && $v->getDatatypeUri() === Config::get('constants.OLAC_DISCOURSE_TYPE')['uri'];
+            return $v instanceof Literal && $v->getDatatypeUri() === Config::get('corpusparole.olac_discourse_type')['uri'];
         }));
     }
 
     public function getOtherTypes() {
         $res = array_values(array_filter($this->getTypes(), function($v) {
-            return $v instanceof Resource || $v->getDatatypeUri() !== Config::get('constants.OLAC_DISCOURSE_TYPE')['uri'];
+            return $v instanceof Resource || $v->getDatatypeUri() !== Config::get('corpusparole.olac_discourse_type')['uri'];
         }));
         return $res;
     }
 
+    /**
+     * change discourse type list
+     */
     public function updateDiscourseTypes(array $discoursesTypes) {
 
         $this->startDelta();
 
+        //delete
         foreach($this->getDiscourseTypes() as $discourseType) {
-            $this->delete('dc11:type', $discourseType);
-            $this->currentDelta->getDeletedGraph()->add($this, 'dc11:type', new Literal($discourseType, null, Config::get('constants.OLAC_DISCOURSE_TYPE')['uri']));
+            $literalValue = new Literal($discourseType, null, Config::get('corpusparole.olac_discourse_type')['uri']);
+            $this->getProvidedCHO()->delete('dc11:type', $literalValue);
+            $this->currentDelta->getDeletedGraph()->add($this->getProvidedCHO(), 'dc11:type', new Literal($discourseType, null, Config::get('corpusparole.olac_discourse_type')['uri']));
         }
-        // re-add them
 
+        // and re-add them
         foreach($discoursesTypes as $dType) {
-            $this->add('dc11:type', new Literal($dType, null, Config::get('constants.OLAC_DISCOURSE_TYPE')['uri']));
-            $this->currentDelta->getAddedGraph()->add($this, 'dc11:type', new Literal($dType, null, Config::get('constants.OLAC_DISCOURSE_TYPE')['uri']));
+            $this->getProvidedCHO()->add('dc11:type', new Literal($dType, null, Config::get('corpusparole.olac_discourse_type')['uri']));
+            $this->currentDelta->getAddedGraph()->add($this->getProvidedCHO(), 'dc11:type', new Literal($dType, null, Config::get('corpusparole.olac_discourse_type')['uri']));
         }
+
+        $this->clearMemoizationCache();
     }
 
     public function isIsomorphic($doc) {
@@ -142,6 +208,7 @@
                 'id' => $this->getId(),
                 'uri' => $this->getUri(),
                 'title' => $this->getTitle()->getValue(),
+                'modified' => $this->getModified(),
                 'publishers' => $publishers,
                 'mediaArray'=> $mediaArray
             ];