Ejemplo n.º 1
0
        public IG_Table ExportTable(string branch)
        {
            // 각종 테이블정보 초기화
            InitiateInfo();

            int cnt = 0;

            Dictionary<string, string> define;
            Dictionary<string, string> dataType;
            Dictionary<string, Dictionary<string, string>> subgroups;

            IG_Table table = new IG_Table(GetTableName());

            if (table == null) throw new Exception("테이블명이 제대로 설정되지 않았습니다.");

            if (!BranchDefines.ContainsKey(branch)) throw new Exception("[" + branch + "] 브랜치 설정이 존재하지 않습니다.");

            define = BranchDefines[branch];
            ChangeBranch(branch);

            dataType = BranchDataTypes[branch];
            subgroups = GetSubgroups();

            // row 순서대로 브랜치를 체크하여 데이터로 출력
            foreach (Excel.Worksheet ws in Globals.IG_PlanAddIn.Application.Worksheets)
            {
                foreach (Excel.ListObject lo in ws.ListObjects)
                {
                    if (lo.Name.Length >= Properties.Settings.Default.TablePrefix.Length && lo.Name.Substring(0, Properties.Settings.Default.TablePrefix.Length).Equals(Properties.Settings.Default.TablePrefix))
                    {
                        // contentsID가 없는 테이블은 무사통과
                        bool hasContentsID = false;

                        // 컨텐츠ID 체크
                        for (int i = 1; i <= lo.ListColumns.Count; i++)
                            if (lo.ListColumns.get_Item(i).Name.ToUpper().Equals("contentsID".ToUpper()))
                            {
                                hasContentsID = true;
                                break;
                            }

                        // 필드가 define에 존재하는지 체크
                        // Alias 필드인지 확인
                        Dictionary<string, int> indexMatch = new Dictionary<string, int>();

                        foreach (string k in BranchDefines[branch].Keys)
                        {
                            try
                            {
                                if (BranchAliases[branch][k] == null) indexMatch.Add(k, lo.ListColumns[k].Index);
                                else indexMatch.Add(k, lo.ListColumns[BranchAliases[branch][k]].Index);
                            }
                            catch
                            {
                                indexMatch.Add(k, 0);
                            }
                        }

                        // 유일키 인덱스 구하기
                        int keyIndex = lo.ListColumns[BranchDefines[branch].First().Key].Index;

                        for (int r = 1; r <= lo.ListRows.Count; r++)
                        {
                            // tableinfo에서 설정된 contents만 출력
                            if (hasContentsID == false || IsValidContentsId(lo.DataBodyRange[r, lo.ListColumns["contentsID"].Index].value2))
                            {
                                cnt++;
                                int id = Convert.ToInt32(lo.DataBodyRange[r, keyIndex].value2);
                                if (id > 0)
                                {
                                    if (table.ContainsKey(id))
                                        throw new Exception(String.Format("인덱스 중복오류: {0}", id));

                                    table.StartAdd(id);

                                    foreach (string k in BranchDefines[branch].Keys)
                                    {
                                        object tmp = lo.DataBodyRange[r, indexMatch[k]].value2;

                                        // 데이터 타입 검증 후, 출력
                                        try
                                        {
                                            // 필드정의에 존재하나 실데이터가 없는 경우, 기본값 출력
                                            if (indexMatch[k] > 0 && tmp != null && dataType[k] != null)
                                                table.AddElement(k, ToXmlString(GetValidateData(Convert.ToString(tmp), dataType[k], subgroups)), dataType[k], subgroups);
                                            else
                                                table.AddElement(k, ToXmlString(GetValidateData(Convert.ToString(BranchDefines[branch][k]), dataType[k], subgroups)), dataType[k], subgroups);
                                        }
                                        catch(Exception e)
                                        {
                                            System.Windows.Forms.Clipboard.Clear();
                                            System.Windows.Forms.Clipboard.SetText(Convert.ToString(id));
                                            if (dataType[k] != null)
                                                throw new Exception(String.Format("[{0} 데이터타입 오류]\n인덱스: {1}\n필드명: {2}", e.Message, id, k));
                                            else
                                                throw new Exception(String.Format("[데이터타입 미설정 오류]\n필드명: {0}", k));
                                        }
                                    }
                                    table.EndAdd();
                                }
                            }
                        }
                    }
                }
            }
            if (cnt <= 0) throw new Exception("테이블명이 정확하지 않습니다.");

            return table;
        }