Exemplo n.º 1
0
 public TransferManager(IAdaperSetting setting)
 {
     this.TransferAdapter = FileTransferAdapter.GetFileTransferAdapter(setting);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Job Init processing
        /// </summary>
        /// <param name="sessionKey">Service generated GUID</param>
        /// <param name="appId">Application ID</param>
        /// <returns></returns>
        public List <RunNumberAndOutput> Processing(string sessionKey, int appId)
        {
            List <RunNumberAndOutput> objRunNumberAndOutputList = new List <RunNumberAndOutput>();

            emailList = new List <string>();
            try
            {
                String desLoc = String.Empty;
                SingletonLogger.Instance.Debug("Get application info");
                //Get application by id
                var appInfo = objApplicationRepository.Find(appId);
                if (appInfo == null)
                {
                    throw new NullReferenceException("Application details not found in database for Application Id : " + appId);
                }
                //Get applications Client information
                var clientInfo = objClientRepository.Find(appInfo.ClientId);
                if (clientInfo == null)
                {
                    throw new NullReferenceException("Client details not found in database for Client Id : " + appInfo.ClientId);
                }

                SingletonLogger.Instance.Debug("Client Id = " + clientInfo.ClientId + "\t Application Id : " + appId);
                SingletonLogger.Instance.Debug("Client Name = " + clientInfo.Name + "\t Application name : " + appInfo.Name);

                SingletonLogger.Instance.Debug("Adding Mapper...");
                mapper = new PatternMatchingMapper();
                mapper.SetCurrentDateFormat();
                mapper.SetClientAndAppDetails(clientInfo.Name, appInfo.Name);
                SingletonLogger.Instance.Debug("Mapper Added.");

                SingletonLogger.Instance.Debug("Get file transfer settings for application for File by Setting Id " + appInfo.FileTransferSettingId);
                var fileTransferSetting = objFileTransferSettingsRepository.Find(appInfo.FileTransferSettingId);
                if (fileTransferSetting == null)
                {
                    throw new Exception("No file transfer setting details found for File transfersettingId " + appInfo.FileTransferSettingId);
                }

                SingletonLogger.Instance.Debug("Get queue type for file transfer setting id " + fileTransferSetting.QueueTypeId);

                // Converting queue type from string, if not able to convert throw exception
                SingletonLogger.Instance.Debug("Checking for queue type.");
                QuequeType type = (QuequeType)Enum.ToObject(typeof(QuequeType), fileTransferSetting.QueueTypeId);
                SingletonLogger.Instance.Debug("Queue type is : " + type.ToString());

                var applicationFiles = objApplicationFileRepository.GetApplicationFileListByAppID(appInfo.ApplicationId);
                if (applicationFiles == null)
                {
                    throw new NullReferenceException("No app files (Input configuration) found in db for appId : " + appInfo.ApplicationId + "appName : " + appInfo.Name);
                }

                if (applicationFiles.Count() == 0)
                {
                    throw new InvalidDataException("No app files (Input configuration) found in db for appId : " + appInfo.ApplicationId + "appName : " + appInfo.Name);
                }

                SingletonLogger.Instance.Debug(string.Format("{0} files found for current application. ", applicationFiles.Count()));

                SingletonLogger.Instance.Debug("Processing files...");
                var validationType = ValidationType.Default;
                if (appInfo.IsBatch)
                {
                    validationType = ValidationType.Batch;
                }

                var adaptorSettings = SetLocationAdptorSettings(fileTransferSetting, appInfo, type);

                if (adaptorSettings == null)
                {
                    throw new NullReferenceException("Null validation adapter setting found when getting valid adapter.");
                }

                // Initializing transfer manager
                IFileTransferAdapter transferAdaptor = FileTransferAdapter.GetFileTransferAdapter(adaptorSettings);

                if (transferAdaptor == null)
                {
                    throw new NullReferenceException("Null transfer adapter found.");
                }

                FileTransferManager transferManager = new FileTransferManager(transferAdaptor);

                if (transferManager == null)
                {
                    throw new NullReferenceException("Null transfer manager found.");
                }

                SingletonLogger.Instance.Debug("validationType : " + validationType + "applicationFiles :" + applicationFiles.FirstOrDefault() + " transferManager :" + transferManager);

                IValidationPlugin validationAdaptor = GetValidationAdptor(validationType, applicationFiles.ToArray(), transferManager);

                if (validationAdaptor == null)
                {
                    throw new NullReferenceException("Null validation adapter found when getting valid adapter.");
                }

                // Validate
                if (validationAdaptor.Validate())
                {
                    SingletonLogger.Instance.Debug("file is  Validated by " + validationAdaptor + " now Ready to download");
                    int count = 1;
                    // If There are some valid files then insert run number information to DB.
                    if (validationAdaptor.ValidFiles.Count > 0 && validationAdaptor.Ready)
                    {
                        foreach (var file in validationAdaptor.ValidFiles)
                        {
                            RunNumberAndOutput objRunNumberAndOutput = new RunNumberAndOutput();

                            #region Create Job
                            switch (validationType)
                            {
                            case ValidationType.Default:
                                //Create run number and add entry in database
                                runNumber = GetRunNumber(clientInfo.Code, appInfo.Code, appId);
                                SingletonLogger.Instance.Debug("file is  Run in Single processing. RunNumber = " + runNumber);
                                runId = InsertRunDetails(appInfo.ApplicationId, runNumber);
                                SingletonLogger.Instance.Debug("Run details is saved in db successfully.");
                                break;

                            case ValidationType.Batch:
                                //Create run number and add entry in database
                                if (count == 1)
                                {
                                    runNumber = GetRunNumber(clientInfo.Code, appInfo.Code, appId);
                                    runId     = InsertRunDetails(appInfo.ApplicationId, runNumber);
                                }
                                SingletonLogger.Instance.Debug("Insert runDetails in db for RunNumber " + runNumber + " Client " + clientInfo.Name + " and Application " + appInfo.Name);
                                break;

                            case ValidationType.Custom:
                                throw new NotSupportedException("Functionality for custom validation not supported.");

                            default:
                                throw new InvalidOperationException("Invalid Validation type found.");
                            }
                            #endregion

                            #region Countinue Common Processing
                            mapper.SetClientAndAppDetails(runNumber);
                            if (String.IsNullOrEmpty(appInfo.HotFolder))
                            {
                                throw new Exception("Destination location is null or empty for " + appInfo.Name);
                            }

                            desLoc = mapper.EvaluateString(appInfo.HotFolder);
                            SingletonLogger.Instance.Debug("file is  downloading at " + desLoc);
                            transferAdaptor.Settings.DestinationLocation = desLoc;

                            // Add Run number and destination location
                            objRunNumberAndOutput.Output = desLoc;

                            #endregion

                            #region Download files
                            // download valid files
                            if (transferManager.DownloadFile(file))
                            {
                                SingletonLogger.Instance.Debug(file + " file is  downloaded successfully.");
                                //Inserting details to db
                                if (InsertRawFileDetails(runId, file, desLoc))
                                {
                                    SingletonLogger.Instance.Debug("file detail is saved in db in raw files");
                                    if (appInfo.IsArchive)
                                    {
                                        var arcvInpuFile = desLoc + "\\" + Path.GetFileName(file);
                                        mapper.SetFileFormat(arcvInpuFile);
                                        var outFile = mapper.EvaluateString(appInfo.ArchivePath + "\\" + appInfo.ArchiveFileName);
                                        Archieve(arcvInpuFile, outFile);
                                    }

                                    if (appInfo.IsFileMove)
                                    {
                                        try
                                        {
                                            SingletonLogger.Instance.Debug("Process start to delete file from " + file);
                                            transferManager.DeleteFile(file);
                                            SingletonLogger.Instance.Debug(file + " file is deleted from source location.");
                                        }
                                        catch (Exception ex)
                                        {
                                            throw ex;
                                        }
                                    }
                                }
                            }
                            #endregion

                            #region Create Run number and output List
                            switch (validationType)
                            {
                            case ValidationType.Default:        // Run each file as a single file
                                objRunNumberAndOutput.RunNumber = runNumber;
                                objRunNumberAndOutputList.Add(objRunNumberAndOutput);
                                break;

                            case ValidationType.Batch:
                                if (count == 1)       // If file downloading type is 'Batch' then no need to add directory in array, run file as a batch
                                {
                                    objRunNumberAndOutput.RunNumber = runNumber;
                                    objRunNumberAndOutputList.Add(objRunNumberAndOutput);
                                }
                                break;

                            case ValidationType.Custom:
                                throw new NotSupportedException("Functionality for custom validation not supported.");

                            default:
                                throw new InvalidOperationException("Invalid Validation type found.");
                            }
                            #endregion

                            #region Email

                            emailList.Add(Path.GetFileName(file));

                            #endregion

                            count++;
                        }
                    }
                    else
                    {
                        SingletonLogger.Instance.Debug("No valid files found or Files are not ready to download.");
                        objProcSessionsRepository.UpdateBySessionKey(sessionKey, Convert.ToByte(JobStatusType.Complete));
                    }
                    objProcSessionsRepository.UpdateBySessionKey(sessionKey, Convert.ToByte(JobStatusType.Complete));
                    SingletonLogger.Instance.Debug("Update values in ProcSession corresponding key" + sessionKey + " Value" + Convert.ToByte(JobStatusType.Complete));
                }

                SingletonLogger.Instance.Debug("No valid files found or Files are not ready to download.");
                objProcSessionsRepository.UpdateBySessionKey(sessionKey, Convert.ToByte(JobStatusType.Complete));

                //Prepare for email
                SendInputEmail inputEmail = new SendInputEmail();
                if (emailList != null)
                {
                    if (emailList.Count > 0)
                    {
                        inputEmail.SendInputFileEmail(appInfo, runId, emailList);
                    }
                }
                SingletonLogger.Instance.Debug("Email has been sent");
                return(objRunNumberAndOutputList);
            }
            catch (Exception ex)
            {
                objProcSessionsRepository.UpdateBySessionKey(sessionKey, Convert.ToByte(JobStatusType.Error));
                throw new Exception("Error in Job-init " + ex);
            }
        }
Exemplo n.º 3
0
        public bool StartProcessing()
        {
            try
            {
                var applicationList = _applicationRepository.FindAllActive();
                SingletonLogger.Instance.Debug(applicationList.Count() + " application found in DB");
                var procComponent = _procComponentRepository.GetFirstRecord();
                var list          = applicationList.GroupBy(x => new { x.FileTransferSettingId, x.InputPath });
                SingletonLogger.Instance.Debug(list.Count() + " distinct input path of application");
                _inputFileList = new List <string>();
                foreach (var item in list)
                {
                    try
                    {
                        var fileTransferSetting = _fileTransferRepository.Find(item.Key.FileTransferSettingId);
                        _setting         = SetAdaptorSetting(fileTransferSetting);
                        _transferAdapter = FileTransferAdapter.GetFileTransferAdapter(_setting);
                        var fileList = _transferAdapter.ShowFileList(item.Key.InputPath);
                        SingletonLogger.Instance.Debug(fileList.Count + " files found at " + item.Key.InputPath);
                        foreach (var subitem in fileList)
                        {
                            Console.WriteLine("Files : " + subitem);
                            try
                            {
                                SingletonLogger.Instance.Debug("Checking file accessibility of " + subitem);
                                if (_transferAdapter.IsAccessible(subitem))
                                {
                                    _inputFileList.Add(Path.GetFileName(subitem));
                                }
                            }
                            catch (Exception ex)
                            {
                                SingletonLogger.Instance.Debug(ex.Message);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        SingletonLogger.Instance.Debug(ex.Message);
                    }
                }

                SingletonLogger.Instance.Debug(_inputFileList.Count + " file found ready to download");
                if (_inputFileList.Count() == 0)
                {
                    return(true);
                }

                var appIdList = AppIdsbyMasks(_inputFileList.ToList());
                SingletonLogger.Instance.Debug(appIdList.Count() + " distinct application found for " + _inputFileList.Count + " file");
                foreach (var item in appIdList)
                {
                    var  frequencyList = _scheduledRepository.GetScheduledFrequencyListbyAppId(item);
                    var  currentTime   = DateTime.Now.TimeOfDay;
                    bool IsValidTime   = true;
                    foreach (var subItem in frequencyList)
                    {
                        if (subItem.StartTime < currentTime && subItem.EndTime > currentTime)
                        {
                            IsValidTime = true;
                            break;
                        }
                        else
                        {
                            IsValidTime = false;
                        }
                    }
                    if (IsValidTime)
                    {
                        SingletonLogger.Instance.Debug("ApplicationId = " + item + " has been started for processing");
                        if (InstanceLimitAvailable(item, procComponent.ProcComponentId, procComponent.LicenceLimit))
                        {
                            SingletonLogger.Instance.Debug("License available for applicationId = " + item);
                            int    processId;
                            String sessionKey = LaunchInstance(procComponent.ExecutablePath, item, out processId);
                            SingletonLogger.Instance.Debug("Process has been launched for " + item);
                            var procSession = new ProcSession
                            {
                                CreatedAt        = DateTimeOffset.Now,
                                ExpectedDateTime = DateTime.Now.AddMinutes(30),
                                KillRequired     = false,
                                ProcComponentID  = procComponent.ProcComponentId,
                                ProcessID        = Convert.ToString(processId),
                                ApplicationId    = item,
                                SessionKey       = sessionKey,
                                ProcStatus       = 1,
                                Status           = true
                            };
                            if (_procSessionRepository.Save(procSession) > 0)
                            {
                                SingletonLogger.Instance.Debug("New record of proc session updated in database");
                            }
                        }
                        else
                        {
                            SingletonLogger.Instance.Debug("License doesn't available for applicationId = " + item);
                        }
                    }
                    else
                    {
                        SingletonLogger.Instance.Debug("Frequency time doesn't match for application.");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(true);
        }