Пример #1
0
        public override bool ExecutePackage(ICommunicationPackage communicationPackage)
        {
            SessionManager.VolatileElements.DeferredTransactionId = communicationPackage.XmlData.DeferredTransactionId;
            SessionManager.VolatileElements.LocalTransactionId    = this.LocalTransactionId;

            try
            {
                if (this.IsHeadquarter == true)
                {
                    if (base.ExecutePackage(communicationPackage) == false)
                    {
                        return(false);
                    }
                }
                else
                {
                    this.CurrentPackage = new DBXml(XDocument.Parse(communicationPackage.XmlData.Content));
                }

                if (this.CurrentPackage.Table("documentAttrValue") == null)
                {
                    throw new Exception("CommercialDocumentExScript: Brak atrybutu Attribute_TargetBranchId");
                }

                string targetBranch = PackageExecutionHelper.GetDocumentAttrValue(this.CurrentPackage.Table("documentAttrValue").Xml,
                                                                                  Makolab.Fractus.Kernel.Enums.DocumentFieldName.Attribute_TargetBranchId,
                                                                                  "textValue");
                if (targetBranch == null)
                {
                    throw new Exception("CommercialDocumentExScript: Brak atrybutu Attribute_TargetBranchId");
                }

                Guid targetBranchId = new Guid(targetBranch);
                if (PackageExecutionHelper.IsSameDatabase(Makolab.Fractus.Kernel.Mappers.ConfigurationMapper.Instance.DatabaseId, targetBranchId) == true)
                {
                    using (Kernel.Coordinators.DocumentCoordinator coordinator = new Makolab.Fractus.Kernel.Coordinators.DocumentCoordinator(false, false))
                    {
                        coordinator.CreateOrUpdateReservationFromOrder(this.CurrentPackage.Xml.Root);
                    }
                }
                else if (this.IsHeadquarter == true && MustForwardPackage())
                {
                    ForwardPackage(communicationPackage, targetBranchId);
                }
            }
            catch (SqlException e)
            {
                if (e.Number == 50012) // Conflict detection
                {
                    throw new ConflictException("Conflict detected while changing " + this.MainObjectTag + " id: " + this.CurrentPackage.Table(this.MainObjectTag).FirstRow().Element("id").Value);
                }
                else
                {
                    this.Log.Error("CommercialDocumentExScript:ExecutePackage " + e.ToString());
                    return(false);
                }
            }
            return(true);
        }
Пример #2
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);
        }
Пример #3
0
        public override void SetPreviousVersion(DBXml dbXml, Dictionary <string, string> previousVersions)
        {
            if (dbXml.Table(MainObjectTag).Rows.Count() == 0)
            {
                return;
            }

            if (dbXml.Table(MainObjectTag).Rows.Any(r => r.Action == DBRowState.Delete))
            {
                PackageExecutionHelper.SetPreviousVersion(dbXml, previousVersions, this.MainObjectTag);
            }
            else
            {
                base.SetPreviousVersion(dbXml, previousVersions);
            }
        }
Пример #4
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)
        {
            if (base.ExecutePackage(communicationPackage) == false)
            {
                return(false);
            }

            CommunicationPackage payment = PackageExecutionHelper.ExtractPaymentPackage(this.CurrentPackage, communicationPackage);

            if (payment != null)
            {
                payment.XmlData.LocalTransactionId = this.LocalTransactionId;
                var pkgRepo   = new CommunicationPackageRepository(this.UnitOfWork);
                var forwarder = Makolab.Fractus.Commons.DependencyInjection.IoC.Get <IPackageForwarder>();
                forwarder.Log = this.Log;
                forwarder.ForwardPackage(payment, pkgRepo);
            }
            return(true);
        }
Пример #5
0
        private bool MustForwardPackage()
        {
            string oppositeDocumentId = PackageExecutionHelper.GetDocumentAttrValue(this.CurrentPackage.Table("documentAttrValue").Xml,
                                                                                    Makolab.Fractus.Kernel.Enums.DocumentFieldName.Attribute_OppositeDocumentId,
                                                                                    "textValue");

            if (oppositeDocumentId == null)
            {
                return(true);
            }

            DBXml oppositeSnapshot = this.repository.FindCommercialDocumentSnapshot(new Guid(oppositeDocumentId));

            if (oppositeSnapshot == null)
            {
                return(true);
            }

            string orderStatus = PackageExecutionHelper.GetDocumentAttrValue(this.CurrentPackage.Table("documentAttrValue").Xml,
                                                                             Makolab.Fractus.Kernel.Enums.DocumentFieldName.Attribute_OrderStatus,
                                                                             "textValue");
            string oppositeOrderStatus = (oppositeSnapshot.Table("documentAttrValue") == null) ? null :
                                         PackageExecutionHelper.GetDocumentAttrValue(oppositeSnapshot.Table("documentAttrValue").Xml,
                                                                                     Makolab.Fractus.Kernel.Enums.DocumentFieldName.Attribute_OrderStatus,
                                                                                     "textValue");

            //should work like this (orderStatus is a number) if ((orderStatus == null && oppositeOrderStatus != null) || (orderStatus != null && orderStatus.Equals(oppositeOrderStatus, StringComparison.OrdinalIgnoreCase) == false)) return true;
            if (orderStatus != oppositeOrderStatus)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #6
0
 public override void SetPreviousVersion(DBXml dbXml, Dictionary <string, string> previousVersions)
 {
     PackageExecutionHelper.SetPreviousVersion(dbXml, previousVersions, this.MainObjectTag);
 }
Пример #7
0
 /// <summary>
 /// Removes the previous version number from database xml.
 /// </summary>
 /// <param name="dbXml">The db XML.</param>
 /// <returns>
 /// Specified database xml without previous version number.
 /// </returns>
 public override DBXml RemovePreviousVersion(DBXml dbXml)
 {
     return(PackageExecutionHelper.RemovePreviousVersion(dbXml));
 }
Пример #8
0
 /// <summary>
 /// Gets the number indicating previous version of contractor relations within database xml.
 /// </summary>
 /// <param name="dbXml">The database xml.</param>
 /// <returns>Previous version of contractor relations.</returns>
 public override Dictionary <string, string> GetPreviousVersion(DBXml dbXml)
 {
     return(PackageExecutionHelper.GetPreviousVersion(dbXml, this.MainObjectTag));
 }
Пример #9
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);
            }
        }