Beispiel #1
0
        public bool Create(Domain.Entity entity, params string[] defaultAttributeNames)
        {
            if (_entityRepository.Exists(x => x.Name == entity.Name))
            {
                throw new XmsException(_loc["name_already_exists"]);
            }
            var solutionid = entity.SolutionId;                     //当前解决方案

            entity.SolutionId = SolutionDefaults.DefaultSolutionId; //组件属于默认解决方案
            var result = true;

            if (defaultAttributeNames.IsEmpty())
            {
                //默认添加主键字段
                defaultAttributeNames = new string[] { entity.Name + "Id" };
            }
            else if (!defaultAttributeNames.Contains(entity.Name + "Id", StringComparer.InvariantCultureIgnoreCase))
            {
                var namesList = defaultAttributeNames.ToList();
                namesList.Add(entity.Name + "Id");
                defaultAttributeNames = namesList.ToArray();
            }
            var parentEntity      = entity.ParentEntityId.HasValue ? _entityRepository.FindById(entity.ParentEntityId.Value) : null;
            var defaultAttributes = _defaultAttributeProvider.GetSysAttributes(entity).Where(x => defaultAttributeNames.Contains(x.Name, StringComparer.InvariantCultureIgnoreCase)).Distinct().ToList();

            using (UnitOfWork.Build(_entityRepository.DbContext))
            {
                result = _entityRepository.Create(entity, defaultAttributes, _defaultAttributeProvider.GetSysAttributeRelationShips(entity, defaultAttributes));

                //创建默认字段
                _attributeCreater.CreateDefaultAttributes(entity, defaultAttributeNames);
                //如果是子实体,则创建引用字段
                if (parentEntity != null)
                {
                    _attributeCreater.Create(new Domain.Attribute {
                        Name = parentEntity.Name + "Id"
                        , AttributeTypeName  = AttributeTypeIds.LOOKUP
                        , EntityId           = entity.EntityId
                        , EntityName         = entity.Name
                        , IsRequired         = true
                        , LocalizedName      = parentEntity.LocalizedName
                        , ReferencedEntityId = parentEntity.EntityId
                    });
                }
                //事件发布
                _eventPublisher.Publish(new ObjectCreatedEvent <Domain.Entity>(entity));
                //solution component
                _solutionComponentService.Create(solutionid, entity.EntityId, EntityDefaults.ModuleName);
                //本地化标签
                _localizedLabelService.Append(entity.SolutionId, entity.LocalizedName.IfEmpty(""), EntityDefaults.ModuleName, "LocalizedName", entity.EntityId)
                .Append(entity.SolutionId, entity.Description.IfEmpty(""), EntityDefaults.ModuleName, "Description", entity.EntityId)
                .Save();

                //add to cache
                _cacheService.SetEntity(entity);
            }
            return(result);
        }
Beispiel #2
0
        public bool CreateDefaultAttributes(Domain.Entity entity, params string[] defaultAttributeNames)
        {
            var result     = true;
            var attributes = _defaultAttributeProvider.GetSysAttributes(entity).Where(x => defaultAttributeNames.Contains(x.Name, StringComparer.InvariantCultureIgnoreCase)).ToList();

            using (UnitOfWork.Build(_attributeRepository.DbContext))
            {
                result = _attributeRepository.CreateMany(attributes);
                //新建字段选项
                var stateCodeAttr = attributes.Find(n => n.Name.IsCaseInsensitiveEqual("statecode"));
                if (stateCodeAttr != null)
                {
                    result = _stringMapCreater.CreateMany(stateCodeAttr.PickLists);
                }
                var statusCodeAttr = attributes.Find(n => n.Name.IsCaseInsensitiveEqual("statuscode"));
                if (statusCodeAttr != null)
                {
                    _optionSetCreater.Create(statusCodeAttr.OptionSet);
                }
                //创建关系
                result = _relationShipCreater.CreateMany(_defaultAttributeProvider.GetSysAttributeRelationShips(entity, attributes));
                //attribute localization
                foreach (var attr in attributes)
                {
                    var label = attr.TypeIsPrimaryKey() ? entity.LocalizedName : _loc["entity_sys_" + attr.Name];
                    _localizedLabelService.Append(entity.SolutionId, label, AttributeDefaults.ModuleName, "LocalizedName", attr.AttributeId)
                    .Append(entity.SolutionId, label, AttributeDefaults.ModuleName, "Description", attr.AttributeId);
                }
                _localizedLabelService.Save();
                //add to cache
                foreach (var attr in attributes)
                {
                    _cacheService.SetEntity(attr);
                }
            }
            return(result);
        }
Beispiel #3
0
        public void CreateView(Domain.Entity entity)
        {
            var defaultAttributes = _defaultAttributeProvider.GetSysAttributes(entity);

            _metadataProvider.AlterView(entity, defaultAttributes, _defaultAttributeProvider.GetSysAttributeRelationShips(entity, defaultAttributes));
        }