///// <summary>
        ///// It identifies whether any Extrinsic object is exists,else simply pass the Metadata
        ///// </summary>
        ///// <param name="nodeListExtrinsicDocuments"></param>
        ///// <param name="rootElement"></param>
        ///// <returns></returns>
        //public bool IsMissingDocumentMetadata(XmlNodeList nodeListExtrinsicDocuments, XmlElement rootElement)
        //{

        //    List<string> lstDocAttachmentIds = new List<string>();
        //    List<string> lstMetadatadIds = new List<string>();
        //    bool IsMissingDocumentMetadata = true;

        //    try
        //    {
        //        XmlNodeList xnlDocumentNode = rootElement.SelectNodes(@"//*[local-name()='Document']");

        //        foreach (XmlNode node in xnlDocumentNode)
        //        {
        //            lstDocAttachmentIds.Add(node.Attributes["id"].Value);
        //        }

        //        foreach (XmlNode nodeExtrinsicDocument in nodeListExtrinsicDocuments)
        //        {

        //            if (nodeExtrinsicDocument.Attributes["id"] != null)
        //            {
        //                lstMetadatadIds.Add(nodeExtrinsicDocument.Attributes["id"].Value);
        //            }
        //        }

        //        for (int attachmentCount = 0; attachmentCount < lstDocAttachmentIds.Count; attachmentCount++)
        //        {
        //            if (lstMetadatadIds.Contains(lstMetadatadIds[attachmentCount]))
        //            {
        //                IsMissingDocumentMetadata = false;
        //            }
        //            else
        //            {
        //                IsMissingDocumentMetadata = true;

        //                break;
        //            }
        //        }
        //    }
        //    catch
        //    {
        //        IsMissingDocumentMetadata = true;
        //        //throw;
        //    }


        //    return IsMissingDocumentMetadata;
        //}


        public void LogRolledBackData(DocumentRepositoryLog objDocumentRepositoryLog, string errorCode)
        {
            try
            {
                //objDocumentRepositoryLog.Result = "ErrorCode";
                objDocumentRepositoryLog.Result  = errorCode;
                objDocumentRepositoryLog.EndTime = DateTime.Now;
                int noOfRows = LogRepositoryData(objDocumentRepositoryLog);
            }
            catch
            {
                throw;
            }
        }
        public int LogRepositoryData(DocumentRepositoryLog objDocumentRepositoryLog)
        {
            //Was implemented in Service Layer,Need to Paste it here
            int noOfRows = 0;

            try
            {
                RepositoryDataAccess objRepositoryDataAccess = new RepositoryDataAccess();
                noOfRows = objRepositoryDataAccess.LogRepositoryData(objDocumentRepositoryLog);
            }
            catch
            {
                throw;
            }
            return(noOfRows);
        }
        public int LogRepositoryData(DocumentRepositoryLog objDocumentRepositoryLog)
        {
            Int32 noOfRows = 0;

            try
            {
                Database  dbIHEDB = DatabaseFactory.CreateDatabase(CONST_CONNECTIONSTRING_NAME);
                DbCommand dbLogRepositoryCommand = dbIHEDB.GetStoredProcCommand("SaveRepositoryLogData_DocumentRepositoryLog");
                dbIHEDB.AddInParameter(dbLogRepositoryCommand, "RequesterIdentity", DbType.String, objDocumentRepositoryLog.RequesterIdentity);
                dbIHEDB.AddInParameter(dbLogRepositoryCommand, "RequestMetadata", DbType.String, objDocumentRepositoryLog.RequestMetadata);
                dbIHEDB.AddInParameter(dbLogRepositoryCommand, "Transaction", DbType.String, objDocumentRepositoryLog.Transaction);
                dbIHEDB.AddInParameter(dbLogRepositoryCommand, "StartTime", DbType.DateTime, objDocumentRepositoryLog.StartTime);
                dbIHEDB.AddInParameter(dbLogRepositoryCommand, "FinishTime", DbType.DateTime, objDocumentRepositoryLog.EndTime);
                dbIHEDB.AddInParameter(dbLogRepositoryCommand, "Result", DbType.String, objDocumentRepositoryLog.Result);
                noOfRows = dbIHEDB.ExecuteNonQuery(dbLogRepositoryCommand);
            }
            catch
            {
                throw;
            }
            return(noOfRows);
        }
        Message IDocumentRepository.ProvideAndRegisterDocumentSet(Message msgRequest)
        {
            Message          msgResponse           = null;
            XmlDocument      xmlDocRequest         = null;
            XmlDocument      xmlDocResponse        = null;
            XmlNode          nodeRegistryError     = null;
            string           errorCode             = null;
            RepositoryLogic  repositoryLogic       = null;
            StringDictionary stringDictionary      = null;
            string           eventOutcomeIndicator = "0";
            string           submissionSetUniqueID = string.Empty;
            string           sourceUserID          = string.Empty;
            string           destinationUserID     = string.Empty;

            try
            {
                repositoryLogic = new RepositoryLogic();

                objDocumentRepositoryLog                   = new DocumentRepositoryLog();
                objDocumentRepositoryLog.StartTime         = DateTime.Now;
                objDocumentRepositoryLog.RequesterIdentity = Environment.UserName;

                //ATNA Event Source & Destination UserID
                sourceUserID      = GetSourceUserID();
                destinationUserID = GetDestinationUserID();

                //Request XmlDocument
                xmlDocRequest = new XmlDocument();
                xmlDocRequest.Load(msgRequest.GetReaderAtBodyContents());


                //Process Message will Construct Response for Register Transaction Set-B
                //Recieves a message from Provide and register document set B(With no errors from Repositrory)
                xmlDocResponse = ProcessProvideAndRegisterDocumentSet(xmlDocRequest, msgRequest.Headers.MessageVersion, out stringDictionary);

                //ATNA Event
                eventOutcomeIndicator = stringDictionary["$EventIdentification.EventOutcomeIndicator$"];
            }
            catch (ServerTooBusyException serverTooBusyException)
            {
                //Construct Error Response
                xmlDocResponse = CommonUtility.ConstructRegistryErrorResponse(GlobalValues.CONST_RESPONSE_STATUS_TYPE_FAILURE, string.Empty, GlobalValues.CONST_ERROR_CODE_XDSRepositoryTooBusyException, GlobalValues.CONST_ERROR_CODE_XDSRepositoryTooBusyException, GlobalValues.CONST_SEVERITY_TYPE_ERROR, string.Empty);

                nodeRegistryError = xmlDocResponse.SelectSingleNode(@"//*[local-name()='RegistryError']");
                errorCode         = nodeRegistryError.Attributes["errorCode"].Value;
                if (errorCode != null)
                {
                    repositoryLogic.LogRolledBackData(objDocumentRepositoryLog, errorCode);
                }

                //ATNA EVENT LOG
                eventOutcomeIndicator = "8";
            }
            catch (TimeoutException timeoutException)
            {
                //Construct Error Response
                xmlDocResponse = CommonUtility.ConstructRegistryErrorResponse(GlobalValues.CONST_RESPONSE_STATUS_TYPE_FAILURE, string.Empty, GlobalValues.CONST_ERROR_CODE_TimeOut, GlobalValues.CONST_ERROR_CODE_TimeOut, GlobalValues.CONST_SEVERITY_TYPE_ERROR, string.Empty);

                nodeRegistryError = xmlDocResponse.SelectSingleNode(@"//*[local-name()='RegistryError']");
                errorCode         = nodeRegistryError.Attributes["errorCode"].Value;
                if (errorCode != null)
                {
                    repositoryLogic.LogRolledBackData(objDocumentRepositoryLog, errorCode);
                }

                //ATNA EVENT LOG
                eventOutcomeIndicator = "8";
            }
            catch (System.ServiceModel.Security.SecurityAccessDeniedException AuthorizationException)
            {
                //Construct Error Response
                xmlDocResponse = CommonUtility.ConstructRegistryErrorResponse(GlobalValues.CONST_RESPONSE_STATUS_TYPE_FAILURE, string.Empty, GlobalValues.CONST_ERROR_CODE_XDSAuthorizationException, GlobalValues.CONST_ERROR_CODE_XDSAuthorizationException, GlobalValues.CONST_SEVERITY_TYPE_ERROR, string.Empty);

                nodeRegistryError = xmlDocResponse.SelectSingleNode(@"//*[local-name()='RegistryError']");
                errorCode         = nodeRegistryError.Attributes["errorCode"].Value;
                if (errorCode != null)
                {
                    repositoryLogic.LogRolledBackData(objDocumentRepositoryLog, errorCode);
                }

                //ATNA EVENT LOG
                eventOutcomeIndicator = "8";
            }
            catch (OutOfMemoryException RepositoryOutOfResources)
            {
                //Construct Error Response
                xmlDocResponse = CommonUtility.ConstructRegistryErrorResponse(GlobalValues.CONST_RESPONSE_STATUS_TYPE_FAILURE, string.Empty, GlobalValues.CONST_ERROR_CODE_XDSRepositoryOutOfResources, GlobalValues.CONST_ERROR_CODE_XDSRepositoryOutOfResources, GlobalValues.CONST_SEVERITY_TYPE_ERROR, string.Empty);

                nodeRegistryError = xmlDocResponse.SelectSingleNode(@"//*[local-name()='RegistryError']");
                errorCode         = nodeRegistryError.Attributes["errorCode"].Value;
                if (errorCode != null)
                {
                    repositoryLogic.LogRolledBackData(objDocumentRepositoryLog, errorCode);
                }

                //ATNA EVENT LOG
                eventOutcomeIndicator = "8";
            }
            catch (Exception ex)
            {
                if (ex.Message == GlobalValues.CONST_ERROR_CODE_XDSMissingDocumentMetadata)
                {
                    //Construct Error Response
                    xmlDocResponse = CommonUtility.ConstructRegistryErrorResponse(GlobalValues.CONST_RESPONSE_STATUS_TYPE_FAILURE, string.Empty, "XDSDocumentEntry document exists in metadata with no corresponding metatdata", GlobalValues.CONST_ERROR_CODE_XDSMissingDocumentMetadata, GlobalValues.CONST_SEVERITY_TYPE_ERROR, string.Empty);
                }
                else if (ex.Message == GlobalValues.CONST_ERROR_CODE_XDSMissingDocumentAttachment)
                {
                    //Construct Error Response
                    xmlDocResponse = CommonUtility.ConstructRegistryErrorResponse(GlobalValues.CONST_RESPONSE_STATUS_TYPE_FAILURE, string.Empty, "XDSDocumentEntry exists in metadata with no corresponding attached document", GlobalValues.CONST_ERROR_CODE_XDSMissingDocumentAttachment, GlobalValues.CONST_SEVERITY_TYPE_ERROR, string.Empty);
                }
                else if (ex.Message == GlobalValues.CONST_ERROR_CODE_XDSInvalidRequest)
                {
                    //Construct Error Response
                    xmlDocResponse = CommonUtility.ConstructRegistryErrorResponse(GlobalValues.CONST_RESPONSE_STATUS_TYPE_FAILURE, string.Empty, "XDSInvalidRequest - DcoumentId is not unique.", GlobalValues.CONST_ERROR_CODE_XDSInvalidRequest, GlobalValues.CONST_SEVERITY_TYPE_ERROR, string.Empty);
                }
                else if (ex.Message == GlobalValues.CONST_ERROR_CODE_XDSRepositoryMetadataError)
                {
                    //Construct Error Response
                    xmlDocResponse = CommonUtility.ConstructRegistryErrorResponse(GlobalValues.CONST_RESPONSE_STATUS_TYPE_FAILURE, string.Empty, "Error occurred in Parsing the Metadata.", GlobalValues.CONST_ERROR_CODE_XDSRepositoryMetadataError, GlobalValues.CONST_SEVERITY_TYPE_ERROR, string.Empty);
                }
                else if (ex.Message == GlobalValues.CONST_ERROR_CODE_XDSRegistryNotAvailable)
                {
                    //Construct Error Response
                    xmlDocResponse = CommonUtility.ConstructRegistryErrorResponse(GlobalValues.CONST_RESPONSE_STATUS_TYPE_FAILURE, string.Empty, "Repository was unable to access the Registry.", GlobalValues.CONST_ERROR_CODE_XDSRegistryNotAvailable, GlobalValues.CONST_SEVERITY_TYPE_ERROR, string.Empty);
                }
                else
                {
                    //Construct Error Response
                    xmlDocResponse = CommonUtility.ConstructRegistryErrorResponse(GlobalValues.CONST_RESPONSE_STATUS_TYPE_FAILURE, string.Empty, GlobalValues.CONST_ERROR_CODE_XDSRepositoryError, GlobalValues.CONST_ERROR_CODE_XDSRepositoryError, GlobalValues.CONST_SEVERITY_TYPE_ERROR, string.Empty);
                }

                nodeRegistryError = xmlDocResponse.SelectSingleNode(@"//*[local-name()='RegistryError']");

                errorCode = nodeRegistryError.Attributes["errorCode"].Value;
                if (errorCode != null)
                {
                    repositoryLogic.LogRolledBackData(objDocumentRepositoryLog, errorCode);
                }

                //ATNA EVENT LOG
                eventOutcomeIndicator = "8";
            }


            //ATNA EVENT LOG
            submissionSetUniqueID = stringDictionary["$SubmissionSet.UniqueID$"];

            //REPOSITORY-RDS-EXPORT-ITI-42
            repositoryLogic.ProcessRegisterDocumentSetATNAEvent(submissionSetUniqueID, sourceUserID, destinationUserID, eventOutcomeIndicator);
            //REPOSITORY-P-AND-R-IMPORT-ITI-41
            repositoryLogic.ProcessProvideAndRegisterATNAEvent(submissionSetUniqueID, sourceUserID, destinationUserID, eventOutcomeIndicator);

            msgResponse = Message.CreateMessage(msgRequest.Headers.MessageVersion, GlobalValues.CONST_ACTION_ProvideAndRegisterDocumentSet_bResponse, new XmlNodeReader(xmlDocResponse));

            return(msgResponse);
        }