示例#1
0
        private int ReadLearningPathInformation()
        {
            string csvDelimiter = ",";

            if (!File.Exists(_learningPathFile))
            {
                Console.WriteLine("Failed to read input file - {0}", _learningPathFile);
                WriteLog.WriteLogLine("Failed to read input file - {0}", _learningPathFile);
                return(0);
            }

            _operation = new OperationInformation();

            using (StreamReader reader = new StreamReader(_learningPathFile))
            {
                string csvContent = reader.ReadToEnd();
                if (!string.IsNullOrEmpty(csvContent))
                {
                    string[] csvLines   = csvContent.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    string[] parameters = csvLines[0].Split(new string[] { csvDelimiter }, StringSplitOptions.RemoveEmptyEntries);
                    if (parameters.Length > 0)
                    {
                        string operation = parameters[0];

                        switch (operation.Trim().ToLower())
                        {
                        case "a":
                        case "add":
                        {
                            int colCount = parameters.Length;
                            if (colCount == 2)
                            {
                                // Add a single Learning Path only
                                _operation.Operation = OperationType.Add;
                                _operation.LearningPathDisplayName = parameters[1];
                                _operation.LearningPathModulesInfo = null;
                            }
                            else if (colCount >= 3)
                            {
                                int  moduleCount             = 0;
                                bool parseModuleCountSuccess = Int32.TryParse(parameters[2], out moduleCount);
                                if (!parseModuleCountSuccess)
                                {
                                    Console.WriteLine("Invalid input file - {0}, the module count(third column) for 'Add' should be an integer.", _learningPathFile);
                                    WriteLog.WriteLogLine("Invalid input file - {0}, the module count(third column) for 'Add' should be an integer.", _learningPathFile);
                                    return(0);
                                }
                                if (colCount == 3)
                                {
                                    // Add a learning path with modules only
                                    _operation.Operation = OperationType.Add;
                                    _operation.LearningPathDisplayName = parameters[1];
                                    _operation.LearningPathModulesInfo = new int[moduleCount][];

                                    for (int i = 0; i < moduleCount; i++)
                                    {
                                        _operation.LearningPathModulesInfo[i] = null;
                                    }
                                }
                                else
                                {
                                    // Add a learning path with modules, with units
                                    if (moduleCount + 3 == colCount)
                                    {
                                        bool passValidation = true;
                                        for (int i = 0; i < moduleCount; i++)
                                        {
                                            // Match 1.20
                                            string pattern = @"\d*[.]\d*";
                                            bool   isMatch = Regex.IsMatch(parameters[i + 3], pattern);
                                            if (!isMatch)
                                            {
                                                passValidation = false;
                                                Console.WriteLine("Invalid input file - {0}, parameter {1} does not match parttern {digits}.{digits}.", _learningPathFile, parameters[i + 3]);
                                                WriteLog.WriteLogLine("Invalid input file - {0}, parameter {1} does not match parttern {digits}.{digits}.", _learningPathFile, parameters[i + 3]);
                                                return(0);
                                            }
                                        }
                                        if (passValidation)
                                        {
                                            _operation.Operation = OperationType.Add;
                                            _operation.LearningPathDisplayName = parameters[1];
                                            _operation.LearningPathModulesInfo = new int[moduleCount][];
                                            for (int i = 0; i < moduleCount; i++)
                                            {
                                                string[] digits = parameters[i + 3].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                                                _operation.LearningPathModulesInfo[i] = new int[Int32.Parse(digits[1])];
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("Invalid input file - {0}, at least 2 input values are required for 'Add'.", _learningPathFile);
                                WriteLog.WriteLogLine("Invalid input file - {0}, at least 2 input values are required for 'Add'.", _learningPathFile);
                                return(0);
                            }
                        }; break;

                        case "u":
                        case "update":
                        {
                            int colCount = parameters.Length;
                            if (colCount >= 3)
                            {
                                _operation.Operation = OperationType.Update;
                                //_operation.IsCascadeUpdating = false;
                                _operation.SetValue <bool>("$isCascadeUpdating", false);
                                var updateWorkItems   = new List <int>();
                                var isCascadingUpdate = parameters[1].Trim().ToLower();
                                if (isCascadingUpdate == "false" || isCascadingUpdate == "true")
                                {
                                    //_operation.IsCascadeUpdating = Boolean.Parse(isCascadingUpdate);
                                    _operation.SetValue <bool>("$isCascadeUpdating", Boolean.Parse(isCascadingUpdate));
                                }

                                for (int i = 2; i < colCount; i++)
                                {
                                    int theId = 0;
                                    Int32.TryParse(parameters[i], out theId);

                                    //add to list
                                    if (theId != 0)
                                    {
                                        updateWorkItems.Add(theId);
                                    }
                                }
                                _operation.SetValue <List <int> >("$updateWorkItemIds", updateWorkItems);
                            }
                            else
                            {
                                Console.WriteLine("Invalid input file - {0}, at least 3 input values are required for 'Update'.", _learningPathFile);
                                WriteLog.WriteLogLine("Invalid input file - {0}, at least 3 input values are required for 'Update'.", _learningPathFile);
                                return(0);
                            }
                        }; break;

                        case "d":
                        case "delete":
                        {
                            int colCount = parameters.Length;
                            if (colCount >= 3)
                            {
                                _operation.Operation = OperationType.Delete;
                                _operation.SetValue <bool>("$isCascadeDeleting", false);
                                //_operation.IsCascadeDeleting = false;
                                var deleteWorkItems   = new List <int>();
                                var isCascadingDelete = parameters[1].Trim().ToLower();
                                if (isCascadingDelete == "false" || isCascadingDelete == "true")
                                {
                                    //_operation.IsCascadeDeleting = Boolean.Parse(isCascadingDelete);
                                    _operation.SetValue <bool>("$isCascadeDeleting", Boolean.Parse(isCascadingDelete));
                                }
                                for (int i = 2; i < colCount; i++)
                                {
                                    int theId = 0;
                                    Int32.TryParse(parameters[i], out theId);

                                    //add to list
                                    if (theId != 0)
                                    {
                                        deleteWorkItems.Add(theId);
                                    }
                                }

                                _operation.SetValue <List <int> >("$deleteWorkItemIds", deleteWorkItems);
                            }
                            else
                            {
                                Console.WriteLine("Invalid input file - {0}, at least 3 input values are required for 'Delete'.", _learningPathFile);
                                WriteLog.WriteLogLine("Invalid input file - {0}, at least 3 input values are required for 'Delete'.", _learningPathFile);
                                return(0);
                            }
                        }; break;

                        case "q":
                        case "query":
                        {
                            int colCount = parameters.Length;
                            if (colCount >= 2)
                            {
                                List <WITFieldEntity> clauses = new List <WITFieldEntity>();
                                // retrieve the query clause
                                for (int i = 1; i < colCount; i++)
                                {
                                    string    text     = parameters[i];
                                    string [] keyValue = text.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                                    if (keyValue.Length == 2)
                                    {
                                        WITFieldEntity entity = new WITFieldEntity()
                                        {
                                            FieldName  = keyValue[0].Trim(),
                                            FieldValue = keyValue[1].Trim()
                                        };
                                        clauses.Add(entity);
                                    }
                                }
                                _operation.Operation = OperationType.Query;
                                _operation.SetValue <List <WITFieldEntity> >("$QueryClauses", clauses);
                            }
                        }; break;

                        case "qu":
                        case "queryupdate":
                        {
                            int colCount = parameters.Length;
                            if (colCount >= 2)
                            {
                                List <WITFieldEntity> clauses = new List <WITFieldEntity>();
                                // retrieve the query clause
                                for (int i = 1; i < colCount; i++)
                                {
                                    string   text     = parameters[i];
                                    string[] keyValue = text.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                                    if (keyValue.Length == 2)
                                    {
                                        WITFieldEntity entity = new WITFieldEntity()
                                        {
                                            FieldName  = keyValue[0].Trim(),
                                            FieldValue = keyValue[1].Trim()
                                        };
                                        clauses.Add(entity);
                                    }
                                }
                                _operation.Operation = OperationType.QueryUpdate;
                                _operation.SetValue <List <WITFieldEntity> >("$QueryClauses", clauses);
                            }
                        }; break;

                        case "g":
                        case "get":
                        {
                            int colCount = parameters.Length;
                            if (colCount == 2)
                            {
                                int theId = 0;
                                Int32.TryParse(parameters[1], out theId);

                                if (theId != 0)
                                {
                                    _operation.Operation = OperationType.Get;
                                    //_operation.WorkItemIdToDisplay = theId;
                                    _operation.SetValue <int>("$workItemId", theId);
                                }
                                else
                                {
                                    Console.WriteLine("Invalid input file - {0}, second paremeter should be integer for 'Get'.", _learningPathFile);
                                    WriteLog.WriteLogLine("Invalid input file - {0}, second paremeter should be integer for 'Get'.", _learningPathFile);
                                    return(0);
                                }
                            }
                            else
                            {
                                Console.WriteLine("Invalid input file - {0}, only 2 input values are required for 'Get'.", _learningPathFile);
                                WriteLog.WriteLogLine("Invalid input file - {0}, only 2 input values are required for 'Get'.", _learningPathFile);
                                return(0);
                            }
                        }; break;

                        default:
                        {
                            Console.WriteLine("Invalid input file - {0}, operation '{1}' is not support yet.", _learningPathFile, operation);
                            WriteLog.WriteLogLine("Invalid input file - {0}, operation '{1}' is not support yet.", _learningPathFile, operation);
                            return(0);
                        };
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Invalid input file - {0}, content is empty.", _learningPathFile);
                    WriteLog.WriteLogLine("Invalid input file - {0}, content is empty.", _learningPathFile);
                    return(0);
                }
            }

            return(0);
        }