private TableEntity LoadEntity(TableContext context, string entitySetName,
                                       string rowKeyColumn, string rowKeyValue, object origianlRowKeyValue,
                                       string parKeyColumn, string parKeyValue, object origianlParKeyValue)
        {
            // For string values add ''
            string rk = GetValueForQuery(rowKeyValue, origianlRowKeyValue);
            string pk = GetValueForQuery(parKeyValue, origianlParKeyValue);

            if (rowKeyColumn == "New.Guid")
            {
                rowKeyColumn = "RowKey";
            }
            if (parKeyColumn == "New.Guid")
            {
                parKeyColumn = "PartitionKey";
            }

            // columns should be in lower case
            string query = string.Format(S_ENTITY_QUERY_TEMPLATE, entitySetName, rowKeyColumn.ToLower(), rk, parKeyColumn.ToLower(), pk);

            List <TableEntity> results = context.Execute <TableEntity>(new Uri(query, UriKind.Relative)).ToList();

            if (results.Count == 0)
            {
                return(null);
            }

            if (results.Count > 1)
            {
                throw new DuplicateEntityException(query);
            }

            return(results[0]);
        }
        private static void CheckAndUpdateMetadataLastUpdateDate(string metadataSet, string entitySet, string entityKind,
                                                                 DateTime lastUpdateDate, DataLoaderParams Params)
        {
            var context = new TableContext(s_account.TableEndpoint.ToString(), s_account.Credentials, Params)
            {
                RetryPolicy =
                    RetryPolicies.RetryExponential(RetryPolicies.DefaultClientRetryCount,
                                                   RetryPolicies.DefaultClientBackoff)
            };

            string             query   = string.Format(s_metadataEntityQueryTemplate, metadataSet, entitySet, entityKind);
            List <TableEntity> results = context.Execute <TableEntity>(new Uri(query, UriKind.Relative)).ToList();

            if (results.Count == 1)
            {
                DateTime oldLastUpdateDate = new TableMetadataEntity(results[0]).LastUpdateDate;
                if (oldLastUpdateDate > lastUpdateDate)
                {
                    throw new MetadataOutdatedException(oldLastUpdateDate, lastUpdateDate);
                }

                results[0].UpdateProperty(DataLoaderConstants.PropNameLastUpdateDate, lastUpdateDate);
                context.UpdateObject(results[0]);
                context.SaveChanges();
            }
            else if (results.Count > 1)
            {
                throw new DuplicateEntityException(query);
            }
        }
        private static void CheckMetadataChanges(string metadataSet, string entitySet, string entityKind, Entity entity,
                                                 DataLoaderParams Params, MetadataKind metadataKind)
        {
            var context = new TableContext(s_account.TableEndpoint.ToString(), s_account.Credentials, Params)
            {
                RetryPolicy =
                    RetryPolicies.RetryExponential(RetryPolicies.DefaultClientRetryCount,
                                                   RetryPolicies.DefaultClientBackoff)
            };

            string             query   = string.Format(s_metadataEntityQueryTemplate, metadataSet, entitySet, entityKind);
            List <TableEntity> results = context.Execute <TableEntity>(new Uri(query, UriKind.Relative)).ToList();

            if (results.Count == 1)
            {
                var exceprionColumns = new[]
                {
                    DataLoaderConstants.PropNameEntityId,
                    DataLoaderConstants.PropNameLastUpdateDate
                };
                string differences = results[0].FindDifferences(entity, exceprionColumns);
                if (differences != null)
                {
                    throw new MetadataChangedException(entitySet, differences);
                }
            }
            else if (results.Count > 1)
            {
                throw new DuplicateEntityException(query);
            }
            else if (results.Count == 0)
            {
                throw new MetadataNotFoundException(entitySet, metadataKind);
            }
        }
        private static void DeleteRdfMetadata(string metadataSet, string entitySet,
                                              DataLoaderParams parameters)
        {
            var context = new TableContext(s_account.TableEndpoint.ToString(), s_account.Credentials, parameters)
            {
                RetryPolicy =
                    RetryPolicies.RetryExponential(RetryPolicies.DefaultClientRetryCount,
                                                   RetryPolicies.DefaultClientBackoff)
            };

            string             query   = string.Format(s_metadataRdfQueryTemplate, metadataSet, entitySet);
            List <TableEntity> results = context.Execute <TableEntity>(new Uri(query, UriKind.Relative)).ToList();

            if (results.Count > 0)
            {
                foreach (TableEntity i in results)
                {
                    context.DeleteObject(i);
                    context.SaveChanges();
                }
            }
        }
        private static void DeleteMetadata(string metadataSet, string entitySet, string entityKind,
                                           DataLoaderParams parameters)
        {
            var context = new TableContext(s_account.TableEndpoint.ToString(), s_account.Credentials, parameters)
            {
                RetryPolicy =
                    RetryPolicies.RetryExponential(RetryPolicies.DefaultClientRetryCount,
                                                   RetryPolicies.DefaultClientBackoff)
            };

            string             query   = string.Format(s_metadataEntityQueryTemplate, metadataSet, entitySet, entityKind);
            List <TableEntity> results = context.Execute <TableEntity>(new Uri(query, UriKind.Relative)).ToList();

            if (results.Count == 1)
            {
                context.DeleteObject(results.First());
                context.SaveChanges();
            }
            else if (results.Count > 1)
            {
                throw new DuplicateEntityException(query);
            }
        }
        private void StoreEntity(string entitySetName, string parKeyPropName, string rowKeyPropName, Entity entity)
        {
            var account =
                CloudStorageAccount.Parse(ConfigurationManager.AppSettings["DataConnectionString"]);
            var context = new TableContext(account.TableEndpoint.ToString(), account.Credentials, _parameters)
            {
                RetryPolicy = RetryPolicies.RetryExponential(5, new TimeSpan(0, 0, 1))
            };

            var kmlSnippet = (string)entity[DataLoaderConstants.PropNameKmlSnippet];

            if (kmlSnippet != null && kmlSnippet.Length > 32 * 1024)
            {
                string blobName      = Guid.NewGuid().ToString();
                string containerName = entitySetName.ToLower();
                StoreKmlSnippetAsBlob(containerName, blobName, kmlSnippet);
                entity[DataLoaderConstants.PropNameKmlSnippet] = string.Format(DataLoaderConstants.KmlSnippetReference,
                                                                               containerName, blobName);
            }

            var kmlCoords = (string)entity[DataLoaderConstants.PropNameKmlCoords];

            if (kmlCoords != null && kmlCoords.Length > 32 * 1024)
            {
                string blobName      = Guid.NewGuid().ToString();
                string containerName = entitySetName.ToLower();
                StoreKmlSnippetAsBlob(containerName, blobName, kmlCoords);
                entity[DataLoaderConstants.PropNameKmlCoords] = string.Format(DataLoaderConstants.KmlSnippetReference,
                                                                              containerName, blobName);
            }

            TableEntity tableEntity = null;
            bool        isUpdate    = false;

            if (_parameters != null)
            {
                string parKey;
                object pk = entity[parKeyPropName];
                if (string.IsNullOrEmpty(entity.Number))
                {
                    parKey = (pk != null) ? ConvertToProperPartitionKey(ConvertValueToString(pk)) : entity.Id.ToString();
                }
                else
                {
                    parKey = entity.Number;
                }

                string rowKey = null;
                object rk     = entity[rowKeyPropName];
                if (rowKeyPropName.ToLower() == DataLoaderConstants.ValueUniqueAutoGen)
                {
                    rowKey = Guid.NewGuid().ToString();
                }
                else
                {
                    rowKey = (rk != null) ? ConvertValueToString(entity[rowKeyPropName]) : Guid.NewGuid().ToString();
                }


                //try to load entity from storage
                if (_overwriteMode == TableOverwriteMode.Add || _overwriteMode == TableOverwriteMode.Update)
                {
                    tableEntity = LoadEntity(context, entitySetName, rowKeyPropName, rowKey, rk, parKeyPropName, parKey, pk);
                    if (tableEntity != null && _overwriteMode == TableOverwriteMode.Add)
                    {
                        throw new EntityAlreadyExistsException(entitySetName, rowKeyPropName, rowKey, parKeyPropName,
                                                               parKey);
                    }
                    if (tableEntity != null)
                    {
                        tableEntity.UpdateEntity(entity);
                        isUpdate = true;
                    }
                }
                //if not found, create new
                if (tableEntity == null)
                {
                    tableEntity = new TableEntity(entity, parKey, rowKey);
                }
            }
            else
            {
                tableEntity = new TableEntity(entity);
            }

            if (!isUpdate)
            {
                context.AddObject(entitySetName, tableEntity);
            }
            else
            {
                context.UpdateObject(tableEntity);
            }

            try
            {
                context.SaveChanges();
            }
            catch (StorageClientException e)
            {
                if (e.ErrorCode == StorageErrorCode.ResourceAlreadyExists && e.StatusCode == HttpStatusCode.Conflict)
                {
                    throw new DuplicateEntityException(tableEntity.ToString(), e);
                }
            }
            catch (DataServiceRequestException e)
            {
                if (e.InnerException != null &&
                    ((DataServiceClientException)e.InnerException).StatusCode == (int)HttpStatusCode.Conflict)
                {
                    throw new DuplicateEntityException(tableEntity.ToString(), e);
                }
            }
        }