public long UpdateStandardRequirement(StandardRequirementObject sReq)
 {
     try
     {
         return(_sReqManager.UpdateStandardRequirement(sReq));
     }
     catch (Exception ex)
     {
         ErrorLogger.LoggError(ex.StackTrace, ex.Source, ex.Message);
         return(0);
     }
 }
        private static GenericValidator ValidateControl(StandardRequirementObject model)
        {
            var gVal = new GenericValidator();

            try
            {
                if (string.IsNullOrEmpty(model.Title.Trim()))
                {
                    gVal.Error = "Please provide Document Title.";
                    gVal.Code  = 0;
                    return(gVal);
                }
                if (model.ValidFrom.Year < 1)
                {
                    gVal.Error = "Please provide Validity Period Start Date.";
                    gVal.Code  = 0;
                    return(gVal);
                }

                if (model.ValidTo != null && model.ValidTo.Value.Year > 1)
                {
                    var outDate2 = (DateTime)model.ValidTo;

                    if (model.ValidFrom.Year > outDate2.Year)
                    {
                        gVal.Error = "Date obtained must not be later than expiry date.";
                        gVal.Code  = 0;
                        return(gVal);
                    }
                }

                if (model.StandardRequirementTypeId < 1)
                {
                    gVal.Error = "Please select Document Type";
                    gVal.Code  = 0;
                    return(gVal);
                }
                gVal.Code = 1;
                return(gVal);
            }
            catch (Exception)
            {
                gVal.Error = "Process validation failed. Please supply all required entries and try again.";
                gVal.Code  = 0;
                return(gVal);
            }
        }
 public int UpdateStandardRequirement(StandardRequirementObject sReq)
 {
     try
     {
         if (sReq == null)
         {
             return(-2);
         }
         using (var db = new ImportPermitEntities())
         {
             if (db.StandardRequirements.Count(m => m.StandardRequirementTypeId == sReq.StandardRequirementTypeId && m.ImporterId == sReq.ImporterId && m.ValidFrom == sReq.ValidFrom && m.ValidTo == sReq.ValidTo && m.Id != sReq.Id) > 0)
             {
                 return(-3);
             }
             var sReqTypeEntity = ModelMapper.Map <StandardRequirementObject, StandardRequirement>(sReq);
             if (sReqTypeEntity == null || sReq.StandardRequirementTypeId < 1)
             {
                 return(-2);
             }
             db.StandardRequirements.Attach(sReqTypeEntity);
             db.Entry(sReqTypeEntity).State = EntityState.Modified;
             db.SaveChanges();
             return(5);
         }
     }
     catch (DbEntityValidationException e)
     {
         var str = "";
         foreach (var eve in e.EntityValidationErrors)
         {
             str += string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                  eve.Entry.Entity.GetType().Name, eve.Entry.State) + "\n";
             str = eve.ValidationErrors.Aggregate(str, (current, ve) => current + (string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage) + " \n"));
         }
         ErrorLogger.LoggError(e.StackTrace, e.Source, str);
         return(0);
     }
 }
        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));
            }
        }