class to send the request using web client
        /// <summary>
        /// Action method to delete the selected repository.
        /// </summary>
        /// <param name="repositoryId">Repository id.</param>
        /// <returns>Status of the opeartion as json result.</returns>
        public JsonResult DeleteRepository(int repositoryId)
        {
            string message = string.Empty;
            bool status = false;

            if (repositoryId > 0)
            {
                webClientManager = new WebClientRequestManager();

                string responseString = webClientManager.UploadValues(new RequestParams()
                {
                    RequestURL = string.Concat(BaseController.BaseWebApiRepositoryPath + repositoryId),
                    RequestMode = RequestMode.DELETE
                });

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

                if (postResult)
                {
                    status = true;
                }
                else
                {
                    ViewBag.ErrorMessage = Messages.RepositoryErrMsg;
                    status = false;
                    message = Messages.RepositoryErrMsg;
                }
            }
            else
            {
                status = false;
                message = Messages.RepositoryErrMsg;
            }
            return Json(new { Status = status, Message = message });
        }
        /// <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 });
        }
示例#3
0
        /// <summary>
        /// Action method to save the rule data to the database
        /// </summary>
        /// <param name="ruleModel">Rule view Model</param>
        /// <returns>Status of the operation as json result.</returns>
        public JsonResult SaveRule(QCRuleViewModel ruleModel)
        {
            string message = string.Empty;
            bool status = false;
            if (ruleModel != null)
            {
                QualityCheck qcModel = new QualityCheck();
                qcModel = ruleModel.SetValuesTo(qcModel);

                // serlize the data file before passing to API
                string qcRuleData = qcModel.SerializeObject<QualityCheck>("qcRuleModel");
                JavaScriptSerializer jsSerializer = new JavaScriptSerializer();

                webClientManager = new WebClientRequestManager();

                // Checking for the duplicate rule name
                string responseDupruleName = webClientManager.UploadValues(new RequestParams()
                {
                    RequestURL = string.Concat(BaseController.BaseWebApiQCPath + "?ruleName=" + qcModel.Name),
                    RequestMode = RequestMode.POST
                });

                int ruleResult = jsSerializer.Deserialize<int>(responseDupruleName);

                if (ruleResult == 0 || ruleModel.QCRuleId == ruleResult)
                {
                    NameValueCollection values = new NameValueCollection();
                    values.Add("qcRuleModel", qcRuleData.EncodeTo64());

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


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

                    if (postResult)
                    {
                        status = true;
                    }
                    else
                    {
                        ViewBag.ErrorMessage = Messages.QualityCheckErrorMsg;
                        status = false;
                        message = Messages.QualityCheckErrorMsg;
                    }
                }
                else
                {
                    status = false;
                    message = Messages.DuplicateRuleMsg;
                }
            }
            else
            {
                status = false;
                message = Messages.QualityCheckErrorMsg;
            }

            return Json(new { Status = status, Message = message });
        }