/**
  * Gets an entity instance from an entity set identifed by a key
  * 
  * @param ResourceSet $resourceSet     The entity set from which an entity needs to be fetched
  * @param KeyDescriptor $keyDescriptor The key to identify the entity to be fetched
  * 
  * @return Object/NULL Returns entity instance if found else null
  */
 public function getResourceFromResourceSet(ResourceSet $resourceSet, KeyDescriptor $keyDescriptor)
 {
     $resourceSetName = $resourceSet->getName();
     if ($resourceSetName !== 'Customers' && $resourceSetName !== 'Orders' && $resourceSetName !== 'Order_Details' && $resourceSetName !== 'Products' && $resourceSetName !== 'Employees') {
         die('(NorthWindQueryProvider) Unknown resource set ' . $resourceSetName);
     }
     if ($resourceSetName === 'Order_Details') {
         $resourceSetName = 'Order Details';
     }
     $namedKeyValues = $keyDescriptor->getValidatedNamedValues();
     $condition = null;
     foreach ($namedKeyValues as $key => $value) {
         $condition .= $key . ' = ' . $value[0] . ' and ';
     }
     $len = strlen($condition);
     $condition = substr($condition, 0, $len - 5);
     $query = "SELECT * FROM [{$resourceSetName}] WHERE {$condition}";
     $stmt = sqlsrv_query($this->_connectionHandle, $query);
     if ($stmt === false) {
         die(print_r(sqlsrv_errors(), true));
     }
     //If resource not found return null to the library
     if (!sqlsrv_has_rows($stmt)) {
         return null;
     }
     $result = null;
     while ($record = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
         switch ($resourceSetName) {
             case 'Customers':
                 $result = $this->_serializeCustomer($record);
                 break;
             case 'Orders':
                 $result = $this->_serializeOrder($record);
                 break;
                 /**case 'Order Details':                    
                        $result = $this->_serializeOrder_Detail($record);
                        break;
                    case 'Products':
                        $result = $this->_serializeProduct($record);
                        break;**/
             /**case 'Order Details':                    
                    $result = $this->_serializeOrder_Detail($record);
                    break;
                case 'Products':
                    $result = $this->_serializeProduct($record);
                    break;**/
             case 'Employees':
                 $result = $this->_serializeEmployee($record);
                 break;
         }
     }
     sqlsrv_free_stmt($stmt);
     return $result;
 }
 /**
  * Check wrapped resource set's resource type or any of the resource type derived
  * from the this resource type has bag property associated with it.
  * 
  * @param MetadataQueryProviderWrapper $provider Metadata query provider wrapper
  * 
  * @return boolean
  */
 public function hasBagProperty(MetadataQueryProviderWrapper $provider)
 {
     $arrayToDetectLoop = array();
     $hasBagProperty = $this->_resourceSet->getResourceType()->hasBagProperty($arrayToDetectLoop);
     unset($arrayToDetectLoop);
     // This will check only the resource type associated with
     // the resource set, we need to check presence of bag property
     // in resource type which is derived form this resource type also.
     if (!$hasBagProperty) {
         $derivedTypes = $provider->getDerivedTypes($this->_resourceSet->getResourceType());
         if (!is_null($derivedTypes)) {
             foreach ($derivedTypes as $derivedType) {
                 $arrayToDetectLoop = array();
                 if ($derivedType->hasBagProperty($arrayToDetectLoop)) {
                     $hasBagProperty = true;
                     break;
                 }
             }
         }
     }
     return $hasBagProperty;
 }
 /**
  * Gets an entity instance from an entity set identifed by a key
  * 
  * @param ResourceSet   $resourceSet   The entity set from which 
  *                                     an entity needs to be fetched
  * @param KeyDescriptor $keyDescriptor The key to identify the entity to be fetched
  * 
  * @return Object/NULL Returns entity instance if found else null
  */
 public function getResourceFromResourceSet(ResourceSet $resourceSet, KeyDescriptor $keyDescriptor)
 {
     $resourceSetName = $resourceSet->getName();
     if ($resourceSetName !== 'categories' and $resourceSetName !== 'customers' and $resourceSetName !== 'employees' and $resourceSetName !== 'order_details' and $resourceSetName !== 'orders' and $resourceSetName !== 'products' and $resourceSetName !== 'shippers' and $resourceSetName !== 'suppliers') {
         die('(NorthwindQueryProvider) Unknown resource set ' . $resourceSetName);
     }
     $namedKeyValues = $keyDescriptor->getValidatedNamedValues();
     $condition = null;
     foreach ($namedKeyValues as $key => $value) {
         $condition .= $key . ' = ' . $value[0] . ' and ';
     }
     $len = strlen($condition);
     $condition = substr($condition, 0, $len - 5);
     $query = "SELECT * FROM {$resourceSetName} WHERE {$condition}";
     $stmt = mysql_query($query);
     if ($stmt === false) {
         die(print_r(mysql_error(), true));
     }
     //If resource not found return null to the library
     if (!mysql_num_rows($stmt)) {
         return null;
     }
     $result = null;
     while ($record = mysql_fetch_array($stmt, MYSQL_ASSOC)) {
         switch ($resourceSetName) {
             case 'categories':
                 $returnResult = $this->_serializecategory($record);
                 break;
             case 'customers':
                 $returnResult = $this->_serializecustomer($record);
                 break;
             case 'employees':
                 $returnResult = $this->_serializeemployee($record);
                 break;
             case 'order_details':
                 $returnResult = $this->_serializeorder_detail($record);
                 break;
             case 'orders':
                 $returnResult = $this->_serializeorder($record);
                 break;
             case 'products':
                 $returnResult = $this->_serializeproduct($record);
                 break;
             case 'shippers':
                 $returnResult = $this->_serializeshipper($record);
                 break;
             case 'suppliers':
                 $returnResult = $this->_serializesupplier($record);
                 break;
         }
     }
     mysql_free_result($stmt);
     return $returnResult;
 }
 /**
  * Validate the given entity instance.
  * 
  * @param object        $entityInstance Entity instance to validate
  * @param ResourceSet   &$resourceSet   Resource set to which the entity 
  *                                      instance belongs to.
  * @param KeyDescriptor &$keyDescriptor The key descriptor.
  * @param string        $methodName     Method from which this function 
  *                                      invoked.
  *
  * @return void
  * 
  * @throws ODataException
  */
 private function _validateEntityInstance($entityInstance, ResourceSet &$resourceSet, KeyDescriptor &$keyDescriptor, $methodName)
 {
     if (is_null($entityInstance)) {
         ODataException::createResourceNotFoundError($resourceSet->getName());
     }
     $entityName = $resourceSet->getResourceType()->getInstanceType()->getName();
     if (!is_object($entityInstance) || !$entityInstance instanceof $entityName) {
         ODataException::createInternalServerError(Messages::metadataQueryProviderWrapperIDSQPMethodReturnsUnExpectedType($entityName, $methodName));
     }
     foreach ($keyDescriptor->getValidatedNamedValues() as $keyName => $valueDescription) {
         try {
             $keyProperty = new \ReflectionProperty($entityInstance, $keyName);
             $keyValue = $keyProperty->getValue($entityInstance);
             if (is_null($keyValue)) {
                 ODataException::createInternalServerError(Messages::metadataQueryProviderWrapperIDSQPMethodReturnsInstanceWithNullKeyProperties($methodName));
             }
             $convertedValue = $valueDescription[1]->convert($valueDescription[0]);
             if ($keyValue != $convertedValue) {
                 ODataException::createInternalServerError(Messages::metadataQueryProviderWrapperIDSQPMethodReturnsInstanceWithNonMatchingKeys($methodName));
             }
         } catch (\ReflectionException $reflectionException) {
             //throw ODataException::createInternalServerError(
             //  Messages::orderByParserFailedToAccessOrInitializeProperty(
             //      $resourceProperty->getName(), $resourceType->getName()
             //  )
             //);
         }
     }
 }
 /**
  * Gets an entity instance from an entity set identifed by a key
  * 
  * @param ResourceSet   $resourceSet   The entity set from which an entity 
  *                                     needs to be fetched
  * @param KeyDescriptor $keyDescriptor The key to identify the entity 
  *                                     to be fetched
  * 
  * @return Object/NULL Returns entity instance if found else null
  */
 public function getResourceFromResourceSet(ResourceSet $resourceSet, KeyDescriptor $keyDescriptor)
 {
     $resourceSetName = $resourceSet->getName();
     if ($resourceSetName !== 'Posts' && $resourceSetName !== 'Tags' && $resourceSetName !== 'Categories' && $resourceSetName !== 'Comments' && $resourceSetName !== 'Users') {
         die('(WordPressQueryProvider) Unknown resource set ' . $resourceSetName);
     }
     $namedKeyValues = $keyDescriptor->getValidatedNamedValues();
     $keys = array();
     foreach ($namedKeyValues as $key => $value) {
         $keys[] = "{$key} = '{$value['0']}' ";
     }
     $conditionStr = implode(' AND ', $keys);
     switch ($resourceSetName) {
         case 'Posts':
             $query = "SELECT * FROM `wp_posts` WHERE" . " wp_posts.post_type = 'post'" . " AND wp_posts.post_status = 'publish'" . " AND wp_posts.ID = " . $namedKeyValues['PostID'][0];
             $stmt = mysql_query($query);
             //If resource not found return null to the library
             if (!mysql_num_rows($stmt)) {
                 return null;
             }
             $data = mysql_fetch_assoc($stmt);
             $result = $this->_serializePost($data);
             break;
         case 'Tags':
             $query = "SELECT t.*, tt.description" . " FROM `wp_terms` AS t INNER JOIN `wp_term_taxonomy` as tt" . " ON tt.term_id = t.term_id" . " WHERE tt.taxonomy = 'post_tag'" . " AND t.term_id = " . $namedKeyValues['TagID'][0];
             $stmt = mysql_query($query);
             //If resource not found return null to the library
             if (!mysql_num_rows($stmt)) {
                 return null;
             }
             $data = mysql_fetch_assoc($stmt);
             $result = $this->_serializeTag($data);
             break;
         case 'Categories':
             $query = "SELECT t.*, tt.description" . " FROM `wp_terms` AS t INNER JOIN `wp_term_taxonomy` as tt" . " ON tt.term_id = t.term_id" . " WHERE tt.taxonomy = 'category'" . " AND t.term_id = " . $namedKeyValues['CategoryID'][0];
             $stmt = mysql_query($query);
             //If resource not found return null to the library
             if (!mysql_num_rows($stmt)) {
                 return null;
             }
             $data = mysql_fetch_assoc($stmt);
             $result = $this->_serializeCategory($data);
             break;
         case 'Comments':
             $query = "SELECT * FROM `wp_comments`" . " WHERE comment_approved = 1" . " AND comment_ID = " . $namedKeyValues['CommentID'][0];
             $stmt = mysql_query($query);
             //If resource not found return null to the library
             if (!mysql_num_rows($stmt)) {
                 return null;
             }
             $data = mysql_fetch_assoc($stmt);
             $result = $this->_serializeComment($data);
             break;
         case 'Users':
             $query = "SELECT * FROM `wp_users` WHERE ID = " . $namedKeyValues['UserID'][0];
             $stmt = mysql_query($query);
             //If resource not found return null to the library
             if (!mysql_num_rows($stmt)) {
                 return null;
             }
             $data = mysql_fetch_assoc($stmt);
             $result = $this->_serializeUser($data);
             break;
     }
     mysql_free_result($stmt);
     return $result;
 }
 /**
  * To add a navigation property (resource set or resource reference)
  * to a resource type
  * 
  * @param ResourceType         $resourceType         The resource type to add 
  *                                                   the resource reference 
  *                                                   or resource 
  *                                                   reference set property to
  * @param string               $name                 The name of the 
  *                                                   property to add
  * @param ResourceSet          $targetResourceSet    The resource set the 
  *                                                   resource reference
  *                                                   or reference 
  *                                                   set property 
  *                                                   ponits to
  * @param ResourcePropertyKind $resourcePropertyKind The property kind
  * 
  * @return void
  */
 private function _addReferencePropertyInternal(ResourceType $resourceType, $name, ResourceSet $targetResourceSet, $resourcePropertyKind)
 {
     try {
         $resourceType->getInstanceType()->getProperty($name);
     } catch (ReflectionException $exception) {
         throw new InvalidOperationException('Can\'t add a property which does not exist on the instance type.');
     }
     if (!($resourcePropertyKind == ResourcePropertyKind::RESOURCESET_REFERENCE || $resourcePropertyKind == ResourcePropertyKind::RESOURCE_REFERENCE)) {
         throw new InvalidOperationException('Property kind should be ResourceSetReference or ResourceReference');
     }
     $targetResourceType = $targetResourceSet->getResourceType();
     $resourceProperty = new ResourceProperty($name, null, $resourcePropertyKind, $targetResourceType);
     $resourceType->addProperty($resourceProperty);
     //Create instance of AssociationSet for this relationship
     $sourceResourceSet = $resourceType->getCustomState();
     if (is_null($sourceResourceSet)) {
         throw new InvalidOperationException('Failed to retrieve the custom state from ' . $resourceType->getName());
     }
     //Customer_Orders_Orders, Order_Customer_Customers
     //(source type::name _ source property::name _ target set::name)
     $assoicationSetKey = $resourceType->getName() . '_' . $name . '_' . $targetResourceSet->getName();
     $associationSet = new ResourceAssociationSet($assoicationSetKey, new ResourceAssociationSetEnd($sourceResourceSet, $resourceType, $resourceProperty), new ResourceAssociationSetEnd($targetResourceSet, $targetResourceSet->getResourceType(), null));
     $this->associationSets[$assoicationSetKey] = $associationSet;
 }
 /**
  * Gets the maximum page size for an entity set resource
  * 
  * @param ResourceSet $resourceSet Entity set for which to get the page size
  * 
  * @return int
  */
 public function getEntitySetPageSize(ResourceSet $resourceSet)
 {
     if (!array_key_exists($resourceSet->getName(), $this->_pageSizes)) {
         return $this->_defaultPageSize;
     }
     return $this->_pageSizes[$resourceSet->getName()];
 }
 /**
  * test ResourceSet class
  */
 public function testResourceSet()
 {
     try {
         $exceptionThrown = false;
         try {
             $int64 = ResourceType::getPrimitiveResourceType(EdmPrimitiveType::INT64);
             $customerResourceSet = new ResourceSet('Customers', $int64);
         } catch (\InvalidArgumentException $exception) {
             $exceptionThrown = true;
             $this->AssertStringStartsWith('The ResourceTypeKind property of a ResourceType instance associated with a ResourceSet', $exception->getMessage());
         }
         $customerResType = $this->_getCustomerResourceType();
         $customerResourceSet = new ResourceSet('Customers', $customerResType);
         $this->AssertEquals($customerResourceSet->getName(), 'Customers');
         $this->AssertEquals($customerResourceSet->getResourceType()->getName(), 'Customer');
         if (!$exceptionThrown) {
             $this->fail('An expected InvalidArgumentException for \'non-entity type resource type\' has not been raised');
         }
     } catch (\Exception $exception) {
         $this->fail('An unexpected Exception has been raised' . $exception->getMessage());
     }
 }
 /**
  * Gets an entity instance from an entity set identifed by a key
  * 
  * @param ResourceSet   $resourceSet   The entity set from which 
  *                                     an entity needs to be fetched
  * @param KeyDescriptor $keyDescriptor The key to identify the entity to be fetched
  * 
  * @return Object/NULL Returns entity instance if found else null
  */
 public function getResourceFromResourceSet(ResourceSet $resourceSet, KeyDescriptor $keyDescriptor)
 {
     $resourceSetName = $resourceSet->getName();
     if ($resourceSetName === 'events') {
         $resourceSetName = 'event';
     }
     if ($resourceSetName === 'event_inventories') {
         $resourceSetName = 'event_inventory';
     }
     if ($resourceSetName === 'event_users') {
         $resourceSetName = 'event_user';
     }
     if ($resourceSetName === 'inventories') {
         $resourceSetName = 'inventory';
     }
     if ($resourceSetName === 'items') {
         $resourceSetName = 'item';
     }
     if ($resourceSetName === 'users') {
         $resourceSetName = 'user';
     }
     if ($resourceSetName !== 'event' and $resourceSetName !== 'event_inventory' and $resourceSetName !== 'event_user' and $resourceSetName !== 'inventory' and $resourceSetName !== 'item' and $resourceSetName !== 'user') {
         die('(InventoryQueryProvider) Unknown resource set ' . $resourceSetName);
     }
     $namedKeyValues = $keyDescriptor->getValidatedNamedValues();
     $condition = null;
     foreach ($namedKeyValues as $key => $value) {
         $condition .= $key . ' = ' . $value[0] . ' and ';
     }
     $len = strlen($condition);
     $condition = substr($condition, 0, $len - 5);
     $query = "SELECT * FROM {$resourceSetName} WHERE {$condition}";
     $stmt = mysql_query($query);
     if ($stmt === false) {
         die(print_r(mysql_error(), true));
     }
     //If resource not found return null to the library
     if (!mysql_num_rows($stmt)) {
         return null;
     }
     $result = null;
     while ($record = mysql_fetch_array($stmt, MYSQL_ASSOC)) {
         switch ($resourceSetName) {
             case 'event':
                 $returnResult = $this->_serializeevent($record);
                 break;
             case 'event_inventory':
                 $returnResult = $this->_serializeevent_inventory($record);
                 break;
             case 'event_user':
                 $returnResult = $this->_serializeevent_user($record);
                 break;
             case 'inventory':
                 $returnResult = $this->_serializeinventory($record);
                 break;
             case 'item':
                 $returnResult = $this->_serializeitem($record);
                 break;
             case 'user':
                 $returnResult = $this->_serializeuser($record);
                 break;
         }
     }
     mysql_free_result($stmt);
     return $returnResult;
 }
 /**
  * To check this relationship belongs to a specfic resource set, type 
  * and property
  * 
  * @param ResourceSet      $resourceSet      Resource set for the association
  *                                           end
  * @param ResourceType     $resourceType     Resource type for the association
  *                                           end
  * @param ResourceProperty $resourceProperty Resource property for the 
  *                                           association end
  * 
  * @return boolean
  */
 public function isBelongsTo(ResourceSet $resourceSet, ResourceType $resourceType, ResourceProperty $resourceProperty)
 {
     return strcmp($resourceSet->getName(), $this->_resourceSet->getName()) == 0 && $this->_resourceType->isAssignableFrom($resourceType) && (is_null($resourceProperty) && is_null($this->_resourceProperty) || !is_null($resourceProperty) && !is_null($this->_resourceProperty) && strcmp($resourceProperty->getName(), $this->_resourceProperty->getName()) == 0);
 }