public static LabelTranslationCreateListRequest GetLabelTranslationCreateListRequest()
        {
            var request = new LabelTranslationCreateListRequest(CurrentUserId, UidOne, UidOne, new List <TranslationListInfo>()
            {
                GetTranslationListInfo()
            });

            return(request);
        }
Beispiel #2
0
        public async Task <IActionResult> UploadLabelTranslationFromCSVFile(UploadLabelTranslationFromCSVFileModel model)
        {
            if (model.IsNotValid())
            {
                model.SetInputModelValues();
                return(View(model));
            }

            var translationListInfos = new List <TranslationListInfo>();

            var lines = new List <string>();

            using (var reader = new StreamReader(model.CSVFile.OpenReadStream()))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    lines.Add(line);
                }
            }

            for (var i = 1; i < lines.Count; i++)
            {
                var values = lines[i].Split(',');
                if (values.Length != 2)
                {
                    model.ErrorMessages.Add("file_has_more_columns_than_expected");
                    model.ErrorMessages.Add("error line : " + i);
                    model.SetInputModelValues();
                    return(View(model));
                }

                translationListInfos.Add(new TranslationListInfo
                {
                    LanguageIsoCode2 = values[0],
                    Translation      = values[1]
                });
            }

            var request  = new LabelTranslationCreateListRequest(CurrentUser.Id, model.OrganizationUid, model.LabelUid, translationListInfos);
            var response = await _labelService.CreateTranslationFromList(request);

            if (response.Status.IsNotSuccess)
            {
                model.MapMessages(response);
                model.SetInputModelValues();
                return(View(model));
            }

            var doneModel = new TranslationUploadFromCSVDoneModel();

            doneModel.MapMessages(response);
            doneModel.LabelUid = model.LabelUid;
            doneModel.LabelKey = model.LabelKey;
            doneModel.AddedTranslationCount       = response.AddedTranslationCount;
            doneModel.CanNotAddedTranslationCount = response.CanNotAddedTranslationCount;
            doneModel.TotalRowsProcessed          = lines.Count - 1;

            CurrentUser.IsActionSucceed = true;
            return(View("UploadLabelTranslationFromCSVFileDone", doneModel));
        }
Beispiel #3
0
        public async Task <LabelTranslationCreateListResponse> CreateTranslationFromList(LabelTranslationCreateListRequest request)
        {
            var response = new LabelTranslationCreateListResponse();

            var label = await _labelRepository.Select(x => x.Uid == request.LabelUid);

            if (label.IsNotExist())
            {
                response.SetInvalid();
                response.ErrorMessages.Add("label_not_found");
                return(response);
            }

            if (!label.IsActive)
            {
                response.SetInvalid();
                response.ErrorMessages.Add("label_not_active");
                return(response);
            }

            var currentUser = _cacheManager.GetCachedCurrentUser(request.CurrentUserId);

            if (label.OrganizationId != currentUser.OrganizationId)
            {
                response.SetInvalid();
                return(response);
            }

            if (await _organizationRepository.Any(x => x.Id == label.OrganizationId && !x.IsActive))
            {
                response.SetInvalid();
                response.ErrorMessages.Add("organization_not_active");
                return(response);
            }

            if (await _projectRepository.Any(x => x.Id == label.ProjectId && !x.IsActive))
            {
                response.SetInvalid();
                response.ErrorMessages.Add("project_not_active");
                return(response);
            }

            var languages = await _languageRepository.SelectAll(null);

            var translations = new List <LabelTranslation>();
            var warningCount = 0;

            for (var i = 0; i < request.LabelTranslations.Count; i++)
            {
                var translationInfo = request.LabelTranslations[i];
                var language        = languages.Find(x => x.IsoCode2Char == translationInfo.LanguageIsoCode2);
                if (language == null)
                {
                    warningCount++;
                    continue;
                }

                if (await _labelTranslationRepository.Any(x => x.Translation == translationInfo.Translation &&
                                                          x.LanguageId == language.Id &&
                                                          x.LabelId == label.Id))
                {
                    warningCount++;
                    continue;
                }

                var labelTranslation = _labelTranslationFactory.CreateEntity(translationInfo.Translation, label, language);
                translations.Add(labelTranslation);
            }

            response.CanNotAddedTranslationCount = warningCount;

            if (translations.Any())
            {
                var uowResult = await _labelUnitOfWork.DoCreateTranslationWorkBulk(request.CurrentUserId, translations);

                if (uowResult)
                {
                    response.AddedTranslationCount = translations.Count;
                    response.Status = ResponseStatus.Success;
                    return(response);
                }
            }

            response.SetFailed();
            return(response);
        }