예제 #1
0
        /// <summary>
        /// ProcessSolicit
        /// </summary>
        public void ProcessSolicit(string requestId)
        {
            IDocumentManager documentManager;
            IRequestManager  requestManager;

            AppendAuditLogEvent("Enter SolicitFilesFromFolder.ProcessSolicit({0})",
                                requestId);

            GetServiceImplementation(out requestManager);
            GetServiceImplementation(out documentManager);

            string sourceFolderPath;

            if (!ConfigurationArguments.TryGetValue(SOURCE_FOLDER_PATH, out sourceFolderPath))
            {
                throw new ArgumentException("SOURCE_FOLDER_PATH configuration key not set");
            }
            if (!Directory.Exists(sourceFolderPath))
            {
                throw new DirectoryNotFoundException(string.Format("SOURCE_FOLDER_PATH does not exist: {0}",
                                                                   sourceFolderPath));
            }
            DataRequest dataRequest   = requestManager.GetDataRequest(requestId);
            int         docCount      = 1;
            DateTime    docsNewerThan = DateTime.MinValue;
            string      stateId       = null;

            if (dataRequest.Parameters.IsByName)
            {
                TryGetParameterByName(dataRequest, PARAM_STATE_ID, ref stateId);
                TryGetParameterByName(dataRequest, PARAM_DOCUMENT_COUNT, ref docCount);
                TryGetParameterByName(dataRequest, PARAM_DOCUMENTS_NEWER_THAN, ref docsNewerThan);
            }
            else
            {
                TryGetParameterByIndex(dataRequest, 0, ref stateId);
                TryGetParameterByIndex(dataRequest, 1, ref docCount);
                TryGetParameterByIndex(dataRequest, 2, ref docsNewerThan);
            }
            if (!string.IsNullOrEmpty(stateId))
            {
                sourceFolderPath = Path.Combine(sourceFolderPath, stateId);
                if (!Directory.Exists(sourceFolderPath))
                {
                    throw new DirectoryNotFoundException(string.Format("State folder path does not exist: {0}",
                                                                       stateId));
                }
            }
            if (docCount < 0)
            {
                throw new ArgumentException("Invalid document count specified: {0}", docCount.ToString());
            }
            if (docsNewerThan > DateTime.Now.AddHours(2))
            {
                throw new ArgumentException("Invalid documents-newer-than date specified: {0}", docsNewerThan.ToString());
            }

            AppendAuditLogEvent("PARAM_DOCUMENT_COUNT ({0}), PARAM_DOCUMENTS_NEWER_THAN ({1})",
                                docCount.ToString(), docsNewerThan.ToString());

            List <KeyValuePair <string, DateTime> > docPaths = new List <KeyValuePair <string, DateTime> >();

            using (FileSystemEnumerator enumerator = new FileSystemEnumerator(sourceFolderPath, "*", false,
                                                                              FileSystemEnumerator.EReturnTypes.eReturnFiles))
            {
                foreach (string path in enumerator)
                {
                    // Check date
                    DateTime fileTime = File.GetLastWriteTime(path);
                    if (fileTime > docsNewerThan)
                    {
                        docPaths.Add(new KeyValuePair <string, DateTime>(path, fileTime));
                    }
                }
                if (docPaths.Count > 0)
                {
                    docPaths.Sort(delegate(KeyValuePair <string, DateTime> x, KeyValuePair <string, DateTime> y)
                    {
                        return(y.Value.CompareTo(x.Value));
                    });
                    if (docPaths.Count > docCount)
                    {
                        if (docCount > 0)
                        {
                            // Only return the # requested
                            docPaths.RemoveRange(docCount, docPaths.Count - docCount);
                        }
                    }
                }
            }
            if (docPaths.Count > 0)
            {
                AppendAuditLogEvent("Found {0} document(s) to add to solicit request",
                                    docPaths.Count.ToString());
                documentManager.AddDocument(dataRequest.TransactionId, CommonTransactionStatusCode.Processed,
                                            string.Empty, GetSolictZipFile(docPaths));
            }
            else
            {
                AppendAuditLogEvent("Didn't find any documents to add to solicit request");
            }

            AppendAuditLogEvent("Exit SolicitFilesFromFolder.ProcessSolicit({0})",
                                requestId);
        }
예제 #2
0
        /// <summary>
        /// ProcessSubmit
        /// </summary>
        public void ProcessSubmit(string transactionId)
        {
            ITransactionManager transactionManager;
            ISettingsProvider   settingsProvider;
            IDocumentManager    documentManager;

            AppendAuditLogEvent("Enter CopySubmitFilesToFolder.ProcessSubmit({0})",
                                transactionId);

            GetServiceImplementation(out transactionManager);
            GetServiceImplementation(out settingsProvider);
            GetServiceImplementation(out documentManager);

            string destinationFolderPath;

            if (!ConfigurationArguments.TryGetValue(DESTINATION_FOLDER_PATH, out destinationFolderPath))
            {
                throw new ArgumentException("DESTINATION_FOLDER_PATH configuration key not set");
            }
            if (!Directory.Exists(destinationFolderPath))
            {
                throw new DirectoryNotFoundException(string.Format("DESTINATION_FOLDER_PATH does not exist: {0}",
                                                                   destinationFolderPath));
            }
            bool createUsernameSubfolder = false;

            {
                string usernameSubfolder;
                if (ConfigurationArguments.TryGetValue(USE_USERNAME_SUBFOLDER, out usernameSubfolder) &&
                    !string.IsNullOrEmpty(usernameSubfolder))
                {
                    try
                    {
                        createUsernameSubfolder = bool.Parse(usernameSubfolder);
                    }
                    catch (Exception)
                    {
                        throw new FormatException(string.Format("Could not parse USE_USERNAME_SUBFOLDER true/false value: {0}",
                                                                usernameSubfolder));
                    }
                }
            }

            if (createUsernameSubfolder)
            {
                string username = transactionManager.GetTransactionUsername(transactionId);
                if (string.IsNullOrEmpty(username))
                {
                    throw new ArgumentException(string.Format("Could not username for transaction: {0}",
                                                              transactionId));
                }
                username = FileUtils.ReplaceInvalidFilenameChars(username, '_');
                destinationFolderPath = Path.Combine(destinationFolderPath, username);
            }

            AppendAuditLogEvent("DESTINATION_FOLDER_PATH: \"{0}\"", destinationFolderPath);

            IList <string> dbDocIds = transactionManager.GetAllUnprocessedDocumentDbIds(transactionId);

            if (!CollectionUtils.IsNullOrEmpty(dbDocIds))
            {
                if (!Directory.Exists(destinationFolderPath))
                {
                    Directory.CreateDirectory(destinationFolderPath);
                    AppendAuditLogEvent("Created folder DESTINATION_FOLDER_PATH: \"{0}\"",
                                        destinationFolderPath);
                }
                foreach (string dbDocId in dbDocIds)
                {
                    Document document = documentManager.GetDocument(transactionId, dbDocId, true);
                    string   docName;
                    if (!string.IsNullOrEmpty(document.DocumentName))
                    {
                        docName = document.DocumentName;
                    }
                    else if (!string.IsNullOrEmpty(document.DocumentId))
                    {
                        docName = document.DocumentName;
                    }
                    else
                    {
                        docName = dbDocId;
                    }
                    docName = FileUtils.ReplaceInvalidFilenameChars(docName, '_');
                    docName =
                        Path.ChangeExtension(docName, CommonContentAndFormatProvider.GetFileExtension(document.Type));
                    string copyPath = FileUtils.MakeUniqueIncrementalFilePath(destinationFolderPath, docName);
                    File.WriteAllBytes(copyPath, document.Content);
                    AppendAuditLogEvent("Copied file \"{0}\"", copyPath);
                    documentManager.SetDocumentStatus(transactionId, dbDocId, CommonTransactionStatusCode.Processed,
                                                      "Processed " + dbDocId);
                }
            }

            AppendAuditLogEvent("Exit CopySubmitFilesToFolder.ProcessSubmit({0})",
                                transactionId);
        }