/// <summary> /// Adds the record to the table storage service, or if it already exists /// updates the last active date and time. /// </summary> /// <param name="record">Request record of the entity being added or updated.</param> private void Set(RequestRecord record) { lock (_serviceContext) { // Get the entity if it exists. RequestEntity entity = GetEntityFromContext(record); if (entity == null) { entity = GetEntityFromTable(record); } if (entity == null) { // Add the new entity to the table storage. _serviceContext.AddObject( _tableName, new RequestEntity(record)); } else { // Update the last active time. entity.LastActiveDate = record.LastActiveDateAsDateTime; _serviceContext.UpdateObject(entity); } // Commit the changes back to the table service. _serviceContext.SaveChanges(); } CheckIfServiceRequired(); }
/// <summary> /// Removes the device behind the request. /// </summary> /// <param name="request">The request from the device.</param> public void Remove(HttpRequest request) { if (_enabled == false) { return; } RequestRecord record = new RequestRecord(request); Remove(record); }
/// <summary> /// Sets the last active time for the device making the request. /// </summary> /// <param name="request">The request from the device.</param> public void Set(HttpRequest request) { if (_enabled == false) { return; } RequestRecord record = new RequestRecord(request); Set(record); }
/// <summary> /// Returns the entity if present from the current context. /// </summary> /// <param name="record">Request record of the entity being sought.</param> /// <returns>The entity if present in the context.</returns> private RequestEntity GetEntityFromContext(RequestRecord record) { var descripter = _serviceContext.Entities.FirstOrDefault( i => i.State != EntityStates.Deleted && ((RequestEntity)i.Entity).PartitionKey == record.PartitionKey && ((RequestEntity)i.Entity).RowKey == record.RowKey); if (descripter != null) { return(descripter.Entity as RequestEntity); } return(null); }
/// <summary> /// Removes the request device details from the table, and then removes /// from memory once the table has been updated. /// </summary> /// <param name="record">Request record of the entity being removed.</param> private void Remove(RequestRecord record) { lock (_serviceContext) { // Get the entity matching the record held in the context. RequestEntity entity = GetEntityFromContext(record); if (entity == null) { // Get the entity from the table if it exists. entity = GetEntityFromTable(record); } // If the entity has been found remove it from the context and the table. if (entity != null) { _serviceContext.DeleteObject(entity); _serviceContext.SaveChanges(); } } }
/// <summary> /// Returns the entity if present from the table service. Should only be called after /// checking the entity is not already present in the context. /// </summary> /// <param name="record">Request record of the entity being sought.</param> /// <returns>The entity if present in the table service.</returns> private RequestEntity GetEntityFromTable(RequestRecord record) { // Create query to return all matching entities for this record. CloudTableQuery<RequestEntity> query = (from entity in _serviceContext.CreateQuery<RequestEntity>(_tableName) where entity.PartitionKey == record.PartitionKey && entity.RowKey == record.RowKey select entity).AsTableServiceQuery<RequestEntity>(); // Return the first or default entity found. try { return query.Execute().FirstOrDefault(); } catch (DataServiceQueryException ex) { //If an exception occurs checked for a 404 error code meaning the resource was not found. if (ex.Response.StatusCode == 404) return null; throw ex; } }
/// <summary> /// Returns the entity if present from the table service. Should only be called after /// checking the entity is not already present in the context. /// </summary> /// <param name="record">Request record of the entity being sought.</param> /// <returns>The entity if present in the table service.</returns> private RequestEntity GetEntityFromTable(RequestRecord record) { // Create query to return all matching entities for this record. CloudTableQuery <RequestEntity> query = (from entity in _serviceContext.CreateQuery <RequestEntity>(_tableName) where entity.PartitionKey == record.PartitionKey && entity.RowKey == record.RowKey select entity).AsTableServiceQuery <RequestEntity>(); // Return the first or default entity found. try { return(query.Execute().FirstOrDefault()); } catch (DataServiceQueryException ex) { //If an exception occurs checked for a 404 error code meaning the resource was not found. if (ex.Response.StatusCode == 404) { return(null); } throw ex; } }
/// <summary> /// Determines if the device making the request has been seen by the web site previously. /// </summary> /// <param name="request">The request from the device.</param> /// <returns>True if the device has been seen before, otherwise false.</returns> public bool IsPresent(HttpRequest request) { if (_enabled == false) { return(false); } RequestRecord record = new RequestRecord(request); lock (_serviceContext) { // Get the entity if it exists. RequestEntity entity = GetEntityFromContext(record); if (entity == null) { entity = GetEntityFromTable(record); } // If the entity isn't null and the last active time is within the timeout period. return(entity != null && entity.LastActiveDate.AddMinutes(_redirectTimeout) >= record.LastActiveDateAsDateTime); } }
/// <summary> /// Constructs an instance of RequestDataModel using the unique /// request ID of the device as the primary key, and the last /// date the device related to the request was active. /// </summary> internal RequestEntity(RequestRecord record) : base(record.PartitionKey, record.RowKey) { LastActiveDate = record.LastActiveDateAsDateTime; }
/// <summary> /// Returns the entity if present from the current context. /// </summary> /// <param name="record">Request record of the entity being sought.</param> /// <returns>The entity if present in the context.</returns> private RequestEntity GetEntityFromContext(RequestRecord record) { var descripter = _serviceContext.Entities.FirstOrDefault( i => i.State != EntityStates.Deleted && ((RequestEntity)i.Entity).PartitionKey == record.PartitionKey && ((RequestEntity)i.Entity).RowKey == record.RowKey); if (descripter != null) return descripter.Entity as RequestEntity; return null; }
/// <summary> /// Removes the device behind the request. /// </summary> /// <param name="request">The request from the device.</param> public void Remove(HttpRequest request) { if (_enabled == false) return; RequestRecord record = new RequestRecord(request); Remove(record); }
/// <summary> /// Determines if the device making the request has been seen by the web site previously. /// </summary> /// <param name="request">The request from the device.</param> /// <returns>True if the device has been seen before, otherwise false.</returns> public bool IsPresent(HttpRequest request) { if (_enabled == false) return false; RequestRecord record = new RequestRecord(request); lock (_serviceContext) { // Get the entity if it exists. RequestEntity entity = GetEntityFromContext(record); if (entity == null) entity = GetEntityFromTable(record); // If the entity isn't null and the last active time is within the timeout period. return entity != null && entity.LastActiveDate.AddMinutes(_redirectTimeout) >= record.LastActiveDateAsDateTime; } }
/// <summary> /// Adds the record to the table storage service, or if it already exists /// updates the last active date and time. /// </summary> /// <param name="record">Request record of the entity being added or updated.</param> private void Set(RequestRecord record) { lock (_serviceContext) { // Get the entity if it exists. RequestEntity entity = GetEntityFromContext(record); if (entity == null) entity = GetEntityFromTable(record); if (entity == null) { // Add the new entity to the table storage. _serviceContext.AddObject( _tableName, new RequestEntity(record)); } else { // Update the last active time. entity.LastActiveDate = record.LastActiveDateAsDateTime; _serviceContext.UpdateObject(entity); } // Commit the changes back to the table service. _serviceContext.SaveChanges(); } CheckIfServiceRequired(); }
/// <summary> /// Removes the request device details from the table, and then removes /// from memory once the table has been updated. /// </summary> /// <param name="record">Request record of the entity being removed.</param> private void Remove(RequestRecord record) { lock (_serviceContext) { // Get the entity matching the record held in the context. RequestEntity entity = GetEntityFromContext(record); if (entity == null) // Get the entity from the table if it exists. entity = GetEntityFromTable(record); // If the entity has been found remove it from the context and the table. if (entity != null) { _serviceContext.DeleteObject(entity); _serviceContext.SaveChanges(); } } }