Example #1
0
        private void SaveConfigurationRole(int configUrlId, ConfigureUrlEntity configUrlEntity)
        {
            var today = DateTime.Now;

            foreach (RoleEntity role in configUrlEntity.Roles)
            {
                TB_M_CONFIG_ROLE configRole = _context.TB_M_CONFIG_ROLE.FirstOrDefault(x => x.CONFIG_URL_ID == configUrlId &&
                                                                                       x.ROLE_ID == role.RoleId);

                if (configRole == null && role.IsDelete == false)
                {
                    configRole = new TB_M_CONFIG_ROLE();
                    configRole.CONFIG_URL_ID = configUrlId;
                    configRole.ROLE_ID       = role.RoleId;
                    configRole.CREATE_USER   = configUrlEntity.CreateUser.UserId;
                    configRole.CREATE_DATE   = today;
                    _context.TB_M_CONFIG_ROLE.Add(configRole);
                }

                if (configRole != null && role.IsDelete == true)
                {
                    _context.TB_M_CONFIG_ROLE.Remove(configRole);
                }
            }

            this.Save();
        }
Example #2
0
        public bool IsDuplicateConfigureUrl(ConfigureUrlEntity configUrlEntity)
        {
            var cnt = _context.TB_M_CONFIG_URL.Where(
                x => x.CONFIG_NAME.ToUpper() == configUrlEntity.SystemName.ToUpper() &&
                x.CONFIG_URL.ToUpper() == configUrlEntity.Url.ToUpper() &&
                x.MENU_ID == configUrlEntity.Menu.MenuId &&
                x.CONFIG_URL_ID != configUrlEntity.ConfigureUrlId
                ).Count();

            return(cnt > 0);
        }
Example #3
0
 public bool IsDuplicateConfigureUrl(ConfigureUrlEntity configUrlEntity)
 {
     _commonDataAccess = new CommonDataAccess(_context);
     return(_commonDataAccess.IsDuplicateConfigureUrl(configUrlEntity));
 }
Example #4
0
 public bool SaveConfigurationUrl(ConfigureUrlEntity configureUrlEntity)
 {
     _commonDataAccess = new CommonDataAccess(_context);
     return(_commonDataAccess.SaveConfigureUrl(configureUrlEntity));
 }
Example #5
0
        public bool SaveConfigureUrl(ConfigureUrlEntity configUrlEntity)
        {
            var today = DateTime.Now;

            try
            {
                using (var transaction = _context.Database.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    _context.Configuration.AutoDetectChangesEnabled = false;

                    try
                    {
                        TB_M_CONFIG_URL dbConfigUrl = null;

                        if (configUrlEntity.ConfigureUrlId == 0)
                        {
                            dbConfigUrl = new TB_M_CONFIG_URL();
                            dbConfigUrl.CONFIG_URL_ID = configUrlEntity.ConfigureUrlId;
                            dbConfigUrl.CONFIG_NAME   = configUrlEntity.SystemName;
                            dbConfigUrl.CONFIG_URL    = configUrlEntity.Url;
                            dbConfigUrl.IMAGE         = configUrlEntity.ImageFile; //path
                            dbConfigUrl.STATUS        = configUrlEntity.Status;
                            dbConfigUrl.CREATE_USER   = configUrlEntity.CreateUser.UserId;
                            dbConfigUrl.UPDATE_USER   = configUrlEntity.UpdateUser.UserId;
                            dbConfigUrl.CREATE_DATE   = today;
                            dbConfigUrl.UPDATE_DATE   = today;
                            dbConfigUrl.MENU_ID       = configUrlEntity.Menu.MenuId;
                            dbConfigUrl.FONT_NAME     = configUrlEntity.FontName;

                            _context.TB_M_CONFIG_URL.Add(dbConfigUrl);
                            this.Save();

                            // Save Config Role
                            if (configUrlEntity.Roles != null && configUrlEntity.Roles.Count > 0)
                            {
                                this.SaveConfigurationRole(dbConfigUrl.CONFIG_URL_ID, configUrlEntity);
                            }
                        }
                        else
                        {
                            //Edit
                            dbConfigUrl =
                                _context.TB_M_CONFIG_URL.FirstOrDefault(
                                    x => x.CONFIG_URL_ID == configUrlEntity.ConfigureUrlId);
                            if (dbConfigUrl != null)
                            {
                                if (!string.IsNullOrWhiteSpace(configUrlEntity.ImageFile))
                                {
                                    var prevFile =
                                        StreamDataHelpers.GetApplicationPath(string.Format(CultureInfo.InvariantCulture, "{0}{1}",
                                                                                           Constants.ConfigUrlPath,
                                                                                           dbConfigUrl.IMAGE));

                                    if (StreamDataHelpers.TryToDelete(prevFile))
                                    {
                                        // Save new file
                                        dbConfigUrl.IMAGE = configUrlEntity.ImageFile;
                                    }
                                    else
                                    {
                                        var newFile =
                                            StreamDataHelpers.GetApplicationPath(string.Format(CultureInfo.InvariantCulture, "{0}{1}",
                                                                                               Constants.ConfigUrlPath, configUrlEntity.ImageFile));
                                        StreamDataHelpers.TryToDelete(newFile);
                                        throw new CustomException(string.Format(CultureInfo.InvariantCulture, "Could not delete existing file : {0}",
                                                                                prevFile));
                                    }
                                }

                                dbConfigUrl.CONFIG_NAME = configUrlEntity.SystemName;
                                dbConfigUrl.CONFIG_URL  = configUrlEntity.Url;
                                dbConfigUrl.STATUS      = configUrlEntity.Status;
                                dbConfigUrl.UPDATE_USER = configUrlEntity.UpdateUser.UserId;
                                dbConfigUrl.UPDATE_DATE = today;
                                dbConfigUrl.MENU_ID     = configUrlEntity.Menu.MenuId;
                                dbConfigUrl.FONT_NAME   = configUrlEntity.FontName;

                                SetEntryStateModified(dbConfigUrl);
                                this.Save();

                                if (configUrlEntity.Roles != null && configUrlEntity.Roles.Count > 0)
                                {
                                    this.SaveConfigurationRole(dbConfigUrl.CONFIG_URL_ID, configUrlEntity);
                                }
                            }
                            else
                            {
                                Logger.ErrorFormat("CONFIG_URL_ID: {0} does not exist",
                                                   configUrlEntity.ConfigureUrlId);
                            }
                        }

                        transaction.Commit();
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        Logger.Error("Exception occur:\n", ex);
                    }
                    finally
                    {
                        _context.Configuration.AutoDetectChangesEnabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
            }

            return(false);
        }
Example #6
0
        public ActionResult Edit(ConfigurationViewModel configVM)
        {
            Logger.Info(_logMsg.Clear().SetPrefixMsg("Save Configuration").Add("SystemName", configVM.SystemName)
                        .Add("URL", configVM.Url).ToInputLogString());
            try
            {
                //if (configVM.ConfigureUrlId != null)
                //{
                //    ModelState.Remove("File");
                //}

                if (ModelState.IsValid)
                {
                    var selectedRole = configVM.RoleCheckBoxes.Where(x => x.Checked == true)
                                       .Select(x => new RoleEntity
                    {
                        RoleId = x.Value.ToNullable <int>()
                    }).ToList();

                    if (!selectedRole.Any(x => x.IsDelete == false))
                    {
                        ViewBag.ErrorMessage = Resource.ValErr_AtLeastOneItem;
                        goto Outer;
                    }

                    //var file = configVM.File;
                    ConfigureUrlEntity configUrlEntity = new ConfigureUrlEntity();
                    configUrlEntity.ConfigureUrlId = configVM.ConfigureUrlId ?? 0;
                    configUrlEntity.SystemName     = configVM.SystemName;
                    configUrlEntity.Url            = configVM.Url;
                    configUrlEntity.Status         = configVM.Status;
                    configUrlEntity.CreateUser     = UserInfo;
                    configUrlEntity.UpdateUser     = UserInfo;
                    configUrlEntity.Menu           = new MenuEntity {
                        MenuId = configVM.MenuId ?? 0
                    };
                    configUrlEntity.FontName = configVM.FontName;

                    #region "Check Duplicate"
                    // Check Duplicate
                    _configFacade = new ConfigurationFacade();
                    if (_configFacade.IsDuplicateConfigureUrl(configUrlEntity) == true)
                    {
                        ViewBag.ErrorMessage = Resource.Error_SaveUrl;
                        goto Outer;
                    }
                    #endregion


                    #region "comment out"
                    //if (file != null && file.ContentLength > 0)
                    //{
                    //    _commonFacade = new CommonFacade();
                    //    ParameterEntity paramSingleFileSize = _commonFacade.GetCacheParamByName(Constants.ParameterName.SingleFileSize);
                    //    int? limitSingleFileSize = paramSingleFileSize.ParamValue.ToNullable<int>();

                    //    if (file.ContentLength > limitSingleFileSize)//2MB = 2097152 , 1MB = 1048576
                    //    {
                    //        ViewBag.ErrorMessage = Resource.ValError_FileSizeUploadMaxLimit;
                    //        goto Outer;
                    //    }

                    //    // extract only the filename
                    //    var fiWithoutExt = Path.GetFileNameWithoutExtension(file.FileName);
                    //    var fiExt = Path.GetExtension(file.FileName);
                    //    var fileName = string.Format("{0}_{1}{2}", fiWithoutExt, DateTime.Now.FormatDateTime("yyyyMMddHHmmssfff"), fiExt);

                    //    //const string regexPattern = @"^.*\.(jpg|jpeg|doc|docx|xls|xlsx|ppt|txt)$";
                    //    ParameterEntity param = _commonFacade.GetCacheParamByName(Constants.ParameterName.RegexConfigIcon);
                    //    Match match = Regex.Match(fileName, param.ParamValue, RegexOptions.IgnoreCase);

                    //    if (!match.Success)
                    //    {
                    //        ViewBag.ErrorMessage = Resource.ValError_FileExtensionConfigUrl;
                    //        goto Outer;
                    //    }

                    //    string imgPath = StreamDataHelpers.GetApplicationPath(string.Format("{0}{1}", Constants.ConfigUrlPath, fileName));
                    //    file.SaveAs(imgPath);

                    //    configUrlEntity.ImageFile = fileName;
                    //}

                    #endregion

                    #region "Get Roles"

                    if (configVM.RoleList != null && configVM.RoleList.Count > 0)
                    {
                        var prevRoles = (from rl in configVM.RoleList
                                         select new RoleEntity
                        {
                            RoleId = rl.RoleId,
                            RoleName = rl.RoleName,
                            IsDelete = !selectedRole.Select(x => x.RoleId).Contains(rl.RoleId)
                        }).ToList();

                        var dupeRoles = new List <RoleEntity>(selectedRole);
                        dupeRoles.AddRange(prevRoles);

                        var duplicates = dupeRoles.GroupBy(x => new { x.RoleId })
                                         .Where(g => g.Count() > 1)
                                         .Select(g => (object)g.Key.RoleId);

                        if (duplicates.Any())
                        {
                            Logger.Info(_logMsg.Clear().SetPrefixMsg("Duplicate ID in list")
                                        .Add("IDs", StringHelpers.ConvertListToString(duplicates.ToList(), ","))
                                        .ToInputLogString());
                            prevRoles.RemoveAll(x => duplicates.Contains(x.RoleId));
                        }

                        selectedRole.AddRange(prevRoles);
                    }

                    configUrlEntity.Roles = selectedRole;

                    #endregion

                    bool success = _configFacade.SaveConfigurationUrl(configUrlEntity);
                    if (success)
                    {
                        CacheLayer.Clear(Constants.CacheKey.MainMenu);
                        return(RedirectToAction("Search", "Configuration"));
                    }

                    ViewBag.ErrorMessage = Resource.Error_SaveFailed;
                }

Outer:
                TempData["configurationVM"] = configVM;
                return(InitEdit());
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
                Logger.Info(_logMsg.Clear().SetPrefixMsg("Save Configuration").Add("Error Message", ex.Message).ToFailLogString());
                return(Error(new HandleErrorInfo(ex, this.ControllerContext.RouteData.Values["controller"].ToString(),
                                                 this.ControllerContext.RouteData.Values["action"].ToString())));
            }
        }
Example #7
0
        public ActionResult InitEdit(int?ConfigureUrlId = null)
        {
            ConfigurationViewModel configVM = null;

            if (TempData["configurationVM"] != null)
            {
                configVM = (ConfigurationViewModel)TempData["configurationVM"];
            }
            else
            {
                configVM = new ConfigurationViewModel {
                    ConfigureUrlId = ConfigureUrlId
                };
            }

            _commonFacade = new CommonFacade();
            var statusList = _commonFacade.GetStatusSelectList();

            configVM.StatusList = new SelectList((IEnumerable)statusList, "Key", "Value", string.Empty);

            #region "For show in hint"

            //ParameterEntity param = _commonFacade.GetCacheParamByName(Constants.ParameterName.RegexConfigIcon);
            //ParameterEntity paramSingleFileSize = _commonFacade.GetCacheParamByName(Constants.ParameterName.SingleFileSize);

            //int? limitSingleFileSize = paramSingleFileSize.ParamValue.ToNullable<int>();
            //var singleLimitSize = limitSingleFileSize.HasValue ? (limitSingleFileSize / 1048576) : 0;
            //ViewBag.UploadLimitType = string.Format(param.ParamDesc, singleLimitSize);

            #endregion

            _configFacade = new ConfigurationFacade();
            var menuList = _configFacade.GetAllMenu();
            configVM.MenuList = new SelectList((IEnumerable)menuList, "Key", "Value", string.Empty);

            List <RoleEntity> roles = _configFacade.GetAllRole();
            if (roles != null && roles.Count > 0)
            {
                configVM.RoleCheckBoxes = roles.Select(x => new CheckBoxes
                {
                    Value   = x.RoleId.ToString(),
                    Text    = x.RoleName,
                    Checked = false
                }).ToList();
            }

            if (configVM.ConfigureUrlId != null)
            {
                ConfigureUrlEntity configUrlEntity = _configFacade.GetConfigureURLById(configVM.ConfigureUrlId.Value);
                configVM.ConfigureUrlId = configUrlEntity.ConfigureUrlId;
                configVM.SystemName     = configUrlEntity.SystemName;
                configVM.Url            = configUrlEntity.Url;
                configVM.Status         = configUrlEntity.Status;
                configVM.MenuId         = configUrlEntity.Menu.MenuId;
                configVM.FileUrl        = configUrlEntity.ImageUrl;
                configVM.FontName       = configUrlEntity.FontName;

                configVM.CreateUser  = configUrlEntity.CreateUser != null ? configUrlEntity.CreateUser.FullName : "";
                configVM.CreatedDate = configUrlEntity.CreatedDate.FormatDateTime(Constants.DateTimeFormat.DefaultFullDateTime);
                configVM.UpdateDate  = configUrlEntity.Updatedate.FormatDateTime(Constants.DateTimeFormat.DefaultFullDateTime);
                configVM.UpdateUser  = configUrlEntity.UpdateUser != null ? configUrlEntity.UpdateUser.FullName : "";

                if (configVM.RoleCheckBoxes != null && configUrlEntity.Roles != null)
                {
                    configVM.JsonRole = JsonConvert.SerializeObject(configUrlEntity.Roles);
                    configVM.RoleCheckBoxes.Where(x => configUrlEntity.Roles.Select(r => r.RoleId.ToString()).Contains(x.Value))
                    .ToList().ForEach(x => x.Checked = true);
                }
            }
            else
            {
                // default UserLogin
                if (this.UserInfo != null)
                {
                    var today = DateTime.Now;
                    configVM.CreateUser  = this.UserInfo.FullName;
                    configVM.CreatedDate = today.FormatDateTime(Constants.DateTimeFormat.DefaultFullDateTime);
                    configVM.UpdateDate  = today.FormatDateTime(Constants.DateTimeFormat.DefaultFullDateTime);
                    configVM.UpdateUser  = this.UserInfo.FullName;
                }
            }

            // List Font
            int refPageIndex = 0;
            configVM.FontList        = GetFont(null, null, ref refPageIndex).FontList;
            configVM.PageIndexOfFont = refPageIndex;

            return(View("~/Views/Configuration/Edit.cshtml", configVM));
        }