private void FormatCName(DataStreamRicCreationBulkTemplate bulkTemp)
        {
            string name1 = bulkTemp.Name1.Trim();
            string name2 = bulkTemp.Name2.Trim();

            string names = string.Empty;

            if (!name1.EndsWith("."))
            {
                names = name1 + " " + name2;
            }
            else
            {
                names = name1 + name2;
            }

            if (names.EndsWith(".") && (names.Length + bulkTemp.NameSuffix.Length) <= 24)
            {
                names += bulkTemp.NameSuffix;
            }
            else if ((!names.EndsWith(".")) && (names.Length + 1 + bulkTemp.NameSuffix.Length) <= 24)
            {
                names += " " + bulkTemp.NameSuffix;
            }

            else if (names.Length > 24)
            {
                names = names.Substring(0, 24);
            }

            bulkTemp.CName = names.PadRight(24, ' ');
        }
        private void FormatBulkNamesForHShare(DataStreamRicCreationBulkTemplate bulkTemp)
        {
            string hShareEnding = "'H'";

            if (bulkTemp.Name2.Trim() != "")
            {
                bulkTemp.Name2 = bulkTemp.Name2.Substring(0, 21) + hShareEnding;
            }
            else if (bulkTemp.Name1.Trim().Length > 20)
            {
                bulkTemp.Name2 = hShareEnding.PadRight(24, ' ');
            }
            else
            {
                bulkTemp.Name1 = bulkTemp.Name1.Substring(0, 21) + hShareEnding;
            }
        }
        private void FormatBulkNames(string companyName, DataStreamRicCreationBulkTemplate bulkTemp)
        {
            //Rules:
            //1. 提取第一个单词后trim
            //2. 去掉5个后缀 if 有
            //3. 去掉company后缀, if 有
            //4. 如果Joint。。。Company出现在中间, 用JSC.替换
            //5. 开始找缩写
            //6. 开始format两个name
            //7. if > 48, user input
            string temp = string.Empty;

            if (string.IsNullOrEmpty(companyName))
            {
                bulkTemp.Name1 = temp.PadRight(24, ' ');
                bulkTemp.Name2 = temp.PadRight(24, ' ');
                return;
            }

            string inputConpanyName = companyName;

            companyName = companyName.ToUpper();

            string[] nameWords = companyName.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            //Company Name only contains one word.
            if (nameWords.Length == 1)
            {
                string abbr = companyName;
                if (namesAbbs.ContainsKey(companyName))
                {
                    abbr = namesAbbs[companyName];
                }
                bulkTemp.Name1 = abbr.PadRight(24, ' ');
                bulkTemp.Name2 = temp.PadRight(24, ' ');
                return;
            }

            companyName = FormatCompanyName(companyName);

            string nameSuffix = string.Empty;
            string nameLeft   = TrimCompanyEndings(companyName, ref nameSuffix);

            bulkTemp.NameSuffix = nameSuffix;

            List <string> abbreviations = GetNameAbbreviations(nameLeft);

            string namePart1 = string.Empty;
            string namePart2 = string.Empty;

            bool reFormat = false;

            do
            {
                if (reFormat)
                {
                    if (abbreviations[abbreviations.Count - 1] == "CORP.")
                    {
                        bulkTemp.NameSuffix = "CORP.";
                    }

                    if (abbreviations.Contains("CORP."))
                    {
                        abbreviations.Remove("CORP.");
                    }
                    if (abbreviations.Contains("COMPANY"))
                    {
                        abbreviations.Remove("COMPANY");
                    }
                }
                string formatedName = string.Join("", abbreviations.ToArray()).Trim();
                if (formatedName.Length > 48)
                {
                    if (reFormat)
                    {
                        string[] namesInput = DataStreamRicCreationNamesInput.Prompt(inputConpanyName, formatedName);
                        if (namesInput != null)
                        {
                            namePart1 = namesInput[0];
                            namePart2 = namesInput[1];
                        }
                    }
                    else
                    {
                        reFormat = true;
                    }
                }
                else if (formatedName.Length <= 24)
                {
                    namePart1 = formatedName;
                }

                else
                {
                    int    index   = 24;
                    int    index2  = 24;
                    char[] charArr = formatedName.ToCharArray();
                    if (charArr[index] == ' ')
                    {
                        index2 = index;
                    }
                    else
                    {
                        while (true)
                        {
                            char middleChar = charArr[index];
                            if (middleChar == ' ' || middleChar == '.')
                            {
                                break;
                            }
                            index--;
                        }
                        index2 = index + 1;
                    }
                    namePart1 = formatedName.Substring(0, index2);
                    namePart2 = formatedName.Substring(index2).Trim();
                    if (namePart2.Length > 24)
                    {
                        if (reFormat)
                        {
                            string[] namesInput = DataStreamRicCreationNamesInput.Prompt(inputConpanyName, formatedName);
                            if (namesInput != null)
                            {
                                namePart1 = namesInput[0];
                                namePart2 = namesInput[1];
                            }
                        }
                        else
                        {
                            reFormat = true;
                        }
                    }
                }
            }while (reFormat);

            bulkTemp.Name1 = namePart1.PadRight(24, ' ');
            bulkTemp.Name2 = namePart2.PadRight(24, ' ');
        }
        private void FormatOutputLine(List <DataStreamRicCreationInfo> ricCreations)
        {
            List <string> normalLine = new List <string>();
            List <string> hShareLine = new List <string>();

            foreach (DataStreamRicCreationInfo item in ricCreations)
            {
                string temp = "";
                DataStreamRicCreationBulkTemplate bulkTemp = new DataStreamRicCreationBulkTemplate();
                bulkTemp.Seqn = temp.PadRight(7, ' ');
                string initials = GetInitialsFromName(item.CompanyName);
                bulkTemp.Mnem = "K:" + initials;
                if (string.IsNullOrEmpty(item.Sedol))
                {
                    string sedol = GetSedol(item.Isin);
                    if (string.IsNullOrEmpty(sedol))
                    {
                        sedol = temp.PadRight(9, ' ');
                    }
                    else
                    {
                        sedol = "UK" + sedol;
                    }
                    bulkTemp.Sedol = sedol;
                }
                else
                {
                    bulkTemp.Sedol = "UK" + item.Sedol;
                }

                bulkTemp.IsHShare = CheckIfHShare(item.SecurityDescription);

                FormatBulkNames(item.CompanyName, bulkTemp);

                FormatCName(bulkTemp);

                if (bulkTemp.IsHShare)
                {
                    FormatBulkNamesForHShare(bulkTemp);
                }

                if (item.FirstTradingDate.Equals("-9999999"))
                {
                    bulkTemp.Bdate = temp.PadRight(8, ' ');
                }
                else
                {
                    DateTime dt;
                    string   bdate = string.Empty;
                    bdate          = DateTime.TryParseExact(item.FirstTradingDate, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dt) ? dt.ToString("dd/MM/yy", DateTimeFormatInfo.InvariantInfo) : temp.PadRight(8, ' ');
                    bulkTemp.Bdate = bdate;
                }


                bulkTemp.DefMki = "HK";

                bulkTemp.Nosh  = "1";
                bulkTemp.Mnem2 = "HK" + item.Ticker.TrimStart('0');
                bulkTemp.Mnem2 = bulkTemp.Mnem2.PadRight(12, ' ');
                bulkTemp.Isin  = string.IsNullOrEmpty(item.Isin) ? temp.PadRight(12, ' ') : item.Isin;

                if (bulkTemp.IsHShare)
                {
                    bulkTemp.Geog = "051";
                    bulkTemp.Dcur = "079";
                }
                else
                {
                    bulkTemp.Geog = temp.PadRight(3, ' ');
                    bulkTemp.Dcur = temp.PadRight(3, ' ');
                }

                string line = string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\",\"{10}\",\"{11}\",\"{12}\"",
                                            bulkTemp.Seqn, bulkTemp.Mnem, bulkTemp.Sedol, bulkTemp.Name1, bulkTemp.Name2,
                                            bulkTemp.Bdate, bulkTemp.DefMki, bulkTemp.Nosh, bulkTemp.Mnem2, bulkTemp.Isin, bulkTemp.CName, bulkTemp.Geog, bulkTemp.Dcur);
                if (bulkTemp.IsHShare)
                {
                    hShareLine.Add(line);
                }
                else
                {
                    normalLine.Add(line);
                }

                if (ricCreations.Count > 1)
                {
                    System.Threading.Thread.Sleep(5000);
                }
            }
            normalOutputLine = string.Join("\r\n", normalLine.ToArray());
            hShareOutputLine = string.Join("\r\n", hShareLine.ToArray());
        }