Ejemplo n.º 1
0
        /// <summary>
        /// Process data that was received by web service method.
        /// </summary>
        /// <param name="data">The data received via web service method call.</param>
        /// <returns>Response to web service client.</returns>
        public object DataReceived(object data)
        {
            SendDataParameters methodParams = (SendDataParameters)data;

            if (methodParams == null)
            {
                return(null);
            }

            SendDataResponse response = new SendDataResponse();

            ICommunicationPackage communicationPackage = (methodParams.Xml == null) ? null : CreateCommunicationPackage(methodParams);

            DatabaseConnector.DatabaseConnectorManager dbm = GetDatabaseConnector();
            dbm.StartModule();
            try
            {
                using (IUnitOfWork uow = new UnitOfWork(dbm))
                {
                    uow.MapperFactory = IoC.Get <IMapperFactory>();
                    uow.StartTransaction(IsolationLevel.Serializable);

                    CommunicationPackageRepository repo = new CommunicationPackageRepository(uow);
                    if (communicationPackage != null)
                    {
                        repo.Add(communicationPackage);
                    }
                    if (methodParams.IsLastInTransaction)
                    {
                        repo.MarkTransactionAsCompleted(communicationPackage.XmlData.LocalTransactionId);
                    }
                    if (methodParams.Statistics != null)
                    {
                        repo.UpdateStatistics(methodParams.Statistics, methodParams.DepartmentIdentifier);
                    }

                    uow.SubmitChanges();
                }
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                response.Result = false;
                Log.Error(e.ToString(), false);
                return(response);
            }
            catch (CommunicationPackageExistsException e) //Xml already in database.
            {
                Log.Info(e.ToString(), false);
            }
            finally
            {
                dbm.StopModule();
            }

            response.Result = true;
            return(response);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Task main method.
        /// </summary>
        /// <remarks>
        /// Run method steps:
        /// 1. Retrives next package.
        /// 2. Presists package.
        /// 3. Waits before repeating process.
        /// </remarks>
        protected override void Run()
        {
            Guid?lastReceivedXmlId         = null;
            Guid?lastReceivedTransactionId = null;
            Guid databaseId = this.Manager.DatabaseId;

            lastReceivedTransactionId = GetLastTransactionId();

            while (IsEnabled)
            {
                lock (Makolab.Fractus.Communication.Transmitter.TransmitterSemaphore.locker)
                {
                    try
                    {
                        GetDataResponse response = GetNextXmlPackage(lastReceivedXmlId, databaseId);
                        if (lastReceivedTransactionId != null && response != GetDataResponse.ExceptionResponse && (response.XmlData == null || response.XmlData.LocalTransactionId != lastReceivedTransactionId.Value))
                        {
                            using (IUnitOfWork uow = Manager.CreateUnitOfWork())
                            {
                                uow.MapperFactory = this.mapperFactory;
                                CommunicationPackageRepository repo = new CommunicationPackageRepository(uow);
                                repo.MarkTransactionAsCompleted(lastReceivedTransactionId.Value);
                                lastReceivedTransactionId = null;
                            }
                        }

                        if (response.XmlData != null && ProcessXml(response.XmlData, response.DatabaseId) == true)
                        {
                            lastReceivedXmlId         = response.XmlData.Id;
                            lastReceivedTransactionId = response.XmlData.LocalTransactionId;
                        }
                        else
                        {
                            lastReceivedXmlId = null;
                        }

                        if (response.AdditionalData != null && response.AdditionalData.UndeliveredPackagesQuantity != null)
                        {
                            this.WaitingPackages = response.AdditionalData.UndeliveredPackagesQuantity.Value;
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Error(String.Format(CultureInfo.InvariantCulture, "Uncaught exception in PackageReceiver: {0}", e.ToString()));
                    }
                }

                Wait();
            }
        }