예제 #1
0
        /// <summary>
        /// Peek at the id in a table and only create wrapper if id is in given set
        /// </summary>
        private static bool LoadById(Table table, IConfigurationItem configuration, int[] ids)
        {
            if (ids == null)
            {
                return(true);
            }

            var fieldConfiguration = configuration.FieldConfigurations.FirstOrDefault(x => x.ReferenceFieldName == CoreFieldReferenceNames.Id);

            if (fieldConfiguration == null)
            {
                return(false);
            }

            var range = WordSyncHelper.GetCellRange(table, fieldConfiguration.RowIndex, fieldConfiguration.ColIndex);
            int id;

            if (range == null || int.TryParse(range.Text.Replace("\r\a", string.Empty), out id) == false)
            {
                return(false);
            }

            return(ids.Contains(id));
        }
예제 #2
0
        private static IConfigurationItem GetConfigurationItemFromTable(Table table, IEnumerable <IConfigurationItem> definitions)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }

            var tableRowsCountCache    = table.Rows.Count;
            var tableColumnsCountCache = table.Columns.Count;
            var tableCellRangesCache   = new Range[tableRowsCountCache, tableColumnsCountCache];

            IConfigurationItem exactConfiguration = null;
            IConfigurationItem regexConfiguration = null;

            foreach (var configuration in definitions)
            {
                // Get the cell range.
                if (tableRowsCountCache < configuration.ReqTableCellRow || tableColumnsCountCache < configuration.ReqTableCellCol)
                {
                    continue;
                }


                var range = tableCellRangesCache[configuration.ReqTableCellRow - 1, configuration.ReqTableCellCol - 1];
                if (range == null)
                {
                    try
                    {
                        range = WordSyncHelper.GetCellRange(table, configuration.ReqTableCellRow, configuration.ReqTableCellCol);
                        tableCellRangesCache[configuration.ReqTableCellRow - 1, configuration.ReqTableCellCol - 1] = range;
                    }
                    catch (ConfigurationException)
                    {
                        SyncServiceTrace.I(Resources.TheReferencesTableIdentifierCellNotExists, configuration.ReqTableCellRow, configuration.ReqTableCellCol);
                        continue;
                    }
                }

                if (range == null)
                {
                    continue;
                }

                // Match cell text with exact work item type mapping
                var cellText = WordSyncHelper.FormatCellContent(range.Text);
                if (configuration.WorkItemTypeMapping.Equals(cellText))
                {
                    exactConfiguration = configuration;
                }
                // Compare the regex expression for a direct mathc
                if (configuration.ReqTableIdentifierExpression.Equals(cellText))
                {
                    exactConfiguration = configuration;
                }

                // Check regular expression.
                if (!string.IsNullOrEmpty(configuration.ReqTableIdentifierExpression) &&
                    Regex.IsMatch(cellText, configuration.ReqTableIdentifierExpression))
                {
                    regexConfiguration = configuration;
                }
            }

            //If a exact match is available return it, else the regex
            if (exactConfiguration != null)
            {
                return(exactConfiguration);
            }
            else if (regexConfiguration != null)
            {
                return(regexConfiguration);
            }
            else
            {
                return(null);
            }
        }
예제 #3
0
        /// <summary>
        /// Creates a new <see cref="IWorkItem"/> object in this document.
        /// </summary>
        /// <param name="configuration">The configuration of the work item to create</param>
        /// <returns>The new <see cref="IWorkItem"/> object or null if the adapter failed to create work item.</returns>
        public IWorkItem CreateNewWorkItem(IConfigurationItem configuration)
        {
            Guard.ThrowOnArgumentNull(configuration, "configuration");

            SyncServiceTrace.D(Resources.CreateWorkItemBasedOnTemplate + configuration.RelatedTemplateFile);
            SyncServiceTrace.D("Assembly is " + Assembly.GetExecutingAssembly().Location);

            if (!File.Exists(configuration.RelatedTemplateFile))
            {
                SyncServiceTrace.E(Resources.LogService_Export_NoFile);
                return(null);
            }

            // Check if cursor is not in table, add new paragraph
            var selection = Document.ActiveWindow.Selection;

            if (selection == null)
            {
                SyncServiceTrace.E(Resources.SelectionNotExists);
                return(null);
            }

            if (WordSyncHelper.IsCursorBehindTable(selection))
            {
                selection.TypeParagraph();
            }

            if (WordSyncHelper.IsCursorInTable(selection))
            {
                return(null);
            }

            // insert template. Selection is collapsed to end when inserting,
            // so note old start to get range where file is inserted
            var fullPath = Path.GetFullPath(configuration.RelatedTemplateFile);

            SyncServiceTrace.D(Resources.FullPathOfTemplateFile + fullPath);
            var oldStart = selection.Start;

            if (!File.Exists(fullPath))
            {
                SyncServiceTrace.E(Resources.LogService_Export_NoFile);
                return(null);
            }


            selection.InsertFile(fullPath, ConfirmConversions: false);

            Range tableRange = Document.Range(oldStart, selection.End);

            Table table = tableRange.Tables.Cast <Table>().FirstOrDefault();

            if (table == null)
            {
                SyncServiceTrace.E(Resources.LogService_Export_NoTable);
            }
            else
            {
                // Add table marker if none is defined
                var tableRange2 = WordSyncHelper.GetCellRange(table, configuration.ReqTableCellRow, configuration.ReqTableCellCol);
                if (tableRange2 != null)
                {
                    if (tableRange2.Text.Replace("\r\a", string.Empty).Length == 0)
                    {
                        tableRange2.Text = configuration.WorkItemType + "\r\a";
                    }
                }

                var newItem = new WordTableWorkItem(table, configuration.WorkItemType, Configuration, configuration);
                WorkItems.Add(newItem);
                return(newItem);
            }

            return(null);
        }