Exemple #1
0
 /**
  * Test the service factory: dynamical instantiation and single instance serving
  */
 public function testMultiple()
 {
     $propClass = new core_kernel_classes_Class(RDF_PROPERTY);
     $q = "SELECT subject, count(object)\n                FROM statements\n                    WHERE predicate = ?\n                    GROUP BY subject\n                    HAVING (count(object) > 1)";
     foreach ($propClass->getInstances(true) as $property) {
         $property = new core_kernel_classes_Property($property);
         if (!$property->isMultiple() && !$property->isLgDependent()) {
             // bypass generis
             $result = core_kernel_classes_DbWrapper::singleton()->query($q, array($property->getUri()));
             while ($statement = $result->fetch()) {
                 $this->fail($property->getUri() . ' has multiple values but is not multiple.');
             }
         }
     }
 }
    /**
     * Short description of method delete
     *
     * @access public
     * @author Joel Bout, <*****@*****.**>
     * @param  Resource resource
     * @param  boolean deleteReference
     * @return boolean
     */
    public function delete(\core_kernel_classes_Resource $resource, $deleteReference = false)
    {
        $returnValue = (bool) false;
        $dbWrapper = \core_kernel_classes_DbWrapper::singleton();
        $tableName = ResourceReferencer::singleton()->resourceLocation($resource);
        if (empty($tableName)) {
            return $returnValue;
        }
        $uri = $resource->getUri();
        $resourceId = HardapiUtils::getResourceIdByTable($resource, $tableName);
        if ($resourceId) {
            /*
             * Delete all the references of the resource first, before the resource is delete of course,
             * if the parameter $deleteReference is true
             */
            if ($deleteReference) {
                $properties = array();
                //get the resource classes (type)
                $types = '';
                foreach ($resource->getTypes() as $type) {
                    $properties[$type->getUri()] = array();
                    $types = "'" . $type->getUri() . "',";
                }
                $types = substr($types, 0, strlen($types) - 1);
                if (!empty($types)) {
                    //get all the properties that have one of the resource class as range
                    $sqlQuery = 'SELECT "subject", "object" FROM "statements" WHERE "predicate" = \'' . RDFS_RANGE . '\' AND object IN (' . $types . ')';
                    $result = $dbWrapper->query($sqlQuery);
                    while ($row = $result->fetch()) {
                        //fill the properties range: propertyUri => domains:
                        $propertyUri = $row['subject'];
                        $rangeUri = $row['object'];
                        $properties[$rangeUri][$propertyUri] = array();
                        //get the domain of the property:
                        $property = new \core_kernel_classes_Property($propertyUri);
                        foreach ($property->getDomain()->getIterator() as $domain) {
                            if ($domain instanceof \core_kernel_classes_Class) {
                                $properties[$rangeUri][$propertyUri][] = $domain->getUri();
                            }
                        }
                    }
                    //delete the references
                    $referencer = ResourceReferencer::singleton();
                    foreach ($properties as $rangeUri => $propertyUris) {
                        foreach ($propertyUris as $propertyUri => $domains) {
                            //property -> column
                            $property = new \core_kernel_classes_Property($propertyUri);
                            $isMulti = $property->isMultiple() || $property->isLgDependent();
                            $columnName = '';
                            if (!$isMulti) {
                                $columnName = HardapiUtils::getShortName($property);
                                if (empty($columnName)) {
                                    continue;
                                }
                            }
                            foreach ($domains as $domainUri) {
                                //classLocations -> table
                                $classLocations = $referencer->classLocations(new \core_kernel_classes_Class($domainUri));
                                foreach ($classLocations as $classLocation) {
                                    if ($property->isMultiple()) {
                                        //delete the row in the props table
                                        $query = 'DELETE FROM "' . $classLocation['table'] . 'props"
												WHERE "property_uri" = ? 
												AND ("property_value" = ? OR "property_foreign_uri" = ?)';
                                        $dbWrapper->exec($query, array($propertyUri, $uri, $uri));
                                    } else {
                                        //set the col value to NULL
                                        $query = 'UPDATE "' . $classLocation['table'] . '"
												SET "' . $columnName . '" = NULL 
												WHERE "' . $columnName . '" = ?';
                                        $dbWrapper->exec($query, array($uri));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $queries = array();
            // Delete records in the main table
            $queries[] = 'DELETE FROM "' . $tableName . '" WHERE "id" = \'' . $resourceId . '\'';
            // Delete records in the properties table
            $queries[] = 'DELETE FROM "' . $tableName . 'props" WHERE "instance_id" = \'' . $resourceId . '\'';
            foreach ($queries as $query) {
                try {
                    $result = $dbWrapper->exec($query);
                    if ($result === false) {
                        $returnValue = false;
                        break;
                    } else {
                        $returnValue = true;
                    }
                } catch (\PDOException $e) {
                    throw new Exception("Unable to delete resource ({$resource->getUri()}) ;" . $e->getMessage());
                }
            }
            // Unreference the resource
            ResourceReferencer::singleton()->unReferenceResource($resource);
        }
        return (bool) $returnValue;
    }
 public function testHardModel()
 {
     $this->hardify();
     $referencer = ResourceReferencer::singleton();
     $propertyProxy = PropertyProxy::singleton();
     $proxy = ResourceProxy::singleton();
     $domainProperty = new core_kernel_classes_Property(RDFS_DOMAIN);
     $rangeProperty = new core_kernel_classes_Property(RDFS_RANGE);
     $literalClass = new core_kernel_classes_Class(RDFS_LITERAL);
     $subClassOfProperty = new core_kernel_classes_Property(RDFS_SUBCLASSOF);
     $this->assertTrue($this->targetActorsProperty->exists());
     $this->assertTrue($this->targetMovieClass->exists());
     $this->assertTrue($this->targetWorkClass->isSubclassOf($this->taoClass));
     $this->assertTrue($this->targetWorkClass->getOnePropertyValue($subClassOfProperty)->getUri() == $this->taoClass->getUri());
     $this->assertTrue($referencer->isClassReferenced($this->targetWorkClass));
     $this->assertFalse(is_a($proxy->getImpToDelegateTo($this->targetWorkClass), 'core_kernel_persistence_smoothsql_Class'));
     $this->assertFalse(is_a($proxy->getImpToDelegateTo($this->targetMovieClass), 'core_kernel_persistence_smoothsql_Class'));
     $this->assertTrue($this->targetAuthorProperty->getOnePropertyValue($domainProperty)->getUri() == $this->targetWorkClass->getUri());
     $this->assertTrue($this->targetAuthorProperty->getOnePropertyValue($rangeProperty)->getUri() == RDFS_LITERAL);
     $this->assertTrue($this->targetMovieClass->isSubclassOf($this->targetWorkClass));
     $this->assertTrue($this->targetMovieClass->getOnePropertyValue($subClassOfProperty)->getUri() == $this->targetWorkClass->getUri());
     $this->assertTrue($referencer->isClassReferenced($this->targetMovieClass));
     $this->assertTrue($this->targetProducerProperty->getOnePropertyValue($domainProperty)->getUri() == $this->targetMovieClass->getUri());
     $this->assertTrue($this->targetProducerProperty->getOnePropertyValue($rangeProperty)->getUri() == RDFS_LITERAL);
     $this->assertTrue($this->targetActorsProperty->getOnePropertyValue($domainProperty)->getUri() == $this->targetMovieClass->getUri());
     $this->assertTrue($this->targetActorsProperty->getOnePropertyValue($rangeProperty)->getUri() == RDFS_LITERAL);
     $this->assertTrue($this->targetRelatedMoviesProperty->getOnePropertyValue($domainProperty)->getUri() == $this->targetMovieClass->getUri());
     $this->assertTrue($this->targetRelatedMoviesProperty->getOnePropertyValue($rangeProperty)->getUri() == $this->targetMovieClass->getUri());
     $parentClasses = $this->targetMovieClass->getParentClasses();
     $this->assertEquals(1, count($parentClasses));
     $this->assertTrue(array_key_exists($this->targetWorkClass->getUri(), $parentClasses));
     $prop = new core_kernel_classes_Property($this->targetRelatedMoviesProperty);
     $this->assertTrue($prop->isMultiple());
 }