View model class for add edit repository.
        private ManageReopsitoryViewModel GetSelectedViewModel(int repositoryId)
        {
            ManageReopsitoryViewModel viewModel = new ManageReopsitoryViewModel();

            try
            {
                JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                jsSerializer.MaxJsonLength = int.MaxValue;

                // create the object of HttpWeb Request Manager
                webRequestManager = new HttpWebRequestManager();

                // set the request details to get repostiry list
                webRequestManager.SetRequestDetails(new RequestParams()
                {
                    RequestURL = string.Concat(BaseController.BaseWebApiRepositoryTypesPath),
                });

                string repositoryTypeList = webRequestManager.RetrieveWebResponse();

                var lstRepositoryType = jsSerializer.Deserialize<IList<BaseRepository>>(repositoryTypeList);

                if (repositoryId > 0)
                {
                    // set the request details to get file list
                    webRequestManager.SetRequestDetails(new RequestParams()
                    {
                        RequestURL = string.Concat(BaseController.BaseWebApiRepositoryPath + repositoryId),
                    });

                    string selRepositoryStr = webRequestManager.RetrieveWebResponse();
                    var selRepository = jsSerializer.Deserialize<Repository>(selRepositoryStr);
                    viewModel.SetValuesFrom(selRepository);
                }
                else
                {
                    // Add default metadata
                    viewModel.AddDefaultMetaData();
                }
                Session[Constants.REPOSITORYVIEWMODEL] = viewModel;
                viewModel.RepositoryTypes = new SelectList(lstRepositoryType, Constants.BASEREPOSITORYID, Constants.NAMESTRING);

            }
            catch
            {
                viewModel = null;
            }

            return viewModel;
        }
 /// <summary>
 /// Method to traverse nodes of MetadataTypeViewModel collection
 /// </summary>
 /// <param name="node">XmlNode node</param>
 /// <param name="metadataType">MetadataTypeViewModel object</param>
 private static void TraverseNodes(XmlNode node, ManageReopsitoryViewModel metadataType)
 {
     foreach (XmlNode childNode in node.ChildNodes)
     {
         if (childNode.NodeType != XmlNodeType.Comment && childNode.NodeType != XmlNodeType.Attribute)
         {
             if (childNode.ChildNodes.Count > 0)
             {
                 if (childNode.ChildNodes.Count == 1 && (childNode.ChildNodes[0].NodeType == XmlNodeType.Comment || childNode.ChildNodes[0].NodeType == XmlNodeType.Text || childNode.ChildNodes[0].Name.ToUpper(CultureInfo.InvariantCulture) == "PARA"))
                 {
                     AddDataRow(childNode, metadataType);
                 }
                 else
                 {
                     TraverseNodes(childNode, metadataType);
                 }
             }
             else
             {
                 AddDataRow(childNode, metadataType);
             }
         }
     }
 }
        /// <summary>
        /// Method to add header row
        /// </summary>
        /// <param name="headerName">headerName string</param>
        /// <param name="currentHeaderRowIndex">currentHeaderRow index</param>
        /// <param name="metadataType">MetadataType object</param>
        private static void AddHeaderRow(string headerName, ref int currentHeaderRowIndex, ManageReopsitoryViewModel viewModel)
        {
            var metaDataTypes = GetMetaDataTypes();

            var headeRow = viewModel.RepositoryMetaDataFieldList.FirstOrDefault(m => (string.Compare(m.MetadataNodeName, headerName, StringComparison.Ordinal) == 0));
            if (headeRow == null)
            {
                var metaDataHeaderRow = new RepositoryMetadataFieldViewModel();
                metaDataHeaderRow.RowType = MetaDataRowType.HeaderRow;
                metaDataHeaderRow.MetadataNodeName = headerName;
                //metaDataHeaderRow.MetaDataTypes = new SelectList(metaDataTypes, Helpers.Constants.ID, Helpers.Constants.NAME, metaDataTypes.FirstOrDefault().Id.ToString());
                viewModel.RepositoryMetaDataFieldList.Add(metaDataHeaderRow);
            }
            else
            {
                currentHeaderRowIndex = viewModel.RepositoryMetaDataFieldList.FindIndex(m => (string.Compare(m.MetadataNodeName, headerName, StringComparison.Ordinal) == 0));
            }
        }
        /// <summary>
        /// Method adds the xml node to MetadataType List collection
        /// </summary>
        /// <param name="childNode">xml child node</param>
        /// <param name="metadataType">MetadataTypeViewModel object</param>
        /// <returns>returns currentHeaderRow Index</returns>
        private static int AddDataRow(XmlNode childNode, ManageReopsitoryViewModel metadataType)
        {
            var metaDataTypes = GetMetaDataTypes();

            var currentHeaderRowIndex = -1;
            var metaDataDetailRow = new RepositoryMetadataFieldViewModel();
            metaDataDetailRow.MappedLocation = GetParentNodePath(childNode);
            metaDataDetailRow.MetaDataTypes = new SelectList(metaDataTypes, Helpers.Constants.ID, Helpers.Constants.NAME, metaDataTypes.FirstOrDefault().Id.ToString());
            metaDataDetailRow.IsBlankRow = false;
            metaDataDetailRow.MetaDataTypeId = metaDataTypes.FirstOrDefault().Id.ToString();
            //metaDataDetailRow.MetadataNodeName = childNode.Name;
            ////before adding the data row,If the header row is not there add it else get the index of the header row to add this item next to the header index
            AddHeaderRow(metaDataDetailRow.MappedLocation, ref currentHeaderRowIndex, metadataType);
            metaDataDetailRow.MappedLocation = string.Concat(metaDataDetailRow.MappedLocation, ".", childNode.Name);
            //parents tag and body tag should not be added
            if (metaDataDetailRow.MappedLocation != "xml" && metaDataDetailRow.MappedLocation != ".xml" && !metaDataDetailRow.MappedLocation.Contains(".body"))
            {
                metaDataDetailRow.MappedLocation = metaDataDetailRow.MappedLocation.Replace(".#text", string.Empty);
                if (currentHeaderRowIndex > -1)
                {
                    metadataType.RepositoryMetaDataFieldList.Insert(currentHeaderRowIndex + 1, metaDataDetailRow);
                }
                else
                {
                    metadataType.RepositoryMetaDataFieldList.Add(metaDataDetailRow);
                }
            }

            return currentHeaderRowIndex;
        }
        /// <summary>
        /// Action method to save the repository data to the database.
        /// </summary>
        /// <param name="repositoryViewModel">Repository viewmodel.</param>
        /// <returns>Json result with the status.</returns>
        public ActionResult SaveRepository(ManageReopsitoryViewModel repositoryViewModel)
        {
            string message = string.Empty;
            string status = "success";
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            try
            {
                if (repositoryViewModel != null)
                {
                    Repository repModel = new Repository();
                    repModel = repositoryViewModel.SetValuesTo(repModel);

                    // serlize the data file before passing to API
                    string repositoryData = repModel.SerializeObject<Repository>("repositoryModel");

                    // create the object of HttpWeb Request Manager
                    webRequestManager = new HttpWebRequestManager();
                    webClientManager = new WebClientRequestManager();
                    webRequestManager.SetRequestDetails(new RequestParams()
                    {
                        RequestURL = string.Concat(BaseController.BaseWebApiRepositoryPath + "?repositoryName=" + repModel.Name),
                    });

                    string repositoryIdResult = webRequestManager.RetrieveWebResponse();
                    int repositoryResult = jsSerializer.Deserialize<int>(repositoryIdResult);

                    if (repositoryResult == 0 || repModel.RepositoryId == repositoryResult)
                    {
                        NameValueCollection values = new NameValueCollection();
                        values.Add("repositoryModel", repositoryData.EncodeTo64());

                        string responseString = webClientManager.UploadValues(new RequestParams()
                        {
                            RequestURL = string.Concat(BaseController.BaseWebApiRepositoryPath),
                            RequestMode = RequestMode.POST,
                            AllowAutoRedirect = false,
                            Values = values
                        });

                        if (!string.IsNullOrEmpty(webClientManager.RedirectionURL))
                        {
                            status = "redirect";
                            message = webClientManager.RedirectionURL;
                            return Json(new { Status = status, Message = message });
                        }

                        bool postResult = jsSerializer.Deserialize<bool>(responseString);

                        if (postResult)
                        {
                            status = "success";
                            // after updating the repostory metadata field,delete the repository metadata field that are marked for deletion
                            if (repositoryViewModel.DeletedMetaDataFieldIds != null && repositoryViewModel.DeletedMetaDataFieldIds.Length > 0)
                            {
                                string deleteResponseString = webClientManager.UploadValues(new RequestParams()
                                {
                                    RequestURL = string.Concat(BaseController.BaseWebApiRepositoryPath, "?repositorId=", repositoryViewModel.RepositoryId, "&repositoryMetadataFields=", repositoryViewModel.DeletedMetaDataFieldIds),
                                    RequestMode = RequestMode.DELETE
                                });

                                jsSerializer = new JavaScriptSerializer();
                                jsSerializer.Deserialize<bool>(deleteResponseString);
                            }

                        }
                        else
                        {
                            ViewBag.ErrorMessage = Messages.RepositoryErrMsg;
                            status = "error";
                            message = Messages.RepositoryErrMsg;
                        }
                    }
                    else
                    {
                        status = "error";
                        message = Messages.DuplicateRepositoryMsg;
                    }
                }
                else
                {
                    status = "error";
                    message = Messages.RepositoryErrMsg;
                }
            }
            catch (WebException webException)
            {
                using (Stream stream = webException.Response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        status = "error";
                        message = reader.ReadToEnd();
                        jsSerializer = new JavaScriptSerializer();
                        var data = jsSerializer.Deserialize<Dictionary<string,object>>(message);
                        message = (string)data.First(m=>m.Key == "Message").Value;
                    }
                }
            }
            catch (Exception exception)
            {
                message = exception.Message;
            }

            return Json(new { Status = status, Message = message });
        }
        /// <summary>
        /// Method to set Reporsitry meta data
        /// </summary>        
        /// <param name="viewModel">AddEditReopsitoryViewModel object</param>
        /// <param name="repositoryMetaData">RepositoryMetadata object</param>
        private static void SetRepositoryMetaData(ManageReopsitoryViewModel viewModel, Repository model)
        {
            var metaDataTypes = GetMetaDataTypes();
            try
            {
                // first set the repository metadata
                if (model.RepositoryMetadata == null || model.RepositoryMetadata.Count == 0)
                {
                    viewModel.RepositoryMetaDataName = "DefaultMetaData";
                }
                else
                {
                    viewModel.RepositoryMetaDataId = model.RepositoryMetadata.FirstOrDefault().RepositoryMetadataId;
                    viewModel.RepositoryMetaDataName = model.RepositoryMetadata.FirstOrDefault().Name;
                }

                if (model.RepositoryMetadata != null || model.RepositoryMetadata.Count > 0)
                {
                    if (model.RepositoryMetadata.FirstOrDefault() == null || model.RepositoryMetadata.FirstOrDefault().RepositoryMetadataFields == null || model.RepositoryMetadata.FirstOrDefault().RepositoryMetadataFields.Count == 0)
                    {
                        viewModel.RepositoryMetaDataFieldList.Add(new RepositoryMetadataFieldViewModel()
                        {
                            MetaDataTypeId = metaDataTypes.FirstOrDefault().Id.ToString(),
                            MetaDataTypes = new SelectList(metaDataTypes, Helpers.Constants.ID, Helpers.Constants.NAME, metaDataTypes.FirstOrDefault().Id.ToString()),
                            IsBlankRow = true
                        });
                    }
                    else
                    {
                        foreach (var repositoryMetaDataField in model.RepositoryMetadata.FirstOrDefault().RepositoryMetadataFields)
                        {
                            viewModel.RepositoryMetaDataName = model.RepositoryMetadata.FirstOrDefault().Name;
                            viewModel.RepositoryMetaDataId = model.RepositoryMetadata.FirstOrDefault().RepositoryMetadataId;

                            viewModel.RepositoryMetaDataFieldList.Add(new RepositoryMetadataFieldViewModel()
                            {
                                Description = repositoryMetaDataField.Description,
                                Field = repositoryMetaDataField.Name,
                                MappedLocation = repositoryMetaDataField.Mapping,
                                RangeValues = repositoryMetaDataField.Range,
                                IsRequired = repositoryMetaDataField.IsRequired != null ? Convert.ToBoolean(repositoryMetaDataField.IsRequired) : false,
                                MetaDataTypeId = repositoryMetaDataField.MetadataTypeId.ToString(),
                                MetaDataTypes = new SelectList(metaDataTypes, Helpers.Constants.ID, Helpers.Constants.NAME, repositoryMetaDataField.MetadataTypeId.ToString()),
                                RepositoryMetaDataFieldId = repositoryMetaDataField.RepositoryMetadataFieldId,
                                RepositoryMetaDataId = repositoryMetaDataField.RepositoryMetadataId
                            });
                        }
                    }
                }
                else//Add a blank Row
                {
                    viewModel.RepositoryMetaDataName = viewModel.RepositoryName + "DefaultMetaData";

                    viewModel.RepositoryMetaDataFieldList.Add(new RepositoryMetadataFieldViewModel()
                    {
                        MetaDataTypeId = metaDataTypes.FirstOrDefault().Id.ToString(),
                        MetaDataTypes = new SelectList(metaDataTypes, Helpers.Constants.ID, Helpers.Constants.NAME, metaDataTypes.FirstOrDefault().Id.ToString()),
                        IsBlankRow = true
                    });

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }