Beispiel #1
0
        /// <summary>
        /// Проверяет текст запроса на корректность
        /// </summary>
        internal static bool IsQueryQueryCorrect(string userQuery, out string errorMessage)
        {
            errorMessage = null;
            using (QPConfiguration.OutOfTransaction())
            {
                try
                {
                    var viewName          = $"uq_v_test_{DateTime.Now.Ticks}";
                    var schema            = SqlQuerySyntaxHelper.DbSchemaName(QPContext.DatabaseType);
                    var createTestViewSql = $"CREATE VIEW {schema}.{viewName} AS {userQuery}";
                    using (var connect = QPContext.CreateDbConnection())
                    {
                        connect.Open();
                        Common.ExecuteSql(connect, createTestViewSql);
                        Common.DropView(connect, viewName);
                    }

                    return(true);
                }
                catch (SqlException ex)
                {
                    errorMessage = ex.ErrorsToString();
                    return(false);
                }
                catch (NpgsqlException ex)
                {
                    errorMessage = ex.Message;
                    return(false);
                }
            }
        }
Beispiel #2
0
 private void FillViewBagData()
 {
     ViewBag.AllowSelectCustomerCode = QPConfiguration.AllowSelectCustomerCode;
     ViewBag.CustomerCodes           = QPConfiguration.GetCustomerCodes().Select(cc => new QPSelectListItem {
         Text = cc, Value = cc
     }).OrderBy(cc => cc.Text);
 }
        private static void Notify(string connectionString, int id, string code, bool disableInternalNotifications)
        {
            using (new TransactionScope(TransactionScopeOption.Suppress, new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted
            }))
            {
                var cnn = new DBConnector(connectionString)
                {
                    CacheData = false,
                    DisableServiceNotifications  = true,
                    DisableInternalNotifications = disableInternalNotifications,
                    ExternalExceptionHandler     = HandleException,
                    ThrowNotificationExceptions  = false
                };

                QPConfiguration.SetAppSettings(cnn.DbConnectorSettings);

                try
                {
                    foreach (var simpleCode in code.Split(';'))
                    {
                        cnn.SendNotification(id, simpleCode);
                    }
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }
Beispiel #4
0
        public void Run()
        {
            Logger.Info("QP8.ArticleScheduler starting...");
            var unityConfig = new UnityContainerCustomizer();

            _cancellationTokenSource = new CancellationTokenSource();
            _task = new Task(() =>
            {
                do
                {
                    try
                    {
                        QPConfiguration.ConfigServiceUrl   = _props.ConfigServiceUrl;
                        QPConfiguration.ConfigServiceToken = _props.ConfigServiceToken;
                        QPConfiguration.XmlConfigPath      = _props.XmlConfigPath;

                        var customers = QPConfiguration.GetCustomers(AppName)
                                        .Where(c => c.DbType == DatabaseType.SqlServer)
                                        .Where(c => !c.ExcludeFromSchedulers)
                                        .ToList();
                        new QpScheduler(unityConfig.UnityContainer, customers, _props.PrtgLoggerTasksQueueCheckShiftTime).Run();
                    }
                    catch (Exception ex)
                    {
                        Logger.Error().Exception(ex).Message("There was an error while starting the service job").Write();
                    }
                } while (!_cancellationTokenSource.Token.WaitHandle.WaitOne(_props.RecurrentTimeout));
            }, _cancellationTokenSource.Token, TaskCreationOptions.LongRunning);
            _task.Start();
            Logger.Info("QP8.ArticleScheduler started");
        }
Beispiel #5
0
        public void Process(IEnumerable <CsvDbUpdateModel> data, bool updateExisting)
        {
            using (var ts = QPConfiguration.CreateTransactionScope(IsolationLevel.ReadCommitted))
                using (new QPConnectionScope())
                {
                    var articlesData = new List <ArticleData>();
                    foreach (var csvFileData in data)
                    {
                        foreach (var csvRowFields in csvFileData.Fields.Values)
                        {
                            var dbFields  = GetFieldsByNames(csvFileData.ContentId, csvRowFields);
                            var dataToAdd = CreateArticleDatas(csvFileData.ContentId, csvRowFields, dbFields, updateExisting);
                            dataToAdd = InsertFields(csvFileData.ContentId, dataToAdd, dbFields, csvRowFields);
                            foreach (var extensionContentId in dataToAdd.Where(ad => ad.ContentId != csvFileData.ContentId).Select(ad => ad.ContentId))
                            {
                                var contentNamePrefix = $"{_contentRepository.GetById(extensionContentId).Name}.";
                                var extensionDbFields = GetFieldsByNames(extensionContentId, csvRowFields, contentNamePrefix);
                                dataToAdd = InsertFields(extensionContentId, dataToAdd, extensionDbFields, csvRowFields, contentNamePrefix);
                            }

                            articlesData.AddRange(dataToAdd);
                        }
                    }

                    articlesData = FilterNotExistedReferences(articlesData);
                    _articleService.BatchUpdate(articlesData);
                    ts.Complete();
                }
        }
Beispiel #6
0
        public static MessageResult AssembleContents(int id)
        {
            var site = SiteRepository.GetById(id);

            if (site == null)
            {
                throw new Exception(string.Format(SiteStrings.SiteNotFound, id));
            }

            if (!site.IsDotNet)
            {
                return(MessageResult.Error(string.Format(SiteStrings.ShouldBeDotNet)));
            }

            var sqlMetalPath = QPConfiguration.ConfigVariable(Config.SqlMetalKey);
            var extDbType    = (QP.ConfigurationService.Models.DatabaseType)QPContext.DatabaseType;

            if (site.ExternalDevelopment)
            {
                var liveTempDirectory  = $@"{site.TempDirectoryForClasses}{Path.DirectorySeparatorChar}live";
                var stageTempDirectory = $@"{site.TempDirectoryForClasses}{Path.DirectorySeparatorChar}stage";

                if (Directory.Exists(liveTempDirectory))
                {
                    Directory.Delete(liveTempDirectory, true);
                }

                Directory.CreateDirectory(liveTempDirectory);
                if (Directory.Exists(stageTempDirectory))
                {
                    Directory.Delete(stageTempDirectory, true);
                }

                Directory.CreateDirectory(stageTempDirectory);
                if (File.Exists(site.TempArchiveForClasses))
                {
                    File.Delete(site.TempArchiveForClasses);
                }
                new AssembleContentsController(id, sqlMetalPath, QPContext.CurrentDbConnectionString, extDbType)
                {
                    SiteRoot = liveTempDirectory,
                    IsLive   = true,
                    DisableClassGeneration = site.DownloadEfSource
                }.Assemble();

                new AssembleContentsController(id, sqlMetalPath, QPContext.CurrentDbConnectionString, extDbType)
                {
                    SiteRoot = stageTempDirectory,
                    IsLive   = false,
                    DisableClassGeneration = site.DownloadEfSource
                }.Assemble();
                var urlHelper = HttpContext.RequestServices.GetRequiredService <IUrlHelper>();
                ZipFile.CreateFromDirectory(site.TempDirectoryForClasses, site.TempArchiveForClasses);
                return(MessageResult.Download(urlHelper.Content($"~/Site/GetClassesZip/{id}")));
            }
            new AssembleContentsController(id, sqlMetalPath, QPContext.CurrentDbConnectionString, extDbType).Assemble();

            return(null);
        }
Beispiel #7
0
        public static MessageResult AssembleContents(int id)
        {
            var site = SiteRepository.GetById(id);

            if (site == null)
            {
                throw new Exception(string.Format(SiteStrings.SiteNotFound, id));
            }

            if (!site.IsDotNet)
            {
                return(MessageResult.Error(string.Format(SiteStrings.ShouldBeDotNet)));
            }

            var sqlMetalPath = QPConfiguration.ConfigVariable(Config.SqlMetalKey);

            if (site.ExternalDevelopment)
            {
                var liveTempDirectory  = $@"{site.TempDirectoryForClasses}\live";
                var stageTempDirectory = $@"{site.TempDirectoryForClasses}\stage";

                if (Directory.Exists(liveTempDirectory))
                {
                    Directory.Delete(liveTempDirectory, true);
                }

                Directory.CreateDirectory(liveTempDirectory);
                if (Directory.Exists(stageTempDirectory))
                {
                    Directory.Delete(stageTempDirectory, true);
                }

                Directory.CreateDirectory(stageTempDirectory);
                if (File.Exists(site.TempArchiveForClasses))
                {
                    File.Delete(site.TempArchiveForClasses);
                }

                new AssembleContentsController(id, sqlMetalPath, QPContext.CurrentDbConnectionString)
                {
                    SiteRoot = liveTempDirectory,
                    IsLive   = true,
                    DisableClassGeneration = site.DownloadEfSource
                }.Assemble();

                new AssembleContentsController(id, sqlMetalPath, QPContext.CurrentDbConnectionString)
                {
                    SiteRoot = stageTempDirectory,
                    IsLive   = false,
                    DisableClassGeneration = site.DownloadEfSource
                }.Assemble();

                ZipFile.CreateFromDirectory(site.TempDirectoryForClasses, site.TempArchiveForClasses);
                return(MessageResult.Download($"/Backend/Site/GetClassesZip/{id}"));
            }

            new AssembleContentsController(id, sqlMetalPath, QPContext.CurrentDbConnectionString).Assemble();
            return(null);
        }
Beispiel #8
0
 internal Notification()
 {
     FromBackenduserId    = SpecialIds.AdminUserId;
     FromDefaultName      = true;
     FromBackenduser      = true;
     DefaultSenderName    = QPConfiguration.ConfigVariable(Config.MailFromNameKey);
     SelectedReceiverType = ReceiverType.EveryoneInHistory;
 }
Beispiel #9
0
        public void SendNotification(string connectionString, int siteId, string code, int id, bool isLive)
        {
            var cnn = new DBConnector(connectionString)
            {
                CacheData = false
            };

            QPConfiguration.SetAppSettings(cnn.DbConnectorSettings);
            cnn.SendNotification(siteId, code, id, string.Empty, isLive);
        }
Beispiel #10
0
        public MultistepActionStepResult Step(int step)
        {
            var settings = HttpContext.Session.GetValue <ImportSettings>(HttpContextSession.ImportSettingsSessionKey);

            Ensure.NotNull(settings);

            var reader = new CsvReader(SiteId, ContentId, settings);
            var result = new MultistepActionStepResult();

            using (var ts = QPConfiguration.CreateTransactionScope(IsolationLevel.ReadCommitted))
            {
                using (new QPConnectionScope())
                {
                    try
                    {
                        reader.Process(step, ItemsPerStep, out var processedItemsCount);
                        var lastStep = step * ItemsPerStep >= reader.ArticleCount - ItemsPerStep;
                        if (lastStep)
                        {
                            ContentRepository.UpdateContentModification(ContentId);
                            reader.PostUpdateM2MRelationAndO2MRelationFields();
                        }

                        settings = HttpContext.Session.GetValue <ImportSettings>(HttpContextSession.ImportSettingsSessionKey);

                        var logData = new ImportArticlesLogData()
                        {
                            Id = settings.Id,
                            InsertedArticleIds = settings.InsertedArticleIds.ToArray(),
                            UpdatedArticleIds  = settings.UpdatedArticleIds.ToArray(),
                            ImportAction       = (CsvImportMode)settings.ImportAction
                        };

                        ImportLogger.Trace()
                        .Message("Import articles step: {step}.", step)
                        .Property("result", logData)
                        .Property("customerCode", QPContext.CurrentCustomerCode)
                        .Write();

                        result.ProcessedItemsCount = processedItemsCount;
                        result.TraceResult         = reader.GetTraceResult();
                        result.AdditionalInfo      = $"{MultistepActionStrings.InsertedArticles}: {settings.InsertedArticleIds.Count}; {MultistepActionStrings.UpdatedArticles}: {settings.UpdatedArticleIds.Count}.";
                    }
                    catch (Exception ex)
                    {
                        throw new ImportException(string.Format(ImportStrings.ImportInterrupted, ex.Message, reader.LastProcessed), ex, settings);
                    }
                }

                ts.Complete();
            }

            return(result);
        }
Beispiel #11
0
 private static void SetupQpContext(string connectionStringOrCustomerCode)
 {
     if (SqlHelpers.TryParseConnectionString(connectionStringOrCustomerCode, out var cnsBuilder))
     {
         QPContext.CurrentDbConnectionString = QPConfiguration.TuneConnectionString(cnsBuilder.ConnectionString, out var _);
     }
     else
     {
         QPContext.CurrentCustomerCode = connectionStringOrCustomerCode;
     }
 }
Beispiel #12
0
 private void InitViewBag(DirectLinkOptions directLinkOptions)
 {
     ViewBag.AllowSelectCustomerCode = QPConfiguration.AllowSelectCustomerCode;
     ViewBag.CustomerCodes           = QPConfiguration.GetCustomerCodes().Select(c => new QPSelectListItem {
         Text = c, Value = c
     }).OrderBy(n => n.Text);
     ViewBag.AutoLoginLinkQuery = "?UseAutoLogin=false";
     if (directLinkOptions != null && directLinkOptions.IsDefined())
     {
         ViewBag.AutoLoginLinkQuery += "&" + directLinkOptions.ToUrlParams();
     }
 }
Beispiel #13
0
        private static void SetupQpContext(string connectionStringOrCustomerCode, DatabaseType dbType)
        {
            if (SqlHelpers.TryParseConnectionString(connectionStringOrCustomerCode, (QP.ConfigurationService.Models.DatabaseType)(int) dbType, out var cnsBuilder))
            {
                QPContext.CurrentDbConnectionInfo = new QpConnectionInfo(
                    QPConfiguration.TuneConnectionString(cnsBuilder.ConnectionString, dbType: dbType),
                    dbType
                    );
            }
            else
            {
                QPContext.CurrentCustomerCode = connectionStringOrCustomerCode;
            }

            QPContext.CurrentUserId = SpecialIds.AdminUserId;
        }
Beispiel #14
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var qpController = filterContext.Controller as QPController;

            if (qpController == null || !filterContext.HttpContext.IsXmlDbUpdateReplayAction())
            {
                if (Mode == ConnectionScopeMode.TransactionOn)
                {
                    var transactionScope = QPConfiguration.CreateTransactionScope();
                    SetTransactionScope(filterContext.HttpContext, transactionScope);
                }

                SetConnectionScope(filterContext.HttpContext, new QPConnectionScope());
            }

            base.OnActionExecuting(filterContext);
        }
        private static void Notify(string connectionString, int id, string code, bool disableInternalNotifications)
        {
            var cnn = new DBConnector(connectionString)
            {
                CacheData = false,
                DisableServiceNotifications  = true,
                DisableInternalNotifications = disableInternalNotifications,
                ExternalExceptionHandler     = HandleException,
                ThrowNotificationExceptions  = false
            };

            QPConfiguration.SetAppSettings(cnn.AppSettings);
            foreach (var simpleCode in code.Split(';'))
            {
                cnn.SendNotification(id, simpleCode);
            }
        }
Beispiel #16
0
        public JsonCamelCaseResult <JSendResponse> ReplayRecordedUserActions(string xmlString, bool disableFieldIdentity, bool disableContentIdentity, bool useGuidSubstitution)
        {
            new XmlDbUpdateReplayService(
                QPConfiguration.GetConnectionString(QPContext.CurrentCustomerCode),
                CommonHelpers.GetDbIdentityInsertOptions(disableFieldIdentity, disableContentIdentity),
                QPContext.CurrentUserId,
                useGuidSubstitution,
                _xmlDbUpdateLogService,
                _appInfoRepository,
                _actionsCorrecterService,
                _httpContextProcessor
                ).Process(xmlString);

            return(new JSendResponse
            {
                Status = JSendStatus.Success,
                Message = "Xml data successfully processed"
            });
        }
        public MultistepActionStepResult Step(int step)
        {
            var context = HttpContext.Session.GetValue <RecreateDynamicImagesContext>(HttpContextSession.RecreateDynamicImagesServiceProcessingContext);
            var currentProcessedImages = new HashSet <string>(context.ProcessedImages);
            var dataForStep            = context.ArticleData.Skip(step * ItemsPerStep).Take(ItemsPerStep).ToArray();

            var flds = GetFields(context.FieldId);
            var dimg = flds.Item1.DynamicImage;
            var baseImagePathInfo = flds.Item2.PathInfo;

            if (dataForStep.Any())
            {
                var ids = dataForStep.Select(d => d.Item1).ToArray();
                var notificationRepository = new NotificationPushRepository {
                    IgnoreInternal = true
                };
                notificationRepository.PrepareNotifications(context.ContentId, ids, NotificationCode.Update);

                foreach (var dfs in dataForStep)
                {
                    using (var transaction = QPConfiguration.CreateTransactionScope(IsolationLevel.ReadCommitted))
                    {
                        var newValue = dimg.GetValue(dimg.GetDesiredFileName(dfs.Item2));
                        RecreateDynamicImagesRepository.UpdateDynamicFieldValue(dimg.Field.Id, dfs.Item1, newValue);

                        if (!currentProcessedImages.Contains(dfs.Item2))
                        {
                            dimg.CreateDynamicImage(baseImagePathInfo.GetPath(dfs.Item2), dfs.Item2);
                            currentProcessedImages.Add(dfs.Item2);
                            context.ProcessedImages.Add(dfs.Item2);
                        }
                        transaction.Complete();
                    }
                }

                notificationRepository.SendNotifications();
            }

            return(new MultistepActionStepResult {
                ProcessedItemsCount = dataForStep.Count()
            });
        }
Beispiel #18
0
        public void Execute(IJobExecutionContext context)
        {
            var customers           = QPConfiguration.GetCustomers(AppName).Where(c => !(c.ExcludeFromSchedulers || c.ExcludeFromSchedulersCdcElastic)).ToList();
            var customersDictionary = new Dictionary <QaConfigCustomer, bool>();
            var prtgErrorsHandlerVm = new PrtgErrorsHandlerViewModel(customers);

            var customersWithEnabledCdc = customers.Where(customer =>
            {
                try
                {
                    return(ShouldUseCdcForCustomerCode(customer));
                }
                catch (Exception ex)
                {
                    ex.Data.Clear();
                    ex.Data.Add("CustomerCode", customer.CustomerName);
                    Logger.Log.Warn($"There was an error while reading customer code settings: {customer.CustomerName}", ex);
                    prtgErrorsHandlerVm.EnqueueNewException(ex);
                }

                return(false);
            }).ToList();

            foreach (var customer in customersWithEnabledCdc)
            {
                try
                {
                    customersDictionary.Add(customer, IsCustomerQueueEmpty(customer));
                }
                catch (Exception ex)
                {
                    ex.Data.Clear();
                    ex.Data.Add("CustomerCode", customer.CustomerName);
                    Logger.Log.Warn($"There was an error while reading customer code settings: {customer.CustomerName}", ex);
                    prtgErrorsHandlerVm.EnqueueNewException(ex);
                }
            }

            CdcSynchronizationContext.ReplaceData(customersDictionary);
            _prtgLogger.LogMessage(prtgErrorsHandlerVm);
        }
Beispiel #19
0
        public Task Execute(IJobExecutionContext context)
        {
            var customers = QPConfiguration.GetCustomers(AppName)
                            .Where(c => c.DbType == DatabaseType.SqlServer)
                            .Where(c => !(c.ExcludeFromSchedulers || c.ExcludeFromSchedulersCdcElastic))
                            .ToList();
            var customersDictionary = new Dictionary <QaConfigCustomer, bool>();

            var customersWithEnabledCdc = customers.Where(customer =>
            {
                try
                {
                    return(ShouldUseCdcForCustomerCode(customer));
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex, "There was an error while reading customer code: {customerCode}", customer.CustomerName);
                }

                return(false);
            }).ToList();

            foreach (var customer in customersWithEnabledCdc)
            {
                if (context.CancellationToken.IsCancellationRequested)
                {
                    return(Task.FromCanceled(context.CancellationToken));
                }
                try
                {
                    customersDictionary.Add(customer, IsCustomerQueueEmpty(customer));
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex, "There was an error while processing customer code: {customerCode}", customer.CustomerName);
                }
            }

            CdcSynchronizationContext.ReplaceData(customersDictionary);
            return(Task.CompletedTask);
        }
Beispiel #20
0
        public virtual void Process(string xmlString, IList <string> filePathes = null)
        {
            Ensure.Argument.NotNullOrWhiteSpace(xmlString, nameof(xmlString));

            var filteredXmlDocument = FilterFromSubRootNodeDuplicates(xmlString);
            var currentDbVersion    = String.Empty;

            using (new QPConnectionScope(ConnectionInfo, _identityInsertOptions))
            {
                currentDbVersion = _appInfoRepository.GetCurrentDbVersion();
                ValidateReplayInput(filteredXmlDocument, currentDbVersion);
            }

            var filteredXmlString = filteredXmlDocument.ToNormalizedString(SaveOptions.DisableFormatting);
            var dbLogEntry        = new XmlDbUpdateLogModel
            {
                UserId   = _userId,
                Body     = filteredXmlString,
                FileName = filePathes == null ? null : string.Join(",", filePathes),
                Applied  = DateTime.Now,
                Hash     = HashHelpers.CalculateMd5Hash(filteredXmlString)
            };

            using (new ThreadStorageScopeContext())
                using (var ts = QPConfiguration.CreateTransactionScope(IsolationLevel.ReadCommitted))
                    using (new QPConnectionScope(ConnectionInfo, _identityInsertOptions))
                    {
                        if (_dbLogService.IsFileAlreadyReplayed(dbLogEntry.Hash))
                        {
                            var throwEx = new XmlDbUpdateLoggingException("XmlDbUpdate conflict: current xml document(s) already applied and exist at database.");
                            throwEx.Data.Add("LogEntry", dbLogEntry.ToJsonLog());
                            throw throwEx;
                        }

                        var updateId = _dbLogService.InsertFileLogEntry(dbLogEntry);
                        ReplayActionsFromXml(filteredXmlDocument.Root?.Elements(), currentDbVersion, filteredXmlDocument.Root?.Attribute(XmlDbUpdateXDocumentConstants.RootBackendUrlAttribute)?.Value, updateId);
                        ts.Complete();
                    }
        }
Beispiel #21
0
        public void Run()
        {
            var unityConfig = new UnityContainerCustomizer();
            var prtgLogger  = new PrtgErrorsHandler(unityConfig.UnityContainer.Resolve <IPrtgNLogFactory>());

            _cancellationTokenSource = new CancellationTokenSource();
            _task = new Task(() =>
            {
                do
                {
                    try
                    {
                        var customers = QPConfiguration.GetCustomers(AppName).Where(c => !c.ExcludeFromSchedulers).ToList();
                        new QpScheduler(unityConfig.UnityContainer, prtgLogger, customers, _tasksQueueCheckShiftTime).Run();
                    }
                    catch (Exception ex)
                    {
                        Logger.Log.Error("There was an error while starting the service job", ex);
                    }
                } while (!_cancellationTokenSource.Token.WaitHandle.WaitOne(_recurrentTimeout));
            }, _cancellationTokenSource.Token, TaskCreationOptions.LongRunning);
            _task.Start();
        }
Beispiel #22
0
 protected static string GetConnectionString(string connectionStringOrCustomerCode) => SqlHelpers.TryParseConnectionString(connectionStringOrCustomerCode, out var cnsBuilder)
     ? cnsBuilder.ConnectionString
     : QPConfiguration.GetConnectionString(connectionStringOrCustomerCode);
Beispiel #23
0
 protected static string GetConnectionString(string connectionStringOrCustomerCode, DatabaseType dbType) => SqlHelpers.TryParseConnectionString(connectionStringOrCustomerCode, (QP.ConfigurationService.Models.DatabaseType)(int) dbType, out var cnsBuilder)
     ? cnsBuilder.ConnectionString
     : QPConfiguration.GetConnectionString(connectionStringOrCustomerCode);
Beispiel #24
0
 public QaConfigCustomer[] GetItems()
 {
     return(QPConfiguration.GetCustomers(AppName).Where(c => !c.ExcludeFromSchedulers).ToArray());
 }
Beispiel #25
0
 public IEnumerator <QaConfigCustomer> GetEnumerator()
 {
     return(QPConfiguration.GetCustomers(_descriptor.Name).Where(c => !c.ExcludeFromSchedulers).GetEnumerator());
 }