예제 #1
0
 /**
  * Short description of method removeType
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @param  Resource resource
  * @param  Class class
  * @return boolean
  */
 public function removeType(\core_kernel_classes_Resource $resource, \core_kernel_classes_Class $class)
 {
     $returnValue = (bool) false;
     $dbWrapper = \core_kernel_classes_DbWrapper::singleton();
     $referencer = ResourceReferencer::singleton();
     if ($resource->hasType($class)) {
         $resourceId = intval(Utils::getResourceToTableId($resource));
         $classInfo = Utils::getClassInfo($class);
         $triples = $resource->getRdfTriples();
         if (!empty($resourceId)) {
             $resource->delete(false);
             $referencer->unReferenceResource($resource);
             $query = 'INSERT INTO "statements" ("modelid", "subject", "predicate", "object", "l_language") VALUES  (?, ?, ?, ?, ?);';
             foreach ($triples as $t) {
                 $dbWrapper->exec($query, array(99999, $t->subject, $t->predicate, $t->object, $t->lg));
             }
         }
     }
     $returnValue = true;
     return (bool) $returnValue;
 }
예제 #2
0
 /**
  * (non-PHPdoc)
  * @see core_kernel_persistence_ClassInterface::createInstanceWithProperties()
  */
 public function createInstanceWithProperties(\core_kernel_classes_Class $type, $properties)
 {
     $returnValue = null;
     if (isset($properties[RDF_TYPE])) {
         throw new \core_kernel_persistence_Exception('Additional types in createInstanceWithProperties not permited');
     }
     $uri = \common_Utils::getNewUri();
     $table = '_' . HardapiUtils::getShortName($type);
     // prepare properties
     $hardPropertyNames = array("uri" => $uri);
     $dbWrapper = \core_kernel_classes_DbWrapper::singleton();
     if (is_array($properties)) {
         if (count($properties) > 0) {
             // Get the table name
             $referencer = ResourceReferencer::singleton();
             $queryProps = array();
             foreach ($properties as $propertyUri => $value) {
                 $property = new \core_kernel_classes_Property($propertyUri);
                 $propertyLocation = $referencer->propertyLocation($property);
                 if (in_array("{$table}props", $propertyLocation) || !$referencer->isPropertyReferenced($property)) {
                     $propertyRange = $property->getRange();
                     $lang = $property->isLgDependent() ? \common_session_SessionManager::getSession()->getDataLanguage() : '';
                     $formatedValues = array();
                     if ($value instanceof \core_kernel_classes_Resource) {
                         $formatedValues[] = $dbWrapper->quote($value->getUri());
                     } else {
                         if (is_array($value)) {
                             foreach ($value as $val) {
                                 if ($val instanceof \core_kernel_classes_Resource) {
                                     $formatedValues[] = $dbWrapper->quote($val->getUri());
                                 } else {
                                     $formatedValues[] = $dbWrapper->quote($val);
                                 }
                             }
                         } else {
                             $formatedValues[] = $dbWrapper->quote($value);
                         }
                     }
                     if (is_null($propertyRange) || $propertyRange->getUri() == RDFS_LITERAL) {
                         foreach ($formatedValues as $formatedValue) {
                             $queryProps[] = "'{$property->getUri()}', {$formatedValue}, null, '{$lang}'";
                         }
                     } else {
                         foreach ($formatedValues as $formatedValue) {
                             $queryProps[] = "'{$property->getUri()}', null, {$formatedValue}, '{$lang}'";
                         }
                     }
                 } else {
                     $propertyName = HardapiUtils::getShortName($property);
                     if (is_array($value)) {
                         if (count($value) > 1) {
                             throw new Exception("try setting multivalue for the non multiple property {$property->getLabel()} ({$property->getUri()})");
                         } else {
                             $value = count($value) == 0 ? null : reset($value);
                             // take the only element
                         }
                     }
                     if ($value instanceof \core_kernel_classes_Resource) {
                         $value = $value->getUri();
                     }
                     $hardPropertyNames[$propertyName] = $value;
                 }
             }
         }
     }
     // spawn
     $returnValue = new \core_kernel_classes_Resource($uri, __METHOD__);
     $varnames = '"' . implode('","', array_keys($hardPropertyNames)) . '"';
     try {
         $query = 'INSERT INTO "' . $table . '" (' . $varnames . ') VALUES (' . implode(',', array_fill(0, count($hardPropertyNames), '?')) . ')';
         $result = $dbWrapper->exec($query, array_values($hardPropertyNames));
         // reference the newly created instance
         ResourceReferencer::singleton()->referenceResource($returnValue, $table, array($type), true);
         // @todo this shoould be retrievable without an aditional query
         $instanceId = Utils::getInstanceId($returnValue);
         // @todo Merge into a single query
         if (!empty($queryProps)) {
             $prefixed = array();
             foreach ($queryProps as $row) {
                 $prefixed[] = ' (' . $instanceId . ', ' . $row . ')';
             }
             try {
                 $query = 'INSERT INTO "' . $table . 'props" ("instance_id", "property_uri", "property_value", "property_foreign_uri", "l_language") VALUES ' . implode(',', $prefixed);
                 $result = $dbWrapper->exec($query);
             } catch (\PDOException $e) {
                 throw new Exception("Unable to set properties (multiple) Value for the instance {$returnValue->getUri()} in {$tableName} : " . $e->getMessage());
             }
         }
     } catch (\PDOException $e) {
         throw new Exception("Unable to create instance for the class {$type->getUri()} in the table {$table} : " . $e->getMessage());
     }
     return $returnValue;
 }