/// <summary>
        /// Gets a cell.
        /// </summary>
        /// <param name="sectionCellLocator">The section cell locator.</param>
        /// <param name="currentCell">The current cell.</param>
        /// <returns>
        /// The cell.
        /// </returns>
        public ICell GetCell(
            SectionCellLocator sectionCellLocator,
            ICell currentCell)
        {
            if (sectionCellLocator == null)
            {
                throw new ArgumentNullException(nameof(sectionCellLocator));
            }

            if (currentCell == null)
            {
                throw new ArgumentNullException(nameof(currentCell));
            }

            if (!this.cellToSectionIdMap.TryGetValue(currentCell, out var sectionId))
            {
                throw new CellNotFoundException(Invariant($"{nameof(currentCell)} is not a cell in the report."), sectionCellLocator);
            }

            var reportCellLocator = new ReportCellLocator(sectionId, sectionCellLocator.CellId, sectionCellLocator.SlotId, sectionCellLocator.SlotSelectionStrategy);

            var result = this.GetCell(reportCellLocator);

            return(result);
        }
示例#2
0
        /// <summary>
        /// Builds a <see cref="ReportCellLocator"/>.
        /// </summary>
        /// <param name="sectionId">The id of the section that contains the cell.</param>
        /// <param name="cellId">The id of the cell.</param>
        /// <param name="slotId">OPTIONAL id of the slot to use -OR- null if not addressing an <see cref="ISlottedCell"/>.  DEFAULT is to address an <see cref="INotSlottedCell"/>.</param>
        /// <param name="slotSelectionStrategy">OPTIONAL strategy to use to select a slot if addressing an <see cref="ISlottedCell"/>.  DEFAULT is to throw if addressing an <see cref="ISlottedCell"/> -AND- <paramref name="slotId"/> is not specified.</param>
        /// <returns>
        /// A <see cref="ReportCellLocator"/>.
        /// </returns>
        public static ReportCellLocator InThisReport(
            string sectionId,
            string cellId,
            string slotId = null,
            SlotSelectionStrategy slotSelectionStrategy = SlotSelectionStrategy.ThrowIfSlotIdNotSpecified)
        {
            var result = new ReportCellLocator(sectionId, cellId, slotId, slotSelectionStrategy);

            return(result);
        }
        /// <summary>
        /// Gets a cell.
        /// </summary>
        /// <typeparam name="TCell">The type of cell to return.</typeparam>
        /// <param name="reportCellLocator">The report cell locator.</param>
        /// <returns>
        /// The cell.
        /// </returns>
        public TCell GetCell <TCell>(
            ReportCellLocator reportCellLocator)
            where TCell : ICell
        {
            var cell = this.GetCell(reportCellLocator);

            if (!(cell is TCell result))
            {
                throw new CellNotFoundException(Invariant($"The operation addresses a cell of type {cell.GetType().ToStringReadable()}, which is not assignable to the specified {nameof(TCell)}."), reportCellLocator);
            }

            return(result);
        }
        /// <summary>
        /// Gets a cell.
        /// </summary>
        /// <param name="reportCellLocator">The report cell locator.</param>
        /// <returns>
        /// The cell.
        /// </returns>
        public ICell GetCell(
            ReportCellLocator reportCellLocator)
        {
            if (reportCellLocator == null)
            {
                throw new ArgumentNullException(nameof(reportCellLocator));
            }

            var sectionId = reportCellLocator.SectionId;

            if (!this.sectionIdToCellIdToCellMap.ContainsKey(sectionId))
            {
                throw new CellNotFoundException(Invariant($"There is no section with id '{sectionId}'."), reportCellLocator);
            }

            var cellIdToCellMap = this.sectionIdToCellIdToCellMap[sectionId];

            var cellId = reportCellLocator.CellId;

            if (!cellIdToCellMap.TryGetValue(cellId, out var cell))
            {
                throw new CellNotFoundException(Invariant($"There is no cell with id '{cellId}' in section '{sectionId}'."), reportCellLocator);
            }

            ICell result;

            var slotId = reportCellLocator.SlotId;

            if (string.IsNullOrWhiteSpace(slotId))
            {
                result = cell;
            }
            else
            {
                if (cell is ISlottedCell slottedCell)
                {
                    if (slottedCell.SlotIdToCellMap.ContainsKey(slotId))
                    {
                        result = slottedCell.SlotIdToCellMap[slotId];
                    }
                    else
                    {
                        throw new CellNotFoundException(Invariant($"Slot id '{slotId}' was specified, but the addressed cell '{cellId}' in section '{sectionId}' does not contain a slot having that id."), reportCellLocator);
                    }
                }
                else
                {
                    throw new CellNotFoundException(Invariant($"Slot id '{slotId}' was specified, but the addressed cell '{cellId}' in section '{sectionId}' is not a slotted cell."), reportCellLocator);
                }
            }

            if (result is ISlottedCell addressedSlottedCell)
            {
                if (reportCellLocator.SlotSelectionStrategy == SlotSelectionStrategy.DefaultSlot)
                {
                    result = addressedSlottedCell.SlotIdToCellMap[addressedSlottedCell.DefaultSlotId];
                }
                else if (reportCellLocator.SlotSelectionStrategy == SlotSelectionStrategy.ThrowIfSlotIdNotSpecified)
                {
                    throw new CellNotFoundException(Invariant($"The operation addresses an {nameof(ISlottedCell)} (and not a slot within that cell) and {nameof(SlotSelectionStrategy)} is {nameof(SlotSelectionStrategy.ThrowIfSlotIdNotSpecified)}."), reportCellLocator);
                }
                else
                {
                    throw new NotSupportedException(Invariant($"This {nameof(SlotSelectionStrategy)} is not supported: {reportCellLocator.SlotSelectionStrategy}."));
                }
            }

            return(result);
        }