/// <summary>
        /// This creates the YAML file in the specified dir and uploads it
        /// </summary>
        /// <param name="result"></param>
        private void UploadYamlFileToResources(ParseSqlResult result)
        {
            File.WriteAllText(_yamlFullFilePath, result.Yaml);

            using (IRSAPIClient rsapiClient = GetServiceFactory().CreateProxy <IRSAPIClient>())
            {
                ResourceFileRequest rfRequest = new ResourceFileRequest();

                rfRequest.FullFilePath = _yamlFullFilePath;
                rfRequest.AppGuid      = new Guid(DefaultAppGuid);
                rfRequest.FileName     = Path.GetFileName(_yamlFullFilePath);

                try
                {
                    rsapiClient.PushResourceFiles(rsapiClient.APIOptions, new List <ResourceFileRequest>()
                    {
                        rfRequest
                    });
                    Console.WriteLine($"{nameof(UploadYamlFileToResources)} - Local YAML file ({_yamlFullFilePath}) - was uploaded successfully");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{nameof(UploadYamlFileToResources)} - Could not upload ({_yamlFullFilePath}) - Exception: {ex.Message}");
                }
            }
        }
        private static ParseSqlResult GatherFilesAndParse()
        {
            string[]         files      = Directory.GetFiles(dirSourceFolder);
            List <SqlScript> sqlScripts = ExtractSqlScriptsFromFiles(files);

            ParseSqlResult result = parser.Parse(sqlScripts, yamlFileName);

            return(result);
        }
 private void BuildJson(ParseSqlResult result, string yamlFileName)
 {
     foreach (SqlScript sqlScript in result.SqlScripts)
     {
         JObject dapiJObject = new JObject(new JProperty("YamlFileName", yamlFileName)
                                           , new JProperty("Parameters", new JArray(
                                                               from x in sqlScript.Parameters
                                                               select new JObject(new JProperty("name", x.Key), new JProperty("value", x.Value)))));
         sqlScript.Json = dapiJObject.ToString();
     }
 }
        private static void WriteToDirectory(ParseSqlResult result)
        {
            WriteAllText(yamlFullFilePath, result.Yaml);
            System.Console.WriteLine($"YAML has been created under ({yamlFullFilePath})");

            foreach (SqlScript sqlScript in result.SqlScripts)
            {
                string jsonPath = dirDestinationFolder + Path.DirectorySeparatorChar + sqlScript.Namespace + "-" + sqlScript.Name + ".json";
                WriteAllText(jsonPath, sqlScript.Json);
                System.Console.WriteLine($"JSON has been created under ({jsonPath})");
            }
        }
        /// <summary>
        /// Use this Overloaded Constructor when you're NOT an Admin User.  In that case, the YAML file will already have to been uploaded.
        /// DynamicAPI Helper (DapiHelper) exists to allow you to run SQL via a Web API that is hosted on a custom page.
        /// For specific formatting and parameters, visit https://github.com/relativitydev/relativity-dynamicapi
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="protocol"></param>
        /// <param name="serverAddress"></param>
        /// <param name="workspaceId"></param>
        /// <param name="yamlFileName"></param>
        /// <param name="yamlFullFilePath"></param>
        /// <param name="dapiRapFullFilePath"></param>
        /// <param name="sqlList"></param>
        public DapiHelper(string userName, string password, string protocol, string serverAddress, int workspaceId, string yamlFileName, IEnumerable <string> sqlList)
        {
            _userName      = userName;
            _password      = password;
            _protocol      = protocol;
            _serverAddress = serverAddress;

            _dynamicApiWorkspaceId = workspaceId;

            _sqlList = ConvertToSqlScripts(sqlList).ToList();

            ParseSqlResult result = _parser.Parse(_sqlList, yamlFileName);

            _sqlList = result.SqlScripts;
        }
        /// <summary>
        /// Use this Overloaded Constructor only if you're an Admin user.
        /// DynamicAPI Helper (DapiHelper) exists to allow you to run SQL via a Web API that is hosted on a custom page.
        /// For specific formatting and parameters, visit https://github.com/relativitydev/relativity-dynamicapi
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="protocol"></param>
        /// <param name="serverAddress"></param>
        /// <param name="workspaceId"></param>
        /// <param name="yamlFileName"></param>
        /// <param name="yamlFullFilePath"></param>
        /// <param name="dapiRapFullFilePath"></param>
        /// <param name="sqlList"></param>
        public DapiHelper(string userName, string password, string protocol, string serverAddress, int workspaceId, string yamlFileName, string yamlFullFilePath, string dapiRapFullFilePath, IEnumerable <string> sqlList)
        {
            _userName      = userName;
            _password      = password;
            _protocol      = protocol;
            _serverAddress = serverAddress;

            _dynamicApiWorkspaceId = workspaceId;

            _yamlFullFilePath = yamlFullFilePath;

            _sqlList             = ConvertToSqlScripts(sqlList).ToList();
            _dapiRapFullFilePath = dapiRapFullFilePath;

            ParseSqlResult result = _parser.Parse(_sqlList, yamlFileName);

            _sqlList = result.SqlScripts;

            UploadYamlFileToResources(result);
            SetUp();
        }
        /// <summary>
        /// Grabs formatted SQL and attempts to convert it to YAML, JSON, and a sample Test Class (C#)
        /// Small reference for .CORE to .NET - https://stackify.com/cross-platform-net-core-apps/
        /// </summary>
        /// <param name="sqlScripts"></param>
        /// <returns></returns>
        public ParseSqlResult Parse(List <SqlScript> sqlScripts, string yamlFileName)
        {
            ParseSqlResult result = new ParseSqlResult {
                SqlScripts = sqlScripts
            };

            foreach (SqlScript sqlScript in result.SqlScripts)
            {
                string yaml = Constants.TemplateYaml.YamlBlueprint;

                List <ParseSqlError> validations = Validate(sqlScript);

                if (validations.Count == 0)
                {
                    ExtractHeaderYaml(ref yaml, sqlScript);
                    ExtractInputYaml(ref yaml, sqlScript);
                    ExtractSqlYaml(ref yaml, sqlScript);
                }
                else
                {
                    yaml = $"{Environment.NewLine + Constants.TemplateYaml.YamlDocumentSplitter}";
                    foreach (ParseSqlError error in validations)
                    {
                        yaml += $"{Environment.NewLine}Source: {error.Sql.Source} - Error {error.Error}";
                    }
                }

                result.Errors.AddRange(validations);
                result.Yaml += yaml;
            }

            result.Yaml += Constants.TemplateYaml.YamlDocumentEndOfFile;

            if (result.Success)
            {
                BuildJson(result, yamlFileName);
            }

            return(result);
        }
        private static void RunApp()
        {
            ParseSqlResult result = GatherFilesAndParse();

            WriteToDirectory(result);
            if (publishToRelativity && result.Success)
            {
                UploadYamlFileToResources();
            }
            else
            {
                System.Console.ForegroundColor = ConsoleColor.DarkRed;
                System.Console.WriteLine($"Did not attempt to upload to Relativity.");
                foreach (ParseSqlError error in result.Errors)
                {
                    System.Console.WriteLine($"Error Source: {error.Sql.Source}");
                    System.Console.WriteLine($"Error Message: {error.Error}");
                    System.Console.WriteLine();
                }
                System.Console.ForegroundColor = ConsoleColor.Gray;
            }
        }