public OperationDataResult <TemplateListModel> Index(TemplateRequestModel templateRequestModel)
        {
            try
            {
                try
                {
                    var core         = _coreHelper.GetCore();
                    var templatesDto = core.TemplateItemReadAll(false,
                                                                "",
                                                                templateRequestModel.NameFilter,
                                                                templateRequestModel.IsSortDsc,
                                                                templateRequestModel.Sort,
                                                                templateRequestModel.TagIds);

                    var model = new TemplateListModel
                    {
                        NumOfElements = 40,
                        PageNum       = templateRequestModel.PageIndex,
                        Templates     = templatesDto
                    };


                    return(new OperationDataResult <TemplateListModel>(true, model));
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("PrimeDb"))
                    {
                        var lines = File.ReadAllLines(
                            System.Web.Hosting.HostingEnvironment.MapPath("~/bin/Input.txt"));

                        var connectionStr = lines.First();
                        var adminTool     = new AdminTools(connectionStr);
                        adminTool.DbSettingsReloadRemote();
                        return(new OperationDataResult <TemplateListModel>(false, LocaleHelper.GetString("CheckConnectionString")));
                    }
                    else
                    {
                        if (ex.InnerException.Message.Contains("Cannot open database"))
                        {
                            try
                            {
                                var core = _coreHelper.GetCore();
                            }
                            catch (Exception)
                            {
                                return(new OperationDataResult <TemplateListModel>(false, LocaleHelper.GetString("CoreIsNotStarted")));
                            }
                            return(new OperationDataResult <TemplateListModel>(false, LocaleHelper.GetString("CheckSettingsBeforeProceed")));
                        }
                    }
                    return(new OperationDataResult <TemplateListModel>(false, LocaleHelper.GetString("CheckSettingsBeforeProceed")));
                }
            }
            catch (Exception)
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }
        }
Exemplo n.º 2
0
 public async Task <IActionResult> Index([FromBody] TemplateRequestModel templateRequestModel)
 {
     try
     {
         return(Ok(await _templatesService.Index(templateRequestModel)));
     }
     catch (Exception)
     {
         return(Unauthorized());
     }
 }
Exemplo n.º 3
0
        public async Task <OperationDataResult <TemplateListModel> > GetAvailableEforms(
            TemplateRequestModel templateRequestModel,
            int groupId)
        {
            try
            {
                TimeZoneInfo timeZoneInfo = await _userService.GetCurrentUserTimeZoneInfo();

                var result = new TemplateListModel
                {
                    Templates = new List <TemplateDto>()
                };
                var core = await _coreHelper.GetCore();

                var locale = await _userService.GetCurrentUserLocale();

                Language language     = core.DbContextHelper.GetDbContext().Languages.Single(x => x.LanguageCode.ToLower() == locale.ToLower());
                var      templatesDto = await core.TemplateItemReadAll(false,
                                                                       "",
                                                                       templateRequestModel.NameFilter,
                                                                       templateRequestModel.IsSortDsc,
                                                                       templateRequestModel.Sort,
                                                                       templateRequestModel.TagIds, timeZoneInfo, language).ConfigureAwait(false);

                var eformsInGroup = await _dbContext.EformInGroups
                                    .Where(x => x.SecurityGroupId == groupId)
                                    .Select(x => x.TemplateId)
                                    .ToListAsync();

                if (templatesDto.Any())
                {
                    foreach (var templateDto in templatesDto)
                    {
                        if (!eformsInGroup.Contains(templateDto.Id))
                        {
                            result.Templates.Add(templateDto);
                        }
                    }

                    result.NumOfElements = templatesDto.Count;
                    result.PageNum       = 1;
                }

                return(new OperationDataResult <TemplateListModel>(true, result));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                _logger.LogError(e.Message);
                return(new OperationDataResult <TemplateListModel>(false,
                                                                   _localizationService.GetString("ErrorWhileObtainingAvailableEForms")));
            }
        }
Exemplo n.º 4
0
        public HttpResponseMessage Get(TemplateRequestModel trequest)
        {
            HttpResponseMessage result = Request.CreateResponse(HttpStatusCode.OK);

            result.Content = new StreamContent(_templatesRepo.Get(trequest));
            result.Content.Headers.ContentDisposition =
                new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            if (trequest.Format != null)
            {
                if (trequest.Format.Equals("csv", StringComparison.CurrentCultureIgnoreCase))
                {
                    result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/csv");
                }
                // according to RFC-7231 do no send MIME type for unknown data
            }

            return(result);
        }
Exemplo n.º 5
0
        public Stream Get(TemplateRequestModel templateRequest)
        {
            Stream strm = null;

            if (templateRequest != null && templateRequest.Name != null && templateRequest.Format != null)
            {
                Assembly assembly = Assembly.Load("Keithlink.Svc.Impl");
                if (templateRequest.Name != null)
                {
                    if (templateRequest.Name.Equals("importcustominventory", StringComparison.CurrentCultureIgnoreCase) &&
                        templateRequest.Format.Equals("csv", StringComparison.CurrentCultureIgnoreCase))
                    {
                        strm = assembly.GetManifestResourceStream
                                   ("KeithLink.Svc.Impl.Templates.importcustominventory.csv");
                    }
                }
            }
            else
            {
                throw new ApplicationException("template request needs to include name and format");
            }
            return(strm);
        }
 public async Task <OperationDataResult <TemplateListModel> > GetAvailableEforms(TemplateRequestModel model,
                                                                                 int groupId)
 {
     return(await _eformGroupService.GetAvailableEforms(model, groupId));
 }
Exemplo n.º 7
0
 public async Task <Tuple <List <TemplateListViewModel>, int> > SearchAsync(TemplateRequestModel req)
 {
     return(await(_dbContext as Model.PunnelContext).msp_Template_SearchAsync(req));
 }
        public async Task <OperationDataResult <TemplateListModel> > Index(TemplateRequestModel templateRequestModel)
        {
            var timeZoneInfo = await _userService.GetCurrentUserTimeZoneInfo();

            Log.LogEvent("TemplateService.Index: called");
            try
            {
                var core = await _coreHelper.GetCore();

                await using var sdkDbContext = core.DbContextHelper.GetDbContext();
                Log.LogEvent("TemplateService.Index: try section");

                var language = await _userService.GetCurrentUserLanguage();

                if (language == null)
                {
                    language = await sdkDbContext.Languages.SingleOrDefaultAsync(x => x.Name == "Danish");

                    if (language != null)
                    {
                        language.LanguageCode = "da";
                        await language.Update(sdkDbContext);
                    }
                }

                var query = sdkDbContext.CheckListTranslations
                            .Include(x => x.CheckList)
                            .ThenInclude(x => x.Taggings)
                            .ThenInclude(x => x.Tag)
                            .Include(x => x.CheckList.CheckListSites)
                            .ThenInclude(x => x.Site)
                            .Include(x => x.CheckList.Cases)
                            .Where(x => x.CheckList.ParentId == null)
                            .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                            .Where(x => x.CheckList.WorkflowState != Constants.WorkflowStates.Removed)
                            .Where(x => x.LanguageId == language.Id);

                query = QueryHelper.AddFilterAndSortToQuery(query, templateRequestModel, new List <string> {
                    "Text"
                },
                                                            new List <string> {
                    "Id", "CreatedAt"
                });

                if (templateRequestModel.Sort is "Id" or "CreatedAt")
                {
                    if (templateRequestModel.IsSortDsc)
                    {
                        query = query
                                .CustomOrderByDescending(templateRequestModel.Sort);
                    }
                    else
                    {
                        query = query
                                .CustomOrderBy(templateRequestModel.Sort);
                    }
                }

                if (templateRequestModel.TagIds.Any())
                {
                    query = query.Where(x => x.CheckList.Taggings
                                        .Where(y => y.Tag.WorkflowState != Constants.WorkflowStates.Removed)
                                        .Where(y => y.WorkflowState != Constants.WorkflowStates.Removed)
                                        .Any(y => templateRequestModel.TagIds.Contains((int)y.TagId)));
                }

                var templatesDto = await query.Select(x => new TemplateDto
                {
                    Id        = x.CheckListId,
                    CreatedAt =
                        TimeZoneInfo.ConvertTimeFromUtc((DateTime)x.CheckList.CreatedAt, timeZoneInfo),
                    DeployedSites = x.CheckList.CheckListSites
                                    .Where(y => y.WorkflowState != Constants.WorkflowStates.Removed)
                                    .Select(y => new SiteNameDto
                    {
                        SiteUId   = (int)y.Site.MicrotingUid,
                        SiteName  = y.Site.Name,
                        CreatedAt = y.Site.CreatedAt,
                        UpdatedAt = y.Site.UpdatedAt
                    })
                                    .ToList(),
                    Description   = x.Description,
                    Label         = x.Text,
                    Repeated      = (int)x.CheckList.Repeated,
                    FolderName    = x.CheckList.FolderName,
                    WorkflowState = x.CheckList.WorkflowState,
                    HasCases      = x.CheckList.Cases
                                    .Any(y => y.WorkflowState != Constants.WorkflowStates.Removed),
                    DisplayIndex = x.CheckList.DisplayIndex,
                    Tags         = x.CheckList.Taggings
                                   .Where(y => y.WorkflowState != Constants.WorkflowStates.Removed)
                                   .Where(y => y.Tag.WorkflowState != Constants.WorkflowStates.Removed)
                                   .Select(y => new KeyValuePair <int, string>(y.Tag.Id, y.Tag.Name))
                                   .ToList(),
                    FolderId = x.CheckList.CheckListSites
                               .Where(y => y.WorkflowState != Constants.WorkflowStates.Removed)
                               .Select(y => y.FolderId)
                               .FirstOrDefault(),
                    IsLocked            = x.CheckList.IsLocked,
                    IsEditable          = x.CheckList.IsEditable,
                    DocxExportEnabled   = x.CheckList.DocxExportEnabled,
                    JasperExportEnabled = x.CheckList.JasperExportEnabled,
                    ExcelExportEnabled  = x.CheckList.ExcelExportEnabled,
                    IsAchievable        = x.CheckList.IsAchievable,
                    IsDoneAtEditable    = x.CheckList.IsDoneAtEditable
                }).ToListAsync();

                //var templatesDto = await core.TemplateItemReadAll(false,
                //    "",
                //    templateRequestModel.NameFilter,
                //    templateRequestModel.IsSortDsc,
                //    templateRequestModel.Sort,
                //    templateRequestModel.TagIds,
                //    timeZoneInfo, language);

                var model = new TemplateListModel
                {
                    NumOfElements = await query.Where(y => y.CheckList.WorkflowState != Constants.WorkflowStates.Removed).Select(x => x.CheckListId).CountAsync(),
                    //PageNum = templateRequestModel.PageIndex,
                    Templates = new List <TemplateDto>()
                };

                var pluginIds = await _dbContext.EformPlugins
                                .Select(x => x.PluginId)
                                .ToListAsync();

                if (!_userService.IsAdmin())
                {
                    var isEformsInGroups = await _dbContext.SecurityGroupUsers
                                           .Where(x => x.EformUserId == _userService.UserId)
                                           .Where(x => x.SecurityGroup.EformsInGroup.Any())
                                           .AnyAsync();

                    if (isEformsInGroups)
                    {
                        var eformIds = _dbContext.EformInGroups
                                       .Where(x =>
                                              x.SecurityGroup.SecurityGroupUsers.Any(y => y.EformUserId == _userService.UserId))
                                       .Select(x => x.TemplateId)
                                       .ToList();

                        foreach (var templateDto in templatesDto.Where(templateDto => eformIds.Contains(templateDto.Id)))
                        {
                            await templateDto.CheckForLock(_dbContext);

                            model.Templates.Add(templateDto);
                        }
                    }
                    else
                    {
                        foreach (var templateDto in templatesDto)
                        {
                            await templateDto.CheckForLock(_dbContext, pluginIds);

                            model.Templates.Add(templateDto);
                        }
                    }
                }
                else
                {
                    foreach (var templateDto in templatesDto)
                    {
                        await templateDto.CheckForLock(_dbContext, pluginIds);

                        model.Templates.Add(templateDto);
                    }
                }

                //foreach (var template in model.Templates)
                //{
                //    var tagsForRemove = new List<KeyValuePair<int, string>>();
                //    foreach (var tag in template.Tags)
                //    {
                //        if (await sdkDbContext.Tags
                //            .Where(y => y.WorkflowState == Constants.WorkflowStates.Removed)
                //            .AnyAsync(x => x.Id == tag.Key))
                //        {
                //            tagsForRemove.Add(tag);
                //        }
                //    }

                //    foreach (var tag in tagsForRemove)
                //    {
                //        template.Tags.Remove(tag);
                //    }
                //}

                return(new OperationDataResult <TemplateListModel>(true, model));
            }
            catch (Exception ex)
            {
                Log.LogEvent("TemplateService.Index: catch section");
                Log.LogException($"TemplatesService.Index: Got exception {ex.Message}");
                Log.LogException($"TemplatesService.Index: Got stacktrace {ex.StackTrace}");
                if (ex.Message.Contains("PrimeDb"))
                {
                    var sdkConnectionString = _connectionStringsSdk.Value.SdkConnection;
                    var adminTool           = new AdminTools(sdkConnectionString);
                    await adminTool.DbSettingsReloadRemote();

                    return(new OperationDataResult <TemplateListModel>(false,
                                                                       _localizationService.GetString("CheckConnectionString")));
                }

                if (ex.InnerException != null && ex.InnerException.Message.Contains("Cannot open database"))
                {
                    try
                    {
                        var _ = await _coreHelper.GetCore();
                    }
                    catch (Exception ex2)
                    {
                        return(new OperationDataResult <TemplateListModel>(false,
                                                                           _localizationService.GetString("CoreIsNotStarted") + " " + ex2.Message));
                    }

                    return(new OperationDataResult <TemplateListModel>(false,
                                                                       _localizationService.GetString("CheckSettingsBeforeProceed")));
                }

                return(new OperationDataResult <TemplateListModel>(false,
                                                                   _localizationService.GetString("CheckSettingsBeforeProceed")));
            }
        }
Exemplo n.º 9
0
        public async Task <OperationDataResult <TemplateListModel> > Index(TemplateRequestModel templateRequestModel)
        {
            var timeZoneInfo = await _userService.GetCurrentUserTimeZoneInfo();

            Log.LogEvent("TemplateService.Index: called");
            try
            {
                Log.LogEvent("TemplateService.Index: try section");
                var core = await _coreHelper.GetCore();

                await using var sdkDbContext = core.DbContextHelper.GetDbContext();
                var locale = await _userService.GetCurrentUserLocale();

                var language = await sdkDbContext.Languages.SingleOrDefaultAsync(x => x.LanguageCode.ToLower() == locale.ToLower());

                if (language == null)
                {
                    language = await sdkDbContext.Languages.SingleOrDefaultAsync(x => x.Name == "Danish");

                    if (language != null)
                    {
                        language.LanguageCode = "da";
                        await language.Update(sdkDbContext);
                    }
                }
                var templatesDto = await core.TemplateItemReadAll(false,
                                                                  "",
                                                                  templateRequestModel.NameFilter,
                                                                  templateRequestModel.IsSortDsc,
                                                                  templateRequestModel.Sort,
                                                                  templateRequestModel.TagIds,
                                                                  timeZoneInfo, language);

                var model = new TemplateListModel
                {
                    NumOfElements = 40,
                    PageNum       = templateRequestModel.PageIndex,
                    Templates     = new List <TemplateDto>()
                };

                var eformIds = new List <int>();
                //List<string> plugins = await _dbContext.EformPlugins.Select(x => x.PluginId).ToListAsync();

                if (!_userService.IsAdmin())
                {
                    var isEformsInGroups = await _dbContext.SecurityGroupUsers
                                           .Where(x => x.EformUserId == _userService.UserId)
                                           .Where(x => x.SecurityGroup.EformsInGroup.Any())
                                           .AnyAsync();

                    if (isEformsInGroups)
                    {
                        eformIds = _dbContext.EformInGroups
                                   .Where(x =>
                                          x.SecurityGroup.SecurityGroupUsers.Any(y => y.EformUserId == _userService.UserId))
                                   .Select(x => x.TemplateId)
                                   .ToList();

                        foreach (TemplateDto templateDto in templatesDto)
                        {
                            if (eformIds.Contains(templateDto.Id))
                            {
                                await templateDto.CheckForLock(_dbContext);

                                templateDto.CreatedAt = TimeZoneInfo.ConvertTimeFromUtc((DateTime)templateDto.CreatedAt, timeZoneInfo);
                                model.Templates.Add(templateDto);
                            }
                        }
                    }
                    else
                    {
                        foreach (TemplateDto templateDto in templatesDto)
                        {
                            await templateDto.CheckForLock(_dbContext);

                            templateDto.CreatedAt = TimeZoneInfo.ConvertTimeFromUtc((DateTime)templateDto.CreatedAt, timeZoneInfo);
                            model.Templates.Add(templateDto);
                        }
                    }
                }
                else
                {
                    foreach (TemplateDto templateDto in templatesDto)
                    {
                        await templateDto.CheckForLock(_dbContext);

                        templateDto.CreatedAt = TimeZoneInfo.ConvertTimeFromUtc((DateTime)templateDto.CreatedAt, timeZoneInfo);
                        model.Templates.Add(templateDto);
                    }
                }

                foreach (var template in model.Templates)
                {
                    var tagsForRemove = new List <KeyValuePair <int, string> >();
                    foreach (var tag in template.Tags)
                    {
                        if (await sdkDbContext.Tags
                            .Where(y => y.WorkflowState == Constants.WorkflowStates.Removed)
                            .AnyAsync(x => x.Id == tag.Key))
                        {
                            tagsForRemove.Add(tag);
                        }
                    }

                    foreach (var tag in tagsForRemove)
                    {
                        template.Tags.Remove(tag);
                    }
                }

                return(new OperationDataResult <TemplateListModel>(true, model));
            }
            catch (Exception ex)
            {
                Log.LogEvent("TemplateService.Index: catch section");
                Log.LogException($"TemplatesService.Index: Got exception {ex.Message}");
                Log.LogException($"TemplatesService.Index: Got stacktrace {ex.StackTrace}");
                if (ex.Message.Contains("PrimeDb"))
                {
                    var sdkConnectionString = _connectionStringsSdk.Value.SdkConnection;
                    var adminTool           = new AdminTools(sdkConnectionString);
                    await adminTool.DbSettingsReloadRemote();

                    return(new OperationDataResult <TemplateListModel>(false,
                                                                       _localizationService.GetString("CheckConnectionString")));
                }

                if (ex.InnerException != null && ex.InnerException.Message.Contains("Cannot open database"))
                {
                    try
                    {
                        var core = await _coreHelper.GetCore();
                    }
                    catch (Exception ex2)
                    {
                        return(new OperationDataResult <TemplateListModel>(false,
                                                                           _localizationService.GetString("CoreIsNotStarted") + " " + ex2.Message));
                    }

                    return(new OperationDataResult <TemplateListModel>(false,
                                                                       _localizationService.GetString("CheckSettingsBeforeProceed")));
                }

                return(new OperationDataResult <TemplateListModel>(false,
                                                                   _localizationService.GetString("CheckSettingsBeforeProceed")));
            }
        }