Пример #1
0
 public bool DownloadAllFiles()
 {
     try
     {
         foreach (string file in AllFiles)
         {
             transferManager.DownloadFile(file);
         }
         return(true);
     }
     catch (Exception ex)
     {
         throw new Exception("Error to download all files in Batch validation Adptor" + ex);
     }
 }
Пример #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);
            }
        }