public static bool IsCollectionPropertyType(Type type, ReferenceAttribute referenceAttribute)
        {
            PropertyInfo propertyInfo = type.GetProperty(referenceAttribute.RefencedPropertyName);

            // return propertyInfo.PropertyType.IsAssignableFrom(typeof(IEnumerable));
            return(typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType));
        }
        //public static string GetReferencedId(ReferenceAttribute referenceAttribute)
        //{
        //    return referenceAttribute.RefencedPropertyName + "Id";
        //}

        public static PropertyInfo GetReferencedPropertyId(BaseEntity baseEntity, ReferenceAttribute referenceAttribute)
        {
            PropertyInfo        propertyInfo        = baseEntity.GetType().GetProperty(referenceAttribute.RefencedPropertyName);
            ForeignKeyAttribute foreignKeyAttribute = propertyInfo.GetCustomAttribute <ForeignKeyAttribute>();

            return(baseEntity.GetType().GetProperty(foreignKeyAttribute.Name));
            //return referenceAttribute.RefencedPropertyName + "Id";
        }
Пример #3
0
		public ReferenceExpression(ReferenceAttribute referrerProperty, Expression expression)
		{
			if (expression == null)
				throw new ArgumentNullException("expression");
			_referrerProperty = new PropertyLiteral((NodeAttribute)referrerProperty);
			_expression = expression;
			//_existenceOnly = false;
		}
Пример #4
0
 private IReferenceConverter GetReferenceConverter(ReferenceAttribute referenceAttribute, Type sourceEntityType, Type referencedEntityType)
 {
     //if (ReferenceConversionUtils.IsCollectionPropertyType(sourceEntityType, referenceAttribute))
     //{
     //    return CreateReferenceConverter(typeof(MultiReferenceAttributeEntityToDtoConverter<,>), sourceEntityType, referencedEntityType);
     //}
     return(CreateReferenceConverter(typeof(MultiListReferenceAttributeEntityToDtoConverter <,>), sourceEntityType, referencedEntityType));
 }
Пример #5
0
 private IReferenceConverter GetReferenceConverter(ReferenceAttribute referenceAttribute, Type sourceEntityType, Type referencedEntityType)
 {
     if (ReferenceConversionUtils.IsCollectionPropertyType(sourceEntityType, referenceAttribute))
     {
         return(CreateReferenceConverter(typeof(MultiReferenceAttributeDtoToEntityConverter <,>), sourceEntityType, referencedEntityType));
     }
     return(CreateReferenceConverter(typeof(SingleReferenceAttributeDtoToEntityConverter <,>), sourceEntityType, referencedEntityType));
 }
Пример #6
0
        public override void Convert(Connection connection, T entity, U dto, PropertyInfo sourcePropertyInfo)
        {
            ReferenceAttribute referenceAttribute  = sourcePropertyInfo.GetCustomAttribute <ReferenceAttribute>();
            ReferenceString    referenceString     = sourcePropertyInfo.GetValue(dto) as ReferenceString;
            Type referencedEntityType              = GetReferencedEntityType(entity, referenceAttribute);
            IReferenceConverter referenceConverter = GetReferenceConverter(referenceAttribute, typeof(T), referencedEntityType);

            referenceConverter.Convert(connection, entity, dto, sourcePropertyInfo, referenceAttribute, referenceString);
        }
Пример #7
0
 public ReferenceExpression(ReferenceAttribute referrerProperty, Expression expression)
 {
     if (expression == null)
     {
         throw new ArgumentNullException("expression");
     }
     _referrerProperty = new PropertyLiteral((NodeAttribute)referrerProperty);
     _expression       = expression;
 }
Пример #8
0
        public override void Convert(IUnitOfWork unitOfWork, T dto, U entity, PropertyInfo sourcePropertyInfo)
        {
            ReferenceAttribute referenceAttribute  = sourcePropertyInfo.GetCustomAttribute <ReferenceAttribute>();
            ReferenceString    referenceString     = sourcePropertyInfo.GetValue(dto) as ReferenceString;
            Type referencedEntityType              = GetReferencedEntityType(entity, referenceAttribute);
            IReferenceConverter referenceConverter = GetReferenceConverter(referenceAttribute, typeof(U), referencedEntityType);

            referenceConverter.Convert(unitOfWork, entity, dto, sourcePropertyInfo, referenceAttribute, referenceString);
        }
Пример #9
0
 private Reference(IEntityInfo entityInfo, PropertyInfo prop, ReferenceAttribute referenceAttribute)
     : base(entityInfo, prop, null)
 {
     _type = typeof(IList).IsAssignableFrom(prop.PropertyType)
         ? ReferenceType.OneToMany
         : ReferenceType.ManyToOne;
     ReferenceEntityType   = referenceAttribute.ReferenceEntityType;
     LocalReferenceField   = referenceAttribute.LocalReferenceField;
     ForeignReferenceField = referenceAttribute.ForeignReferenceField;
 }
        /// <summary>
        /// Gets the type of the referenced entity. If it is multiple reference the type is got from the generic type of the collection.
        /// </summary>
        /// <param name="entity">The source entity</param>
        /// <param name="referencedAttribute">The referenced attribute of the DTO property</param>
        /// <returns>The type of the referenced entity</returns>
        protected Type GetReferencedEntityType(BaseEntity entity, ReferenceAttribute referenceAttribute)
        {
            PropertyInfo referencePropertyInfo = entity.GetType().GetProperty(referenceAttribute.RefencedPropertyName);

            if (ReferenceConversionUtils.IsCollectionPropertyType(entity.GetType(), referenceAttribute))
            {
                return(GetReferencedTargetPropertyGenericType(referencePropertyInfo));
            }
            return(referencePropertyInfo.PropertyType);
        }
Пример #11
0
        private void TreatReference(PropertyInfo prop, ReferenceAttribute referenceAttribute)
        {
            if (referenceAttribute == null)
            {
                return;
            }

            var reference = Reference.Create(this, prop, referenceAttribute);

            References.Add(reference);
        }
Пример #12
0
        public ElementReferenceProperty(IServiceProvider serviceProvider, ElementLookup lookup, ElementViewModel parent, PropertyDescriptor declaringProperty, IEnumerable <Attribute> additionalAttributes)
            : base(serviceProvider, parent, declaringProperty, additionalAttributes.Union(new Attribute[] { new ValidationAttribute(typeof(ElementReferenceValidator)) }))
        {
            if (declaringProperty.PropertyType != typeof(string))
            {
                throw new ArgumentException(Resources.ReferencePropertyInvalidType);
            }

            this.lookup = lookup;
            this.changeScopePropertyWatcher = new ChangeScopePropertyWatcher();
            this.changeScopePropertyWatcher.ChangeScopePropertyChanged += new EventHandler(ChangeScopePropertyWatcherChangeScopePropertyChanged);

            referenceAttribute = base.Attributes.OfType <ReferenceAttribute>().FirstOrDefault();
            Debug.Assert(referenceAttribute != null);

            ((INotifyCollectionChanged)ValidationResults).CollectionChanged += ValidationCollectionChanged;
        }
Пример #13
0
        public static NodeAttribute GetNodeAttribute(ReferenceAttribute attr)
        {
            switch (attr)
            {
            case ReferenceAttribute.Parent:
                return(NodeAttribute.Parent);

            case ReferenceAttribute.LockedBy:
                return(NodeAttribute.LockedBy);

            case ReferenceAttribute.CreatedBy:
                return(NodeAttribute.CreatedBy);

            case ReferenceAttribute.ModifiedBy:
                return(NodeAttribute.ModifiedBy);

            default:
                throw new NotImplementedException("Unknown ReferenceAttribute");
            }
        }
Пример #14
0
            public DeferredElementScope(ReferenceAttribute referenceAttribute,
                                        ElementViewModel baseDeclaringElement,
                                        ElementLookup lookup)
            {
                this.referenceAttribute = referenceAttribute;
                this.lookup             = lookup;

                if (referenceAttribute.ScopeIsDeclaringElement)
                {
                    CreateElementScope(baseDeclaringElement);
                }
                else
                {
                    if (!TryCreateScopeElement())
                    {
                        scopeChangeScope =
                            lookup.CreateChangeScope(x => x.ConfigurationType.IsAssignableFrom(referenceAttribute.ScopeType));
                        scopeChangeScope.CollectionChanged += ScopeChangedHandler;
                    }
                }
            }
        void UpdateText()
        {
            Text = kNoneText;

            if (Value.IsNotNone())
            {
                var lookup = IoC.Get <ProtoData.IProtoDataObjectLookup>(ReferenceAttribute.GetExportContractName());
                if (lookup == null)
                {
                    Text = "INTERNAL ERROR";
                    Debug.Trace.PhxStudio.TraceDataSansId(System.Diagnostics.TraceEventType.Error,
                                                          "Failed to resolve ObjectLookup",
                                                          ReferenceAttribute.GetExportContractName(),
                                                          base.Name);
                    return;
                }

                var provider = lookup.SourceObjectDatabase.Provider;
                var list     = provider.GetNamesInterface(lookup.SourceObjectDatabaseKindId);
                Text = list.TryGetMemberName(this.Value);
            }
        }
Пример #16
0
		public SearchOrder(ReferenceAttribute propertyToOrder, OrderDirection direction) : this((NodeAttribute)propertyToOrder, direction) { }
Пример #17
0
 public ReferenceExpression(ReferenceAttribute referrerProperty, Node referencedNode)
 {
     _referrerProperty = new PropertyLiteral((NodeAttribute)referrerProperty);
     _referencedNode   = referencedNode;
     //_existenceOnly = false;
 }
        public void Convert(IUnitOfWork unitOfWork, BaseEntity sourceEntity, BaseDto targetDto, PropertyInfo sourcePropertyInfo, ReferenceAttribute referenceAttribute, ReferenceString referenceString)
        {
            PropertyInfo referencedEntityPropertyInfo   = sourceEntity.GetType().GetProperty(referenceAttribute.RefencedPropertyName);
            PropertyInfo referencedEntityIdPropertyInfo = ReferenceConversionUtils.GetReferencedPropertyId(sourceEntity, referenceAttribute); //sourceEntity.GetType().GetProperty(ReferenceConversionUtils.GetReferencedId(referenceAttribute));
            U            referencedEntity = (U)referencedEntityPropertyInfo.GetValue(sourceEntity, null);

            if (referencedEntity != null)
            {
                sourcePropertyInfo.SetValue(targetDto, new ReferenceString(referencedEntity.Id, referencedEntity.ToString()));
            }
        }
        public void AddReference(Type referenceType, string referenceField, bool isArray, ReferenceType refType)
        {
            if (this.Registered & !this.AllowRuntimeChanges) throw new InvalidOperationException("The Entity is already registered, it cannot be modified");

            var attr = new ReferenceAttribute(referenceType, referenceField);
            attr.ReferenceType = ReferenceType.OneToMany;
            if (refType == ReferenceType.ManyToMany) attr.ReferenceType = refType;
            attr.IsArray = isArray;
            attr.IsList = !isArray;

            this.References.Add(attr);
            if (this.Registered && EntityDefinitionChanged != null)
                EntityDefinitionChanged.Invoke(this, new EntityTypeChangedArgs(this, attr));
        }
Пример #20
0
        public void Convert(IUnitOfWork unitOfWork, BaseEntity sourceEntity, BaseDto dto, PropertyInfo sourcePropertyInfo, ReferenceAttribute referenceAttribute, ReferenceString referenceString)
        {
            PropertyInfo targetProperty = sourceEntity.GetType().GetProperty(referenceAttribute.RefencedPropertyName);
            // TODO ???
            //if(referenceString == null || referenceString.Value == null)
            //{
            //    return;
            //}
            List <Guid> referencedIds = new List <Guid>();

            if (referenceString != null && referenceString.Value != null)
            {
                referencedIds = referenceString.GetIds();
            }

            ICollection <U> referencedEntities = (ICollection <U>)targetProperty.GetValue(sourceEntity);

            if (IsReferenciesCreated(referencedIds, referencedEntities))
            {
                CreateMultiReferences(unitOfWork, sourceEntity, targetProperty, referencedIds, referencedEntities);
            }
            else
            {
                UpdateMultiReference(unitOfWork, sourceEntity, targetProperty, referencedIds, referencedEntities);
            }
        }
Пример #21
0
		public ReferenceExpression(ReferenceAttribute referrerProperty)
		{
			_referrerProperty = new PropertyLiteral((NodeAttribute)referrerProperty);
			_existenceOnly = true;
		}
        public void Convert(IUnitOfWork unitOfWork, BaseEntity sourceEntity, BaseDto dto, PropertyInfo sourcePropertyInfo, ReferenceAttribute referenceAttribute, ReferenceString referenceString)
        {
            //PropertyInfo targetProperty = sourceEntity.GetType().GetProperty(ReferenceConversionUtils.GetReferencedId(referenceAttribute));
            PropertyInfo targetProperty = ReferenceConversionUtils.GetReferencedPropertyId(sourceEntity, referenceAttribute);

            if (referenceString == null || string.IsNullOrEmpty(referenceString.Value))
            {
                targetProperty.SetValue(sourceEntity, null);
            }
            else
            {
                targetProperty.SetValue(sourceEntity, referenceString.GetId());
            }
        }
        public void Convert(Connection connection, BaseEntity sourceEntity, BaseDto dto, PropertyInfo sourcePropertyInfo, ReferenceAttribute referenceAttribute, ReferenceString referenceString)
        {
            if (referenceString == null)
            {
                return;
            }
            PropertyInfo    targetProperty     = sourceEntity.GetType().GetProperty(referenceAttribute.RefencedPropertyName);
            List <Guid>     referencedIds      = referenceString.GetIds();
            ICollection <U> referencedEntities = (ICollection <U>)targetProperty.GetValue(sourceEntity);

            if (IsReferenciesCreated(referencedIds, referencedEntities))
            {
                CreateMultiReferences(connection, sourceEntity, targetProperty, referencedIds, referencedEntities);
            }
            else
            {
                UpdateMultiReference(connection, sourceEntity, targetProperty, referencedIds, referencedEntities);
            }
        }
Пример #24
0
        public bool SetCountProperty(IEntity entity, string referencePropertyName, Guid referencedEntityID)
        {
            bool wasChanged = false;

            using (LogGroup logGroup = LogGroup.StartDebug("Setting the specified reference count property."))
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                LogWriter.Debug("Entity type: " + entity.GetType().FullName);
                LogWriter.Debug("Reference property name: " + referencePropertyName);
                LogWriter.Debug("Referenced entity ID: " + referencedEntityID);

                ReferenceAttribute attribute = EntitiesUtilities.GetReferenceAttribute(entity, referencePropertyName);

                Type referencedType = EntitiesUtilities.GetReferenceType(entity, referencePropertyName);

                // If the referenced entity type is not null then set the corresponding count property
                // otherwise skip it be cause a null referenced entity type means its a dynamically type reference property which hasn't been set
                if (referencedType != null)
                {
                    LogWriter.Debug("Referenced type: " + referencedType.FullName);

                    if (attribute.CountPropertyName != String.Empty)
                    {
                        LogWriter.Debug("Count property name: " + attribute.CountPropertyName);

                        string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(entity, referencePropertyName);

                        PropertyInfo property = entity.GetType().GetProperty(attribute.CountPropertyName);

                        // Get the original reference count
                        int originalCount = (int)property.GetValue(entity, null);

                        LogWriter.Debug("Original count: " + originalCount);

                        if (property == null)
                        {
                            throw new Exception("'" + attribute.CountPropertyName + "' count property not found on type '" + entity.ShortTypeName + "'.");
                        }

                        // Get the latest reference count
                        int count = Provider.Counter.CountEntitiesWithReference(
                            entity.GetType(),
                            entity.ID,
                            referencePropertyName,
                            referencedType,
                            mirrorPropertyName
                            );

                        LogWriter.Debug("Count: " + count.ToString());

                        // If the new count is not the same as the old one then update the property
                        // otherwise skip
                        if (count != originalCount)
                        {
                            wasChanged = true;

                            property.SetValue(entity, count, null);
                        }
                    }
                    else
                    {
                        LogWriter.Debug("No count property specified on the reference attribute.");
                    }
                }
            }
            return(wasChanged);
        }
Пример #25
0
 public static string GetReferencedId(ReferenceAttribute referenceAttribute)
 {
     return(referenceAttribute.RefencedPropertyName + "Id");
 }
Пример #26
0
 public SearchOrder(ReferenceAttribute propertyToOrder) : this(propertyToOrder, OrderDirection.Asc)
 {
 }
 private IReferenceConverter GetReferenceConverter(ReferenceAttribute referenceAttribute, Type sourceEntityType, Type referencedEntityType)
 {
     return(CreateReferenceConverter(typeof(MultiReferenceAttributeDtoToEntityConverter <,>), sourceEntityType, referencedEntityType));
 }
Пример #28
0
		public SearchOrder(ReferenceAttribute propertyToOrder) : this(propertyToOrder, OrderDirection.Asc) { }
Пример #29
0
 public static Reference Create(EntityInfo entityInfo, PropertyInfo prop, ReferenceAttribute referenceAttribute)
 {
     return(new Reference(entityInfo, prop, referenceAttribute));
 }
Пример #30
0
 internal EntityTypeChangedArgs(EntityInfo info, ReferenceAttribute reference)
 {
     EntityInfo = info;
     ReferenceAttribute = reference;
     FieldAttribute = null;
     TableCreated = false;
 }
Пример #31
0
 public ReferenceExpression(ReferenceAttribute referrerProperty)
 {
     _referrerProperty = new PropertyLiteral((NodeAttribute)referrerProperty);
     _existenceOnly    = true;
 }
Пример #32
0
		public static NodeAttribute GetNodeAttribute(ReferenceAttribute attr)
		{
			switch (attr)
			{
				case ReferenceAttribute.Parent:
					return NodeAttribute.Parent;
				case ReferenceAttribute.LockedBy:
					return NodeAttribute.LockedBy;
				case ReferenceAttribute.CreatedBy:
					return NodeAttribute.CreatedBy;
				case ReferenceAttribute.ModifiedBy:
					return NodeAttribute.ModifiedBy;
				default:
					throw new NotImplementedException("Unknown ReferenceAttribute");
			}
		}
Пример #33
0
 public SearchOrder(ReferenceAttribute propertyToOrder, OrderDirection direction) : this((NodeAttribute)propertyToOrder, direction)
 {
 }
Пример #34
0
		public ReferenceExpression(ReferenceAttribute referrerProperty, Node referencedNode)
		{
			_referrerProperty = new PropertyLiteral((NodeAttribute)referrerProperty);
			_referencedNode = referencedNode;
			//_existenceOnly = false;
		}
Пример #35
0
        private static Expression ParseReferenceExpression(XmlNode node, XmlNamespaceManager nsmgr, SchemaRoot schema)
        {
            XmlAttribute attr;

            bool existenceOnly = false;

            attr = node.Attributes["existenceOnly"];
            if (attr != null)
            {
                existenceOnly = attr.Value == "yes";
            }

            object       leftValue      = ParseLeftValue(node, nsmgr, schema);
            PropertyType leftProp       = leftValue as PropertyType;
            Node         referencedNode = null;

            attr = node.Attributes["referencedNodeId"];
            if (attr != null)
            {
                int referencedNodeId = XmlConvert.ToInt32(attr.Value);
                referencedNode = Node.LoadNode(referencedNodeId);
                if (referencedNode == null)
                {
                    throw new ApplicationException(String.Concat("Referred node is not found: ", referencedNodeId));
                }
                if (leftProp != null)
                {
                    return(new ReferenceExpression(leftProp, referencedNode));
                }
                ReferenceAttribute leftAttr = (ReferenceAttribute)leftValue;
                return(new ReferenceExpression(leftAttr, referencedNode));
            }
            if (node.SelectSingleNode("x:*[1]", nsmgr) == null)
            {
                if (leftProp != null)
                {
                    if (existenceOnly)
                    {
                        return(new ReferenceExpression(leftProp));
                    }
                    return(new ReferenceExpression(leftProp, (Node)null));
                }
                ReferenceAttribute leftAttr = (ReferenceAttribute)leftValue;
                if (existenceOnly)
                {
                    return(new ReferenceExpression(leftAttr));
                }
                return(new ReferenceExpression(leftAttr, (Node)null));
            }

            object rightValue = ParseRightValue(node, nsmgr, schema);

            Expression rightExpr = rightValue as Expression;

            if (leftProp != null)
            {
                if (rightExpr != null)
                {
                    return(new ReferenceExpression(leftProp, rightExpr));
                }
            }
            else
            {
                ReferenceAttribute leftAttr = (ReferenceAttribute)leftValue;
                if (rightExpr != null)
                {
                    return(new ReferenceExpression(leftAttr, rightExpr));
                }
            }
            throw new ApplicationException(String.Concat("Unrecognized Reference expression: ", node.OuterXml));
        }
        public void Convert(Connection connection, BaseEntity sourceEntity, BaseDto dto, PropertyInfo sourcePropertyInfo, ReferenceAttribute referenceAttribute, ReferenceString referenceString)
        {
            PropertyInfo    referencedEntityPropertyInfo = sourceEntity.GetType().GetProperty(referenceAttribute.RefencedPropertyName);
            IEnumerable <U> referencedEntities           = (IEnumerable <U>)referencedEntityPropertyInfo.GetValue(sourceEntity, null);

            if (referencedEntities == null)
            {
                return;
            }

            ReferenceString referencedString = new ReferenceString(string.Empty);

            foreach (U referencedEntity in referencedEntities)
            {
                referencedString.Append(referencedEntity.Id, referencedEntity.ToString());
            }
            sourcePropertyInfo.SetValue(dto, referencedString);
        }