/// <summary>インポート処理</summary>
        /// <param name="source"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <ImportResult> ImportAsync(MasterImportSource source, CancellationToken token = default(CancellationToken))
        {
            var mode     = (ImportMethod)source.ImportMethod;
            var encoding = Encoding.GetEncoding(source.EncodingCodePage);
            var csv      = encoding.GetString(source.Data);

            var companyTask = companyProcessor.GetAsync(new CompanySearch {
                Id = source.CompanyId,
            }, token);
            var loginUsersTask = loginUserProcessor.GetAsync(new LoginUserSearch {
                CompanyId = source.CompanyId,
            }, token);
            var appConTask  = applicationControlProcessor.GetAsync(source.CompanyId, token);
            var sectionTask = sectionProcessor.GetAsync(new SectionSearch {
                CompanyId = source.CompanyId,
            }, token);

            await Task.WhenAll(companyTask, loginUsersTask, appConTask, sectionTask);

            var company             = companyTask.Result.First();
            var loginUserDictionary = loginUsersTask.Result.ToDictionary(x => x.Code);
            var loginUser           = loginUsersTask.Result.First(x => x.Id == source.LoginUserId);
            var appCon            = appConTask.Result;
            var sectionDictionary = sectionTask.Result.ToDictionary(x => x.Code);

            var definition = new SectionWithLoginUserFileDefinition(new DataExpression(appCon));
            var parser     = new CsvParser {
                Encoding      = encoding,
                StreamCreator = new PlainTextMemoryStreamCreator(),
            };

            definition.SectionCodeField.GetModelsByCode   = codes => sectionDictionary;
            definition.LoginUserCodeField.GetModelsByCode = codes => loginUserDictionary;

            var importer = definition.CreateImporter(x => new { x.SectionId, x.LoginUserId }, parser);

            importer.UserId      = source.LoginUserId;
            importer.UserCode    = loginUser.Code;
            importer.CompanyId   = source.CompanyId;
            importer.CompanyCode = company.Code;

            importer.LoadAsync = () => sectionWithLoginUserProcessor.GetAsync(new SectionWithLoginUserSearch {
                CompanyId = source.CompanyId,
            }, token);
            importer.RegisterAsync = x => sectionWithLoginUserProcessor.ImportAsync(x.New, x.Dirty, x.Removed, token);

            var result = await importer.ImportAsync(csv, mode, token, null);

            result.Logs = importer.GetErrorLogs();

            return(result);
        }
Esempio n. 2
0
        private void Export()
        {
            ClearStatusMessage();
            var serverPath = string.Empty;

            try
            {
                Task <List <SectionWithLoginUser> > loadTask = SectionWithLoginUserData();
                var task = ServiceProxyFactory.LifeTime(async factory =>
                {
                    var service = factory.Create <GeneralSettingMasterClient>();
                    GeneralSettingResult result = await service.GetByCodeAsync(SessionKey, CompanyId, "サーバパス");

                    if (result.ProcessResult.Result)
                    {
                        serverPath = result.GeneralSetting?.Value;
                    }

                    if (!Directory.Exists(serverPath))
                    {
                        serverPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    }
                });
                ProgressDialog.Start(ParentForm, Task.WhenAll(task, loadTask), false, SessionKey);
                List <SectionWithLoginUser> list = loadTask.Result;

                if (!list.Any())
                {
                    ShowWarningDialog(MsgWngNoExportData);
                    return;
                }

                var filePath = string.Empty;
                var fileName = $"入金部門・担当者対応マスター{DateTime.Today:yyyyMMdd}.csv";
                if (!ShowSaveExportFileDialog(serverPath, fileName, out filePath))
                {
                    return;
                }

                var definition = new SectionWithLoginUserFileDefinition(
                    new DataExpression(ApplicationControl));
                var exporter = definition.CreateExporter();
                exporter.UserId      = Login.UserId;
                exporter.UserCode    = Login.UserCode;
                exporter.CompanyId   = Login.CompanyId;
                exporter.CompanyCode = Login.CompanyCode;

                ProgressDialog.Start(ParentForm, (cancel, progress) =>
                {
                    return(exporter.ExportAsync(filePath, list, cancel, progress));
                }, true, SessionKey);

                if (exporter.Exception != null)
                {
                    NLogHandler.WriteErrorLog(this, exporter.Exception, SessionKey);
                    ShowWarningDialog(MsgErrExportError);
                    return;
                }

                DispStatusMessage(MsgInfFinishExport);
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.ToString());
                NLogHandler.WriteErrorLog(this, ex, SessionKey);
                DispStatusMessage(MsgErrExportError);
            }
        }
Esempio n. 3
0
        private void Import()
        {
            ClearStatusMessage();
            try
            {
                ImportSetting importSetting = null;
                var           task          = Util.GetMasterImportSettingAsync(Login, ImportFileType.SectionWithLoginUser);
                ProgressDialog.Start(ParentForm, task, false, SessionKey);
                importSetting = task.Result;
                var definition = new SectionWithLoginUserFileDefinition(new DataExpression(ApplicationControl));
                definition.SectionCodeField.GetModelsByCode = val =>
                {
                    Dictionary <string, Section> product = null;
                    ServiceProxyFactory.LifeTime(factory =>
                    {
                        var section           = factory.Create <SectionMasterClient>();
                        SectionsResult result = section.GetByCode(SessionKey, CompanyId, val);

                        if (result.ProcessResult.Result)
                        {
                            product = result.Sections
                                      .ToDictionary(c => c.Code);
                        }
                    });
                    return(product ?? new Dictionary <string, Section>());
                };

                definition.LoginUserCodeField.GetModelsByCode = val =>
                {
                    Dictionary <string, LoginUser> product = null;
                    ServiceProxyFactory.LifeTime(factory =>
                    {
                        var loginUser   = factory.Create <LoginUserMasterClient>();
                        UsersResult res = loginUser.GetByCode(SessionKey, CompanyId, val);

                        if (res.ProcessResult.Result)
                        {
                            product = res.Users
                                      .ToDictionary(c => c.Code);
                        }
                    });
                    return(product ?? new Dictionary <string, LoginUser>());
                };

                var importer = definition.CreateImporter(m => new { m.LoginUserId, m.SectionId });
                importer.UserId      = Login.UserId;
                importer.UserCode    = Login.UserCode;
                importer.CompanyId   = CompanyId;
                importer.CompanyCode = Login.CompanyCode;
                importer.LoadAsync   = async() => await SectionWithLoginUserData();

                importer.RegisterAsync = async unitOfWork => await RegisterForImportAsync(unitOfWork);

                var importResult = DoImport(importer, importSetting);
                if (!importResult)
                {
                    return;
                }
                ClearFromTo();
                BeforeParentSearch();
                txtLoginUserCode.Clear();
                lblLoginUserNames.Clear();
                grdLoginUserModify.DataSource = null;
                grdLoginUserOrigin.DataSource = null;
                txtLoginUserCode.Focus();
                Modified = false;
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.ToString());
                NLogHandler.WriteErrorLog(this, ex, SessionKey);
                ShowWarningDialog(MsgErrImportErrorWithoutLog);
            }
        }