/// <summary>
 ///
 /// </summary>
 /// <param name="dA"></param>
 /// <param name="cC"></param>
 /// <param name="userID"></param>
 /// <param name="docInfo"></param>
 /// <param name="errText"></param>
 /// <returns></returns>
 public static bool AddPawnDocument(
     OracleDataAccessor dA,
     SecuredCouchConnector cC,
     string userID,
     ref PawnDocInfo docInfo,
     out string errText)
 {
     return(AddPawnDocument(dA, cC, userID, null, null, ref docInfo, out errText));
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="dA"></param>
        /// <param name="cC"></param>
        /// <param name="searchInfo"></param>
        /// <param name="retrieveDocFlag">If set to true, will retrieve document data from CouchDB server</param>
        /// <param name="documents"></param>
        /// <param name="errText"></param>
        /// <returns></returns>
        public static bool GetPawnDocument(
            OracleDataAccessor dA,
            SecuredCouchConnector cC,
            PawnDocInfo searchInfo,
            bool retrieveDocFlag,
            out List <PawnDocInfo> documents,
            out string errText)
        {
            errText   = string.Empty;
            documents = new List <PawnDocInfo>(1);

            //Verify inputs
            if (dA == null || dA.Initialized == false)
            {
                errText = "GetPawnDocument: Database accessor is invalid.";
                if (FileLogger.Instance.IsLogError)
                {
                    FileLogger.Instance.logMessage(LogLevel.ERROR, "CouchDbUtils::GetPawnDocument", errText);
                }
                return(false);
            }

            //Search type must be specified
            if (cC == null || searchInfo == null || string.IsNullOrEmpty(searchInfo.DocumentSearchType))
            {
                errText = "GetPawnDocument: Couch DB Accessor info is invalid.";
                if (FileLogger.Instance.IsLogError)
                {
                    FileLogger.Instance.logMessage(LogLevel.ERROR, "CouchDbUtils::GetPawnDocument", errText);
                }
                return(false);
            }

            //Log search type
            if (FileLogger.Instance.IsLogDebug)
            {
                FileLogger.Instance.logMessage(LogLevel.DEBUG,
                                               "CouchDbUtils::GetPawnDocument",
                                               "Doc search string = {0}", searchInfo.DocumentSearchType);
            }

            string srchID01 = string.Empty;
            string srchID02 = string.Empty;

            //Switch search type
            switch (searchInfo.DocSearchEnumType)
            {
            case DocSearchType.STORAGE:
                srchID01 = searchInfo.StorageId;
                break;

            case DocSearchType.RECEIPT:
                srchID01 = searchInfo.ReceiptNumber.ToString();
                break;

            case DocSearchType.CUSTOMER:
                srchID01 = searchInfo.CustomerNumber;
                break;

            case DocSearchType.STORE_TICKET:
            case DocSearchType.POLICE_CARD:
                srchID01 = searchInfo.StoreNumber;
                srchID02 = searchInfo.TicketNumber.ToString();
                searchInfo.DocSearchEnumType = DocSearchType.STORE_TICKET;

                break;

            case DocSearchType.RELEASE_FINGERPRINTS:
                srchID01 = searchInfo.StoreNumber;
                srchID02 = searchInfo.TicketNumber.ToString();
                searchInfo.DocSearchEnumType = DocSearchType.STORE_TICKET;
                break;

            case DocSearchType.STORE_CUSTOMER:
                srchID01 = searchInfo.StoreNumber;
                srchID02 = searchInfo.CustomerNumber;
                break;

            case DocSearchType.INVALID:
                break;
            }

            //Ensure that at least one of the search constraints
            //have been specified
            if (string.IsNullOrEmpty(srchID01) &&
                string.IsNullOrEmpty(srchID02))
            {
                errText = "Not enough information specified to " +
                          "search for document";
                if (FileLogger.Instance.IsLogError)
                {
                    FileLogger.Instance.logMessage(LogLevel.ERROR,
                                                   "CouchDbUtils::GetPawnDocument",
                                                   errText);
                }
            }

            //Retrieve document registry information
            string    spErrCode, spErrText;
            DataTable dataTable;

            //string nowTime = DateTime.Now.FormatDate();
            //Fix to base the time the document search is performed on the current shop date time
            var searchTime = ShopDateTime.Instance.FullShopDateTime.FormatDate();

            if (!GetPawnDocumentRegistry(
                    dA,
                    srchID01,
                    srchID02,
                    "0", //TODO: Disabled doc chain fetch for now
                    "0", //TODO: Disabled date range fetch for now
                    searchInfo.DocumentSearchType,
                    searchTime,
                    searchTime,
                    out dataTable,
                    out spErrCode,
                    out spErrText))
            {
                errText =
                    "GetPawnDocument: Could not retrieve document registry info. " +
                    "Registry error code: " + spErrCode + ", error text: " + spErrText;
                if (FileLogger.Instance.IsLogError)
                {
                    FileLogger.Instance.logMessage(LogLevel.ERROR, "CouchDbUtils::GetPawnDocument", errText);
                }
                return(false);
            }

            if (dataTable == null || dataTable.Rows == null || dataTable.Rows.Count <= 0)
            {
                errText = "GetPawnDocument: Data table returned from registry is invalid.";
                if (FileLogger.Instance.IsLogError)
                {
                    FileLogger.Instance.logMessage(LogLevel.ERROR, "CouchDbUtils.GetPawnDocument", errText);
                }
                return(false);
            }

            foreach (DataRow dR in dataTable.Rows)
            {
                //Validate data row
                if (dR == null || dR.HasErrors || dR.ItemArray.Length <= 0)
                {
                    continue;
                }

                //Set data from data table
                searchInfo.StorageId    = Utilities.GetStringValue(dR["storage_id"], string.Empty);
                searchInfo.StoreNumber  = Utilities.GetStringValue(dR["storenumber"], string.Empty);
                searchInfo.TicketNumber = Utilities.GetIntegerValue(dR["ticket_number"], 0);
                //GJL - 6/15/2010 - Column for receipt detail will now hold receipt number
                searchInfo.ReceiptNumber  = Utilities.GetIntegerValue(dR["receiptdetail_number"], 0);
                searchInfo.CustomerNumber = Utilities.GetStringValue(dR["customernumber"], string.Empty);
                searchInfo.AuxInfo        = Utilities.GetStringValue(dR["storage_auxinf"], string.Empty);
                searchInfo.DocumentType   = (Document.DocTypeNames)
                                            Enum.Parse(typeof(Document.DocTypeNames), Utilities.GetStringValue(dR["doc_type"], "INVALID"));

                //If retrieve document flag is true, make the call to the couch server
                Document doc = null;
                if (retrieveDocFlag)
                {
                    //Get next document from couch DB server
                    if (!CouchDbUtils.GetDocument(
                            searchInfo.StorageId,
                            cC,
                            out doc,
                            out errText))
                    {
                        errText =
                            "GetPawnDocument: Could not get the document from the storage server.";
                        if (FileLogger.Instance.IsLogError)
                        {
                            FileLogger.Instance.logMessage(LogLevel.ERROR,
                                                           "CouchDbUtils::GetPawnDocument",
                                                           errText);
                        }

                        //If the couch retrieval fails, do not add an entry to returned
                        //document list
                        continue;
                    }
                }

                //Create doc info container
                var newInfo = new PawnDocInfo
                {
                    StorageId      = searchInfo.StorageId,
                    StoreNumber    = searchInfo.StoreNumber,
                    TicketNumber   = searchInfo.TicketNumber,
                    ReceiptNumber  = searchInfo.ReceiptNumber,
                    CustomerNumber = searchInfo.CustomerNumber,
                    AuxInfo        = searchInfo.AuxInfo,
                    DocumentType   = searchInfo.DocumentType,
                    DocumentObject = doc
                };
                newInfo.SetDocumentSearchType(searchInfo.DocSearchEnumType);
                documents.Add(newInfo);
            }

            return(true);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="dA"></param>
        /// <param name="cC"></param>
        /// <param name="userID"></param>
        /// <param name="metaData"></param>
        /// <param name="auxInfo"></param>
        /// <param name="docInfo"></param>
        /// <param name="errText"></param>
        /// <returns></returns>
        public static bool AddPawnDocument(
            OracleDataAccessor dA,
            SecuredCouchConnector cC,
            string userID,
            Dictionary <string, string> metaData,
            string auxInfo,
            ref PawnDocInfo docInfo,
            out string errText)
        {
            errText = string.Empty;
            if (dA == null || dA.Initialized == false)
            {
                errText = "AddPawnDocument: Database accessor is invalid.";
                if (FileLogger.Instance.IsLogError)
                {
                    FileLogger.Instance.logMessage(LogLevel.ERROR, "CouchDbUtils::AddPawnDocument", errText);
                }
                return(false);
            }
            if (cC == null || docInfo == null || string.IsNullOrEmpty(userID))
            {
                errText = "AddPawnDocument: Couch DB Accessor info is invalid.";
                if (FileLogger.Instance.IsLogError)
                {
                    FileLogger.Instance.logMessage(LogLevel.ERROR, "CouchDbUtils::AddPawnDocument", errText);
                }
                return(false);
            }

            //Generate storage id if the incoming storage id is blank
            if (string.IsNullOrEmpty(docInfo.StorageId))
            {
                docInfo.StorageId = System.Guid.NewGuid().ToString();
                FileLogger.Instance.logMessage(LogLevel.INFO, "Storage Id of the document to store in couch db", docInfo.StorageId);
            }

            //Validate other document info properties
            if (string.IsNullOrEmpty(docInfo.DocFileName))
            {
                errText = "AddPawnDocument: Must specify a valid file name";
                if (FileLogger.Instance.IsLogError)
                {
                    FileLogger.Instance.logMessage(LogLevel.ERROR, "CouchDbUtils::AddPawnDocument", errText);
                    return(false);
                }
            }

            //Send document to couch DB server
            Document doc;

            if (!CouchDbUtils.StoreDocument(
                    docInfo.DocFileName,
                    docInfo.StorageId,
                    cC, docInfo.OverrideDocument,
                    docInfo.DocumentType,
                    metaData, out doc, out errText))
            {
                errText = "AddPawnDocument: Could not add the document to the storage server." + errText;
                if (FileLogger.Instance.IsLogError)
                {
                    FileLogger.Instance.logMessage(LogLevel.ERROR, "CouchDbUtils::AddPawnDocument", errText);
                }
                return(false);
            }

            //Store document registry info in the database
            //DateTime storeDate, storeTime;
            if (docInfo.UseCurrentShopDateTime)
            {
                DateTime sDate = ShopDateTime.Instance.ShopDate;
                docInfo.StorageDate = sDate.FormatDate();
                docInfo.StorageTime = ShopDateTime.Instance.ShopTransactionTime;
            }

            string spErrCode, spErrText;

            if (!AddPawnDocumentRegistry(dA,
                                         docInfo.StorageId,
                                         docInfo.ParentStorageId,
                                         docInfo.StoreNumber,
                                         docInfo.TicketNumber,
                                         docInfo.ReceiptNumber,
                                         docInfo.CustomerNumber,
                                         docInfo.StorageDate,
                                         docInfo.StorageTime,
                                         docInfo.DocumentType.ToString(),
                                         auxInfo,
                                         userID,
                                         out spErrCode,
                                         out spErrText))
            {
                errText =
                    "AddPawnDocument: Could not register document. " +
                    "Registry error code: " + spErrCode + ", error text: " + spErrText;
                if (FileLogger.Instance.IsLogError)
                {
                    FileLogger.Instance.logMessage(LogLevel.ERROR, "CouchDbUtils.AddPawnDocument", errText);
                }
                return(false);
            }

            //Return true if we reach this point!!
            return(true);
        }