public async Task <MasterDataKeyValue> Save(JObject data, bool isAuthroize = false) { dynamic masterDataDto = data; int? masterDataId = masterDataDto.Id; var masterData = new MasterDataKeyValue { Id = masterDataId ?? 0 }; bool isNew = masterDataDto.IsNew; //bool isPath; //try //{ // isPath = masterDataDto.IsPath; //} //catch (Exception) //{ // isPath = false; //} //bool isPathSecond; //try //{ // isPathSecond = masterDataDto.IsPathSecond; //} //catch (Exception) //{ // isPathSecond = false; //} int pathOrUrlProtocolId, secondPathOrUrlProtocolId; try { pathOrUrlProtocolId = masterDataDto.PathOrUrlProtocolId; } catch (Exception) { pathOrUrlProtocolId = (int)Protocol.LocalUrlPorotocol; } try { secondPathOrUrlProtocolId = masterDataDto.SecondPathOrUrlProtocolId; } catch (Exception) { secondPathOrUrlProtocolId = (int)Protocol.LocalUrlPorotocol; } var currentMasterData = await ContentManagementContext.MasterDataKeyValues.AsNoTracking().SingleOrDefaultAsync(md => md.Id == masterData.Id); if (!isNew) { if (currentMasterData == null) { throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.MasterDataKeyValuesNotFound)); } if (currentMasterData.EditMode) { SourceControl.CheckCodeCheckOute(masterData); } masterData = currentMasterData; masterData.RowVersion = masterDataDto.RowVersion; ContentManagementContext.MasterDataKeyValues.Attach(masterData); } else { ContentManagementContext.MasterDataKeyValues.Add(masterData); } masterData.TypeId = masterDataDto.TypeId; //if (!isPath) //{ // if (masterDataUrl.IndexOf(Helper.RootUrl, StringComparison.Ordinal) != 0) // { // masterDataUrl = Helper.RootUrl + masterDataUrl; // } //} //else //{ // if ( masterDataUrl.IndexOf("~", StringComparison.Ordinal) != 0) // { // if (masterDataUrl.IndexOf(Helper.RootUrl, StringComparison.Ordinal) != 0) // masterDataUrl = "~" + Helper.RootUrl + masterDataUrl; // else // masterDataUrl = "~" + masterDataUrl; // } //} string masterDataUrlOrPath = ProperPathOrUrl(Convert.ToString(masterDataDto.PathOrUrl), pathOrUrlProtocolId); if (masterDataUrlOrPath != Helper.RootUrl && masterData.TypeId != (int)EntityIdentity.BundleSource) { var repeatedMasterData = await ContentManagementContext.MasterDataKeyValues.Where( md => md.PathOrUrl == masterDataUrlOrPath && md.TypeId == masterData.TypeId).CountAsync() ; if ((repeatedMasterData > 0 && isNew) || (repeatedMasterData > 1 && !isNew)) { throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.RepeatedValue, masterDataUrlOrPath)); } } masterData.PathOrUrl = masterDataUrlOrPath; try { int parentId = masterDataDto.ParentId; if (currentMasterData?.ParentId != parentId || isNew) { var parentCode = await ContentManagementContext.MasterDataKeyValues.SingleOrDefaultAsync(md => md.Id == parentId); if (parentCode == null) { throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.ParentRecordNotFound)); } AuthorizeManager.CheckParentNodeModifyAccessForAddingChildNode(parentCode, parentCode.Id); } masterData.ParentId = parentId; } catch (KhodkarInvalidException) { throw; } catch (Exception) { masterData.ParentId = null; } try { masterData.Key = masterDataDto.Key; } catch (Exception) { masterData.Key = null; } try { masterData.Value = masterDataDto.Value; } catch (Exception) { masterData.Value = null; } try { masterData.ForeignKey1 = masterDataDto.ForeignKey1; } catch (Exception) { masterData.ForeignKey1 = null; } try { masterData.ForeignKey2 = masterDataDto.ForeignKey2; } catch (Exception) { masterData.ForeignKey2 = null; } try { masterData.ForeignKey3 = masterDataDto.ForeignKey3; } catch (Exception) { masterData.ForeignKey3 = null; } masterData.SecondCode = masterDataDto.SecondCode; string secondMasterDataPathOrUrl = masterDataDto.SecondPathOrUrl; masterData.SecondPathOrUrl = ProperPathOrUrl(secondMasterDataPathOrUrl, secondPathOrUrlProtocolId); masterData.Name = masterDataDto.Name; masterData.Code = masterDataDto.Code; if (masterData.Code != null) { var repeatedMasterData = await ContentManagementContext.MasterDataKeyValues.Where( md => md.Code == masterData.Code && md.TypeId == masterData.TypeId).CountAsync(); if ((repeatedMasterData > 0 && isNew) || (repeatedMasterData > 1 && !isNew)) { throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.RepeatedValue, masterData.Code)); } } masterData.Guid = masterDataDto.Guid; masterData.Description = masterDataDto.Description; masterData.Data = masterDataDto.Data; masterData.Version = (currentMasterData?.Version ?? 0) + 1; try { masterData.Order = masterDataDto.Order; } catch (Exception) { masterData.Order = 1; } masterData.IsLeaf = masterDataDto.IsLeaf; masterData.IsType = masterDataDto.IsType; if (masterData.IsType && masterData.IsLeaf) { throw new ConstraintException(LanguageManager.ToAsErrorMessage(ExceptionKey.MasterDataTypeIsLeaf)); } try { masterData.TypeId = masterDataDto.TypeId; } catch (Exception) { throw new ValidationException(LanguageManager.ToAsErrorMessage(ExceptionKey.FieldMustBeNumeric, "TypeId")); } try { masterData.ParentTypeId = masterDataDto.ParentTypeId; } catch (Exception) { if (masterData.IsType) { throw new ValidationException(LanguageManager.ToAsErrorMessage(ExceptionKey.FieldMustBeNumeric, "ParentTypeId")); } masterData.ParentTypeId = null; } masterData.Language = Config.DefaultsLanguage; //if(masterData.IsLeaf) if (masterData.TypeId == (int)EntityIdentity.Permission && !isAuthroize) { throw new UnauthorizedAccessException(LanguageManager.ToAsErrorMessage(ExceptionKey.InvalidGrant)); } if (currentMasterData != null) { masterData.ViewRoleId = currentMasterData.ViewRoleId; masterData.ModifyRoleId = currentMasterData.ModifyRoleId; masterData.AccessRoleId = currentMasterData.AccessRoleId; } AuthorizeManager.SetAndCheckModifyAndAccessRole(masterData, masterDataDto); masterData.Status = masterDataDto.Status; masterData.EditMode = masterDataDto.EditMode; masterData.EnableCache = masterDataDto.EnableCache; try { masterData.SlidingExpirationTimeInMinutes = masterDataDto.SlidingExpirationTimeInMinutes; } catch (Exception) { masterData.SlidingExpirationTimeInMinutes = 0; } await ContentManagementContext.SaveChangesAsync(); if (masterData.TypeId == (int)EntityIdentity.SqlServerConnections) { WebConfigManager.AddOrUpdateConnection(masterData.Code, masterData.SecondCode, ConnectionProvider.SqlServer); } else { UpdateMasterDatakeyVlaueSettingOfWebConfig(masterData); } return(masterData); }
public async Task <MasterDataKeyValue> Save(JObject data) { dynamic serviceDto = data; int? serviceId = serviceDto.Id; var service = new MasterDataKeyValue { Id = serviceId ?? 0, TypeId = (int)EntityIdentity.Service }; bool isNew = serviceDto.IsNew; bool checkIn = serviceDto.CheckIn; string comment = serviceDto.Comment; var currentService = await _contentManagementContext .MasterDataKeyValues.AsNoTracking().SingleOrDefaultAsync(sv => sv.Id == service.Id && sv.TypeId == (int)EntityIdentity.Service); if (!isNew) { if (currentService == null) { throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.ServiceNotFound)); } if (currentService.EditMode) { _sourceControl.CheckCodeCheckOute(currentService); } service = currentService; service.RowVersion = serviceDto.RowVersion; _contentManagementContext.MasterDataKeyValues.Attach(service); } else { _contentManagementContext.MasterDataKeyValues.Add(service); } string serviceCode = serviceDto.Code; if (serviceCode.IndexOf(_codeTemplate.ServicePrefix, StringComparison.Ordinal) != 0) { serviceCode = _codeTemplate.ServicePrefix + "." + serviceCode; } string serviceUrl = serviceDto.Url; if (serviceUrl.IndexOf(Helper.RootUrl, StringComparison.Ordinal) != 0) { serviceUrl = Helper.RootUrl + serviceUrl; } if (serviceUrl.LastIndexOf(Helper.RootUrl, StringComparison.Ordinal) == serviceUrl.Length - 1 && serviceUrl != "/") { serviceUrl = serviceUrl.Remove(serviceUrl.LastIndexOf(Helper.RootUrl, StringComparison.Ordinal)); } var repeatedService = await _contentManagementContext .MasterDataKeyValues.Where(sr => sr.PathOrUrl == serviceUrl && sr.TypeId == (int)EntityIdentity.Service).CountAsync() ; if ((repeatedService > 0 && isNew) || (repeatedService > 1 && !isNew)) { throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.RepeatedValue, serviceUrl)); } int?parentId = serviceDto.ParentId; if (currentService?.ParentId != parentId || isNew) { var parentCode = await _contentManagementContext .MasterDataKeyValues.SingleOrDefaultAsync(sr => sr.Id == parentId && sr.TypeId == (int)EntityIdentity.Service); if (parentCode == null) { throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.ParentRecordNotFound)); } AuthorizeManager.CheckParentNodeModifyAccessForAddingChildNode(parentCode, parentCode.Id); } service.ParentId = parentId; service.Name = serviceDto.Name; service.Code = serviceCode; repeatedService = await _contentManagementContext .MasterDataKeyValues.Where(sr => sr.Code == service.Code && sr.TypeId == (int)EntityIdentity.Service) .CountAsync(); if ((repeatedService > 0 && isNew) || (repeatedService > 1 && !isNew)) { throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.RepeatedValue, service.Code)); } service.Guid = serviceDto.Guid; service.Description = serviceDto.Description; service.Version = (currentService?.Version ?? 0) + 1; service.PathOrUrl = serviceUrl; try { service.Order = serviceDto.Order; } catch (Exception) { service.Order = 1; } //LOG SERVICE EVENT?1:TRUE,2:FALSE try { service.Key = serviceDto.Key; } catch (Exception) { service.Key = 0; } service.IsLeaf = serviceDto.IsLeaf; service.Language = Config.DefaultsLanguage; //if(service.IsLeaf) if (currentService != null) { service.ViewRoleId = currentService.ViewRoleId; service.ModifyRoleId = currentService.ModifyRoleId; service.AccessRoleId = currentService.AccessRoleId; } AuthorizeManager.SetAndCheckModifyAndAccessRole(service, serviceDto); service.Status = serviceDto.Status; service.EditMode = serviceDto.EditMode; service.EnableCache = serviceDto.EnableCache; try { service.SlidingExpirationTimeInMinutes = serviceDto.SlidingExpirationTimeInMinutes; } catch (Exception) { service.SlidingExpirationTimeInMinutes = 0; } await _contentManagementContext.SaveChangesAsync(); string jsCode = serviceDto.JsCode; if (!string.IsNullOrEmpty(jsCode)) { await _sourceControl.AddChange(Config.ServicesSourceCodePath, service.Guid + ".js", jsCode, service.Version, comment); if (checkIn) { await WriteFileAsync(Config.ServicesSourceCodePath, service.Guid, ".js", jsCode); } } CacheManager.ClearAllItemContainKey(service.PathOrUrl); UpdateServiceSettingOfWebConfig(service); return(service); }