Ejemplo n.º 1
0
 public ActionResult UpdateEntity(FormCollection collection)
 {
     ICacheManager cacheManager=new MemoryCacheManager();
     ENTITY model=new ENTITY();
     ResultModel resultModel=new ResultModel();
     this.TryUpdateModel(model);
     bool isNewEntity=false;
     string ErrorMessage=EntityNameAvailable(model.EntityName,model.EntityID);
     if(String.IsNullOrEmpty(ErrorMessage)==false) {
         ModelState.AddModelError("EntityName",ErrorMessage);
     }
     ErrorMessage=EntityCodeAvailable(model.EntityCode,model.EntityID);
     if(String.IsNullOrEmpty(ErrorMessage)==false) {
         ModelState.AddModelError("EntityCode",ErrorMessage);
     }
     if(ModelState.IsValid) {
         ENTITY entity=AdminRepository.FindEntity(model.EntityID);
         if(entity==null) {
             entity=new ENTITY();
             entity.CreatedDate=DateTime.Now;
             isNewEntity=true;
         }
         entity.EntityName=model.EntityName;
         entity.EntityCode=model.EntityCode;
         entity.Enabled=model.Enabled;
         IEnumerable<ErrorInfo> errorInfo=AdminRepository.SaveEntity(entity);
         if(errorInfo!=null) {
             resultModel.Result+=ValidationHelper.GetErrorInfo(errorInfo);
         } else {
             resultModel.Result="True||"+entity.EntityID;
             // Create default user
             if(isNewEntity) {
                 string username="******";
                 string password="******";
                 string firstname="admin";
                 string lastname="admin";
                 string email="*****@*****.**";
                 USER user=new USER();
                 user.Login=username;
                 user.PasswordSalt=SecurityExtensions.CreateSalt();
                 user.PasswordHash=password.CreateHash(user.PasswordSalt);
                 user.Email=email;
                 user.FirstName=firstname;
                 user.LastName=lastname;
                 user.EntityID=entity.EntityID;
                 user.CreatedDate=DateTime.Now;
                 user.LastUpdatedDate=DateTime.Now;
                 user.Enabled=true;
                 user.IsAdmin=true;
                 AdminRepository.SaveUser(user);
             }
         }
         // Create entity menu
         if(AdminRepository.GetEntityMenuCount(entity.EntityID)<=0) {
             AdminRepository.SaveEntityMenu(entity.EntityID);
             // Remove entity menu cache
             cacheManager.RemoveByPattern(string.Format(MenuHelper.ENTITYMENUKEY,entity.EntityID));
         }
     } else {
         foreach(var values in ModelState.Values.ToList()) {
             foreach(var err in values.Errors.ToList()) {
                 if(string.IsNullOrEmpty(err.ErrorMessage)==false) {
                     resultModel.Result+=err.ErrorMessage+"\n";
                 }
             }
         }
     }
     return View("Result",resultModel);
 }
Ejemplo n.º 2
0
 private IEnumerable<ErrorInfo> Validate(ENTITY entity)
 {
     return ValidationHelper.Validate(entity);
 }