Exemplo n.º 1
0
        private void ReplayActionsFromXml(IEnumerable <XElement> actionElements, string currentDbVersion, string backendUrl, int updateId)
        {
            foreach (var xmlAction in actionElements)
            {
                XmlDbUpdateRecordedAction action;
                var xmlActionString = xmlAction.ToNormalizedString(SaveOptions.DisableFormatting, true);

                try
                {
                    action = XmlDbUpdateSerializerHelpers.DeserializeAction(xmlAction);
                }
                catch (Exception ex)
                {
                    var throwEx = new XmlDbUpdateReplayActionException("Error while deserializing xml action string.", ex);
                    throwEx.Data.Add(LoggerData.XmlDbUpdateExceptionXmlActionStringData, xmlActionString.ToJsonLog());
                    throw throwEx;
                }

                var logEntry = new XmlDbUpdateActionsLogModel
                {
                    UserId    = _userId,
                    Applied   = DateTime.Now,
                    ParentId  = action.ParentId,
                    UpdateId  = updateId,
                    SourceXml = xmlActionString,
                    Hash      = HashHelpers.CalculateMd5Hash(xmlActionString)
                };

                if (_dbLogService.IsActionAlreadyReplayed(logEntry.Hash))
                {
                    Logger.Log.Warn($"XmlDbUpdateAction conflict: Current action already applied and exist at database. Entry: {logEntry.ToJsonLog()}");
                    continue;
                }

                var xmlActionStringLog = xmlAction.RemoveDescendants().ToString(SaveOptions.DisableFormatting);
                Logger.Log.Debug($"-> Begin replaying action [{logEntry.Hash}]: -> {xmlActionStringLog}");
                var replayedAction = ReplayAction(action, backendUrl);
                Logger.Log.Debug($"End replaying action [{logEntry.Hash}]: {xmlActionStringLog}");

                logEntry.Ids       = string.Join(",", replayedAction.Ids);
                logEntry.ResultXml = XmlDbUpdateSerializerHelpers.SerializeAction(replayedAction, currentDbVersion, backendUrl, true).ToNormalizedString(SaveOptions.DisableFormatting, true);
                _dbLogService.InsertActionLogEntry(logEntry);
            }
        }
Exemplo n.º 2
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();
                    }
        }