Ejemplo n.º 1
0
        /// <summary>
        /// Process communication package persisting it's data to database.
        /// </summary>
        /// <param name="communicationPackage">The communication package to execute.</param>
        /// <returns>
        ///     <c>true</c> if execution succeeded; otherwise, <c>false</c>
        /// </returns>
        public override bool ExecutePackage(ICommunicationPackage communicationPackage)
        {
            SessionManager.VolatileElements.DeferredTransactionId = communicationPackage.XmlData.DeferredTransactionId;
            SessionManager.VolatileElements.LocalTransactionId    = this.LocalTransactionId;

            this.CurrentPackage = new DBXml(XDocument.Parse(communicationPackage.XmlData.Content));

            List <Guid> relationsId = new List <Guid>();

            foreach (DBRow row in this.CurrentPackage.Table(this.MainObjectTag).Rows)
            {
                Guid relationId = new Guid(row.Element("id").Value);
                relationsId.Add(relationId);
            }

            DBXml dbSnapshot = GetCurrentSnapshot(relationsId);

            try
            {
                // TODO conflict detection & resolution
                //// Conflict detection
                //if (dbSnapshot != null && ValidateVersion(CurrentPackage, dbSnapshot) == false)
                //{
                //    throw new ConflictException("Conflict detected while changing " + this.MainObjectTag + " id: " + mainObjectId.ToString());
                //}

                PackageExecutionHelper.RemoveDeletedRows(this.CurrentPackage, dbSnapshot, this.MainObjectTag, this.Log);

                if (this.CurrentPackage.Table(MainObjectTag).HasRows == false)
                {
                    return(true);
                }
                //else do the rest

                DBXml changeset = GenerateChangeset(CurrentPackage, dbSnapshot);
                ExecuteChangeset(changeset);
            }
            catch (SqlException e)
            {
                if (e.Number == 50012) // Conflict detection
                {
                    throw new ConflictException("Conflict detected while changing " + this.MainObjectTag);
                }
                else
                {
                    this.Log.Error("SnapshotScript:ExecutePackage " + e.ToString());
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Process communication package persisting it's data to database.
        /// </summary>
        /// <param name="communicationPackage">The communication package to execute.</param>
        /// <param name="executionMode">The execution mode. 1 - everything at once, 0 - single valuation per transaction.</param>
        /// <returns>
        ///     <c>true</c> if execution succeeded; otherwise, <c>false</c>
        /// </returns>
        public bool ExecutePackage(ICommunicationPackage communicationPackage, int executionMode)
        {
            this.executionMode = executionMode;

            SessionManager.VolatileElements.DeferredTransactionId = communicationPackage.XmlData.DeferredTransactionId;
            SessionManager.VolatileElements.LocalTransactionId    = this.LocalTransactionId;


            this.CurrentPackage = new DBXml(XDocument.Parse(communicationPackage.XmlData.Content));

            if (this.IsHeadquarter == true)
            {
                List <Guid> valuationsId = new List <Guid>();

                foreach (DBRow row in this.CurrentPackage.Table(this.MainObjectTag).Rows)
                {
                    Guid valuationId = new Guid(row.Element("id").Value);
                    valuationsId.Add(valuationId);
                }

                DBXml dbSnapshot   = GetCurrentSnapshot(valuationsId);
                bool  hasSavepoint = false;
                try
                {
                    // TODO conflict detection & resolution
                    //// Conflict detection
                    //if (dbSnapshot != null && ValidateVersion(CurrentPackage, dbSnapshot) == false)
                    //{
                    //    throw new ConflictException("Conflict detected while changing " + this.MainObjectTag + " id: " + mainObjectId.ToString());
                    //}

                    PackageExecutionHelper.RemoveDeletedRows(this.CurrentPackage, dbSnapshot, this.MainObjectTag, this.Log);

                    if (this.CurrentPackage.Table(this.MainObjectTag).HasRows == false)
                    {
                        return(true);
                    }
                    //else do the rest

                    DBXml changeset = GenerateChangeset(CurrentPackage, dbSnapshot);

                    this.repository.CreateSavepoint("warehouseValuationSP");
                    hasSavepoint = true;
                    ExecuteChangeset(changeset);

                    //check whether headquarter is a target branch - we have a document so it's not a big deal, and if so valuate incom shift
                    var distributedLines = this.CurrentPackage.Table(this.MainObjectTag).Rows
                                           .Where(
                        row => row.Element("isDistributed") != null &&
                        row.Element("isDistributed").Value.Equals("True", StringComparison.OrdinalIgnoreCase) &&
                        row.Element("warehouseDocumentHeaderId") != null &&
                        row.Action != DBRowState.Delete)
                                           .GroupBy(row => row.Element("warehouseDocumentHeaderId").Value);

                    if (distributedLines.Count() > 0)
                    {
                        this.ExecutionController.ExecuteCommand(() => ValuateIncomeFromOutcome(distributedLines));
                    }
                }
                catch (ClientException e)
                {
                    string s = "";
                    foreach (var parameter in e.Parameters)
                    {
                        s += ";" + parameter;
                    }
                    this.Log.Info(s);
                    throw;
                }
                catch (SqlException e)
                {
                    if (e.Number == 547)
                    {
                        if (hasSavepoint)
                        {
                            this.repository.RollbackToSavepoint("warehouseValuationSP");
                        }
                        this.Log.Info(String.Format("Paczka wyceny {0} pominieta - brak wycenianego dokumentu: {1}", communicationPackage.OrderNumber, e.ToString()));
                        return(true);
                    }
                    if (e.Number == 50012) // Conflict detection
                    {
                        throw new ConflictException("Conflict detected while changing " + this.MainObjectTag);
                    }
                    else
                    {
                        this.Log.Error("SnapshotScript:ExecutePackage " + e.ToString());
                        return(false);
                    }
                }
                if (this.CurrentPackage.Xml.Root.Attribute("skipPackage") == null)
                {
                    this.CurrentPackage.Xml.Root.Add(new XAttribute("skipPackage", true));
                }
                communicationPackage.XmlData.Content = this.CurrentPackage.Xml.ToString(SaveOptions.DisableFormatting);
                return(true);
            }
            else if (this.CurrentPackage.Table(this.MainObjectTag).Rows.Any(row => row.Element("isDistributed") != null &&
                                                                            row.Element("isDistributed").Value.Equals("True", StringComparison.OrdinalIgnoreCase)))
            {
                // if the branch is not headquarther we consider it as target branch for shift document and try to valuate income shift
                this.ExecutionController.ExecuteCommand(() => ValuateIncomeFromOutcome());
                return(true);
            }
            else
            {
                //its not a shift valuation,
                // just other warehouse document valuation returned from HQ probably cause of some accounting operations
                List <Guid> valuationsId = new List <Guid>();

                foreach (DBRow row in this.CurrentPackage.Table(this.MainObjectTag).Rows)
                {
                    Guid valuationId = new Guid(row.Element("id").Value);
                    valuationsId.Add(valuationId);
                }

                DBXml dbSnapshot   = GetCurrentSnapshot(valuationsId);
                bool  hasSavepoint = false;
                try
                {
                    // TODO conflict detection & resolution
                    //// Conflict detection
                    //if (dbSnapshot != null && ValidateVersion(CurrentPackage, dbSnapshot) == false)
                    //{
                    //    throw new ConflictException("Conflict detected while changing " + this.MainObjectTag + " id: " + mainObjectId.ToString());
                    //}

                    PackageExecutionHelper.RemoveDeletedRows(this.CurrentPackage, dbSnapshot, this.MainObjectTag, this.Log);

                    if (this.CurrentPackage.Table(this.MainObjectTag).HasRows == false)
                    {
                        return(true);
                    }
                    //else do the rest

                    DBXml changeset = GenerateChangeset(CurrentPackage, dbSnapshot);

                    this.repository.CreateSavepoint("warehouseValuationSP");
                    hasSavepoint = true;
                    ExecuteChangeset(changeset);
                }
                catch (ClientException e)
                {
                    string s = "";
                    foreach (var parameter in e.Parameters)
                    {
                        s += ";" + parameter;
                    }
                    this.Log.Info(s);
                    throw;
                }
                catch (SqlException e)
                {
                    if (e.Number == 547)
                    {
                        if (hasSavepoint)
                        {
                            this.repository.RollbackToSavepoint("warehouseValuationSP");
                        }
                        this.Log.Info(String.Format("Paczka wyceny {0} pominieta - brak wycenianego dokumentu: {1}", communicationPackage.OrderNumber, e.ToString()));
                        return(true);
                    }
                    if (e.Number == 50012) // Conflict detection
                    {
                        throw new ConflictException("Conflict detected while changing " + this.MainObjectTag);
                    }
                    else
                    {
                        this.Log.Error("SnapshotScript:ExecutePackage " + e.ToString());
                        return(false);
                    }
                }
                if (this.CurrentPackage.Xml.Root.Attribute("skipPackage") == null)
                {
                    this.CurrentPackage.Xml.Root.Add(new XAttribute("skipPackage", true));
                }
                communicationPackage.XmlData.Content = this.CurrentPackage.Xml.ToString(SaveOptions.DisableFormatting);
                return(true);
            }
        }