Exemplo n.º 1
0
        /// <exclude />
        public static Dictionary<string, string> PreparePiggybag(this Dictionary<string, string> piggybag, TreeNode parentNode, EntityToken parentEntityToken)
        {
            var newPiggybag = new Dictionary<string, string>();

            foreach (KeyValuePair<string, string> kvp in piggybag)
            {
                if (kvp.Key.StartsWith(ParentEntityTokenPiggybagString))
                {
                    int generation = int.Parse(kvp.Key.Substring(ParentEntityTokenPiggybagString.Length));

                    generation += 1;

                    newPiggybag.Add(string.Format("{0}{1}", ParentEntityTokenPiggybagString, generation), kvp.Value);
                }
                else if (kvp.Key.StartsWith(ParentNodeIdPiggybagString))
                {
                    int generation = int.Parse(kvp.Key.Substring(ParentNodeIdPiggybagString.Length));

                    generation += 1;

                    newPiggybag.Add(string.Format("{0}{1}", ParentNodeIdPiggybagString, generation), kvp.Value);
                }
                else
                {
                    newPiggybag.Add(kvp.Key, kvp.Value);
                }
            }

            newPiggybag.Add(string.Format("{0}1", ParentEntityTokenPiggybagString), EntityTokenSerializer.Serialize(parentEntityToken));
            newPiggybag.Add(string.Format("{0}1", ParentNodeIdPiggybagString), parentNode.Id.ToString());

            return newPiggybag;
        }
Exemplo n.º 2
0
        private bool IsInParentTree(TreeNode treeNode)
        {
            TreeNode searchTreeNode = treeNode.ParentNode;
            while (searchTreeNode != null)
            {
                var node = searchTreeNode as DataElementsTreeNode;
                if (node != null)
                {
                    if (node.InterfaceType == this.OwnerNode.InterfaceType)
                    {
                        // Found in parent tree.
                        return true;
                    }

                    searchTreeNode = searchTreeNode.ParentNode;
                }
                else
                {
                    searchTreeNode = searchTreeNode.ParentNode;
                }
            }

            return false;
        }
Exemplo n.º 3
0
 /// <exclude />
 public void SetOwnerNode(TreeNode treeNode)
 {
     this.OwnerNode = (DataElementsTreeNode)treeNode;
 }
Exemplo n.º 4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ownerTreeNode">This is only used to add validation errors</param>
        private void Initialize_DataFieldValueHelper(TreeNode ownerTreeNode)
        {
            if (DataFieldValueHelper.ContainsDataField(this.Template) == false) return;

            this.DataFieldValueHelper = new DataFieldValueHelper(this.Template);
            this.DataFieldValueHelper.Initialize(ownerTreeNode);
        }
Exemplo n.º 5
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="ownerTreeNode">This is only used to add validation errors</param>
 public void Initialize(TreeNode ownerTreeNode)
 {
     Initialize_DataFieldValueHelper(ownerTreeNode);
 }
Exemplo n.º 6
0
 internal bool IsDescendant(TreeNode treeNode)
 {
     return this.ChildNodes.Any(childNode => childNode.Id == treeNode.Id || childNode.IsDescendant(treeNode));
 }
Exemplo n.º 7
0
        internal bool IsAncestor(TreeNode treeNode)
        {
            TreeNode ancestorNode = this.ParentNode;

            while (ancestorNode != null)
            {
                if (ancestorNode.Id == treeNode.Id) return true;

                ancestorNode = ancestorNode.ParentNode;
            }

            return false;
        }
Exemplo n.º 8
0
 internal void RemoveChildTreeNode(TreeNode treeNode)
 {
     _childNodes.Remove(treeNode);
 }
Exemplo n.º 9
0
        internal TreeNode AddChildTreeNode(TreeNode treeNode)
        {
            treeNode.ParentNode = this;
            _childNodes.Add(treeNode);

            return this;
        }
Exemplo n.º 10
0
 /// <exclude />
 public AncestorResult(TreeNode treeNode, EntityToken entityToken)
 {
     this.TreeNode = treeNode;
     this.EntityToken = entityToken;
 }
Exemplo n.º 11
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ownerTreeNode">This is only used to add validation errors</param>
        public void Initialize(TreeNode ownerTreeNode)
        {
            foreach (Match match in TemplateRegex.Matches(this.Template))
            {
                if (match.Value.StartsWith(PreFix) == false) continue;

                string value = match.Groups["string"].Value;
                string[] values = value.Split(':');
                if (values.Length != 4)
                {
                    ownerTreeNode.AddValidationError("TreeValidationError.DataFieldValueHelper.WrongFormat", match.Value, string.Format(@"{0}[InterfaceType]:[FieldName]}}", PreFix));
                    return;
                }


                string typeName = values[2];
                Type interfaceType = TypeManager.TryGetType(typeName);
                if (interfaceType == null)
                {
                    ownerTreeNode.AddValidationError("TreeValidationError.Common.UnknownInterfaceType", typeName);
                    return;
                }

                if (typeof(IData).IsAssignableFrom(interfaceType) == false)
                {
                    ownerTreeNode.AddValidationError("TreeValidationError.Common.NotImplementingIData", interfaceType, typeof(IData));
                    return;
                }


                string fieldName = values[3];
                PropertyInfo propertyInfo = interfaceType.GetPropertiesRecursively(f => f.Name == fieldName).SingleOrDefault();
                if (propertyInfo == null)
                {
                    ownerTreeNode.AddValidationError("TreeValidationError.Common.MissingProperty", interfaceType, fieldName);
                    return;
                }

                bool possibleAttachmentPointsExist = ownerTreeNode.Tree.PossibleAttachmentPoints.OfType<IDataItemAttachmentPoint>().Any(f => f.InterfaceType == interfaceType);
                bool attachmentPointsExist = ownerTreeNode.Tree.AttachmentPoints.OfType<IDataItemAttachmentPoint>().Any(f => f.InterfaceType == interfaceType);
                if (!possibleAttachmentPointsExist 
                    && !attachmentPointsExist 
                    && !ownerTreeNode.SelfAndParentsHasInterface(interfaceType))
                {
                    ownerTreeNode.AddValidationError("TreeValidationError.DataFieldValueHelper.InterfaceNotInParentTree", interfaceType);
                    return;
                }

                bool isReferencingProperty = DataReferenceFacade.GetForeignKeyProperties(interfaceType).Any(f => f.SourcePropertyInfo.Equals(propertyInfo));

                DataFieldValueHelperEntry entry = new DataFieldValueHelperEntry(
                    match.Value,
                    interfaceType,
                    propertyInfo,
                    isReferencingProperty
                );

                if (_entries.Contains(entry) == false)
                {
                    _entries.Add(entry);
                }
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// This method finds the callers tree node own entity token by searching up/down using filters.
        /// In some cases this can be expensive.
        /// </summary>
        /// <param name="treeNode"></param>
        /// <param name="entityToken"></param>
        /// <param name="dynamicContext"></param>
        /// <returns></returns>
        private EntityToken FindOwnEntityToken(TreeNode treeNode, EntityToken entityToken, TreeNodeDynamicContext dynamicContext)
        {
            DataElementsTreeNode dataElementsTreeNode = treeNode as DataElementsTreeNode;
            if (dataElementsTreeNode != null)
            {
                if (dataElementsTreeNode.InterfaceType == this.OwnerNode.InterfaceType) // We found it :)
                {
                    return entityToken;
                }
            }


            TreeDataFieldGroupingElementEntityToken dataFieldGroupingElementEntityToken = entityToken as TreeDataFieldGroupingElementEntityToken;
            if (dataFieldGroupingElementEntityToken != null) // Search 'downwards'
            {

                ParameterExpression parameterExpression = Expression.Parameter(this.OwnerNode.InterfaceType, "data");

                DataKeyPropertyCollection dataKeyPropertyCollection = new DataKeyPropertyCollection();
                dataKeyPropertyCollection.AddKeyProperty("Id", dataFieldGroupingElementEntityToken.ChildGeneratingDataElementsReferenceValue);

                Type dataType = dataFieldGroupingElementEntityToken.ChildGeneratingDataElementsReferenceType;

                IData parentData = DataFacade.GetDataByUniqueKey(dataType, dataKeyPropertyCollection);

                TreeNodeDynamicContext dummyContext = new TreeNodeDynamicContext(TreeNodeDynamicContextDirection.Down);
                dummyContext.CustomData.Add("ParentData", parentData);

                IData resultData;
                if (this.OwnerNode.InterfaceType == TypeManager.GetType(dataFieldGroupingElementEntityToken.Type))
                {
                    Expression filterExpression = this.CreateDownwardsFilterExpression(parameterExpression, dummyContext);

                    Expression whereExpression = ExpressionHelper.CreateWhereExpression(DataFacade.GetData(this.OwnerNode.InterfaceType).Expression, parameterExpression, filterExpression);

                    resultData = (IData)DataFacade.GetData(this.OwnerNode.InterfaceType).Provider.CreateQuery(whereExpression).ToEnumerableOfObjects().First();
                }
                else
                {
                    resultData = parentData;
                }

                return resultData.GetDataEntityToken();
            }


            AncestorResult ancestorResult = treeNode.GetParentEntityToken(entityToken, this.ParentFilterType, dynamicContext);

            if ((this.OwnerNode.Id == ancestorResult.TreeNode.Id) || (this.OwnerNode.IsDescendant(ancestorResult.TreeNode))) // Search upwards
            {
                return FindOwnEntityToken(ancestorResult.TreeNode, ancestorResult.EntityToken, dynamicContext);
            }


            // Search 'downwards' by using the parent datas key value to get 
            DataElementsTreeNode parentDataElementsTreeNode = (DataElementsTreeNode)ancestorResult.TreeNode;
            if (this.ParentFilterType == parentDataElementsTreeNode.InterfaceType)
            {
                DataEntityToken dataEntityToken = (DataEntityToken)ancestorResult.EntityToken;

                ParameterExpression parameterExpression = Expression.Parameter(this.OwnerNode.InterfaceType, "data");

                TreeNodeDynamicContext dummyContext = new TreeNodeDynamicContext(TreeNodeDynamicContextDirection.Down);
                dummyContext.CustomData.Add("ParentData", dataEntityToken.Data);

                Expression filterExpression = this.CreateDownwardsFilterExpression(parameterExpression, dummyContext);

                Expression whereExpression = ExpressionHelper.CreateWhereExpression(DataFacade.GetData(this.OwnerNode.InterfaceType).Expression, parameterExpression, filterExpression);

                IData resultData = (IData)DataFacade.GetData(this.OwnerNode.InterfaceType).Provider.CreateQuery(whereExpression).ToEnumerableOfObjects().First();

                return resultData.GetDataEntityToken();
            }


            throw new InvalidOperationException("Missing parent id filtering or try to simplify the parent id filterings (Unable to find own entity token)");
        }
Exemplo n.º 13
0
        private object Find(TreeNode currentTreeNode, EntityToken currentEntityToken)
        {
            if (this.OwnerNode.IsAncestor(currentTreeNode)) // Is parent
            {
                return currentTreeNode;
            }

            if (this.OwnerNode.Id != currentTreeNode.Id) // Is child
            {
                IEnumerable<ParentIdFilterNode> parentIdFilterNodes = currentTreeNode.FilterNodes.OfType<ParentIdFilterNode>();

                foreach (ParentIdFilterNode parentIdFilterNode in parentIdFilterNodes)
                {
                    Find(parentIdFilterNode.ParentFilterTypeTreeNode, null);
                }
            }

            foreach (TreeNode childTreeNode in this.OwnerNode.ChildNodes)
            {
                object res = Find(childTreeNode, null);
                if (res != null)
                {
                    return res;
                }
            }

            return null;
        }