Example #1
0
        /// <summary>
        ///     Execute Integration Process
        /// </summary>
        /// <param name="pProcessExecutionRequest"></param>
        /// <returns></returns>
        public ProcessExecutionResponse ExecuteIntegrationProcess(ProcessExecutionRequest pProcessExecutionRequest)
        {
            ProcessExecutionResponse         response       = null;
            List <IntegrationProcessMapping> processMapping = null;
            DataTable destinationTable                        = null;
            List <FieldMappingInfo>  mappingInfos             = null;
            PushToDestinationRequest pushToDestinationRequest = null;
            string outputMessage = string.Empty;

            try
            {
                #region Integration Process Metadata

                LogManager.LogMessage(string.Format(ABATS.AppsTalk.Runtime.Properties.Resources.Message_Integration_Process_Execution_Start_Request,
                                                    pProcessExecutionRequest.ProcessCode));

                response = new ProcessExecutionResponse(pProcessExecutionRequest,
                                                        base.AppRuntime.MetadataService.GetIntegrationProcessMetatdata(pProcessExecutionRequest.ProcessCode));

                #endregion

                if (response != null && response.IntegrationProcessMetadata != null)
                {
                    LogManager.LogMessage(string.Format(ABATS.AppsTalk.Runtime.Properties.Resources.Message_Integration_Process_Execution_Started,
                                                        response.IntegrationProcessMetadata.IntegrationProcessCode,
                                                        response.IntegrationProcessMetadata.IntegrationProcessID,
                                                        response.IntegrationProcessMetadata.IntegrationProcessTitle));

                    if (this.ValidateIntegrationProcess(response.IntegrationProcessMetadata, out outputMessage))
                    {
                        LogManager.LogMessage(string.Format(ABATS.AppsTalk.Runtime.Properties.Resources.Message_Integration_Process_Validation_Succeeded,
                                                            response.IntegrationProcessMetadata.IntegrationProcessCode));

                        #region Consume Source

                        using (AbstractAdapter sourceAdapter = AdapterFactory.CreateAdapter(
                                   response.IntegrationProcessMetadata,
                                   response.IntegrationProcessMetadata.SourceIntegrationAdapter,
                                   base.AppRuntime))
                        {
                            response.SourceAdapterResponse = sourceAdapter.ConsumeSource();
                        }

                        #endregion

                        if (response.SourceAdapterResponse != null &&
                            response.SourceAdapterResponse.Status == OperationStatus.Succeeded)
                        {
                            LogManager.LogMessage(string.Format(ABATS.AppsTalk.Runtime.Properties.Resources.Message_Integration_Process_ConsumeSource_Succeeded,
                                                                response.IntegrationProcessMetadata.IntegrationProcessCode,
                                                                response.SourceAdapterResponse.Results.Count.SafeToString("0")));

                            #region Publish To Destination

                            pushToDestinationRequest = new PushToDestinationRequest(
                                pProcessExecutionRequest.RequestDate, pProcessExecutionRequest.RequestUser);

                            #region Mapping

                            processMapping = base.AppRuntime.DataService.GetAll(
                                DataUtilities.BuildIntegrationProcessMappingGetDataRequest(response.IntegrationProcessMetadata)).ToList();
                            destinationTable = new DataTable();
                            mappingInfos     = new List <FieldMappingInfo>();

                            foreach (IntegrationAdapterField destinationAdapterField in
                                     response.IntegrationProcessMetadata.DestinationIntegrationAdapter.IntegrationAdapterFields)
                            {
                                var mappingField = processMapping.FirstOrDefault(item =>
                                                                                 item.DestinationIntegrationAdapterFieldID == destinationAdapterField.IntegrationAdapterFieldID);

                                if (mappingField != null)
                                {
                                    mappingInfos.Add(new FieldMappingInfo()
                                    {
                                        SourceField      = mappingField.SourceIntegrationAdapterField,
                                        DestinationField = destinationAdapterField,
                                        MappingDetail    = mappingField
                                    });

                                    destinationTable.Columns.Add(destinationAdapterField.FieldName);
                                }
                            }

                            #endregion

                            #region Caching

                            // Handle the Integration Adapter Cahce
                            List <AdapterCacheResult> sourceQueryCahce = null;

                            using (AdapterCacheManager queryCacheManager = new AdapterCacheManager(base.AppRuntime))
                            {
                                sourceQueryCahce = queryCacheManager.BuildAdapterCacheResults(response.SourceAdapterResponse);
                            }

                            LogManager.LogMessage(string.Format(ABATS.AppsTalk.Runtime.Properties.Resources.Message_Integration_Process_Cache_Prepration_Completed,
                                                                sourceQueryCahce != null ? sourceQueryCahce.Count.ToString() : "0"));

                            foreach (AdapterCacheResult cacheItem in sourceQueryCahce)
                            {
                                try
                                {
                                    DBRecordInfo destinationRecord = this.FieldMappingManager.BuildDestinationRecord(
                                        destinationTable, cacheItem.DBRecord, mappingInfos);

                                    if (destinationRecord != null)
                                    {
                                        pushToDestinationRequest.AdapterCacheResults.Add(new AdapterCacheResult()
                                        {
                                            DBRecord      = destinationRecord,
                                            DBRecordCache = cacheItem.DBRecordCache
                                        });
                                    }
                                }
                                catch (Exception ex)
                                {
                                    string extraMessage = string.Format("Record Keys: {0} - Parameter: {1}",
                                                                        cacheItem.DBRecord != null ? cacheItem.DBRecord.DbRecordKey : "NO KEYS",
                                                                        cacheItem.DBRecord != null ? cacheItem.DBRecord.ExceptionExtraMessage : string.Empty);

                                    LogManager.LogException(ex, extraMessage);
                                }
                            }

                            #endregion

                            #region Destination Adapter Push Request

                            using (AbstractAdapter destinationAdapter = AdapterFactory.CreateAdapter(
                                       response.IntegrationProcessMetadata,
                                       response.IntegrationProcessMetadata.DestinationIntegrationAdapter,
                                       base.AppRuntime))
                            {
                                response.DestinationAdapterResponse = destinationAdapter.PublishToDestination(
                                    pushToDestinationRequest);

                                if (response.DestinationAdapterResponse != null &&
                                    response.DestinationAdapterResponse.Status == OperationStatus.Succeeded)
                                {
                                    response.Status  = OperationStatus.Succeeded;
                                    response.Message = string.Format(Properties.Resources.Message_Integration_Process_Execution_Succeeded,
                                                                     response.IntegrationProcessMetadata.IntegrationProcessCode);
                                }
                                else
                                {
                                    response.Status  = OperationStatus.Failed;
                                    response.Message = string.Format(Properties.Resources.Message_Integration_Process_Execution_Failed,
                                                                     response.IntegrationProcessMetadata.IntegrationProcessCode);
                                }
                            }

                            #endregion

                            #endregion
                        }
                        else
                        {
                            response.Status  = OperationStatus.Failed;
                            response.Message = string.Format(Properties.Resources.Message_Consume_Source_Failed,
                                                             response.IntegrationProcessMetadata.IntegrationProcessCode);
                        }
                    }
                    else
                    {
                        response.Status  = OperationStatus.Failed;
                        response.Message = string.Format(Properties.Resources.Message_Metadata_Validation_Failed,
                                                         response.IntegrationProcessMetadata.IntegrationProcessCode, Environment.NewLine, outputMessage);
                    }
                }
                else
                {
                    response.Status  = OperationStatus.Failed;
                    response.Message = string.Format(Properties.Resources.Message_Fetch_integration_Process_Metadata_Failed,
                                                     pProcessExecutionRequest.ProcessCode);
                }
            }
            catch (Exception ex)
            {
                LogManager.LogException(ex);
            }
            finally
            {
                if (response != null && !string.IsNullOrEmpty(response.Message))
                {
                    LogManager.LogMessage(response.Message, response.Status);
                }

                if (response != null)
                {
                    //Log Integration Transaction
                    using (TransactionLogManager transactionLogManager = new TransactionLogManager(base.AppRuntime))
                    {
                        transactionLogManager.LogIntegrationProcessTransaction(response);
                    }
                }

                outputMessage = null;
            }

            return(response);
        }