Пример #1
0
        public IMoBiCommand AddFormulaToCacheOrFixReferenceCommand <T>(IBuildingBlock targetBuildingBlock, T usingFormulaObject, FormulaDecoder <T> decoder)
        {
            if (decoder.GetFormula(usingFormulaObject) == null)
            {
                return(new MoBiEmptyCommand());
            }

            var existingFormula = targetBuildingBlock.FormulaCache.FindByName(decoder.GetFormula(usingFormulaObject).Name);

            // There is no existing formula in the cache with the same name
            if (existingFormula == null)
            {
                return(new AddFormulaToFormulaCacheCommand(targetBuildingBlock, decoder.GetFormula(usingFormulaObject)));
            }

            // There is a formula with the same name, but it is not equal to the formula in the object
            if (!_formulaTask.FormulasAreTheSame(existingFormula, decoder.GetFormula(usingFormulaObject)))
            {
                _nameCorrector.AutoCorrectName(targetBuildingBlock.FormulaCache.Select(formula => formula.Name), decoder.GetFormula(usingFormulaObject));
                return(new AddFormulaToFormulaCacheCommand(targetBuildingBlock, decoder.GetFormula(usingFormulaObject)));
            }

            // not null and formulas are the same
            decoder.SetFormula(existingFormula, usingFormulaObject);
            return(new MoBiEmptyCommand());
        }
        public virtual void Merge(ICache <string, T> merge, ICache <string, T> target, Func <T, T, bool> areElementsEquivalent = null)
        {
            var resolved            = 0;
            var conflictResolution  = areElementsEquivalent ?? ((x, y) => false);
            var conflictingElements = GetConflictingElements(merge, target, conflictResolution).ToList();

            var option = MergeConflictOptions.SkipOnce;

            foreach (var key in conflictingElements)
            {
                resolved++;
                if (!option.IsAppliedToAll())
                {
                    option = prompt(merge[key], target[key], conflictingElements.Count() - resolved);
                }

                if (option.IsClone())
                {
                    if (option.IsAutoRename())
                    {
                        _nameCorrector.AutoCorrectName(AppConstants.UnallowedNames.Union(target.Select(targetEntity => targetEntity.Name)), merge[key]);
                    }
                    else
                    {
                        if (!_nameCorrector.CorrectName(AppConstants.UnallowedNames.Union(target.Select(targetEntity => targetEntity.Name)), merge[key]))
                        {
                            return;
                        }
                    }
                    _cloneAction(merge[key], key);
                }
                else if (option.IsMerge())
                {
                    _mergeAction(target[key], merge[key]);
                }

                else if (option.IsSkip())
                {
                    continue;
                }

                else if (option == MergeConflictOptions.Cancel)
                {
                    _context.PublishEvent(new MergeCanceledEvent());
                    _cancelAction();
                    return;
                }
                else
                {
                    _removeAction(target[key]);
                    _addAction(merge[key]);
                }
            }

            GetElementsToAdd(merge, target).Each(_addAction);
        }