add subjects save + set modified date when saing document in rest api
authorymh <ymh.work@gmail.com>
Fri, 04 Mar 2016 10:08:52 +0100
changeset 138 3079cbf80006
parent 137 1baa7c6bd370
child 139 8d688175513a
add subjects save + set modified date when saing document in rest api
server/src/app/Http/Controllers/Api/DocumentController.php
server/src/app/Models/Document.php
server/src/tests/Models/DocumentTest.php
--- a/server/src/app/Http/Controllers/Api/DocumentController.php	Thu Mar 03 17:34:12 2016 +0100
+++ b/server/src/app/Http/Controllers/Api/DocumentController.php	Fri Mar 04 10:08:52 2016 +0100
@@ -100,8 +100,11 @@
             abort(404);
         }
 
-        //for now, update contributors only
+        //for now, update contributors and subjects only
         $doc->setContributors($document['contributors']);
+        $doc->setSubjects($document['subjects']);
+
+        $doc->setModified();
 
         $this->documentRepository->save($doc);
 
--- a/server/src/app/Models/Document.php	Thu Mar 03 17:34:12 2016 +0100
+++ b/server/src/app/Models/Document.php	Fri Mar 04 10:08:52 2016 +0100
@@ -177,11 +177,18 @@
         return is_null($modified)?null:$modified->getValue();
     }
 
-    public function setModified($value) {
+    public function setModified($value = null) {
+        if(is_null($value)) {
+            $value = gmdate(\DateTime::ATOM);
+        } elseif ($value instanceof \DateTime) {
+            $value = $value->format(\DateTime::ATOM);
+        }
+        $value = preg_replace('/[\+\-]00(\:?)00$/', 'Z', $value);
+
         $modified = $this->getModified();
-        if($value && (!$modified || $modified->getValue() != $value) ) {
+        if($value && (!$modified || $modified->getValue() !== $value) ) {
 
-            $newModified = new Literal($value, "http://purl.org/dc/terms/W3CDTF");
+            $newModified = new Literal($value, null, "http://purl.org/dc/terms/W3CDTF");
             $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/terms/modified', $modified, $newModified);
 
             $this->modified = null;
@@ -328,6 +335,35 @@
         return $this->subjects;
     }
 
+    /**
+     * change subjecs list
+     */
+    public function setSubjects($subjects) {
+        $delta = $this->startDelta();
+        //remove old,
+        foreach ($this->getSubjects() as $subject) {
+            $this->getProvidedCHO()->delete('<http://purl.org/dc/elements/1.1/subject>', $subject);
+            $delta->getDeletedGraph()->add($this->getProvidedCHO(), 'http://purl.org/dc/elements/1.1/subject', $subject);
+        }
+
+        //put new
+        foreach ($subjects as $newSubject) {
+            $value = null;
+            if(filter_var($newSubject, FILTER_VALIDATE_URL)) {
+                $value = new Resource($newSubject);
+            }
+            else {
+                $value = new Literal($newSubject, "fr", null);
+            }
+
+            $this->getProvidedCHO()->add('http://purl.org/dc/elements/1.1/subject', $value);
+            $delta->getAddedGraph()->add($this->getProvidedCHO(), 'http://purl.org/dc/elements/1.1/subject', $value);
+        }
+
+        $this->subjects = null;
+
+    }
+
     public function isIsomorphic($doc) {
         return Isomorphic::isomorphic($this->graph, $doc->graph);
     }
--- a/server/src/tests/Models/DocumentTest.php	Thu Mar 03 17:34:12 2016 +0100
+++ b/server/src/tests/Models/DocumentTest.php	Fri Mar 04 10:08:52 2016 +0100
@@ -125,6 +125,24 @@
 
     }
 
+    public function testSetModified() {
+        $currentTime = gmdate(DateTime::ATOM);
+        $doc = new Document("http://corpusdelaparole.huma-num.fr/corpus/res/crdo-CFPP2000_35_SOUND", $this->graph);
+        $doc->setModified($currentTime);
+        $this->assertInstanceOf(EasyRdf\Literal::class, $doc->getModified(), "Modified must be a literal");
+        $this->assertEquals("http://purl.org/dc/terms/W3CDTF", $doc->getModified()->getDatatypeURI(), "type must be http://purl.org/dc/terms/W3CDTF");
+        $this->assertEquals(preg_replace('/[\+\-]00(\:?)00$/', 'Z', $currentTime), $doc->getModified()->getValue(), "modified must be $currentTime");
+    }
+
+    public function testSetModifiedNull() {
+        $doc = new Document("http://corpusdelaparole.huma-num.fr/corpus/res/crdo-CFPP2000_35_SOUND", $this->graph);
+        $doc->setModified();
+        $this->assertInstanceOf(EasyRdf\Literal::class, $doc->getModified(), "Modified must be a literal");
+        $this->assertEquals("http://purl.org/dc/terms/W3CDTF", $doc->getModified()->getDatatypeURI(), "type must be http://purl.org/dc/terms/W3CDTF");
+        $foundDateTime = \DateTime::createFromFormat(\DateTime::ATOM, $doc->getModified()->getValue());
+        $nowUTC = new \DateTime('now', new \DateTimeZone("UTC"));
+        $this->assertTrue($nowUTC->getTimestamp() -  $foundDateTime->getTimestamp() < 2, "must have less than 2 seconds diff");
+    }
 
     public function testPublisher() {
 
@@ -431,7 +449,7 @@
 
         $subjects = $doc->getSubjects();
 
-        $this->assertCount(28, $subjects, "Must have 15 subjects");
+        $this->assertCount(28, $subjects, "Must have 28 subjects");
 
         foreach ($doc->getSubjects() as $s) {
             $this->assertThat(
@@ -444,4 +462,42 @@
         }
     }
 
+    public function testSetSubjects() {
+        $doc = new Document("http://corpusdelaparole.huma-num.fr/corpus/res/crdo-CFPP2000_35_SOUND", $this->graph);
+
+        $newSubjects = [
+            "http://ark.bnf.fr/ark:/12148/cb13318415c",
+            "http://ark.bnf.fr/ark:/12148/cb133188907",
+            "http://ark.bnf.fr/ark:/12148/cb11932762f",
+            "http://ark.bnf.fr/ark:/12148/cb133183660",
+            "http://ark.bnf.fr/ark:/12148/cb122368540",
+            "http://ark.bnf.fr/ark:/12148/cb119418302",
+            "http://ark.bnf.fr/ark:/12148/cb135540729",
+            "http://ark.bnf.fr/ark:/12148/cb133192210",
+            "http://ark.bnf.fr/ark:/12148/cb119377452",
+            "http://ark.bnf.fr/ark:/12148/cb13320451h",
+            "http://ark.bnf.fr/ark:/12148/cb13318422n",
+            "http://ark.bnf.fr/ark:/12148/cb11975823c"
+        ];
+
+        $doc->setSubjects($newSubjects);
+
+        $this->assertTrue($doc->isDirty(), "The document must have changed");
+
+        $subjects = $doc->getSubjects();
+
+        $this->assertCount(12, $subjects, "Must have 12 subjects");
+
+        foreach ($doc->getSubjects() as $s) {
+            $this->assertInstanceOf('EasyRdf\Resource', $s, "Mustbe a Resource");
+            $this->assertTrue(strrpos($s->getUri(), "http://ark.bnf.fr/ark:/12148/cb") === 0, "Must start with http://ark.bnf.fr/ark:/12148/cb");
+            $this->assertContains($s->getUri(), $newSubjects, "$s must be in new subjects list");
+            if(($key = array_search($s->getUri(), $newSubjects)) !== false) {
+                unset($newSubjects[$key]);
+            }
+        }
+        $this->assertEmpty($newSubjects, "all subjects must have been removed");
+    }
+
+
 }