Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5126c3f
commit 8fb3a1f
Showing
12 changed files
with
524 additions
and
395 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Signum.Entities.Basics; | ||
using Signum.Engine.Maps; | ||
using Signum.Utilities; | ||
using System.Reflection; | ||
using Signum.Utilities.ExpressionTrees; | ||
using Signum.Entities; | ||
using Signum.Engine.DynamicQuery; | ||
|
||
namespace Signum.Engine | ||
{ | ||
|
||
public static class SymbolLogic<T> | ||
where T: Symbol | ||
{ | ||
static ResetLazy<Dictionary<T, T>> lazy; | ||
static Func<IEnumerable<T>> getSymbols; | ||
|
||
public static void Start(SchemaBuilder sb, DynamicQueryManager dqm, Func<IEnumerable<T>> getSymbols) | ||
{ | ||
if (sb.NotDefined(typeof(SymbolLogic<T>).GetMethod("Start"))) | ||
{ | ||
sb.Include<T>(); | ||
|
||
dqm.RegisterQuery(typeof(T), () => | ||
from lo in Database.Query<T>() | ||
select new | ||
{ | ||
Entity = lo, | ||
lo.Id, | ||
lo.Key, | ||
}); | ||
|
||
sb.Schema.Initializing[InitLevel.Level0SyncEntities] += () => lazy.Load(); | ||
sb.Schema.Synchronizing += Schema_Synchronizing; | ||
sb.Schema.Generating += Schema_Generating; | ||
|
||
SymbolLogic<T>.getSymbols = getSymbols; | ||
lazy = sb.GlobalLazy(() => | ||
{ | ||
var symbols = getSymbols(); | ||
EnumerableExtensions.JoinStrict( | ||
Database.RetrieveAll<T>(), | ||
symbols, | ||
c => c.Key, | ||
s => s.Key, | ||
(c, s) => { c.id = s.id; c.IsNew = false; c.Modified = ModifiedState.Sealed; return c; } | ||
, "loading {0}. Consider synchronize".Formato(typeof(T).Name)); | ||
return symbols; | ||
}, new InvalidateWith(typeof(T))); | ||
|
||
SymbolManager.SymbolDeserialized.Register((T symbol) => lazy.Value.Contains(); | ||
} | ||
} | ||
|
||
static SqlPreCommand Schema_Generating() | ||
{ | ||
Table table = Schema.Current.Table<T>(); | ||
|
||
IEnumerable<T> should = getSymbols(); | ||
|
||
return should.Select(a => table.InsertSqlSync(a)).Combine(Spacing.Simple); | ||
} | ||
|
||
static SqlPreCommand Schema_Synchronizing(Replacements replacements) | ||
{ | ||
Table table = Schema.Current.Table<T>(); | ||
|
||
List<T> current = Administrator.TryRetrieveAll<T>(replacements); | ||
IEnumerable<T> should = getSymbols(); | ||
|
||
return Synchronizer.SynchronizeScriptReplacing(replacements, typeof(T).Name, | ||
should.ToDictionary(s => s.Key), | ||
current.ToDictionary(c => c.Key), | ||
(k, s) => table.InsertSqlSync(s), | ||
(k, c) => table.DeleteSqlSync(c), | ||
(k, s, c) => | ||
{ | ||
var originalName = c.Key; | ||
c.Key = s.Key; | ||
return table.UpdateSqlSync(c, comment: c.Key); | ||
}, Spacing.Double); | ||
} | ||
|
||
|
||
|
||
static Dictionary<T, T> AssertStarted() | ||
{ | ||
if (lazy == null) | ||
throw new InvalidOperationException("{0} has not been started. Someone should have called {0}.Start before".Formato(typeof(SymbolLogic<T>).TypeName())); | ||
|
||
return lazy.Value; | ||
} | ||
|
||
public static ICollection<T> Symbols | ||
{ | ||
get { return AssertStarted().Keys; } | ||
} | ||
} | ||
} |
Oops, something went wrong.