server/src/app/Libraries/Mappers/CocoonAbstractRdfMapper.php
changeset 18 f2a40bbc27f6
child 19 eadaf0b8f02e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/app/Libraries/Mappers/CocoonAbstractRdfMapper.php	Tue Nov 17 13:11:55 2015 +0100
@@ -0,0 +1,143 @@
+<?php
+namespace CorpusParole\Libraries\Mappers;
+
+use Config;
+
+use CorpusParole\Libraries\Utils;
+use CorpusParole\Libraries\CocoonUtils;
+
+use EasyRdf\RdfNamespace;
+use EasyRdf\Graph;
+use EasyRdf\Literal;
+use EasyRdf\Resource;
+
+
+RdfNamespace::set('edm', 'http://www.europeana.eu/schemas/edm/');
+RdfNamespace::set('ore', 'http://www.openarchives.org/ore/terms/');
+RdfNamespace::set('crdo', 'http://crdo.risc.cnrs.fr/schemas/');
+RdfNamespace::set('olac', 'http://www.language-archives.org/OLAC/1.1/');
+RdfNamespace::set('skos', 'http://www.w3.org/2004/02/skos/core#');
+
+
+/**
+ *
+ */
+abstract class CocoonAbstractRdfMapper implements RdfMapper {
+
+    public function __construct(Graph $inputGraph) {
+        $this->inputGraph = $inputGraph;
+        $this->uri = $inputGraph->getUri();
+        $this->outputGraphes = [];
+    }
+
+    abstract protected function mapResource($res, $outputGraph);
+    abstract protected function getResourceBaseId($res=null);
+
+    public function getOutputGraphes() {
+        return $this->outputGraphes;
+    }
+
+    /**
+     * Map a Cocoon Rdf Graph to a CP graph
+     * @return the new mapped graph
+     */
+    public function mapGraph() {
+
+        $outputGraph = $this->buildOutputGraph();
+
+        $resources = $this->inputGraph->allOfType('crdo:Resource');
+        foreach ($resources as $res) {
+            $this->mapResource($res, $outputGraph);
+        }
+    }
+
+    /**
+     * Build the main outputgraph
+     * @return the new empty graph
+     */
+     protected function buildOutputGraph() {
+
+         //$soundRes = $this->inputGraph->resource($this->uri)->get('http://purl.org/dc/terms/requires');
+         $id = CocoonUtils::getIdFromUri($this->getResourceBaseId());
+
+         $outputGraph = new Graph(CocoonUtils::getCorpusUriFromId($id));
+         $this->outputGraphes[$this->getResourceBaseId()] = $outputGraph;
+
+         return $outputGraph;
+     }
+
+
+    protected function propertyOlacRoleMap($targetRes, $prop, $value) {
+        $targetRes->add($prop, $value);
+        $targetRes->add('http://purl.org/dc/elements/1.1/contributor', $value);
+    }
+
+    protected function propertyIdentityMap($targetRes, $prop, $value) {
+        $targetRes->add($prop, $value);
+    }
+
+    protected function propertyTrimMap($providedCHOResource, $prop, $value) {
+        $providedCHOResource->add($prop, trim($value));
+    }
+
+    protected function addSpatialProperties($destRes, $res, $outputGraph) {
+        $spatials = $res->all($this->inputGraph->resource('http://purl.org/dc/terms/spatial'));
+        $lats = $res->all($this->inputGraph->resource('http://www.w3.org/2003/01/geo/wgs84_pos#lat'));
+        $longs = $res->all($this->inputGraph->resource('http://www.w3.org/2003/01/geo/wgs84_pos#long'));
+        $alts = $res->all($this->inputGraph->resource('http://www.w3.org/2003/01/geo/wgs84_pos#alt'));
+
+        if( count($spatials) > 0 || count($lats) > 0 || count($longs) > 0 || count($alts)) {
+            $placeNode = $outputGraph->newBNode(['edm:Place']);
+            foreach($lats as $latitude) {
+                $placeNode->addLiteral('http://www.w3.org/2003/01/geo/wgs84_pos#lat', $latitude);
+            }
+            foreach($longs as $longitude) {
+                $placeNode->addLiteral('http://www.w3.org/2003/01/geo/wgs84_pos#long', $longitude);
+            }
+            foreach($spatials as $s) {
+                if($s instanceof Literal) {
+                    $placeNode->addLiteral('skos:note', $s);
+                }
+                elseif ($s instanceof Resource) {
+                    $placeNode->addResource('owl:sameAs', $s);
+                }
+            }
+            $destRes->add($outputGraph->resource('http://purl.org/dc/terms/spatial'), $placeNode);
+        }
+    }
+
+    protected function applyPropertiesToRes($sourceRes, $targetRes, $properties) {
+
+        foreach( $properties as list($prop, $mappingMethod)) {
+            if( is_null($mappingMethod) ) {
+                $mappingMethod = 'propertyIdentityMap';
+            }
+
+            foreach($sourceRes->all($this->inputGraph->resource($prop)) as $value) {
+                call_user_func(array($this, $mappingMethod), $targetRes, $prop, $value);
+            }
+        }
+    }
+
+    protected function addDateProperties($targetRes, $res, $outputGraph) {
+        $this->applyPropertiesToRes($res, $targetRes, [
+            ['http://purl.org/dc/terms/available', null],
+            ['http://purl.org/dc/terms/created', null],
+            ['http://purl.org/dc/terms/issued', null],
+            ['http://purl.org/dc/terms/modified', null],
+        ]);
+    }
+
+    protected function mapCollections($res) {
+        $collections = $res->all('dc:isPartOf');
+        foreach($collections as $coll) {
+            if($coll instanceof Resource) {
+                $collectionGraph = new Graph($coll->getUri());
+                $this->outputGraphes[$coll->getUri()] = $collectionGraph;
+                $collectionGraph->resource($coll->getUri(), 'edm:Collection');
+            }
+        }
+    }
+
+
+}