Ejemplo n.º 1
0
        /// <summary>
        /// Chooses the building block to be used for a work item.
        /// </summary>
        /// <param name="workItemNode">The work item to choose the building block for.</param>
        /// <returns>The chosen building block.</returns>
        public BuildingBlock ChooseBuildingBlock(WorkItemTreeNode workItemNode)
        {
            if (workItemNode == null)
            {
                throw new ArgumentNullException("workItemNode");
            }

            BuildingBlock ans = null;

            if (ans == null)
            {
                ans = this.GetBuildingBlock(new BuildingBlockName(workItemNode.WorkItem.Type, workItemNode.Level));
            }

            if (ans == null)
            {
                ans = this.GetBuildingBlock(new BuildingBlockName(workItemNode.WorkItem.Type));
            }

            if (ans == null)
            {
                ans = this.GetBuildingBlock(new BuildingBlockName(workItemNode.Level));
            }

            if (ans == null)
            {
                ans = this.GetBuildingBlock(BuildingBlockName.Default);
            }

            return(ans);
        }
        /// <summary>
        /// Maps a single work item into the document.
        /// </summary>
        /// <param name="node">The node for the work item to map.</param>
        /// <param name="workItemLayout">The work item layout that is to be used.</param>
        /// <param name="bookmarkNamingFunction">A function to return the bookmark to use for the given work item id.</param>
        /// <param name="workItemsCustomXMLPart">The custom XML part that contains the work item data.</param>
        /// <param name="insertionPoint">The relative location where the work item is to be mapped.</param>
        /// <param name="relativeWorkItemId">If inserting relative to a work item, the id of the work item relative to which the insertion is to be done, otherwise ignored.</param>
        private void MapWorkItemIntoDocument(WorkItemTreeNode node, IWorkItemLayout workItemLayout, Func <int, string> bookmarkNamingFunction, CustomXMLPart workItemsCustomXMLPart, InsertionPoint insertionPoint, int relativeWorkItemId)
        {
            BuildingBlock buildingBlock = workItemLayout.ChooseBuildingBlock(node);
            IEnumerable <ContentControl> contentControls = null;

            switch (insertionPoint)
            {
            case InsertionPoint.CurrentLocation:
            {
                contentControls = this.wordDocument.InsertBuildingBlock(buildingBlock, bookmarkNamingFunction(node.WorkItem.Id));
                break;
            }

            case InsertionPoint.BeforeBookmark:
            {
                contentControls = this.wordDocument.InsertBuildingBlockBeforeBookmark(buildingBlock, bookmarkNamingFunction(node.WorkItem.Id), bookmarkNamingFunction(relativeWorkItemId));
                break;
            }

            case InsertionPoint.AfterBookmark:
            {
                contentControls = this.wordDocument.InsertBuildingBlockAfterBookmark(buildingBlock, bookmarkNamingFunction(node.WorkItem.Id), bookmarkNamingFunction(relativeWorkItemId));
                break;
            }
            }

            foreach (ContentControl c in contentControls)
            {
                if (Utilities.IsValidTag(c.Tag) && node.WorkItem.FieldReferenceNames.Contains(c.Tag))
                {
                    if (c.Type == WdContentControlType.wdContentControlText || c.Type == WdContentControlType.wdContentControlDate)
                    {
                        string xpath = string.Format(CultureInfo.InvariantCulture, "/wi:WorkItems/wi:WorkItem[wi:Field[@name='{0}']={1}]/wi:Field[@name='{2}']", Constants.SystemIdFieldReferenceName, node.WorkItem.Id, c.Tag);
                        this.wordDocument.MapContentControl(c, xpath, "xmlns:wi='" + Constants.WorkItemNamespace + "'", workItemsCustomXMLPart);
                    }
                    else if (c.Type == WdContentControlType.wdContentControlRichText)
                    {
                        this.wordDocument.PopulateRichTextContentControlWithHtml(c, node.WorkItem[c.Tag].ToString());
                        c.Tag = string.Concat(c.Tag, "-", node.WorkItem.Id.ToString(CultureInfo.InvariantCulture));
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Runs a tree query.
        /// </summary>
        /// <param name="query">The query to execute.</param>
        /// <param name="cancellationToken">Used to cancel the operation.</param>
        /// <returns>A tree of work items.</returns>
        private static WorkItemTree RunTreeQuery(ITfsQuery query, CancellationToken cancellationToken)
        {
            WorkItemTree ans = new WorkItemTree();

            WorkItemLinkInfo[] wilis = query.RunLinkQuery(cancellationToken);
            int[] ids = new int[wilis.Length];
            for (int i = 0; i < ids.Length; i++)
            {
                ids[i] = wilis[i].TargetId;
            }

            string workItemQueryString     = QueryUtilities.ConvertTreeQueryToQueryForItem(query);
            IList <ITfsWorkItem> workItems = query.WorkItemStore.Query(ids.Distinct().ToArray(), workItemQueryString);

            for (int i = 0; i < wilis.Length; i++)
            {
                WorkItemLinkInfo wili = wilis[i];
                ITfsWorkItem     wi   = workItems.Select(item => item).Where(item => item.Id == wili.TargetId).Single();
                if (wili.SourceId <= 0)
                {
                    ans.RootNodes.Add(new WorkItemTreeNode(wi, 0));
                }
                else
                {
                    WorkItemTreeNode parent = null;
                    try
                    {
                        parent = ans.DepthFirstNodes().Where(node => node.WorkItem.Id == wili.SourceId).Single();
                    }
                    catch (InvalidOperationException)
                    {
                        throw new InvalidOperationException(ModelResources.QueryNotWellFormedTree);
                    }

                    parent.Children.Add(new WorkItemTreeNode(wi, parent.Level + 1));
                }
            }

            return(ans);
        }