public IList<CatalogItem> GetCatalogItem(string accountName, CatalogPathInstance path,
            DataLakeAnalyticsEnums.CatalogItemType itemType)
        {
            var isList = IsCatalogItemOrList(path, itemType);
            var toReturn = new List<CatalogItem>();

            switch (itemType)
            {
                case DataLakeAnalyticsEnums.CatalogItemType.Database:
                    if (isList)
                    {
                        toReturn.AddRange(GetDatabases(accountName));
                    }
                    else
                    {
                        toReturn.Add(GetDatabase(accountName, path.DatabaseName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.Schema:
                    if (isList)
                    {
                        toReturn.AddRange(GetSchemas(accountName, path.DatabaseName));
                    }
                    else
                    {
                        toReturn.Add(GetSchema(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.Assembly:
                    if (isList)
                    {
                        toReturn.AddRange(GetAssemblies(accountName, path.DatabaseName));
                    }
                    else
                    {
                        toReturn.Add(GetAssembly(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.ExternalDataSource:
                    if (isList)
                    {
                        toReturn.AddRange(GetExternalDataSources(accountName, path.DatabaseName));
                    }
                    else
                    {
                        toReturn.Add(GetExternalDataSource(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }

                    break;

                case DataLakeAnalyticsEnums.CatalogItemType.Credential:
                    if (isList)
                    {
                        toReturn.AddRange(GetCredentials(accountName, path.DatabaseName));
                    }
                    else
                    {
                        toReturn.Add(GetCredential(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.Table:
                    if (isList)
                    {
                        toReturn.AddRange(GetTables(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }
                    else
                    {
                        toReturn.Add(GetTable(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName, path.TableOrTableValuedFunctionName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.TablePartition:
                    if (isList)
                    {
                        toReturn.AddRange(GetTablePartitions(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName, path.TableOrTableValuedFunctionName));
                    }
                    else
                    {
                        toReturn.Add(GetTablePartition(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName, path.TableOrTableValuedFunctionName, path.TableStatisticsOrPartitionName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.TableValuedFunction:
                    if (isList)
                    {
                        toReturn.AddRange(GetTableValuedFunctions(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }
                    else
                    {
                        toReturn.Add(GetTableValuedFunction(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName, path.TableOrTableValuedFunctionName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.TableStatistics:
                    if (isList)
                    {
                        toReturn.AddRange(GetTableStatistics(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName, path.TableOrTableValuedFunctionName));
                    }
                    else
                    {
                        toReturn.Add(GetTableStatistic(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName, path.TableOrTableValuedFunctionName,
                            path.TableStatisticsOrPartitionName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.View:
                    if (isList)
                    {
                        toReturn.AddRange(GetViews(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }
                    else
                    {
                        toReturn.Add(GetView(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName, path.TableOrTableValuedFunctionName));
                    }

                    break;

                case DataLakeAnalyticsEnums.CatalogItemType.Procedure:
                    if (isList)
                    {
                        toReturn.AddRange(GetProcedures(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }
                    else
                    {
                        toReturn.Add(GetProcedure(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName, path.TableOrTableValuedFunctionName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.Types:
                    if (isList)
                    {
                        toReturn.AddRange(GetTypes(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }
                    else
                    {
                        throw new InvalidOperationException(Properties.Resources.InvalidUSqlTypeRequest);
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.Secret:
                    if (isList)
                    {
                        throw new InvalidOperationException(Properties.Resources.InvalidUSqlSecretRequest);
                    }
                    else
                    {
                        toReturn.Add(GetSecret(accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }

                    break;
            }

            return toReturn;
        }
        private bool IsCatalogItemOrList(CatalogPathInstance path, DataLakeAnalyticsEnums.CatalogItemType type)
        {
            var isList = false;
            if (path == null || string.IsNullOrEmpty(path.FullCatalogItemPath))
            {
                // in this case, it is a list of ALL catalog items of the specified type across the entire catalog.
                return true;
            }

            switch (type)
            {
                case DataLakeAnalyticsEnums.CatalogItemType.Database:
                    if (string.IsNullOrEmpty(path.DatabaseName))
                    {
                        isList = true;
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.Schema:
                case DataLakeAnalyticsEnums.CatalogItemType.Assembly:
                case DataLakeAnalyticsEnums.CatalogItemType.ExternalDataSource:
                case DataLakeAnalyticsEnums.CatalogItemType.Secret:
                case DataLakeAnalyticsEnums.CatalogItemType.Credential:
                    if (string.IsNullOrEmpty(path.DatabaseName))
                    {
                        throw new CloudException(string.Format(Properties.Resources.InvalidCatalogPath,
                            path.FullCatalogItemPath));
                    }

                    if (string.IsNullOrEmpty(path.SchemaAssemblyOrExternalDataSourceName))
                    {
                        isList = true;
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.Table:
                case DataLakeAnalyticsEnums.CatalogItemType.TableValuedFunction:
                case DataLakeAnalyticsEnums.CatalogItemType.View:
                case DataLakeAnalyticsEnums.CatalogItemType.Procedure:
                case DataLakeAnalyticsEnums.CatalogItemType.Types:
                    if (string.IsNullOrEmpty(path.DatabaseName) ||
                        string.IsNullOrEmpty(path.SchemaAssemblyOrExternalDataSourceName))
                    {
                        throw new CloudException(string.Format(Properties.Resources.InvalidCatalogPath,
                            path.FullCatalogItemPath));
                    }

                    if (string.IsNullOrEmpty(path.TableOrTableValuedFunctionName))
                    {
                        isList = true;
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.TableStatistics:
                case DataLakeAnalyticsEnums.CatalogItemType.TablePartition:
                    if (string.IsNullOrEmpty(path.DatabaseName) ||
                        string.IsNullOrEmpty(path.SchemaAssemblyOrExternalDataSourceName) ||
                        string.IsNullOrEmpty(path.TableOrTableValuedFunctionName))
                    {
                        throw new CloudException(string.Format(Properties.Resources.InvalidCatalogPath,
                            path.FullCatalogItemPath));
                    }

                    if (string.IsNullOrEmpty(path.TableStatisticsOrPartitionName))
                    {
                        isList = true;
                    }
                    break;
            }

            return isList;
        }
        public bool TestCatalogItem(string accountName, CatalogPathInstance path,
            DataLakeAnalyticsEnums.CatalogItemType itemType)
        {
            try
            {
                var result = GetCatalogItem(accountName, path, itemType);
                return result != null && result.Count > 0;
            }
            catch (CloudException ex)
            {
                if (ex.Response != null && ex.Response.StatusCode == HttpStatusCode.NotFound)
                {
                    return false;
                }

                throw;
            }
        }
        public IList<CatalogItem> GetCatalogItem(string resourceGroupName, string accountName, CatalogPathInstance path,
            DataLakeAnalyticsEnums.CatalogItemType itemType)
        {
            if (string.IsNullOrEmpty(resourceGroupName))
            {
                resourceGroupName = GetResourceGroupByAccountName(accountName);
            }

            var isList = IsCatalogItemOrList(path, itemType);
            var toReturn = new List<CatalogItem>();

            switch (itemType)
            {
                case DataLakeAnalyticsEnums.CatalogItemType.Database:
                    if (isList)
                    {
                        toReturn.AddRange(GetDatabases(resourceGroupName, accountName));
                    }
                    else
                    {
                        toReturn.Add(GetDatabase(resourceGroupName, accountName, path.DatabaseName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.Schema:
                    if (isList)
                    {
                        toReturn.AddRange(GetSchemas(resourceGroupName, accountName, path.DatabaseName));
                    }
                    else
                    {
                        toReturn.Add(GetSchema(resourceGroupName, accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.Assembly:
                    if (isList)
                    {
                        toReturn.AddRange(GetAssemblies(resourceGroupName, accountName, path.DatabaseName));
                    }
                    else
                    {
                        toReturn.Add(GetAssembly(resourceGroupName, accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.ExternalDataSource:
                    if (isList)
                    {
                        toReturn.AddRange(GetExternalDataSources(resourceGroupName, accountName, path.DatabaseName));
                    }
                    else
                    {
                        toReturn.Add(GetExternalDataSource(resourceGroupName, accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.Table:
                    if (isList)
                    {
                        toReturn.AddRange(GetTables(resourceGroupName, accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }
                    else
                    {
                        toReturn.Add(GetTable(resourceGroupName, accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName, path.TableOrTableValuedFunctionName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.TableValuedFunction:
                    if (isList)
                    {
                        toReturn.AddRange(GetTableValuedFunctions(resourceGroupName, accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName));
                    }
                    else
                    {
                        toReturn.Add(GetTableValuedFunction(resourceGroupName, accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName, path.TableOrTableValuedFunctionName));
                    }

                    break;
                case DataLakeAnalyticsEnums.CatalogItemType.TableStatistics:
                    if (isList)
                    {
                        toReturn.AddRange(GetTableStatistics(resourceGroupName, accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName, path.TableOrTableValuedFunctionName));
                    }
                    else
                    {
                        toReturn.Add(GetTableStatistic(resourceGroupName, accountName, path.DatabaseName,
                            path.SchemaAssemblyOrExternalDataSourceName, path.TableOrTableValuedFunctionName,
                            path.TableStatisticsName));
                    }

                    break;
            }

            return toReturn;
        }