/** * {@inheritdoc} */ protected function doSave($id, EntityInterface $entity) { $bundle = $entity->bundle(); // Generate an ID before saving, if none is available. If the ID generation // occurs earlier in the process (like on EntityInterface::create()), the // entity might be considered not new by modules that don't strictly use the // EntityInterface::isNew() method. if (empty($id)) { $id = $this->generateId(); $entity->{$this->idKey} = (string) $id; } // If the target graph is set, it has priority over the one the entity is // loaded from. If no target graph is set, use the previous one. $target_graph = $this->getGraphHandler()->getTargetGraphFromEntity($entity); $graph_uri = $this->getBundleGraphUri($bundle, $target_graph); $insert = ''; $properties = $this->mappingHandler->getEntityTypeMappedProperties($entity); $subj = '<' . (string) $id . '>'; $properties_list = "<" . implode(">, <", $properties['flat']) . ">"; foreach ($entity->toArray() as $field_name => $field) { foreach ($field as $field_item) { foreach ($field_item as $column => $value) { if (!isset($properties['by_field'][$field_name][$column])) { continue; } $pred = '<' . (string) $properties['by_field'][$field_name][$column] . '>'; if (!filter_var($value, FILTER_VALIDATE_URL) === FALSE) { $obj = '<' . $value . '>'; } else { // @todo This is most probably prone to Sparql injection..! $obj = '"""' . $value . '"""'; } $insert .= $subj . ' ' . $pred . ' ' . $obj . ' .' . "\n"; } } } // Save the bundle. $rdf_bundle_mapping = $this->mappingHandler->getRdfBundleMappedUri($entity->getEntityType()->getBundleEntityType(), $entity->bundle()); $rdf_bundle = $rdf_bundle_mapping[$entity->bundle()]; $insert .= $subj . ' ' . $this->rdfBundlePredicate . ' <' . $rdf_bundle . '> .' . "\n"; $query = <<<QUERY DELETE { GRAPH <{$graph_uri}> { <{$id}> ?field ?value } } WHERE { GRAPH <{$graph_uri}> { <{$id}> ?field ?value . FILTER (?field IN ({$properties_list})) } } QUERY; if (!$entity->isNew()) { $this->sparql->query($query); } // @todo Do in one transaction... If possible. $query = "INSERT DATA INTO <{$graph_uri}> {\n" . $insert . "\n" . '}'; $this->sparql->query($query); }