Пример #1
0
        public static LabelCreateListRequest GetLabelCreateListRequest()
        {
            var request = new LabelCreateListRequest(CurrentUserId, UidOne, UidOne, new List <LabelListInfo>()
            {
                GetLabelListInfo()
            });

            return(request);
        }
Пример #2
0
        public async Task <IActionResult> CreateBulkLabel(CreateBulkLabelModel model)
        {
            if (model.IsNotValid())
            {
                model.SetInputModelValues();
                return(View(model));
            }

            var labelListInfos = new List <LabelListInfo>();

            var lines = model.BulkLabelData.Split('\n');

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

                labelListInfos.Add(new LabelListInfo
                {
                    LabelKey         = values[0],
                    LanguageIsoCode2 = values[1],
                    Translation      = values[2]
                });
            }

            var request  = new LabelCreateListRequest(CurrentUser.Id, model.OrganizationUid, model.ProjectUid, labelListInfos);
            var response = await _labelService.CreateLabelFromList(request);

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

            var doneModel = new CreateBulkLabelDoneModel();

            doneModel.MapMessages(response);
            doneModel.ProjectUid                       = model.ProjectUid;
            doneModel.ProjectName                      = model.ProjectName;
            doneModel.AddedLabelCount                  = response.AddedLabelCount;
            doneModel.CanNotAddedLabelCount            = response.CanNotAddedLabelCount;
            doneModel.AddedLabelTranslationCount       = response.AddedLabelTranslationCount;
            doneModel.CanNotAddedLabelTranslationCount = response.CanNotAddedLabelTranslationCount;
            doneModel.TotalRowsProcessed               = lines.Length - 1;

            CurrentUser.IsActionSucceed = true;
            return(View("CreateBulkLabelDone", doneModel));
        }
Пример #3
0
        private static void InsertLabels(ILabelService labelService, Project project, string webRootPath)
        {
            var labelsFilePath = Path.Combine(webRootPath, "files", "projectTranslations.csv");

            if (!File.Exists(labelsFilePath))
            {
                return;
            }

            var labelListInfos = new List <LabelListInfo>();
            var lines          = File.ReadAllLines(labelsFilePath, Encoding.UTF8);

            for (var i = 1; i < lines.Length; i++)
            {
                var values = lines[i].Split(',');
                if (values.Length != 3)
                {
                    throw new Exception("projectTranslations.csv is not valid!");
                }

                labelListInfos.Add(new LabelListInfo
                {
                    LabelKey         = values[0],
                    LanguageIsoCode2 = values[1],
                    Translation      = values[2]
                });
            }

            var request  = new LabelCreateListRequest(project.CreatedBy, project.OrganizationUid, project.Uid, labelListInfos);
            var response = labelService.CreateLabelFromList(request).Result;

            if (response.Status.IsNotSuccess)
            {
                throw new Exception("couldn't add project translations!");
            }
        }
Пример #4
0
        public async Task <IActionResult> UploadLabelFromCSVFile(LabelUploadFromCSVModel model)
        {
            if (model.IsNotValid())
            {
                model.SetInputModelValues();
                return(View(model));
            }

            var labelListInfos = new List <LabelListInfo>();

            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 != 3)
                {
                    model.ErrorMessages.Add("file_has_more_columns_than_expected");
                    model.ErrorMessages.Add("error line : " + i);
                    model.SetInputModelValues();
                    return(View(model));
                }

                labelListInfos.Add(new LabelListInfo
                {
                    LabelKey         = values[0],
                    LanguageIsoCode2 = values[1],
                    Translation      = values[2]
                });
            }

            var request  = new LabelCreateListRequest(CurrentUser.Id, model.OrganizationUid, model.ProjectUid, labelListInfos);
            var response = await _labelService.CreateLabelFromList(request);

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

            var doneModel = new LabelUploadFromCSVDoneModel();

            doneModel.MapMessages(response);
            doneModel.ProjectUid                       = model.ProjectUid;
            doneModel.ProjectName                      = model.ProjectName;
            doneModel.AddedLabelCount                  = response.AddedLabelCount;
            doneModel.CanNotAddedLabelCount            = response.CanNotAddedLabelCount;
            doneModel.AddedLabelTranslationCount       = response.AddedLabelTranslationCount;
            doneModel.CanNotAddedLabelTranslationCount = response.CanNotAddedLabelTranslationCount;
            doneModel.TotalRowsProcessed               = lines.Count - 1;

            CurrentUser.IsActionSucceed = true;
            return(View("UploadLabelFromCSVFileDone", doneModel));
        }
Пример #5
0
        public async Task <LabelCreateListResponse> CreateLabelFromList(LabelCreateListRequest request)
        {
            var response = new LabelCreateListResponse();

            var project = await _projectRepository.Select(x => x.Uid == request.ProjectUid);

            if (project.IsNotExist())
            {
                response.SetInvalid();
                response.ErrorMessages.Add("project_not_found");
                return(response);
            }

            if (!project.IsActive)
            {
                response.SetInvalid();
                response.ErrorMessages.Add("project_not_active");
                return(response);
            }

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

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

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

            var languages = await _languageRepository.SelectAll(null);

            var oldLabels = await _labelRepository.SelectAll(x => x.ProjectId == project.Id);

            var oldTranslations = await _labelTranslationRepository.SelectAll(x => x.ProjectId == project.Id);

            var isAlphabetical = new Regex("^[A-Za-z0-9_]+$", RegexOptions.Compiled);
            var labels         = new List <Label>();

            for (var i = 0; i < request.Labels.Count; i++)
            {
                var translationInfo = request.Labels[i];
                if (oldLabels.Any(x => x.Key == translationInfo.LabelKey) ||
                    labels.Any(x => x.Key == translationInfo.LabelKey))
                {
                    continue;
                }

                if (!isAlphabetical.IsMatch(translationInfo.LabelKey))
                {
                    response.CanNotAddedLabelCount++;
                    response.WarningMessages.Add(translationInfo.LabelKey);
                    response.CanNotAddedLabelCount++;
                    continue;
                }

                var label = _labelFactory.CreateEntity(translationInfo.LabelKey, project);
                labels.Add(label);
                response.AddedLabelCount++;
            }

            var translationsToInsert = new List <LabelTranslation>();
            var translationsToUpdate = new List <LabelTranslation>();

            for (var i = 0; i < request.Labels.Count; i++)
            {
                var translationInfo = request.Labels[i];

                var language = languages.Find(x => x.IsoCode2Char == translationInfo.LanguageIsoCode2);
                if (language == null)
                {
                    response.CanNotAddedLabelTranslationCount++;
                    continue;
                }

                var label = labels.Find(x => x.Key == translationInfo.LabelKey);
                if (label == null)
                {
                    var oldLabel = oldLabels.FirstOrDefault(x => x.Key == translationInfo.LabelKey);
                    if (oldLabel != null)
                    {
                        if (translationsToInsert.Any(x => x.LanguageId == language.Id &&
                                                     x.LabelUid == oldLabel.Uid))
                        {
                            continue;
                        }

                        var translationForExistingLabel = oldTranslations.FirstOrDefault(x => x.LabelId == oldLabel.Id && x.LanguageId == language.Id);
                        if (translationForExistingLabel == null)
                        {
                            var translationToInsert = _labelTranslationFactory.CreateEntity(translationInfo.Translation, oldLabel, language);
                            translationsToInsert.Add(translationToInsert);
                            response.AddedLabelTranslationCount++;
                            continue;
                        }

                        if (translationForExistingLabel.Translation == translationInfo.Translation)
                        {
                            continue;
                        }

                        translationForExistingLabel.Translation = translationInfo.Translation;
                        translationsToUpdate.Add(translationForExistingLabel);
                        response.UpdatedLabelTranslationCount++;
                        continue;
                    }

                    response.CanNotAddedLabelTranslationCount++;
                    continue;
                }

                if (translationsToInsert.Any(x => x.LanguageId == language.Id &&
                                             x.LabelUid == label.Uid))
                {
                    continue;
                }

                var oldTranslation = oldTranslations.FirstOrDefault(x => x.LabelUid == label.Uid && x.LanguageId == language.Id);
                if (oldTranslation != null)
                {
                    if (oldTranslation.Translation == translationInfo.Translation)
                    {
                        continue;
                    }

                    oldTranslation.Translation = translationInfo.Translation;
                    translationsToUpdate.Add(oldTranslation);
                    response.UpdatedLabelTranslationCount++;
                }
                else
                {
                    var labelTranslation = _labelTranslationFactory.CreateEntity(translationInfo.Translation, label, language);
                    translationsToInsert.Add(labelTranslation);
                    response.AddedLabelTranslationCount++;
                }
            }

            var uowResult = await _labelUnitOfWork.DoCreateWorkBulk(request.CurrentUserId, labels, translationsToInsert, translationsToUpdate);

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

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