public ProcessResult Merge(ProcessResult another)
		{
			if (another == null)
			{
				return this;
			}

			if (!another.Success)
			{
				foreach (var error in another.Errors)
				{
					AddError(error);
				}
			}

			foreach (var handledItem in another.HandledItems)
			{
				AddItemToHandled(handledItem);
			}

			Handled = Handled || another.Handled;

			return this;
		}
        /// <summary>
        /// Fills content with one content item
        /// </summary>
        /// <param name="contentControl">Content control</param>
        /// <param name="item">Content item</param>
        private ProcessResult FillContent(XContainer contentControl, IContentItem item)
        {
            if (!(item is TableContent))
            {
                return(ProcessResult.NotHandledResult);
            }

            var processResult = new ProcessResult();

            var table = item as TableContent;

            // Find the content control with Table Name
            var tableName = table.Name;

            // If there isn't a table with that name, add an error to the error string,
            // and continue with next table.
            if (contentControl == null)
            {
                processResult.Errors.Add(String.Format("Table Content Control '{0}' not found.",
                                                       tableName));

                return(processResult);
            }

            // If the table doesn't contain content controls in cells, then error and continue with next table.
            var cellContentControl = contentControl
                                     .Descendants(W.sdt)
                                     .FirstOrDefault();

            if (cellContentControl == null)
            {
                processResult.Errors.Add(String.Format(
                                             "Table Content Control '{0}' doesn't contain content controls in cells.",
                                             tableName));
                return(processResult);
            }

            var fieldNames = table.FieldNames.ToList();

            var prototypeRows = GetPrototype(contentControl, fieldNames);

            //Select content controls tag names
            var contentControlTagNames = prototypeRows
                                         .Descendants(W.sdt)
                                         .Select(sdt => sdt.SdtTagName())
                                         .Where(fieldNames.Contains);


            //If there are not content controls with the one of specified field name we need to add the warning
            if (contentControlTagNames.Intersect(fieldNames).Count() != fieldNames.Count())
            {
                processResult.Errors.Add(String.Format(
                                             "Table Content Control '{0}' doesn't contain rows with cell content controls {1}.",
                                             tableName,
                                             string.Join(", ", fieldNames.Select(fn => string.Format("'{0}'", fn)))));
                return(processResult);
            }


            // Create a list of new rows to be inserted into the document.  Because this
            // is a document centric transform, this is written in a non-functional
            // style, using tree modification.
            var newRows = new List <List <XElement> >();

            foreach (var row in table.Rows)
            {
                // Clone the prototypeRows into newRowsEntry.
                var newRowsEntry = prototypeRows.Select(prototypeRow => new XElement(prototypeRow)).ToList();

                // Create new rows that will contain the data that was passed in to this
                // method in the XML tree.
                foreach (var sdt in newRowsEntry.FirstLevelDescendantsAndSelf(W.sdt).ToList())
                {
                    // Get fieldName from the content control tag.
                    var fieldName = sdt.SdtTagName();

                    var content = row.GetContentItem(fieldName);

                    if (content != null)
                    {
                        var contentProcessResult = new ContentProcessor(_context)
                                                   .SetRemoveContentControls(_isNeedToRemoveContentControls)
                                                   .FillContent(sdt, content);

                        if (!contentProcessResult.Success)
                        {
                            processResult.Errors.AddRange(processResult.Errors);
                        }
                    }
                }

                // Add the newRow to the list of rows that will be placed in the newly
                // generated table.
                newRows.Add(newRowsEntry);
            }

            prototypeRows.Last().AddAfterSelf(newRows);

            // Remove the prototype rows
            prototypeRows.Remove();

            return(processResult);
        }
        private ProcessResult FillContent(XElement contentControl, IContentItem item)
        {
            var processResult = new ProcessResult();

            if (!(item is ListContent))
            {
                return(ProcessResult.NotHandledResult);
            }

            var list = item as ListContent;

            var listName = list.Name;

            // If there isn't a list with that name, add an error to the error string.
            if (contentControl == null)
            {
                processResult.Errors.Add(String.Format("List Content Control '{0}' not found.",
                                                       listName));

                return(processResult);
            }

            // If the list doesn't contain content controls in items, then error.
            var itemsContentControl = contentControl
                                      .Descendants(W.sdt)
                                      .FirstOrDefault();

            if (itemsContentControl == null)
            {
                processResult.Errors.Add(String.Format(
                                             "List Content Control '{0}' doesn't contain content controls in items.",
                                             listName));
                return(processResult);
            }

            var fieldNames = list.FieldNames.ToList();

            // Create a prototype of new items to be inserted into the document.
            var prototype = new Prototype(_context, contentControl, fieldNames);

            if (!prototype.IsValid)
            {
                processResult.Errors.Add(String.Format(
                                             "List Content Control '{0}' doesn't contain items with content controls {1}.",
                                             listName,
                                             string.Join(", ", fieldNames)));
                return(processResult);
            }

            new NumberingAccessor(_context.NumberingPart, _context.LastNumIds)
            .ResetNumbering(prototype.PrototypeItems);

            // Propagates a prototype.
            var propagationResult = PropagatePrototype(prototype, list.Items);

            if (!propagationResult.Success)
            {
                processResult.Errors.AddRange(propagationResult.Errors);
            }
            // Remove the prototype row and add all of the newly constructed rows.
            prototype.PrototypeItems.Last().AddAfterSelf(propagationResult.Result);
            prototype.PrototypeItems.Remove();

            return(processResult);
        }