Exemplo n.º 1
0
        /// <summary>
        /// Builds a new Transaction Service preloaded with all available Transaction Type configurations.
        /// </summary>
        /// <param name="executionContext"></param>
        /// <param name="cacheTimeOut">Sets amount of time to cache the transaction service to service future build requests. When null, caching is not used.</param>
        /// <returns></returns>
        public ITransactionService CreateTransactionService(IProcessExecutionContext executionContext, bool useCache = true)
        {
            try
            {
                useCache = useCache && executionContext.Cache != null;

                if (useCache)
                {
                    if (executionContext.Cache.Exists(CACHE_KEY))
                    {
                        executionContext.Trace("Returning Transaction Service from cache.");
                        return(executionContext.Cache.Get <ITransactionService>(CACHE_KEY));
                    }
                }



                IList <ITransactionType> registeredTransactions = new List <ITransactionType>();

                var transactionTypeRecords = DataConnector.GetAllTransactionTypeRecords(executionContext.DataService);
                executionContext.Trace("Retrieved {0} Transaction Type records.", transactionTypeRecords.Count);

                // get all active records that are needed to fully define a transaction type. This includes complex objects for ITransactionProcess
                // and ITransactionRequirements as well as simpler objects for transaction groups, authorized channels, authorized roles,
                // initial fees, and eligible record contexts.
                var processes = getProcesses(executionContext, useCache);

                var requirements = getRequirements(executionContext, useCache);

                var transactionGroups = DataConnector.GetAllTransactionGroups(executionContext.DataService);
                executionContext.Trace("Retrieved {0} Transaction Group records.", transactionGroups.Count);

                var authorizedChannels = DataConnector.GetAllTransactionAuthorizedChannels(executionContext.DataService);
                executionContext.Trace("Retrieved {0} Transaction Authorized Channel records.", authorizedChannels.Count);

                var authorizedRoles = DataConnector.GetAllAuthorizedRoles(executionContext.DataService);
                executionContext.Trace("Retrieved {0} Transaction Authorized Role records.", authorizedRoles.Count);

                var initialFees = DataConnector.GetAllInitialFees(executionContext.DataService);
                executionContext.Trace("Retrieved {0} Transaction Initial Fee records.", initialFees.Count);

                var contexts = DataConnector.GetAllTransactionContextTypes(executionContext.DataService);
                executionContext.Trace("Retrieved {0} Transaction Type Context records.", contexts.Count);

                // Create a new TransactionType object for each transaction type retrieved.
                foreach (ITransactionTypeRecord t in transactionTypeRecords)
                {
                    executionContext.Trace("Processing transaction type '{0}'", t.Name);

                    ISerializedParameters dataRecordConfig = null;
                    try
                    {
                        executionContext.Trace("Parsing Data Record Configuration.");
                        dataRecordConfig = this.ParameterSerializer.CreateParamters(t.DataRecordConfiguration);
                        if (!dataRecordConfig.ContainsKey("RecordType"))
                        {
                            throw new Exception("Missing RecordType.");
                        }
                        if (!dataRecordConfig.ContainsKey("CustomerField"))
                        {
                            throw new Exception("Missing Customer Field");
                        }
                        if (!dataRecordConfig.ContainsKey("TransactionField"))
                        {
                            throw new Exception("Missing Transaction Field");
                        }
                        if (!dataRecordConfig.ContainsKey("NameField"))
                        {
                            throw new Exception("Missing Name Field");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw TransactionException.BuildException(TransactionException.ErrorCode.ProcessStepTypeInvalid, ex);
                    }

                    registeredTransactions.Add(new TransactionType(
                                                   t,
                                                   t.Name,
                                                   t.DisplayRank,
                                                   transactionGroups.Where(r => t.TransactionGroupId != null && r.Id == t.TransactionGroupId.Id).FirstOrDefault(),
                                                   t.StartupProcessId ?? throw new Exception("Transaction type is missing a startup process id."),
                                                   dataRecordConfig,
                                                   authorizedChannels.Where(r => r.ParentId.Id == t.Id).Select(r => r.ChannelId),
                                                   authorizedRoles.Where(r => r.ParentId.Id == t.Id).Select(r => r.RoleId),
                                                   processes.Where(r => r.TransactionTypeId.Id == t.Id),
                                                   requirements.Where(r => r.TransactionTypeId.Id == t.Id),
                                                   initialFees.Where(r => r.TransactionTypeId.Id == t.Id).Select(r => r.FeeId),
                                                   contexts.Where(r => r.TransactionTypeId.Id == t.Id)));
                }

                executionContext.Trace("Creating Transaction Service loaded with {0} Transaction Types.", registeredTransactions.Count);

                // Create a new transaction service and pass in required factory and record services.
                var transactionService = new TransactionService(this.DataConnector, this.AgentFactory, this.TransactionFeeListFactory, this.TransactionContextFactory, this.CustomerFactory, this.TransactionDeficienciesFactory,
                                                                this.DocumentService, this.EvidenceService, this.LocationFactory, RequirementEvaluator, this.TransactionHistoryFactory, this.FeeList, registeredTransactions);

                if (useCache)
                {
                    var settings     = SettingsFactory.CreateSettings(executionContext.Settings);
                    var cacheTimeout = settings.PlatformServiceCacheTimeout;

                    executionContext.Cache.Add <ITransactionService>(CACHE_KEY, transactionService, cacheTimeout.Value);
                    executionContext.Trace("Cached Transaction Service for {0}.", cacheTimeout.Value);
                }

                executionContext.TrackEvent("TransactionServiceFactory.BuildTransactionService");

                return(transactionService);
            }
            catch (Exception)
            {
                throw;
            }
        }