예제 #1
0
        /// <summary>
        /// Generates the shift document status package.
        /// </summary>
        /// <returns>Generated ShiftDocumentStatus package.</returns>
        private CommunicationPackage GenerateShiftDocumentStatusPackage()
        {
            string oppositeShiftDocFieldId = DictionaryMapper.Instance.GetDocumentField(Makolab.Fractus.Kernel.Enums.DocumentFieldName.ShiftDocumentAttribute_OppositeDocumentId).Id.Value.ToString().ToUpperInvariant();
            string oppositeShiftDocId      = this.CurrentPackage.Table("documentAttrValue").Rows
                                             .Where(row => row.Element("documentFieldId").Value.Equals(oppositeShiftDocFieldId))
                                             .Select(row => row.Element("textValue").Value)
                                             .SingleOrDefault();

            if (oppositeShiftDocId == null)
            {
                throw new InvalidDataException("Missing opposite document id in attribute.");
            }

            XDocument statusXml = XDocument.Parse("<root><shiftDocumentStatus/></root>");

            statusXml.Root.Element("shiftDocumentStatus").Add(new XAttribute("incomeShiftId",
                                                                             this.CurrentPackage.Table("warehouseDocumentHeader").FirstRow().Element("id").Value),
                                                              new XAttribute("outcomeShiftId", oppositeShiftDocId),
                                                              new XAttribute("status",
                                                                             this.CurrentPackage.Table("warehouseDocumentHeader").FirstRow().Element("status").Value));

            XmlTransferObject statusPkgData = new XmlTransferObject
            {
                DeferredTransactionId = Guid.NewGuid(),
                Id = Guid.NewGuid(),
                LocalTransactionId = this.LocalTransactionId,
                XmlType            = "ShiftDocumentStatus",
                Content            = statusXml.ToString(SaveOptions.DisableFormatting)
            };

            return(new CommunicationPackage(statusPkgData));
        }
예제 #2
0
        /// <summary>
        /// Extracts the payment package from another package.
        /// </summary>
        /// <param name="communicationXml">The communication XML.</param>
        /// <param name="communicationPackage">The communication package.</param>
        /// <returns>Extracted payment <c>CommunicationPackage</c> object if <b>communicationXml</b> has payment data; otherwise, <c>null</c>.</returns>
        public static CommunicationPackage ExtractPaymentPackage(DBXml communicationXml, ICommunicationPackage communicationPackage)
        {
            if (communicationXml.Table("payment") == null || communicationXml.Table("payment").HasRows == false)
            {
                return(null);
            }

            XDocument commXml = XDocument.Parse("<root/>");

            commXml.Root.Add(communicationXml.Table("payment").Xml);

            var settlements = communicationXml.Table("paymentSettlement");

            if (settlements != null && settlements.HasRows == true)
            {
                commXml.Root.Add(settlements.Xml);
            }

            XmlTransferObject commPkgData = new XmlTransferObject
            {
                DeferredTransactionId = communicationPackage.XmlData.DeferredTransactionId,
                Id = Guid.NewGuid(),
                LocalTransactionId = communicationPackage.XmlData.LocalTransactionId,
                XmlType            = CommunicationPackageType.Payment.ToString(),
                Content            = commXml.ToString(SaveOptions.DisableFormatting)
            };

            return(new CommunicationPackage(commPkgData)
            {
                DatabaseId = communicationPackage.DatabaseId
            });;
        }
예제 #3
0
        /// <summary>
        /// Saves the specified xml to storage.
        /// </summary>
        /// <param name="xmlData">The xml to save.</param>
        /// <param name="databaseId">The database id.</param>
        /// <returns>
        ///     <c>true</c> if  is saving successful; otherwise, <c>false</c>.
        /// </returns>
        public bool ProcessXml(XmlTransferObject xmlData, Guid?databaseId)
        {
            bool isSaved = false;
            ICommunicationPackage xml = this.packageFactory.CreatePackage(xmlData);

            xml.DatabaseId = databaseId;
            xml.Decompress();
            if (xml.CheckSyntax())
            {
                int retryCount = 0;
                using (IUnitOfWork uow = Manager.CreateUnitOfWork())
                {
                    uow.MapperFactory = this.mapperFactory;

                    CommunicationPackageRepository repo = new CommunicationPackageRepository(uow);
                    while (isSaved == false && retryCount < 10)
                    {
                        try
                        {
                            repo.Add(xml);
                            isSaved = true;
                        }
                        catch (SqlException)
                        {
                            ++retryCount;
                            Wait();
                        }
                        catch (CommunicationPackageExistsException e) // Xml with the same id exists in db
                        {
                            this.Log.Info(e.ToString(), false);
                            isSaved = true;
                        }
                    }

                    if (!isSaved)
                    {
                        Log.Error("EXCEPTION: CRITICAL ERROR ALERT! What to do with unset (Saves the specified xml to storage) ProcessXml(XmlTransferObject xmlData,Guid? databaseId) PackageReceiver.cs");
                    }
                }
            }
            else
            {
                Log.Error("Invalid xml received, syntax error. Xml id=" + xmlData.Id + " content=" + xmlData.Content);
                ////it shouldnt be received in the first place = validation on the WebService site

                return(false);
            }

            return(isSaved);
        }
예제 #4
0
        private void ForwardPackage(ICommunicationPackage communicationPackage, Guid targetBranchId)
        {
            XmlTransferObject forwardedPkgData = new XmlTransferObject
            {
                DeferredTransactionId = communicationPackage.XmlData.DeferredTransactionId,
                Id = Guid.NewGuid(),
                LocalTransactionId = Guid.NewGuid(),
                XmlType            = "CommercialDocumentSnapshotEx",
                Content            = this.CurrentPackage.Xml.ToString(System.Xml.Linq.SaveOptions.DisableFormatting)
            };
            ICommunicationPackage pkg = new CommunicationPackage(forwardedPkgData);

            pkg.DatabaseId = Makolab.Fractus.Kernel.Mappers.DictionaryMapper.Instance.GetBranch(targetBranchId).DatabaseId;
            CommunicationPackageRepository pkgRepo = new CommunicationPackageRepository(this.UnitOfWork);

            pkgRepo.PutToOutgoingQueue(pkg);
        }
예제 #5
0
        /// <summary>
        /// Creates the communication package..
        /// </summary>
        /// <param name="data">The object with communication package data.</param>
        /// <returns>Created communication package.</returns>
        public ICommunicationPackage CreatePackage(object data)
        {
            CommunicationPackage package = null;

            XElement xml = data as XElement;

            if (xml == null)
            {
                throw new ArgumentException("Invalid object type", "data");
            }

            XmlTransferObject xmlData = new XmlTransferObject();

            xmlData.Id = new Guid(xml.Element("id").Value);
            xmlData.LocalTransactionId    = new Guid(xml.Element("localTransactionId").Value);
            xmlData.DeferredTransactionId = new Guid(xml.Element("deferredTransactionId").Value);
            xmlData.Content = xml.Element("xml").FirstNode.ToString(SaveOptions.DisableFormatting);

            try
            {
                xmlData.XmlType = xml.Element("type").Value;
            }
            catch (ArgumentException)
            {
                xmlData.XmlType = "Unknown";
            }

            package = new CommunicationPackage(xmlData);

            package.OrderNumber = Int32.Parse(xml.Element("order").Value, CultureInfo.InvariantCulture);

            XElement databaseId = xml.Element("databaseId");

            if (databaseId == null)
            {
                throw new InvalidDataException("DatabaseId cannot be null");
            }
            package.DatabaseId = new Guid(databaseId.Value);

            return(package);
        }
예제 #6
0
 /// <summary>
 /// Creates the communication package from <see cref="XmlTransferObject"/> object.
 /// </summary>
 /// <param name="data">The <see cref="XmlTransferObject"/> with communication package data.</param>
 /// <returns>
 /// Communication package created from <see cref="XmlTransferObject"/> object.
 /// </returns>
 public ICommunicationPackage CreatePackage(XmlTransferObject data)
 {
     return(new CommunicationPackage(data));
 }
예제 #7
0
        private static void ForwardValuations(ICommunicationPackage communicationPackage, XDocument commPkg, ICommunicationPackageRepository repository)
        {
            var distributedLines = commPkg.Root.Element("warehouseDocumentValuation").Elements("entry")
                                   .Where(
                row => row.Element("isDistributed") != null &&
                row.Element("isDistributed").Value.Equals("True", StringComparison.OrdinalIgnoreCase) &&
                row.Element("warehouseDocumentHeaderId") != null &&
                (row.Attribute("action") == null || row.Attribute("action").Value.Equals("delete", StringComparison.OrdinalIgnoreCase) == false))
                                   .GroupBy(row => row.Element("warehouseDocumentHeaderId").Value);

            //this valuation is not for shift document, so we don't forward it
            if (distributedLines.Count() == 0)
            {
                return;
            }

            XDocument valuationTemplate  = XDocument.Parse("<root><warehouseDocumentValuation /></root>");
            Guid      localTransactionId = Guid.NewGuid();

            using (var wrapper = repository.Context.ConnectionManager.SynchronizeConnection())
            {
                SqlConnectionManager.Instance.SetConnection(wrapper.Connection, (SqlTransaction)repository.Context.Transaction);
            }
            using (var whDocCoord = new DocumentCoordinator(false, false))
            {
                foreach (var warehouseDocGroup in distributedLines)
                {
                    WarehouseDocument shift = null;
                    try
                    {
                        shift = (WarehouseDocument)whDocCoord.LoadBusinessObject(Makolab.Fractus.Kernel.Enums.BusinessObjectType.WarehouseDocument,
                                                                                 new Guid(warehouseDocGroup.Key));
                    }
                    catch (ClientException) { }

                    //there is a valuated document in database so we can forward valuation - no document = skip valuations for this doc
                    if (shift != null)
                    {
                        string oppositeWarehouseId = (shift.Attributes.Count() > 0 && shift.Attributes.Children.Any(attr => attr.DocumentFieldName == DocumentFieldName.ShiftDocumentAttribute_OppositeWarehouseId))
                                                    ? shift.Attributes[DocumentFieldName.ShiftDocumentAttribute_OppositeWarehouseId].Value.Value
                                                    : null;
                        if (oppositeWarehouseId != null)
                        {
                            Warehouse oppositeWarehouse = DictionaryMapper.Instance.GetWarehouse(new Guid(oppositeWarehouseId));
                            Warehouse warehouse         = DictionaryMapper.Instance.GetWarehouse(shift.WarehouseId);

                            //skip local shift document valuations
                            if (warehouse.BranchId == oppositeWarehouse.BranchId)
                            {
                                continue;
                            }

                            foreach (var valuation in warehouseDocGroup)
                            {
                                var whDocLine = shift.Lines.Children.Where(line => line.Id.ToString()
                                                                           .Equals(valuation.Element("outcomeWarehouseDocumentLineId").Value, StringComparison.OrdinalIgnoreCase))
                                                .SingleOrDefault();
                                valuation.Add(new XAttribute("outcomeShiftOrdinalNumber", whDocLine.OrdinalNumber));
                            }

                            var valuationPkg = new XDocument(valuationTemplate);
                            valuationPkg.Root.Element("warehouseDocumentValuation").Add(new XAttribute("outcomeShiftId", shift.Id));
                            valuationPkg.Root.Element("warehouseDocumentValuation").Add(warehouseDocGroup);

                            XmlTransferObject valuationData = new XmlTransferObject
                            {
                                DeferredTransactionId = communicationPackage.XmlData.DeferredTransactionId,
                                Id = Guid.NewGuid(),
                                LocalTransactionId = localTransactionId,
                                XmlType            = "WarehouseDocumentValuation",
                                Content            = valuationPkg.ToString(SaveOptions.DisableFormatting)
                            };
                            ICommunicationPackage pkg = new CommunicationPackage(valuationData);
                            pkg.DatabaseId = DictionaryMapper.Instance.GetBranch(oppositeWarehouse.BranchId).DatabaseId;
                            repository.PutToOutgoingQueue(pkg);
                        }
                    }
                }
            }
        }