public function __construct(array $params, $mainEntity)
 {
     $this->at = RestUrlParams::parseTimestamp(RestUrlParams::extractValue($params, RestUrlParams::AT));
     if ($this->at == RestUrlParams::ALL_TIMES) {
         $this->at = self::ALL_TIMES;
     }
     $this->published = RestUrlParams::parseBoolean(RestUrlParams::extractValue($params, RestUrlParams::PUBLISHED));
     $this->queryParamsMap = array();
     $this->scopeMap = array();
     if ($mainEntity != NULL) {
         foreach ($params as $paramName => $paramValue) {
             // Parameters can be specified with or without entity-prefix, e.g. 'entity/property=value' or just
             // 'property=value'. Check if an entity-prefix is provided.
             $entityName = NULL;
             $propertyName = NULL;
             $paramNameParts = explode(RestUrlParams::ENTITY_SEPARATOR, $paramName);
             if (count($paramNameParts) == 1) {
                 // It's like 'property=value'.
                 $entityName = $mainEntity->getName();
                 $propertyName = $paramName;
             } else {
                 if (count($paramNameParts) == 2) {
                     // It's like 'entity/property=value'.
                     $entityName = $paramNameParts[0];
                     $propertyName = $paramNameParts[1];
                 } else {
                     throw new Exception("Illegal query parameter '{$paramName}'.");
                 }
             }
             // If it's a scope parameter...
             if (strcasecmp($propertyName, RestUrlParams::SCOPE) == 0) {
                 // ...then parse it.
                 $this->scopeMap[$entityName] = Scope::parseValue($paramValue);
             } else {
                 // ...else add the value to the map at the correct entity-entry.
                 if (!array_key_exists($entityName, $this->queryParamsMap)) {
                     $this->queryParamsMap[$entityName] = array();
                 }
                 // If $paramName is 'id'...
                 if (strcasecmp($propertyName, RestUrlParams::ID) == 0) {
                     // ...then check if this is allowed for the given entity...
                     if (!$mainEntity->isObjectEntity()) {
                         throw new Exception("Cannot fetch entity '" . $mainEntity->getName() . "' by id.");
                     }
                     // ...and apply the correct property name and value.
                     $propertyName = $mainEntity->getObjectIdColumnName();
                     $paramValue = explode(RestUrlParams::ID_SEPARATOR, $paramValue);
                 }
                 $this->queryParamsMap[$entityName][$propertyName] = $paramValue;
             }
         }
     }
 }
 function __construct(ObjectEntity $entity, $id, array $propertyValues, $modifyingAction, $scopeValue = NULL)
 {
     $this->entity = $entity;
     $this->id = $id;
     $this->propertyValues = $propertyValues;
     $this->modifyingAction = $modifyingAction;
     if ($scopeValue == NULL) {
         $scopeValue = self::DEFAULT_SCOPE;
     }
     $this->scope = Scope::parseValue($scopeValue);
     $this->relatedObjects = array();
     $this->key = $this->entity->getName() . "[{$this->id}]";
 }
 public function fetchRelatedObjectIdsOfEntity(MySQLi $mySQLi, Entity $otherEntity)
 {
     $queryContext = new QueryContext(array(), NULL);
     $scope = Scope::parseValue(Scope::VALUE_C_REF . Scope::VALUE_A_REF . Scope::VALUE_O_REF);
     $fetchedObjectIds = array();
     if ($otherEntity->isObjectEntity()) {
         $fetchedObjectRefs = array();
         $this->fetchRelatedObjectRefsOfEntity($mySQLi, $otherEntity, $queryContext, $scope, $fetchedObjectRefs);
         foreach ($fetchedObjectRefs as $fetchedObjectRef) {
             $fetchedObjectIds[] = $fetchedObjectRef->id;
         }
     } else {
         $fetchedObjectIds[] = 0;
     }
     return $fetchedObjectIds;
 }
 public function establishLinks(Schema $schema, LinkEntity $linkEntity, $oneFkColumnName, $oneId, $otherFkColumnName, array $otherIds, Audit $audit)
 {
     // Get the id's of all existing links.
     $scope = Scope::parseValue(Scope::VALUE_P_ALL);
     $query = new QueryEntity($linkEntity, $this->defaultQueryContext, $scope);
     $query->addWhereClause($oneFkColumnName, $oneId);
     $mySQLi = $schema->getMySQLi();
     $queryString = $query->getQueryString();
     $queryResult = $mySQLi->query($queryString);
     if (!$queryResult) {
         throw new Exception("Error fetching '{$otherFkColumnName}' properties of link entity " . $linkEntity->getName() . "[{$oneId}] - {$mySQLi->error}\n<!--\n{$queryString}\n-->");
     }
     // Compose a list of otherIds that must be deleted and adjust $otherIds, so it will only contain links that
     // must be created.
     $surplusOtherIds = array();
     while ($dbObject = $queryResult->fetch_assoc()) {
         $existingOtherId = $dbObject[$otherFkColumnName];
         $key = array_search($existingOtherId, $otherIds);
         if ($key !== FALSE) {
             // The link is already there.
             unset($otherIds[$key]);
         } else {
             $surplusOtherIds[] = $existingOtherId;
         }
     }
     $queryResult->close();
     // Delete surplus links.
     if (count($surplusOtherIds) > 0) {
         $propertyValues = array();
         $propertyValues[$oneFkColumnName] = $oneId;
         $propertyValues[$otherFkColumnName] = $surplusOtherIds;
         $this->terminateLinks($schema, $linkEntity, $propertyValues, $audit);
     }
     // Create missing links.
     if (count($otherIds) > 0) {
         $propertyValues = array();
         $propertyValues[$oneFkColumnName] = $oneId;
         foreach ($otherIds as $otherId) {
             // Pass a NULL to indicate that a new link object must be inserted.
             $propertyValues[$otherFkColumnName] = $otherId;
             $this->insertOrUpdate($schema, $linkEntity, NULL, $propertyValues, $audit);
         }
     }
 }
 private function getOwnedObjectRefs(Schema $schema, ObjectEntity $entity, $id, $isPublished)
 {
     // Find any related objects that are (still) published.
     // First create a set of entities that are 'owned' by the given one...
     $ownedEntities = array();
     foreach ($entity->getRelationships() as $relationship) {
         if ($relationship->getOwnerEntity() == $entity) {
             $oppositeEntity = $relationship->getOppositeEntity($entity);
             if ($oppositeEntity->isObjectEntity()) {
                 $ownedEntities[] = $oppositeEntity;
             }
         }
     }
     // ...and then fetch the published ObjectRefs.
     if (count($ownedEntities) == 0) {
         return NULL;
     }
     $objectRef = new ObjectRef($entity, $id);
     $params = array(RestUrlParams::PUBLISHED => $isPublished ? 'true' : 'false');
     $queryContext = new QueryContext($params, NULL);
     $scope = Scope::parseValue(Scope::VALUE_C_REF . Scope::VALUE_A_REF . Scope::VALUE_O_REF);
     return $objectRef->fetchAllRelatedObjectRefs($schema->getMySQLi(), $ownedEntities, $queryContext, $scope);
 }
 public function getPropertyValues(ObjectEntity $entity, $id)
 {
     // Compose and execute the query.
     $query = new QueryEntity($entity, new QueryContext(array(), $entity), Scope::parseValue('Pcao'), $id);
     $mySQLi = $this->schema->getMySQLi();
     $queryString = $query->getQueryString();
     $queryResult = $mySQLi->query($queryString);
     if (!$queryResult) {
         throw new Exception("Error fetching all properties of entity '" . $entity->getName() . "'[{$id}] - " . $mySQLi->error . "\n<!--\n{$queryString}\n-->");
     }
     // Copy the query result into $properties.
     $propertyValues = array();
     while ($dbObject = $queryResult->fetch_assoc()) {
         foreach ($dbObject as $columnName => $value) {
             if (strcasecmp($columnName, $entity->getObjectIdColumnName()) == 0 or strcasecmp($columnName, QueryEntity::ID_PUBLISHED) == 0 or strcasecmp($columnName, QueryEntity::ID_TERMINATED) == 0) {
                 continue;
             }
             $property = $entity->getProperty($columnName);
             $propertyValues[$property->getName()] = $this->adjustValueFormat($value, $property->getTypeIndicator());
         }
     }
     $queryResult->close();
     return $propertyValues;
 }