Beispiel #1
0
        private void AddFromTemplateCommand_Executed(object obj)
        {
            List <T2> items = new List <T2>();
            TLCGenTemplateModel <T2> template = SelectedTemplate as TLCGenTemplateModel <T2>;

            // No data provided from view, but a replace value is needed for this template
            if (obj == null && !string.IsNullOrWhiteSpace(template.Replace))
            {
                var dialog = new Dialogs.ApplyTemplateWindow();
                dialog.ShowDialog();
                string ApplyString = dialog.TemplateApplyString;
                if (!string.IsNullOrWhiteSpace(ApplyString))
                {
                    string   list  = ApplyString.Replace(" ", "");
                    string[] elems = list.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var elem in elems)
                    {
                        var tempitems = template.GetItems();
                        foreach (var item in tempitems)
                        {
                            var cloneditem = DeepCloner.DeepClone((T2)item);
                            ModelStringSetter.ReplaceStringInModel(cloneditem, template.Replace, elem);
                            items.Add(cloneditem);
                        }
                    }
                    _SourceVM.InsertItemsFromTemplate(items);
                }
            }
            // Data provided from view, for use with replace value
            else if (obj != null && !string.IsNullOrWhiteSpace(template.Replace))
            {
                var elem = obj as string;
                if (!string.IsNullOrWhiteSpace(elem))
                {
                    var tempitems = template.GetItems();
                    foreach (var item in tempitems)
                    {
                        var cloneditem = DeepCloner.DeepClone((T2)item);
                        ModelStringSetter.ReplaceStringInModel(cloneditem, template.Replace, elem);
                        items.Add(cloneditem);
                    }
                    _SourceVM.InsertItemsFromTemplate(items);
                }
            }
            // Fixed template
            else if (string.IsNullOrWhiteSpace(template.Replace))
            {
                var tempitems = template.GetItems();
                foreach (var item in tempitems)
                {
                    var cloneditem = DeepCloner.DeepClone((T2)item);
                    items.Add(cloneditem);
                }
                _SourceVM.InsertItemsFromTemplate(items);
            }
        }
Beispiel #2
0
        private void ApplyTo(T2 item, object obj)
        {
            TLCGenTemplateModel <T2> template = SelectedTemplate as TLCGenTemplateModel <T2>;
            var tempitem = template.GetItems()?.FirstOrDefault();
            // originalName: name of item if no argument was passed; otherwise, the argument
            // this allows for renaming items in lists belonging to an item
            var orignalName = obj == null ? (item as IHaveName)?.Naam : obj as string;
            // originalItemName: store the item name if an argument was passed
            // this allows to rename potential items in lists of this item while
            // maintaining the name of the item itself
            var orignalItemName = obj != null ? (item as IHaveName)?.Naam : null;

            if (tempitem != null && orignalName != null)
            {
                var cloneditem = DeepCloner.DeepClone((T2)tempitem);
                var props      = typeof(T2).GetProperties().Where(x => x.CanWrite && x.CanRead).ToList();
                foreach (var prop in props)
                {
                    var templateApplicableAttr = prop.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(NoTemplateApplicableAttribute));
                    if (templateApplicableAttr != null)
                    {
                        continue;
                    }

                    object valueOriginal = prop.GetValue(item);
                    object valueCloned   = prop.GetValue(cloneditem);
                    if (prop.PropertyType.IsValueType || prop.PropertyType == typeof(string))
                    {
                        prop.SetValue(item, valueCloned);
                    }
                    else if (valueOriginal is IList elems)
                    {
                        if (elems.Count == 0 || !(elems[0] is IHaveName))
                        {
                            elems.Clear();
                        }
                        else
                        {
                            var l = new List <IHaveName>();
                            foreach (IHaveName i in elems)
                            {
                                l.Add(i);
                            }
                            foreach (var i in l)
                            {
                                Integrity.TLCGenControllerModifier.Default.RemoveModelItemFromController(i.Naam);
                            }
                        }
                        var clonedItems = (IList)valueCloned;
                        foreach (var e in clonedItems)
                        {
                            elems.Add(e);
                        }
                    }
                }
                if (_replaceNameOnApply)
                {
                    ModelStringSetter.ReplaceStringInModel(item, template.Replace, orignalName);
                }
                if (orignalItemName != null)
                {
                    ((IHaveName)item).Naam = orignalItemName;
                }
            }
        }