public RetrieveDocumentSet GetDocumentRepositoryMetaData(string documentID)
        {
            RetrieveDocumentSet objRetrieveDocumentSet = null;

            try
            {
                RepositoryDataAccess obj = new RepositoryDataAccess();
                objRetrieveDocumentSet = obj.GetDocumentRepositoryMetaData(documentID);
            }
            catch
            {
                throw;
            }
            return(objRetrieveDocumentSet);
        }
        private RetrieveDocumentSet GetDocumentRepositoryMetaData(string documentIUD)
        {
            RetrieveDocumentSet objRetrieveDocumentSet = null;

            try
            {
                RepositoryLogic objRepositoryLogic = new RepositoryLogic();
                objRetrieveDocumentSet = objRepositoryLogic.GetDocumentRepositoryMetaData(documentIUD);
            }
            catch
            {
                throw;
            }

            return(objRetrieveDocumentSet);
        }
        public XmlDocument GenerateContentDocument(XmlDocument objTotalDocument, RetrieveDocumentSet objRetrieveDocumentSet)
        {
            XmlElement retirieveDocumentSetElement = objTotalDocument.DocumentElement;
            XmlElement documentResponseElement     = null;

            documentResponseElement = objTotalDocument.CreateElement("DocumentResponse", "urn:ihe:iti:xds-b:2007");
            retirieveDocumentSetElement.AppendChild(documentResponseElement);

            XmlElement homeCommunityElement = objTotalDocument.CreateElement("HomeCommunityID", "urn:ihe:iti:xds-b:2007");

            homeCommunityElement.InnerText = objRetrieveDocumentSet.HomeCommunityID;

            XmlElement repositoryuniqueIdElement = objTotalDocument.CreateElement("RepositoryUniqueId", "urn:ihe:iti:xds-b:2007");

            repositoryuniqueIdElement.InnerText = objRetrieveDocumentSet.RepositoryUniqueID;

            XmlElement docUniqueId = objTotalDocument.CreateElement("DocumentUniqueId", "urn:ihe:iti:xds-b:2007");

            docUniqueId.InnerText = objRetrieveDocumentSet.DocumentID;

            XmlElement eltMimeType = objTotalDocument.CreateElement("mimeType", "urn:ihe:iti:xds-b:2007");

            eltMimeType.InnerText = objRetrieveDocumentSet.MimeType;

            XmlElement documentContentElement = objTotalDocument.CreateElement("Document", "urn:ihe:iti:xds-b:2007");

            byte[] bytArrDocument = new byte[objRetrieveDocumentSet.Content.Length];
            objRetrieveDocumentSet.Content.Read(bytArrDocument, 0, bytArrDocument.Length);

            //documentContentElement.InnerText = System.Text.ASCIIEncoding.ASCII.GetString(bytArrDocument);
            documentContentElement.InnerText = Convert.ToBase64String(bytArrDocument);

            if (!string.IsNullOrEmpty(objRetrieveDocumentSet.HomeCommunityID))
            {
                documentResponseElement.AppendChild(homeCommunityElement);
            }
            documentResponseElement.AppendChild(repositoryuniqueIdElement);
            documentResponseElement.AppendChild(docUniqueId);
            documentResponseElement.AppendChild(eltMimeType);
            documentResponseElement.AppendChild(documentContentElement);

            return(objTotalDocument);
        }
        public RetrieveDocumentSet GetDocumentRepositoryMetaData(string documentID)
        {
            object objValue = null;
            RetrieveDocumentSet objRetrieveDocumentSet = null;

            try
            {
                Database  dbIHEDB   = DatabaseFactory.CreateDatabase(CONST_CONNECTIONSTRING_NAME);
                DbCommand dbCommand = dbIHEDB.GetStoredProcCommand("LoadContentIDByDocumentID_DocumentRepositoryMetadata");
                dbIHEDB.AddInParameter(dbCommand, "DocumentID", DbType.String, documentID);

                using (IDataReader dr = dbIHEDB.ExecuteReader(dbCommand))
                {
                    objRetrieveDocumentSet = new RetrieveDocumentSet();

                    while (dr.Read())
                    {
                        objValue = dr["ContentUUID"];
                        if (objValue != null)
                        {
                            objRetrieveDocumentSet.ContentID = (int)objValue;
                        }

                        objValue = dr["DocumentEntryMimeType"];
                        if (objValue != null)
                        {
                            objRetrieveDocumentSet.MimeType = (string)objValue;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(objRetrieveDocumentSet);
        }
        private XmlDocument ProcessRetrieveDocumentSetResponse(XmlDocument xmlDocRequest, out StringDictionary atnaParametersValue)
        {
            XmlDocument                xmlDocResponse         = null;
            RetrieveDocumentSet        objDocumentMetadata    = null;
            RepositoryLogic            repositoryLogic        = null;
            List <RetrieveDocumentSet> lstRetrieveDocumentSet = null;
            RetrieveDocumentSet        objRetrieveDocumentSet = null;
            XmlElement  eltRoot = null;
            XmlNodeList nodeListDocumentRequest = null;
            XmlNode     node = null;

            string documentEntryUUIDs   = string.Empty;
            string eventOutomeIndicator = "0";

            atnaParametersValue = new StringDictionary();

            try
            {
                repositoryLogic        = new RepositoryLogic();
                lstRetrieveDocumentSet = new List <RetrieveDocumentSet>();

                //Root Element
                eltRoot = xmlDocRequest.DocumentElement;

                nodeListDocumentRequest = eltRoot.SelectNodes(".//*[local-name()='DocumentRequest']");

                foreach (XmlNode nodeDocumentRequest in nodeListDocumentRequest)
                {
                    objRetrieveDocumentSet = new RetrieveDocumentSet();

                    if (nodeDocumentRequest.HasChildNodes)
                    {
                        node = nodeDocumentRequest.SelectSingleNode(".//*[local-name()='HomeCommunityId']");
                        if (node != null)
                        {
                            objRetrieveDocumentSet.HomeCommunityID = node.InnerText;
                        }

                        node = nodeDocumentRequest.SelectSingleNode(".//*[local-name()='RepositoryUniqueId']");
                        if (node != null)
                        {
                            objRetrieveDocumentSet.RepositoryUniqueID = node.InnerText;
                        }

                        node = nodeDocumentRequest.SelectSingleNode(".//*[local-name()='DocumentUniqueId']");
                        if (node != null)
                        {
                            objRetrieveDocumentSet.DocumentID = node.InnerText;
                        }

                        //Used for ATNA Logging
                        if (string.IsNullOrEmpty(documentEntryUUIDs))
                        {
                            documentEntryUUIDs = objRetrieveDocumentSet.DocumentID;
                        }
                        else
                        {
                            documentEntryUUIDs += ", " + objRetrieveDocumentSet.DocumentID;
                        }


                        //CP - No Validation required for RepositoryUniqueID
                        //objRetrieveDocumentSet.IsRepositoryUniqueIDExsists = IsRepositoryIDExists(objRetrieveDocumentSet.RepositoryUniqueID);

                        objDocumentMetadata = GetDocumentRepositoryMetaData(objRetrieveDocumentSet.DocumentID);

                        objRetrieveDocumentSet.ContentID = objDocumentMetadata.ContentID;
                        objRetrieveDocumentSet.MimeType  = objDocumentMetadata.MimeType;

                        if (objRetrieveDocumentSet.ContentID > 0)
                        {
                            objRetrieveDocumentSet.Content = SQLServerStorageService.RetreiveDocument(objRetrieveDocumentSet.ContentID.ToString());
                        }

                        lstRetrieveDocumentSet.Add(objRetrieveDocumentSet);
                    }
                }

                //ATNA
                atnaParametersValue.Add("$DocumentEntry.UUID$", documentEntryUUIDs);

                xmlDocResponse = GenerateTotalRetrieveDocument();

                int contentCount = 0;

                for (int i = 0; i < lstRetrieveDocumentSet.Count; i++)
                {
                    if (lstRetrieveDocumentSet[i].ContentID > 0)
                    {
                        contentCount++;
                    }
                }

                if (contentCount == 0)
                {
                    XmlElement docReosponseRootElement = xmlDocResponse.DocumentElement;
                    XmlElement xEleRegErrList          = xmlDocResponse.CreateElement("tns:RegistryResponse", @"urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0");

                    XmlAttribute xAttStatus = xmlDocResponse.CreateAttribute("status");
                    xAttStatus.Value = GlobalValues.CONST_RESPONSE_STATUS_TYPE_FAILURE;
                    xEleRegErrList.Attributes.Append(xAttStatus);

                    XmlAttribute xAttXmlNSrs = xmlDocResponse.CreateAttribute("xmlns:tns");
                    xAttXmlNSrs.Value = "urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0";
                    xEleRegErrList.Attributes.Append(xAttXmlNSrs);


                    XmlAttribute xAttXmlNS = xmlDocResponse.CreateAttribute("xmlns:rim");
                    xAttXmlNS.Value = GlobalValues.CONST_XML_NAMESPACE_x;
                    xEleRegErrList.Attributes.Append(xAttXmlNS);

                    XmlElement xEleRegErrList1 = xmlDocResponse.CreateElement("tns:RegistryErrorList", @"urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0");

                    XmlAttribute attribhighestSeverity = xmlDocResponse.CreateAttribute("highestSeverity");
                    attribhighestSeverity.Value = "Highest SeverityURI";
                    xEleRegErrList1.Attributes.Append(attribhighestSeverity);

                    for (int loop = 0; loop < lstRetrieveDocumentSet.Count; loop++)
                    {
                        XmlElement xEleRegErr = xmlDocResponse.CreateElement("tns:RegistryError", @"urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0");

                        XmlAttribute xAttcodeContext = xmlDocResponse.CreateAttribute("codeContext");
                        xAttcodeContext.Value = GlobalValues.CONST_ERROR_CODE_XDSUnknownRepositoryUniqueID;
                        xEleRegErr.Attributes.Append(xAttcodeContext);

                        XmlAttribute xAttErrorCode = xmlDocResponse.CreateAttribute("errorCode");
                        xAttErrorCode.Value = string.Format("XDSDocumentEntry {0} exists in metadata with no corresponding attached document", lstRetrieveDocumentSet[loop].DocumentID);
                        xEleRegErr.Attributes.Append(xAttErrorCode);

                        XmlAttribute xAttSeverity = xmlDocResponse.CreateAttribute("severity");
                        xAttSeverity.Value = "Error";
                        xEleRegErr.Attributes.Append(xAttSeverity);

                        XmlAttribute xAttLocation = xmlDocResponse.CreateAttribute("location");
                        xAttLocation.Value = lstRetrieveDocumentSet[loop].DocumentID;
                        xEleRegErr.Attributes.Append(xAttLocation);

                        xEleRegErrList1.AppendChild(xEleRegErr);
                    }

                    xEleRegErrList.AppendChild(xEleRegErrList1);

                    docReosponseRootElement.AppendChild(xEleRegErrList);

                    //ATNA Event Outcome Indicator
                    eventOutomeIndicator = "8"; //Failure
                }
                if (contentCount < lstRetrieveDocumentSet.Count && contentCount > 0)
                {
                    //Partial Error
                    XmlElement rootElement    = xmlDocResponse.DocumentElement;
                    XmlElement xEleRegErrList = xmlDocResponse.CreateElement("tns:RegistryResponse", @"urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0");

                    XmlAttribute xAttStatus = xmlDocResponse.CreateAttribute("status");
                    xAttStatus.Value = GlobalValues.CONST_RESPONSE_STATUS_TYPE_PARTIALSUCCESS;
                    xEleRegErrList.Attributes.Append(xAttStatus);

                    XmlAttribute xAttXmlNSrs = xmlDocResponse.CreateAttribute("xmlns:tns");
                    xAttXmlNSrs.Value = "urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0";
                    xEleRegErrList.Attributes.Append(xAttXmlNSrs);


                    XmlAttribute xAttXmlNS = xmlDocResponse.CreateAttribute("xmlns:rim");
                    xAttXmlNS.Value = GlobalValues.CONST_XML_NAMESPACE_x;
                    xEleRegErrList.Attributes.Append(xAttXmlNS);

                    XmlElement xEleRegErrList1 = xmlDocResponse.CreateElement("tns:RegistryErrorList", @"urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0");


                    XmlAttribute attribhighestSeverity = xmlDocResponse.CreateAttribute("highestSeverity");
                    attribhighestSeverity.Value = "Highest SeverityURI";
                    xEleRegErrList1.Attributes.Append(attribhighestSeverity);

                    for (int loop = 0; loop < lstRetrieveDocumentSet.Count; loop++)
                    {
                        //Rep False || ContentID < 1
                        //if(Rep == false)
                        //
                        //else
                        //
                        ////CP - No Validation required for RepositoryUniqueId
                        #region "CP - No Validation required for RepositoryUniqueId"
                        //if (lstRetrieveDocumentSet[loop].IsRepositoryUniqueIDExsists == false)
                        //{
                        //    //RepositoryUniqueID not available

                        //    XmlElement xEleRegErr = xmlDocTotalRetrieveDocumentSet.CreateElement("tns:RegistryError", @"urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0");

                        //    XmlAttribute xAttcodeContext = xmlDocTotalRetrieveDocumentSet.CreateAttribute("codeContext");
                        //    xAttcodeContext.Value = CONST_ERROR_CODE_XDSUnknownRepositoryUniqueID;
                        //    xEleRegErr.Attributes.Append(xAttcodeContext);

                        //    XmlAttribute xAttErrorCode = xmlDocTotalRetrieveDocumentSet.CreateAttribute("errorCode");
                        //    xAttErrorCode.Value = "The repositoryUniqueId value could not be resolved to a valid document repository or the value does not match the repositoryUniqueId of the Document Repository";
                        //    xEleRegErr.Attributes.Append(xAttErrorCode);

                        //    XmlAttribute xAttSeverity = xmlDocTotalRetrieveDocumentSet.CreateAttribute("severity");
                        //    xAttSeverity.Value = "PartialSuccess";
                        //    xEleRegErr.Attributes.Append(xAttSeverity);

                        //    XmlAttribute xAttLocation = xmlDocTotalRetrieveDocumentSet.CreateAttribute("location");
                        //    xAttLocation.Value = lstRetrieveDocumentSet[loop].DocumentID;
                        //    xEleRegErr.Attributes.Append(xAttLocation);

                        //    xEleRegErrList1.AppendChild(xEleRegErr);
                        //    xEleRegErrList.AppendChild(xEleRegErrList1);
                        //}
                        #endregion

                        if (lstRetrieveDocumentSet[loop].ContentID < 1)
                        {
                            //RepositoryUniqueID is available.....Content ID not available

                            XmlElement xEleRegErr = xmlDocResponse.CreateElement("tns:RegistryError", @"urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0");

                            XmlAttribute xAttcodeContext = xmlDocResponse.CreateAttribute("codeContext");
                            xAttcodeContext.Value = GlobalValues.CONST_ERROR_CODE_XDSRepositoryError;
                            xEleRegErr.Attributes.Append(xAttcodeContext);

                            XmlAttribute xAttErrorCode = xmlDocResponse.CreateAttribute("errorCode");
                            xAttErrorCode.Value = string.Format("XDSDocumentEntry {0} does not exists.", lstRetrieveDocumentSet[loop].DocumentID);
                            xEleRegErr.Attributes.Append(xAttErrorCode);

                            XmlAttribute xAttSeverity = xmlDocResponse.CreateAttribute("severity");
                            xAttSeverity.Value = "Error";
                            xEleRegErr.Attributes.Append(xAttSeverity);

                            XmlAttribute xAttLocation = xmlDocResponse.CreateAttribute("location");
                            xAttLocation.Value = lstRetrieveDocumentSet[loop].DocumentID;
                            xEleRegErr.Attributes.Append(xAttLocation);

                            xEleRegErrList1.AppendChild(xEleRegErr);
                            xEleRegErrList.AppendChild(xEleRegErrList1);
                        }
                        else
                        {
                            //Success
                            xmlDocResponse = repositoryLogic.GenerateContentDocument(xmlDocResponse, lstRetrieveDocumentSet[loop]);
                        }
                    }


                    rootElement.AppendChild(xEleRegErrList);

                    //ATNA Event Outcome Indicator
                    eventOutomeIndicator = "4"; //Partial Success
                }
                else if (contentCount == lstRetrieveDocumentSet.Count)
                {
                    //Success
                    XmlElement rootElement             = xmlDocResponse.DocumentElement;
                    XmlElement registryResponseElement = xmlDocResponse.CreateElement("tns:RegistryResponse", @"urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0");

                    //status
                    registryResponseElement.Attributes.Append(xmlDocResponse.CreateAttribute("status"));
                    registryResponseElement.Attributes["status"].Value = GlobalValues.CONST_RESPONSE_STATUS_TYPE_SUCCESS;

                    //xmlns:tns
                    registryResponseElement.Attributes.Append(xmlDocResponse.CreateAttribute("xmlns:tns"));
                    registryResponseElement.Attributes["xmlns:tns"].Value = "urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0";

                    //xmlns:rim
                    registryResponseElement.Attributes.Append(xmlDocResponse.CreateAttribute("xmlns:rim"));
                    registryResponseElement.Attributes["xmlns:rim"].Value = GlobalValues.CONST_XML_NAMESPACE_x;

                    //Appending Registry Response Element
                    rootElement.AppendChild(registryResponseElement);
                    for (int i = 0; i < lstRetrieveDocumentSet.Count; i++)
                    {
                        xmlDocResponse = repositoryLogic.GenerateContentDocument(xmlDocResponse, lstRetrieveDocumentSet[i]);
                    }

                    //ATNA Event Outcome Indicator
                    eventOutomeIndicator = "0"; //Success
                }
                //xmlDocTotalRetrieveDocumentSet = GenerateContentDocument(xmlDocTotalRetrieveDocumentSet, lstDocEntries);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            //msgRetrieveDocumentSetResponse = Message.CreateMessage(input.Headers.MessageVersion, CONST_ACTION_RETRIEVEDOCSETRESPONSE, new XmlNodeReader(xmlDocTotalRetrieveDocumentSet));

            //ATNA
            atnaParametersValue.Add("$EventIdentification.EventOutcomeIndicator$", eventOutomeIndicator);

            //return msgRetrieveDocumentSetResponse;

            return(xmlDocResponse);
        }