Beispiel #1
0
        private Element CreateElement(EntityToken parentEntityToken, Type referenceType, object referenceValue, TreeNodeDynamicContext dynamicContext, string label, Action <TreeDataFieldGroupingElementEntityToken> entityTokenAction)
        {
            var entityToken = new TreeDataFieldGroupingElementEntityToken(this.Id.ToString(), this.Tree.TreeId, this.SerializedInterfaceType)
            {
                GroupingValues    = new Dictionary <string, object>(dynamicContext.FieldGroupingValues),
                FolderRangeValues = new Dictionary <string, int>(dynamicContext.FieldFolderRangeValues),
                ChildGeneratingDataElementsReferenceType  = referenceType,
                ChildGeneratingDataElementsReferenceValue = referenceValue
            };

            entityTokenAction(entityToken);

            var element = new Element(new ElementHandle(
                                          dynamicContext.ElementProviderName,
                                          entityToken,
                                          dynamicContext.Piggybag.PreparePiggybag(this.ParentNode, parentEntityToken)
                                          ))
            {
                VisualData = new ElementVisualizedData
                {
                    Label       = label,
                    ToolTip     = label,
                    HasChildren = true,
                    Icon        = this.Icon,
                    OpenedIcon  = this.Icon
                }
            };

            element.ElementExternalActionAdding = element.ElementExternalActionAdding.Remove(ElementExternalActionAdding.AllowManageUserPermissions);

            return(element);
        }
Beispiel #2
0
        /// <exclude />
        public override AncestorResult GetParentEntityToken(EntityToken childEntityToken, Type parentInterfaceOfInterest, TreeNodeDynamicContext dynamicContext)
        {
            if (this.ParentNode is DataFolderElementsTreeNode)
            {
                var childGroupingElementEntityToken = childEntityToken as TreeDataFieldGroupingElementEntityToken;

                if (childGroupingElementEntityToken != null)
                {
                    var newGroupingElementEntityToken = new TreeDataFieldGroupingElementEntityToken(
                        this.ParentNode.Id,
                        this.Tree.TreeId,
                        this.SerializedInterfaceType)
                    {
                        GroupingValues =
                            new Dictionary <string, object>(childGroupingElementEntityToken.GroupingValues),
                        FolderRangeValues =
                            new Dictionary <string, int>(childGroupingElementEntityToken.FolderRangeValues)
                    };

                    newGroupingElementEntityToken.GroupingValues.Remove(this.GroupingValuesFieldName);

                    return(new AncestorResult(this.ParentNode, newGroupingElementEntityToken));
                }
            }

            throw new NotImplementedException();
        }
Beispiel #3
0
        /// <exclude />
        public override IEnumerable <EntityToken> GetEntityTokens(EntityToken childEntityToken, TreeNodeDynamicContext dynamicContext)
        {
            IEnumerable <object> labels = GetCurrentAndLocalizableObjects <object>(dynamicContext, true);

            Type   childGeneratingDataElementsReferenceType  = null;
            object childGeneratingDataElementsReferenceValue = null;

            DataEntityToken dataEntityToken = childEntityToken as DataEntityToken;
            var             treeDataFieldGroupingElementEntityToken = childEntityToken as TreeDataFieldGroupingElementEntityToken;

            if (dataEntityToken != null)
            {
                if (this.ChildGeneratingParentIdFilterNode != null)
                {
                    childGeneratingDataElementsReferenceType  = this.ChildGeneratingParentIdFilterNode.ParentFilterType;
                    childGeneratingDataElementsReferenceValue = this.ChildGeneratingParentIdFilterNode.ReferencePropertyInfo.GetValue(dataEntityToken.Data, null);
                }
            }
            else if (treeDataFieldGroupingElementEntityToken != null)
            {
                childGeneratingDataElementsReferenceType  = treeDataFieldGroupingElementEntityToken.ChildGeneratingDataElementsReferenceType;
                childGeneratingDataElementsReferenceValue = treeDataFieldGroupingElementEntityToken.ChildGeneratingDataElementsReferenceValue;
            }


            foreach (object label in labels)
            {
                var tupleIndexer = new TupleIndexer(label);

                var entityToken = new TreeDataFieldGroupingElementEntityToken(this.Id, this.Tree.TreeId, TypeManager.SerializeType(this.InterfaceType))
                {
                    GroupingValues = new Dictionary <string, object>()
                };

                int index = 1;
                foreach (DataFolderElementsTreeNode dataFolderElementsTreeNode in this.AllGroupingNodes)
                {
                    if (dataFolderElementsTreeNode.FolderRanges == null)
                    {
                        object fieldValue = tupleIndexer.GetAtIndex(index++);
                        fieldValue = ConvertFieldValue(dataFolderElementsTreeNode, fieldValue);
                        entityToken.GroupingValues.Add(dataFolderElementsTreeNode.GroupingValuesFieldName, fieldValue);
                    }
                    else
                    {
                        int fieldValue = (int)tupleIndexer.GetAtIndex(index++);
                        entityToken.FolderRangeValues.Add(dataFolderElementsTreeNode.FieldName, fieldValue);
                    }
                }

                entityToken.ChildGeneratingDataElementsReferenceType  = childGeneratingDataElementsReferenceType;
                entityToken.ChildGeneratingDataElementsReferenceValue = childGeneratingDataElementsReferenceValue;

                yield return(entityToken);
            }
        }
Beispiel #4
0
        /// <exclude />
        public static EntityToken Deserialize(string serializedEntityToken)
        {
            string type, source, id;
            Dictionary <string, string> dic;

            DoDeserialize(serializedEntityToken, out type, out source, out id, out dic);

            TreeDataFieldGroupingElementEntityToken entityToken = new TreeDataFieldGroupingElementEntityToken(id, source, type, dic);

            if (dic.ContainsKey("_ReferenceType_"))
            {
                string typeString = StringConversionServices.DeserializeValueString(dic["_ReferenceType_"]);

                entityToken.ChildGeneratingDataElementsReferenceType = TypeManager.GetType(typeString);
            }

            if (dic.ContainsKey("_ReferenceValueType_"))
            {
                Type referenceValueType = StringConversionServices.DeserializeValueType(dic["_ReferenceValueType_"]);
                entityToken.ChildGeneratingDataElementsReferenceValue = StringConversionServices.DeserializeValue(dic["_ReferenceValue_"], referenceValueType);
            }

            return(entityToken);
        }
        public Dictionary <EntityToken, IEnumerable <EntityToken> > GetParents(IEnumerable <EntityToken> entityTokens)
        {
            Dictionary <EntityToken, IEnumerable <EntityToken> > result = new Dictionary <EntityToken, IEnumerable <EntityToken> >();

            foreach (EntityToken entityToken in entityTokens)
            {
                TreeNodeDynamicContext dynamicContext = new TreeNodeDynamicContext(TreeNodeDynamicContextDirection.Up)
                {
                    CurrentEntityToken = entityToken
                };


                if (entityToken is TreeSimpleElementEntityToken ||
                    entityToken is TreeFunctionElementGeneratorEntityToken)
                {
                    var parentEntityTokenSource = entityToken as IEntityTokenContainingParentEntityToken;

                    try
                    {
                        result.Add(entityToken, new[] { parentEntityTokenSource.GetParentEntityToken() });
                    }
                    catch (Exception ex)
                    {
                        string treeId;

                        if (entityToken is TreeSimpleElementEntityToken)
                        {
                            treeId = (entityToken as TreeSimpleElementEntityToken).TreeNodeId;
                        }
                        else if (entityToken is TreeFunctionElementGeneratorEntityToken)
                        {
                            treeId = (entityToken as TreeFunctionElementGeneratorEntityToken).TreeNodeId;
                        }
                        else
                        {
                            throw new InvalidOperationException("This code should not be reachable.");
                        }

                        Log.LogError("TreeFacade", "The tree '{0}' failed to return parent entity tokens and are ignored", treeId);
                        Log.LogError("TreeFacade", ex);
                    }
                }
                else if (entityToken is TreeDataFieldGroupingElementEntityToken)
                {
                    TreeDataFieldGroupingElementEntityToken treeDataFieldGroupingElementEntityToken = entityToken as TreeDataFieldGroupingElementEntityToken;
                    string treeId = entityToken.Source;

                    try
                    {
                        Tree tree = TreeFacade.GetTree(treeId);

                        string   treeNodeId = treeDataFieldGroupingElementEntityToken.TreeNodeId;
                        TreeNode treeNode   = tree.GetTreeNode(treeNodeId);

                        dynamicContext.FieldGroupingValues    = treeDataFieldGroupingElementEntityToken.GroupingValues;
                        dynamicContext.FieldFolderRangeValues = treeDataFieldGroupingElementEntityToken.FolderRangeValues;
                        dynamicContext.CurrentTreeNode        = treeNode;

                        result.Add(entityToken, treeNode.ParentNode.GetEntityTokens(entityToken, dynamicContext));
                    }
                    catch (Exception ex)
                    {
                        Log.LogError("TreeFacade", $"The tree '{treeId}' failed to return parent entity tokens and are ignored");
                        Log.LogError("TreeFacade", ex);
                    }
                }
                else if (entityToken is DataEntityToken)
                {
                    DataEntityToken dataEntityToken = entityToken as DataEntityToken;

                    Type interfaceType = dataEntityToken.InterfaceType;

                    foreach (Tree tree in TreeFacade.AllTrees)
                    {
                        List <TreeNode> treeNodes;
                        if (!tree.BuildProcessContext.DataInteraceToTreeNodes.TryGetValue(interfaceType, out treeNodes))
                        {
                            continue;
                        }

                        IEnumerable <EntityToken> concatList = null;

                        foreach (TreeNode treeNode in treeNodes)
                        {
                            try
                            {
                                dynamicContext.CurrentTreeNode = treeNode;

                                concatList = concatList.ConcatOrDefault(treeNode.ParentNode.GetEntityTokens(entityToken, dynamicContext));
                            }
                            catch (Exception ex)
                            {
                                Log.LogError("TreeFacade", $"The tree '{treeNode.Tree.TreeId}' failed to return parent entity tokens and are ignored");
                                Log.LogError("TreeFacade", ex);
                            }
                        }

                        if (concatList != null)
                        {
                            // Filtering the current element to avoid loops while resolving security
                            concatList = concatList.Where(e => !entityToken.Equals(e));

                            IEnumerable <EntityToken> existingList;
                            if (result.TryGetValue(entityToken, out existingList))
                            {
                                result[entityToken] = existingList.Concat(concatList);
                            }
                            else
                            {
                                result.Add(entityToken, concatList);
                            }
                        }
                    }
                }
                else if (entityToken is TreePerspectiveEntityToken)
                {
                    result.Add(entityToken, new[] { TreeSharedRootsFacade.SharedRootFolders[entityToken.Id].AttachmentPoint.AttachingPoint.EntityToken });
                }
            }

            return(result);
        }
Beispiel #6
0
        public IEnumerable <Element> GetChildren(EntityToken parentEntityToken, Dictionary <string, string> piggybag)
        {
            TreeSharedRootsFacade.Initialize(Context.ProviderName);

            List <Tree> trees;

            if (parentEntityToken is TreePerspectiveEntityToken)
            {
                if (TreeSharedRootsFacade.SharedRootFolders.ContainsKey(parentEntityToken.Id))
                {
                    trees = TreeSharedRootsFacade.SharedRootFolders[parentEntityToken.Id].Trees;
                }
                else
                {
                    trees = new List <Tree>();
                }
            }
            else
            {
                if (piggybag.ContainsKey(StringConstants.PiggybagTreeId))
                {
                    string treeId = piggybag.Where(f => f.Key == StringConstants.PiggybagTreeId).SingleOrDefault().Value;
                    Tree   tree   = TreeFacade.GetTree(treeId);
                    if (tree == null)
                    {
                        return new Element[] { }
                    }
                    ;
                    trees = new List <Tree> {
                        tree
                    };
                }
                else
                {
                    trees = new List <Tree>();

                    int counter = 0;
                    while (true)
                    {
                        string key = StringConstants.PiggybagSharedTreeId + (counter++);

                        if (!piggybag.ContainsKey(key))
                        {
                            break;
                        }

                        string treeId = piggybag[key];
                        Tree   tree   = TreeFacade.GetTree(treeId);
                        if (tree != null)
                        {
                            trees.Add(tree);
                        }
                    }
                }
            }

            IEnumerable <Element> result = new List <Element>();

            foreach (Tree tree in trees)
            {
                TreeNodeDynamicContext dynamicContext = new TreeNodeDynamicContext(TreeNodeDynamicContextDirection.Down)
                {
                    ElementProviderName = this.Context.ProviderName,
                    Piggybag            = piggybag,
                    CurrentEntityToken  = parentEntityToken
                };

                try
                {
                    if (parentEntityToken is TreePerspectiveEntityToken)
                    {
                        TreeNode treeNode = tree.RootTreeNode;

                        dynamicContext.CurrentTreeNode = treeNode;

                        IEnumerable <Element> elements = treeNode.ChildNodes.GetElements(parentEntityToken, dynamicContext);
                        result = result.ConcatOrDefault(elements);
                    }
                    else if (parentEntityToken is TreeSimpleElementEntityToken)
                    {
                        TreeNode treeNode = tree.GetTreeNode(parentEntityToken.Id);
                        if (treeNode == null)
                        {
                            throw new InvalidOperationException("Tree is out of sync");
                        }

                        dynamicContext.CurrentTreeNode = treeNode;

                        IEnumerable <Element> elements = treeNode.ChildNodes.GetElements(parentEntityToken, dynamicContext);
                        result = result.ConcatOrDefault(elements);
                    }
                    else if (parentEntityToken is TreeFunctionElementGeneratorEntityToken)
                    {
                        TreeNode treeNode = tree.GetTreeNode(parentEntityToken.Id);
                        if (treeNode == null)
                        {
                            throw new InvalidOperationException("Tree is out of sync");
                        }

                        dynamicContext.CurrentTreeNode = treeNode;

                        IEnumerable <Element> elements = treeNode.ChildNodes.GetElements(parentEntityToken, dynamicContext);
                        result = result.ConcatOrDefault(elements);
                    }
                    else if (parentEntityToken is TreeDataFieldGroupingElementEntityToken)
                    {
                        TreeDataFieldGroupingElementEntityToken castedParentEntityToken = parentEntityToken as TreeDataFieldGroupingElementEntityToken;
                        TreeNode treeNode = tree.GetTreeNode(parentEntityToken.Id);
                        if (treeNode == null)
                        {
                            throw new InvalidOperationException("Tree is out of sync");
                        }

                        dynamicContext.CurrentTreeNode        = treeNode;
                        dynamicContext.FieldGroupingValues    = castedParentEntityToken.GroupingValues;
                        dynamicContext.FieldFolderRangeValues = castedParentEntityToken.FolderRangeValues;

                        IEnumerable <Element> elements = treeNode.ChildNodes.GetElements(parentEntityToken, dynamicContext);
                        result = result.ConcatOrDefault(elements);
                    }
                    else if (parentEntityToken is DataEntityToken)
                    {
                        DataEntityToken dataEntityToken = parentEntityToken as DataEntityToken;

                        Type interfaceType = dataEntityToken.InterfaceType;

                        List <TreeNode> treeNodes;
                        if (tree.BuildProcessContext.DataInteraceToTreeNodes.TryGetValue(interfaceType, out treeNodes) == false)
                        {
                            throw new InvalidOperationException();
                        }

                        string parentNodeId = piggybag.GetParentIdFromPiggybag();

                        TreeNode treeNode = treeNodes.Where(f => f.ParentNode.Id == parentNodeId).SingleOrDefault();
                        if (treeNode == null)
                        {
                            throw new InvalidOperationException("Tree is out of sync");
                        }

                        dynamicContext.CurrentTreeNode = treeNode;

                        IEnumerable <Element> elements = treeNode.ChildNodes.GetElements(parentEntityToken, dynamicContext);
                        result = result.ConcatOrDefault(elements);
                    }
                    else
                    {
                        throw new NotImplementedException("Unhandled entityt token type");
                    }


                    result = result.Evaluate();
                }
                catch (Exception ex)
                {
                    LoggingService.LogError("TreeFacade", string.Format("Getting elements from the three '{0}' failed", tree.TreeId));
                    LoggingService.LogError("TreeFacade", ex);

                    Element errorElement = ShowErrorElementHelper.CreateErrorElement(
                        StringResourceSystemFacade.GetString("Composite.C1Console.Trees", "KeyFacade.ErrorTreeNode.Label"),
                        tree.TreeId,
                        ex.Message);

                    return(new Element[] { errorElement });
                }
            }

            return(result);
        }
    }
        /// <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)");
        }
        /// <exclude />
        public static EntityToken Deserialize(string serializedEntityToken)
        {
            string type, source, id;
            Dictionary<string, string> dic;

            DoDeserialize(serializedEntityToken, out type, out source, out id, out dic);

            TreeDataFieldGroupingElementEntityToken entityToken = new TreeDataFieldGroupingElementEntityToken(id, source, type, dic);

            if (dic.ContainsKey("_ReferenceType_"))
            {
                string typeString = StringConversionServices.DeserializeValueString(dic["_ReferenceType_"]);

                entityToken.ChildGeneratingDataElementsReferenceType = TypeManager.GetType(typeString);
            }

            if (dic.ContainsKey("_ReferenceValueType_"))
            {
                Type referenceValueType = StringConversionServices.DeserializeValueType(dic["_ReferenceValueType_"]);
                entityToken.ChildGeneratingDataElementsReferenceValue = StringConversionServices.DeserializeValue(dic["_ReferenceValue_"], referenceValueType);
            }

            return entityToken;
        }