コード例 #1
0
        protected override void OnInitialize()
        {
            PropertyChanged += (s, e) => e.Case(() => IsEnabled, a =>
            {
                bool isEnabled  = false;
                bool isReadOnly = false;
                switch (Action)
                {
                case SynchronizationAction.Insert:
                    // Activado => Todos los elementos contenidos se activarán y serán editables
                    // Desactivado => Todos los elementos contenidos se desactivarán y no serán editables
                    isEnabled  = IsEnabled;
                    isReadOnly = !IsEnabled;
                    break;

                case SynchronizationAction.Delete:
                    // Activado => Todos los elementos contenidos se activarán y no serán editables
                    // Desactivado => Todos los elementos contenidos se desactivarán y serán editables
                    isEnabled  = IsEnabled;
                    isReadOnly = IsEnabled;
                    break;

                case SynchronizationAction.Update:
                    // Activado => Todos los elementos contenidos se activarán y serán editables
                    // Desactivado => Todos los elementos contenidos se desactivarán y serán editables
                    isEnabled  = IsEnabled;
                    isReadOnly = false;
                    break;

                case SynchronizationAction.None:
                    if (Relations.Any(r => r.Action != SynchronizationAction.None))
                    {
                        isEnabled  = IsEnabled;
                        isReadOnly = false;
                    }
                    break;

                default:
                    break;
                }
                foreach (var rel in Relations)
                {
                    rel.IsEnabled  = isEnabled;
                    rel.IsReadOnly = isReadOnly;
                }
            });
        }
コード例 #2
0
ファイル: ORMEntity.cs プロジェクト: RickvdLaan/ORM
        private void UpdateIsDirtyList()
        {
            for (int i = 0; i < MutableTableScheme.Count; i++)
            {
                // When an object is new, or change tracking is disabled everything is 'dirty' by default.
                if (IsNew || DisableChangeTracking)
                {
                    DirtyTracker.Update(MutableTableScheme[i], true);
                    continue;
                }

                var thisValue     = this[MutableTableScheme[i]];
                var originalValue = OriginalFetchedValue?[MutableTableScheme[i]];

                if (Relations.Any(x => x != null && x.GetType().Name == MutableTableScheme[i]) && (thisValue == null || this[MutableTableScheme[i]].GetType() != GetType()))
                {
                    if (thisValue != null && !thisValue.Equals(originalValue))
                    {
                        DirtyTracker.Update(MutableTableScheme[i], true);
                    }
                    else
                    {
                        DirtyTracker.Update(MutableTableScheme[i], (thisValue as ORMEntity)?.IsDirty ?? false);
                    }
                }
                else
                {
                    if ((thisValue != null && !thisValue.Equals(originalValue)) ||
                        (thisValue == null && originalValue != null))
                    {
                        DirtyTracker.Update(MutableTableScheme[i], true);
                    }
                    else
                    {
                        DirtyTracker.Update(MutableTableScheme[i], false);
                    }
                }
            }
        }
コード例 #3
0
        public override StringBuilder Generate()
        {
            StringBuilder builder       = new();
            var           currentIndent = 0;

            int importCount      = Imports.Count,
                attributeCount   = ClassAttributes.Count,
                dataCount        = FieldsAndProperties.Count,
                constructorCount = Constructors.Count,
                methodCount      = Methods.Count;

            var allEmpty = dataCount < 1 && constructorCount < 1 && methodCount < 1;

            if (importCount > 0)
            {
                Imports.ForEach(import =>
                {
                    builder.Append($"using {import};{NewLine}");
                });
                builder.Append(NewLine);
            }

            if (!string.IsNullOrWhiteSpace(Namespace))
            {
                builder.Append($"namespace {Namespace}{NewLine}{{{NewLine}");
                currentIndent++;
            }

            if (attributeCount > 0)
            {
                IndentStringBuilder(builder, currentIndent);
                builder.Append($"[{string.Join(", ", ClassAttributes)}]{NewLine}");
            }

            IndentStringBuilder(builder, currentIndent);
            builder.Append($"{ClassAccess} class {FileName}");

            if (Relations.Any(r => r.RelationType == ClassRelationType.Inheritance))
            {
                builder.Append(" :");
            }

            if (Relations.Any(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Class))
            {
                builder.Append(
                    $" {Relations.First(r=>r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Class).Target.Name}");
            }

            if (Relations.Any(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Interface))
            {
                builder.Append(" " + string.Join(", ", Relations.Where(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Interface).Select(x => x.Target.Name)));
            }

            IndentStringBuilder(builder.Append(NewLine), currentIndent++);
            builder.Append($"{{{NewLine}");

            FieldsAndProperties.ForEach(d =>
            {
                IndentStringBuilder(builder, currentIndent);
            });

            Constructors.ForEach(ctr =>
            {
                IndentStringBuilder(builder, currentIndent);

                builder.Append($"{Helper.ToString(ctr.Access)} {ctr.NameType.Name}({string.Join(", ",ctr.Params.Select(p => $"{p.Type} {p.Name}"))}){{{NewLine}");
                IndentStringBuilder(builder, ++currentIndent);
                builder.Append($"{NewLine}");
                IndentStringBuilder(builder, --currentIndent);
                builder.Append($"}}{NewLine}");
            });
            Methods.ForEach(m =>
            {
                IndentStringBuilder(builder, currentIndent);

                builder.Append($"{Helper.ToString(m.Access)} ");
                if (m.NameType.IsStatic)
                {
                    builder.Append(m.NameType.IsStatic ? "static " : "");
                }
                builder.Append($"{m.NameType.Type} {m.NameType.Name}({string.Join(", ", m.Params.Select(p => $"{p.Type} {p.Name}"))}){NewLine}");

                IndentStringBuilder(builder, currentIndent);
                builder.Append($"{{{NewLine}");
                IndentStringBuilder(builder, ++currentIndent);
                builder.Append($"{NewLine}");
                IndentStringBuilder(builder, --currentIndent);
                builder.Append($"}}{NewLine}");
            });

            if (allEmpty)
            {
                IndentStringBuilder(builder, currentIndent);
                builder.Append(NewLine);
            }
            IndentStringBuilder(builder, --currentIndent);
            builder.Append($"}}{(string.IsNullOrWhiteSpace(Namespace) ? "" : NewLine)}");

            if (string.IsNullOrWhiteSpace(Namespace))
            {
                return(builder);
            }
            builder.Append('}');
            currentIndent--;

            return(builder);
        }
コード例 #4
0
 public bool IsBroken()
 => Relations.Any(r => (r.From == From && r.To == To) || (r.From == To && r.To == From));
コード例 #5
0
        public override StringBuilder Generate()
        {
            StringBuilder builder       = new();
            var           currentIndent = 0;

            if (!string.IsNullOrWhiteSpace(Namespace))
            {
                builder.Append($"package {Namespace};{NewLine}{NewLine}");
            }

            if (Imports.Count > 0)
            {
                builder.Append("import " + string.Join($";{NewLine}import ", Imports.Where(i => !i.Equals(Namespace))) + $";{NewLine}{NewLine}");
            }

            ClassAttributes.ForEach(attr =>
            {
                IndentStringBuilder(builder, currentIndent);

                builder.Append($"{attr}{NewLine}");
            });

            IndentStringBuilder(builder, currentIndent);
            builder.Append($"{ClassAccess} class {FileName}");

            if (Relations.Any(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Class))
            {
                builder.Append(
                    $" extends {Relations.First(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Class).Target.Name}");
            }

            if (Relations.Any(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Interface))
            {
                builder.Append(" implements " + string.Join(", ", Relations.Where(r => r.RelationType == ClassRelationType.Inheritance && r.Target.Type == FileType.Interface).Select(x => x.Target.Name)));
            }

            IndentStringBuilder(builder.Append(NewLine), currentIndent++);
            builder.Append($"{{{NewLine}");

            FieldsAndProperties.ForEach(d =>
            {
            });

            Constructors.ForEach(ctr =>
            {
                IndentStringBuilder(builder, currentIndent);

                builder.Append($"{Helper.ToString(ctr.Access)} {ctr.NameType.Name}({string.Join(", ", ctr.Params.Select(p => $"{p.Type} {p.Name}"))}){{{NewLine}");
                IndentStringBuilder(builder, ++currentIndent);
                builder.Append($"{NewLine}");
                IndentStringBuilder(builder, --currentIndent);
                builder.Append($"}}{NewLine}");
            });
            Methods.ForEach(m =>
            {
                IndentStringBuilder(builder, currentIndent);

                builder.Append($"{Helper.ToString(m.Access)} {m.NameType.Type} {m.NameType.Name}({string.Join(", ", m.Params.Select(p => $"{p.Type} {p.Name}"))}){NewLine}");
                IndentStringBuilder(builder, currentIndent);
                builder.Append($"{{{NewLine}");
                IndentStringBuilder(builder, ++currentIndent);
                builder.Append($"{NewLine}");
                IndentStringBuilder(builder, --currentIndent);
                builder.Append($"}}{NewLine}");
            });

            //builder.Append(NewLine);
            IndentStringBuilder(builder, currentIndent);
            builder.Append(NewLine);
            IndentStringBuilder(builder, --currentIndent);
            return(builder.Append('}'));
        }
コード例 #6
0
        public override void Select(MainForm frm)
        {
            if (frm.grpUnit.Text == ToString() && frm.MainTab.SelectedTab == frm.tabUnit)
            {
                return;
            }
            Program.MakeSelected(frm.tabUnit, frm.lstUnit, this);

            try
            {
                frm.grpUnit.Text = ToString();
                frm.grpUnit.Show();
#if DEBUG
                frm.grpUnit.Text += $" - ID: {Id}";
#endif

                frm.lblUnitName.Text    = Name;
                frm.lblUnitAltName.Text = AltName;
                frm.lblUnitCoords.Text  = $"({Coords.X}, {Coords.Y}, {Coords.Z})";
                frm.lblUnitSex.Text     = Sex.ToString();
                frm.lblUnitCiv.Data     = Civ;
                frm.lblUnitPop.Data     = Population;
                frm.lblUnitMood.Text    = Mood.ToString();
                frm.lblUnitHF.Data      = HistFigure;
                frm.lblUnitRace.Data    = Race;
                frm.lblUnitCaste.Text   = CasteID.HasValue ? HistoricalFigure.Castes[CasteID.Value] : "";
                if (ProfessionId.HasValue)
                {
                    frm.lblUnitProfession.Text    = JobTypes[ProfessionId.Value];
                    frm.lblUnitProfession.Visible = true;
                }
                else
                {
                    frm.lblUnitProfession.Visible = false;
                }
                frm.lblUnitSquad.Data    = Squad;
                frm.lblUnitOpponent.Data = Opponent;

                frm.grpUnitFlags.Visible = Flag != null;
                frm.lstUnitFlags.Items.Clear();
                if (frm.grpUnitFlags.Visible)
                {
                    frm.lstUnitFlags.Items.AddRange(Flag.Distinct().Select(x => Flags[x].ToLower().Replace("_", " ").ToTitleCase()).ToArray());
                }

                frm.grpUnitLabors.Visible = Labor != null;
                frm.lstUnitLabors.Items.Clear();
                if (frm.grpUnitLabors.Visible && Labor.Any())
                {
                    frm.lstUnitLabors.Items.AddRange(Labor.Distinct().Select(x => Labors[x].ToLower().Replace("_", " ").ToTitleCase()).ToArray());
                }

                frm.grpUnitRelations.Visible = Relations != null;
                frm.lstUnitRelations.Items.Clear();
                if (Relations != null && Relations.Any())
                {
                    frm.lstUnitRelations.Items.AddRange(Relations.Select(x => x.Value).ToArray());
                }

                frm.grpUnitHealth.Visible = HealthFlag != null;
                frm.lstUnitHealth.Items.Clear();
                if (HealthFlag != null && HealthFlag.Any())
                {
                    frm.lstUnitHealth.Items.AddRange(HealthFlag.Distinct().Select(x => HealthFlags[x].ToLower().Replace("_", " ").ToTitleCase()).ToArray());
                }

                frm.grpUnitItems.Visible = OwnedItemIds != null || TradedItemIds != null || UsedItemIds != null;
                frm.trvUnitItems.Nodes.Clear();
                if (frm.grpUnitItems.Visible)
                {
                    if (OwnedItemIds != null)
                    {
                        var node = new TreeNode("Owned Items");
                        foreach (var itemId in OwnedItemIds.Where(i => World.Items.ContainsKey(i)))
                        {
                            node.Nodes.Add(new TreeNode(World.Items[itemId].ToString())
                            {
                                Tag = World.Items[itemId]
                            });
                        }
                        frm.trvUnitItems.Nodes.Add(node);
                    }
                    if (TradedItemIds != null)
                    {
                        var node = new TreeNode("Traded Items");
                        foreach (var itemId in TradedItemIds.Where(i => World.Items.ContainsKey(i)))
                        {
                            node.Nodes.Add(new TreeNode(World.Items[itemId].ToString())
                            {
                                Tag = World.Items[itemId]
                            });
                        }
                        frm.trvUnitItems.Nodes.Add(node);
                    }
                    if (UsedItemIds != null)
                    {
                        var node = new TreeNode("Used Items");
                        foreach (var itemId in UsedItemIds.Where(i => World.Items.ContainsKey(i)))
                        {
                            node.Nodes.Add(new TreeNode(World.Items[itemId].ToString())
                            {
                                Tag = World.Items[itemId]
                            });
                        }
                        frm.trvUnitItems.Nodes.Add(node);
                    }
                }

                frm.grpUnitInventory.FillListboxWith(frm.lstUnitInventory, InventoryItems);
                frm.grpUnitReferences.FillListboxWith(frm.lstUnitReferences, References);

                frm.grpUnitOwnedBuildings.Visible = OwnedBuildingIds != null;
                frm.lstUnitOwnedBuildings.Items.Clear();
                if (OwnedBuildingIds != null)
                {
                    frm.lstUnitOwnedBuildings.Items.AddRange(
                        OwnedBuildingIds.Where(x => World.Buildings.ContainsKey(x))
                        .Select(x => World.Buildings[x]).ToArray());
                }
            }
            finally
            {
                Program.MakeSelected(frm.tabUnit, frm.lstUnit, this);
            }
        }
コード例 #7
0
ファイル: ORMEntity.cs プロジェクト: RickvdLaan/ORM
        /// <summary>
        /// Saves the current <see cref="ORMEntity"/> (changes) to the database.
        /// </summary>
        public virtual void Save()
        {
            if (IsDirty)
            {
                var sqlBuilder = new SQLBuilder();

                // @Perfomance: it fixes some issues, but this is terrible for the performance.
                // Needs to be looked at! -Rick, 12 December 2020
                foreach (var column in MutableTableScheme)
                {
                    // When a subEntity is not filled through the parent the PopulateChildEntity method
                    // isn't called and therefore the subEntity is not added to the EntityRelations.
                    if (this[column] != null && this[column].GetType().IsSubclassOf(typeof(ORMEntity)))
                    {
                        var subEntity = Activator.CreateInstance(this[column].GetType().UnderlyingSystemType) as ORMEntity;

                        if (!Relations.Any(x => x.GetType() == subEntity.GetType()))
                        {
                            if ((this[column] as ORMEntity).IsDirty ||
                                (OriginalFetchedValue?[column] == null && !(this[column] as ORMEntity).IsDirty))
                            {
                                Relations.Add(subEntity);
                            }
                        }
                    }
                }

                foreach (var relation in Relations)
                {
                    if (this[relation.GetType().Name] == null && OriginalFetchedValue[relation.GetType().Name] != null)
                    {
                        continue;
                    }
                    else if ((this[relation.GetType().Name] as ORMEntity).IsDirty)
                    {
                        (this[relation.GetType().Name] as ORMEntity).Save();

                        for (int i = 0; i < relation.PrimaryKey.Count; i++)
                        {
                            var entityRelationId = (int)(this[relation.GetType().Name] as ORMEntity)[relation.PrimaryKey.Keys[i].ColumnName];
                            var entityJoin       = this[relation.GetType().Name];

                            entityJoin.GetType().GetProperty(relation.PrimaryKey.Keys[i].ColumnName).SetValue(entityJoin, entityRelationId);
                            entityJoin.GetType().GetProperty(nameof(ExecutedQuery)).SetValue(entityJoin, (this[relation.GetType().Name] as ORMEntity).ExecutedQuery);
                        }
                    }
                }

                if (IsNew || IsNew && Relations.Any(r => r.IsNew))
                {
                    sqlBuilder.BuildNonQuery(this, NonQueryType.Insert);

                    var id = SQLExecuter.ExecuteNonQuery(sqlBuilder);

                    if (PrimaryKey.IsCombinedPrimaryKey)
                    {
                        UpdateCombinedPrimaryKey();
                    }
                    else
                    {
                        UpdateSinglePrimaryKey(id);
                    }
                }
                else
                {
                    sqlBuilder.BuildNonQuery(this, NonQueryType.Update);
                    SQLExecuter.ExecuteNonQuery(sqlBuilder);
                }

                ExecutedQuery = sqlBuilder.GeneratedQuery;
            }
        }