Exemplo n.º 1
0
        private void Import()
        {
            ClearStatusMessage();
            try
            {
                ImportSetting importSetting = null;
                var           task          = Util.GetMasterImportSettingAsync(Login, ImportFileType.IgnoreKana);
                ProgressDialog.Start(ParentForm, task, false, SessionKey);
                importSetting = task.Result;

                var definition = new IgnoreKanaFileDefinition(new DataExpression(ApplicationControl));
                definition.ExcludeCategoryIdField.GetModelsByCode = val =>
                {
                    CategoriesResult result = null;
                    ServiceProxyFactory.LifeTime(factory =>
                    {
                        var categoryMaster = factory.Create <CategoryMasterClient>();
                        result             = categoryMaster.GetByCode(SessionKey, CompanyId, ExcludeCategoryType, val);
                    });
                    if (result.ProcessResult.Result)
                    {
                        return(result.Categories
                               .ToDictionary(c => c.Code));
                    }
                    return(new Dictionary <string, Category>());
                };

                var importer = definition.CreateImporter(m => m.Kana);
                importer.UserId      = Login.UserId;
                importer.UserCode    = Login.UserCode;
                importer.CompanyId   = CompanyId;
                importer.CompanyCode = Login.CompanyCode;
                importer.LoadAsync   = async() => await GetListAsync();

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

                var importResult = DoImport(importer, importSetting);
                if (!importResult)
                {
                    return;
                }
                IgnoreKanaList.Clear();
                Task <List <IgnoreKana> > loadTask = GetListAsync();
                Clear(true, false);
                ProgressDialog.Start(ParentForm, loadTask, false, SessionKey);
                IgnoreKanaList.AddRange(loadTask.Result);

                grdExcludeKanaList.DataSource = new BindingSource(IgnoreKanaList, null);
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.ToString());
                NLogHandler.WriteErrorLog(this, ex, SessionKey);
                ShowWarningDialog(MsgErrImportErrorWithoutLog);
            }
        }
        /// <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 loginUserTask = loginUserProcessor.GetAsync(new LoginUserSearch {
                Ids = new[] { source.LoginUserId },
            }, token);
            var appConTask   = applicationControlProcessor.GetAsync(source.CompanyId, token);
            var categoryTask = categoryProcessor.GetAsync(new CategorySearch {
                CompanyId = source.CompanyId, CategoryType = CategoryType.Exclude,
            }, token);

            await Task.WhenAll(companyTask, loginUserTask, appConTask, categoryTask);

            var company            = companyTask.Result.First();
            var loginUser          = loginUserTask.Result.First();
            var appCon             = appConTask.Result;
            var categoryDictionary = categoryTask.Result.ToDictionary(x => x.Code);

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

            definition.ExcludeCategoryIdField.GetModelsByCode = val => categoryDictionary;

            var importer = definition.CreateImporter(m => m.Kana, parser);

            importer.UserId      = source.LoginUserId;
            importer.UserCode    = loginUser.Code;
            importer.CompanyId   = source.CompanyId;
            importer.CompanyCode = company.Code;
            importer.LoadAsync   = () => ignoreKanaProcessor.GetAsync(new IgnoreKana {
                CompanyId = source.CompanyId,
            }, token);
            importer.RegisterAsync = x => ignoreKanaProcessor.ImportAsync(x.New, x.Dirty, x.Removed, token);

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

            result.Logs = importer.GetErrorLogs();

            return(result);
        }
Exemplo n.º 3
0
        private void Export()
        {
            try
            {
                Task <List <IgnoreKana> > loadTask = GetListAsync();
                ProgressDialog.Start(ParentForm, loadTask, false, SessionKey);
                List <IgnoreKana> list = loadTask.Result;

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

                string serverPath = null;
                Task   task       = ServiceProxyFactory.LifeTime(async factory =>
                {
                    var service = factory.Create <GeneralSettingMasterClient>();
                    GeneralSettingResult result = await service.GetByCodeAsync(SessionKey, CompanyId, ExportPathCode);

                    if (result.ProcessResult.Result)
                    {
                        serverPath = result.GeneralSetting?.Value;
                    }
                });
                ProgressDialog.Start(ParentForm, task, false, SessionKey);

                if (!Directory.Exists(serverPath))
                {
                    serverPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                }

                var filePath = string.Empty;
                var fileName = $"除外カナマスター{DateTime.Today:yyyyMMdd}.csv";
                if (!ShowSaveExportFileDialog(serverPath, fileName, out filePath))
                {
                    return;
                }

                var definition = new IgnoreKanaFileDefinition(new DataExpression(ApplicationControl));
                var exporter   = definition.CreateExporter();
                exporter.UserId      = Login.UserId;
                exporter.UserCode    = Login.UserCode;
                exporter.CompanyId   = 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);
            }
        }