public ActionResult GetDocument(long id)
        {
            try
            {
                var document = new StandardRequirementServices().GetStandardRequirement(id);
                if (document == null || document.Id < 1)
                {
                    return(Json(new StandardRequirementObject(), JsonRequestBehavior.AllowGet));
                }

                Session["_sReq"] = document;

                return(Json(document, JsonRequestBehavior.AllowGet));
            }
            catch (Exception)
            {
                return(Json(new StandardRequirementObject(), JsonRequestBehavior.AllowGet));
            }
        }
        public GenericValidator DeleteDocument(long id)
        {
            var gVal = new GenericValidator();

            try
            {
                if (id < 1)
                {
                    gVal.Error = "Invalid Selection";
                    gVal.Code  = -1;
                    return(gVal);
                }
                var path = new StandardRequirementServices().DeleteStandardRequirement(id);
                if (string.IsNullOrEmpty(path))
                {
                    gVal.Error = "Process Failed! Please try again later";
                    gVal.Code  = -1;
                    return(gVal);
                }
                if (!string.IsNullOrWhiteSpace(path))
                {
                    System.IO.File.Delete((path));
                    gVal.Error = "Process Failed! Please try again later";
                    gVal.Code  = -1;
                    return(gVal);
                }

                gVal.Error = "Document Information was successfully deleted.";
                gVal.Code  = 5;
                return(gVal);
            }
            catch (Exception ex)
            {
                ErrorLogger.LoggError(ex.StackTrace, ex.Source, ex.Message);
                gVal.Error = "An unknown error was encountered. Please contact the Administrator or try again later.";
                gVal.Code  = -1;
                return(gVal);
            }
        }
        public ActionResult GetStandardRequirement(JQueryDataTableParamModel param)
        {
            try
            {
                IEnumerable <StandardRequirementObject> filteredParentMenuObjects;
                int countG;
                //var token = GetAccessToken();
                //if (string.IsNullOrEmpty(token))
                //{
                //    return Json(new List<StandardRequirementObject>(), JsonRequestBehavior.AllowGet);
                //}
                var importerInfo = GetLoggedOnUserInfo();
                if (importerInfo.Id < 1)
                {
                    return(Json(new List <StandardRequirementObject>(), JsonRequestBehavior.AllowGet));
                }

                var pagedParentMenuObjects = new StandardRequirementServices().GetStandardRequirements(param.iDisplayLength, param.iDisplayStart, out countG, importerInfo.Id);
                if (pagedParentMenuObjects == null)
                {
                    return(Json(new List <StandardRequirementObject>(), JsonRequestBehavior.AllowGet));
                }

                if (!string.IsNullOrEmpty(param.sSearch))
                {
                    filteredParentMenuObjects = new StandardRequirementServices().Search(param.sSearch, importerInfo.Id);
                }
                else
                {
                    filteredParentMenuObjects = pagedParentMenuObjects;
                }

                if (!filteredParentMenuObjects.Any())
                {
                    return(Json(new List <StandardRequirementObject>(), JsonRequestBehavior.AllowGet));
                }

                var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
                Func <StandardRequirementObject, string> orderingFunction = (c => sortColumnIndex == 1 ? c.Title : sortColumnIndex == 2 ? c.StandardRequirementTypeName : sortColumnIndex == 3 ? c.ValidFromStr : c.ValidToStr);

                var sortDirection = Request["sSortDir_0"]; // asc or desc
                filteredParentMenuObjects = sortDirection == "desc" ? filteredParentMenuObjects.OrderBy(orderingFunction) : filteredParentMenuObjects.OrderByDescending(orderingFunction);

                var displayedPersonnels = filteredParentMenuObjects;

                var result = from c in displayedPersonnels
                             select new[] { Convert.ToString(c.Id), c.Title, c.StandardRequirementTypeName, c.ValidFromStr, c.ValidToStr };
                return(Json(new
                {
                    param.sEcho,
                    iTotalRecords = countG,
                    iTotalDisplayRecords = countG,
                    aaData = result
                },
                            JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                ErrorLogger.LoggError(ex.StackTrace, ex.Source, ex.Message);
                return(Json(new List <FeeObject>(), JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult EditCompanyDocument(StandardRequirementObject model)
        {
            var gVal = new GenericValidator();

            try
            {
                var importerInfo = GetLoggedOnUserInfo();
                if (importerInfo.Id < 1)
                {
                    gVal.Error = "Your session has timed out";
                    gVal.Code  = -1;
                    return(Json(gVal, JsonRequestBehavior.AllowGet));
                }

                var valStatus = ValidateControl(model);
                if (valStatus.Code < 1)
                {
                    gVal.Error = "Process failed.";
                    gVal.Code  = -1;
                    return(Json(gVal, JsonRequestBehavior.AllowGet));
                }

                if (Session["_sReq"] == null)
                {
                    gVal.Error = "Your session has timed out";
                    gVal.Code  = -1;
                    return(Json(gVal, JsonRequestBehavior.AllowGet));
                }

                var sReq = Session["_sReq"] as StandardRequirementObject;

                if (sReq == null)
                {
                    gVal.Error = "Your session has timed out";
                    gVal.Code  = -1;
                    return(Json(gVal, JsonRequestBehavior.AllowGet));
                }

                if (!string.IsNullOrEmpty(model.TempPath))
                {
                    var path = MoveFile(importerInfo.Id, model.TempPath);
                    if (string.IsNullOrEmpty(path))
                    {
                        gVal.Error = "Document processing failed. Please try again.";
                        gVal.Code  = -1;
                        return(Json(gVal, JsonRequestBehavior.AllowGet));
                    }

                    sReq.DocumentPath = path;
                }


                var response = new StandardRequirementServices().UpdateStandardRequirement(sReq);

                if (response < 1)
                {
                    gVal.Error = response == -3? "A similar document already exists" : "Process failed. Please try again.";
                    gVal.Code  = -1;
                    return(Json(gVal, JsonRequestBehavior.AllowGet));
                }

                if (!string.IsNullOrEmpty(sReq.DocumentPath))
                {
                    DeleteFile(sReq.DocumentPath);
                }
                gVal.Code  = 5;
                gVal.Error = "Standard Requirement was successfull processed.";
                return(Json(gVal, JsonRequestBehavior.AllowGet));
            }

            catch (Exception ex)
            {
                ErrorLogger.LoggError(ex.StackTrace, ex.Source, ex.Message);

                gVal.Error = "Process failed. Please try again.";
                gVal.Code  = -1;
                return(Json(gVal, JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult AddCompanyDoc(StandardRequirementObject model)
        {
            var gVal = new GenericValidator();

            try
            {
                var importerInfo = GetLoggedOnUserInfo();
                if (importerInfo.Id < 1)
                {
                    gVal.Error = "Your session has timed out";
                    gVal.Code  = -1;
                    return(Json(gVal, JsonRequestBehavior.AllowGet));
                }

                if (string.IsNullOrEmpty(model.TempPath))
                {
                    gVal.Error = "Document processing failed. Please try again.";
                    gVal.Code  = -1;
                    return(Json(gVal, JsonRequestBehavior.AllowGet));
                }


                if (string.IsNullOrEmpty(model.Title))
                {
                    gVal.Error = "Error: Document processing failed. Please try again.";
                    gVal.Code  = -1;
                    return(Json(gVal, JsonRequestBehavior.AllowGet));
                }

                if (model.StandardRequirementTypeId < 1)
                {
                    gVal.Error = "Please select Document to process";
                    gVal.Code  = 0;
                    return(Json(gVal, JsonRequestBehavior.AllowGet));
                }

                var path = MoveFile(importerInfo.Id, model.TempPath);
                if (string.IsNullOrEmpty(path))
                {
                    gVal.Error = "Document processing failed. Please try again.";
                    gVal.Code  = -1;
                    return(Json(gVal, JsonRequestBehavior.AllowGet));
                }

                model.DocumentPath = path;
                model.ImporterId   = importerInfo.Id;
                model.LastUpdated  = DateTime.Now;
                model.ValidFrom    = DateTime.Now;
                var response = new StandardRequirementServices().AddStandardRequirement(model);

                if (response < 1)
                {
                    DeleteFile(model.DocumentPath);
                    gVal.Error = "Process failed.";
                    gVal.Code  = -1;
                    return(Json(gVal, JsonRequestBehavior.AllowGet));
                }

                gVal.Path  = path;
                gVal.Code  = response;
                gVal.Error = "File was successfully processed.";
                return(Json(gVal, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                ErrorLogger.LoggError(ex.StackTrace, ex.Source, ex.Message);
                gVal.Error = "Process failed. Please try again.";
                gVal.Code  = -1;
                return(Json(gVal, JsonRequestBehavior.AllowGet));
            }
        }