コード例 #1
0
        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);
        }