Пример #1
0
        public void Import(string filePath, IOrganizationService service, BackgroundWorker worker = null)
        {
            var stream = File.OpenRead(filePath);
            var file   = new ExcelPackage(stream);

            var emds = new List <EntityMetadata>();

            var  forms               = new List <Entity>();
            var  ft                  = new FormTranslation();
            var  st                  = new SiteMapTranslation();
            var  db                  = new DashboardTranslation();
            bool hasFormContent      = false;
            bool hasDashboardContent = false;
            bool hasSiteMapContent   = false;

            foreach (var sheet in file.Workbook.Worksheets)
            {
                switch (sheet.Name)
                {
                case "Entities":
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing entities translations...");
                    }

                    var et = new EntityTranslation();
                    et.Import(sheet, emds, service);
                    break;

                case "Attributes":
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing attributes translations...");
                    }

                    var at = new AttributeTranslation();
                    at.Import(sheet, emds, service);
                    break;

                case "Relationships":
                {
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing Relationships with custom label translations...");
                    }

                    var rt = new RelationshipTranslation();
                    rt.Import(sheet, emds, service);
                    break;
                }

                case "RelationshipsNN":
                {
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing NN Relationships with custom label translations...");
                    }

                    var rtNn = new RelationshipNnTranslation();
                    rtNn.Import(sheet, emds, service);
                    break;
                }

                case "Global OptionSets":
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing global optionsets translations...");
                    }

                    var got = new GlobalOptionSetTranslation();
                    got.Import(sheet, service);
                    break;

                case "OptionSets":
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing optionsets translations...");
                    }

                    var ot = new OptionSetTranslation();
                    ot.Import(sheet, service);
                    break;

                case "Booleans":
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing booleans translations...");
                    }

                    var bt = new BooleanTranslation();
                    bt.Import(sheet, service);
                    break;

                case "Views":
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing views translations...");
                    }

                    var vt = new ViewTranslation();
                    vt.Import(sheet, service);
                    break;

                case "Charts":
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing charts translations...");
                    }

                    var vt2 = new VisualizationTranslation();
                    vt2.Import(sheet, service);
                    break;

                case "Forms":
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing forms translations...");
                    }

                    ft.ImportFormName(sheet, service);
                    break;

                case "Forms Tabs":
                    ft.PrepareFormTabs(sheet, service, forms);
                    hasFormContent = true;
                    break;

                case "Forms Sections":
                    ft.PrepareFormSections(sheet, service, forms);
                    hasFormContent = true;
                    break;

                case "Forms Fields":
                    ft.PrepareFormLabels(sheet, service, forms);
                    hasFormContent = true;
                    break;

                case "Dashboards":
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing dashboard translations...");
                    }

                    db.ImportFormName(sheet, service);
                    break;

                case "Dashboards Tabs":
                    db.PrepareFormTabs(sheet, service, forms);
                    hasDashboardContent = true;
                    break;

                case "Dashboards Sections":
                    db.PrepareFormSections(sheet, service, forms);
                    hasDashboardContent = true;
                    break;

                case "Dashboards Fields":
                    db.PrepareFormLabels(sheet, service, forms);
                    hasDashboardContent = true;
                    break;

                case "SiteMap Areas":
                    st.PrepareAreas(sheet, service);
                    hasSiteMapContent = true;
                    break;

                case "SiteMap Groups":
                    st.PrepareGroups(sheet, service);
                    hasSiteMapContent = true;
                    break;

                case "SiteMap SubAreas":
                    st.PrepareSubAreas(sheet, service);
                    hasSiteMapContent = true;
                    break;
                }

                if (hasFormContent)
                {
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing form content translations...");
                    }

                    ft.ImportFormsContent(service, forms);
                }

                if (hasDashboardContent)
                {
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing dashboard content translations...");
                    }

                    db.ImportFormsContent(service, forms);
                }

                if (hasSiteMapContent)
                {
                    if (worker != null && worker.WorkerReportsProgress)
                    {
                        worker.ReportProgress(0, "Importing SiteMap translations...");
                    }

                    st.Import(service);
                }
            }

            if (worker != null && worker.WorkerReportsProgress)
            {
                worker.ReportProgress(0, "Publishing customizations...");
            }

            var paxRequest = new PublishAllXmlRequest();

            service.Execute(paxRequest);
        }
Пример #2
0
        public void Import(string filePath, IOrganizationService service, BackgroundWorker worker = null)
        {
            using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                var file = new ExcelPackage(stream);

                var emds = new List <EntityMetadata>();

                var  forms               = new List <Entity>();
                var  ft                  = new FormTranslation();
                var  st                  = new SiteMapTranslation();
                var  db                  = new DashboardTranslation();
                bool hasFormContent      = false;
                bool hasDashboardContent = false;
                bool hasSiteMapContent   = false;

                var count = file.Workbook.Worksheets.Count(x =>
                                                           !x.Name.StartsWith("Forms ") && !x.Name.StartsWith("Dashboards ") &&
                                                           !x.Name.StartsWith("SiteMap "));

                var hasFormElts      = file.Workbook.Worksheets.Any(x => x.Name.StartsWith("Forms "));
                var hasDashboardElts = file.Workbook.Worksheets.Any(x => x.Name.StartsWith("Dashboards "));
                var hasSiteMapElts   = file.Workbook.Worksheets.Any(x => x.Name.StartsWith("SiteMap "));

                if (hasFormElts)
                {
                    count++;
                }

                if (hasDashboardElts)
                {
                    count++;
                }

                if (hasSiteMapElts)
                {
                    count++;
                }

                int overallProgress = 0;
                foreach (var sheet in file.Workbook.Worksheets)
                {
                    try
                    {
                        switch (sheet.Name)
                        {
                        case "Entities":
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                            {
                                Message = "Importing entities translations...",
                                Item    = 1,
                                Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                            });

                            var et = new EntityTranslation();
                            et.Result += Engine_OnResult;
                            et.Import(sheet, emds, service, worker);

                            break;

                        case "Attributes":
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                            {
                                Message = "Importing attributes translations...",
                                Item    = 1,
                                Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                            });

                            var at = new AttributeTranslation();
                            at.Result += Engine_OnResult;
                            at.Import(sheet, emds, service, worker);
                            break;

                        case "Relationships":
                        {
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                                {
                                    Message = "Importing Relationships with custom label translations...",
                                    Item    = 1,
                                    Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                                });

                            var rt = new RelationshipTranslation();
                            rt.Result += Engine_OnResult;
                            rt.Import(sheet, emds, service, worker);
                            break;
                        }

                        case "RelationshipsNN":
                        {
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                                {
                                    Message = "Importing NN Relationships with custom label translations...",
                                    Item    = 1,
                                    Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                                });

                            var rtNn = new RelationshipNnTranslation();
                            rtNn.Result += Engine_OnResult;
                            rtNn.Import(sheet, emds, service, worker);
                            break;
                        }

                        case "Global OptionSets":
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                            {
                                Message = "Importing global optionsets translations...",
                                Item    = 1,
                                Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                            });

                            var got = new GlobalOptionSetTranslation();
                            got.Result += Engine_OnResult;
                            got.Import(sheet, service, worker);
                            break;

                        case "OptionSets":
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                            {
                                Message = "Importing optionsets translations...",
                                Item    = 1,
                                Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                            });

                            var ot = new OptionSetTranslation();
                            ot.Result += Engine_OnResult;
                            ot.Import(sheet, service, worker);
                            break;

                        case "Booleans":
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                            {
                                Message = "Importing booleans translations...",
                                Item    = 1,
                                Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                            });

                            var bt = new BooleanTranslation();
                            bt.Result += Engine_OnResult;
                            bt.Import(sheet, service, worker);
                            break;

                        case "Views":
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                            {
                                Message = "Importing views translations...",
                                Item    = 1,
                                Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                            });

                            var vt = new ViewTranslation();
                            vt.Result += Engine_OnResult;
                            vt.Import(sheet, service, worker);
                            break;

                        case "Charts":
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                            {
                                Message = "Importing charts translations...",
                                Item    = 1,
                                Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                            });

                            var vt2 = new VisualizationTranslation();
                            vt2.Result += Engine_OnResult;
                            vt2.Import(sheet, service, worker);
                            break;

                        case "Forms":
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                            {
                                Message = "Importing forms translations...",
                                Item    = 1,
                                Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                            });

                            ft.ImportFormName(sheet, service, worker);
                            break;

                        case "Forms Tabs":
                            ft.PrepareFormTabs(sheet, service, forms);
                            hasFormContent = true;
                            break;

                        case "Forms Sections":
                            ft.PrepareFormSections(sheet, service, forms);
                            hasFormContent = true;
                            break;

                        case "Forms Fields":
                            ft.PrepareFormLabels(sheet, service, forms);
                            hasFormContent = true;
                            break;

                        case "Dashboards":
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                            {
                                Message = "Importing dashboard translations...",
                                Item    = 1,
                                Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                            });

                            db.ImportFormName(sheet, service, worker);
                            break;

                        case "Dashboards Tabs":
                            db.PrepareFormTabs(sheet, service, forms);
                            hasDashboardContent = true;
                            break;

                        case "Dashboards Sections":
                            db.PrepareFormSections(sheet, service, forms);
                            hasDashboardContent = true;
                            break;

                        case "Dashboards Fields":
                            db.PrepareFormLabels(sheet, service, forms);
                            hasDashboardContent = true;
                            break;

                        case "SiteMap Areas":
                            st.PrepareAreas(sheet, service);
                            hasSiteMapContent = true;
                            break;

                        case "SiteMap Groups":
                            st.PrepareGroups(sheet, service);
                            hasSiteMapContent = true;
                            break;

                        case "SiteMap SubAreas":
                            st.PrepareSubAreas(sheet, service);
                            hasSiteMapContent = true;
                            break;
                        }

                        if (hasFormContent)
                        {
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                            {
                                Message = "Importing form content translations...",
                                Item    = 1,
                                Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                            });

                            ft.ImportFormsContent(service, forms, worker);
                        }

                        if (hasDashboardContent)
                        {
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                            {
                                Message = "Importing dashboard content translations...",
                                Item    = 1,
                                Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                            });

                            db.ImportFormsContent(service, forms, worker);
                        }

                        if (hasSiteMapContent)
                        {
                            worker.ReportProgressIfPossible(0, new ProgressInfo
                            {
                                Message = "Importing SiteMap translations...",
                                Item    = 1,
                                Overall = overallProgress == 0 ? 1 : overallProgress * 100 / count
                            });

                            st.Import(service);
                        }
                    }
                    catch (Exception error)
                    {
                        Engine_OnResult(this, new TranslationResultEventArgs
                        {
                            Success   = false,
                            SheetName = sheet.Name,
                            Message   = error.Message
                        });
                    }
                    finally
                    {
                        overallProgress++;
                    }
                }

                worker.ReportProgressIfPossible(0, new ProgressInfo
                {
                    Message = "Publishing customizations...",
                    Item    = 100,
                    Overall = 100
                });

                var paxRequest = new PublishAllXmlRequest();
                service.Execute(paxRequest);

                worker.ReportProgressIfPossible(0, new ProgressInfo
                {
                    Message = "",
                    Item    = 100,
                    Overall = 100
                });
            }
        }