-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSyncEngineInterfaces.cs
More file actions
165 lines (144 loc) · 8.32 KB
/
SyncEngineInterfaces.cs
File metadata and controls
165 lines (144 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NETCoreSync.Exceptions;
using System.Reflection;
using System.IO;
using System.IO.Compression;
namespace NETCoreSync
{
public abstract partial class SyncEngine
{
public virtual bool IsServerEngine()
{
//must implement if SyncConfiguration.TimeStampStrategy = UseGlobalTimeStamp
throw new NotImplementedException();
}
public virtual long GetClientLastSync()
{
//must implement if SyncConfiguration.TimeStampStrategy = UseGlobalTimeStamp
throw new NotImplementedException();
}
public virtual void SetClientLastSync(long lastSync)
{
//must implement if SyncConfiguration.TimeStampStrategy = UseGlobalTimeStamp
throw new NotImplementedException();
}
public virtual long GetNextTimeStamp()
{
//must implement if SyncConfiguration.TimeStampStrategy = UseEachDatabaseInstanceTimeStamp
throw new NotImplementedException();
}
public virtual List<KnowledgeInfo> GetAllKnowledgeInfos(string synchronizationId, Dictionary<string, object> customInfo)
{
//must implement if SyncConfiguration.TimeStampStrategy = UseEachDatabaseInstanceTimeStamp
throw new NotImplementedException();
}
public virtual void CreateOrUpdateKnowledgeInfo(KnowledgeInfo knowledgeInfo, string synchronizationId, Dictionary<string, object> customInfo)
{
//must implement if SyncConfiguration.TimeStampStrategy = UseEachDatabaseInstanceTimeStamp
throw new NotImplementedException();
}
public virtual object StartTransaction(Type classType, OperationType operationType, string synchronizationId, Dictionary<string, object> customInfo)
{
return null;
}
public virtual void CommitTransaction(Type classType, object transaction, OperationType operationType, string synchronizationId, Dictionary<string, object> customInfo)
{
}
public virtual void RollbackTransaction(Type classType, object transaction, OperationType operationType, string synchronizationId, Dictionary<string, object> customInfo)
{
}
public virtual void EndTransaction(Type classType, object transaction, OperationType operationType, string synchronizationId, Dictionary<string, object> customInfo)
{
}
public abstract IQueryable GetQueryable(Type classType, object transaction, OperationType operationType, string synchronizationId, Dictionary<string, object> customInfo);
public abstract string SerializeDataToJson(Type classType, object data, object transaction, OperationType operationType, string synchronizationId, Dictionary<string, object> customInfo);
public abstract object DeserializeJsonToNewData(Type classType, JObject jObject, object transaction, OperationType operationType, string synchronizationId, Dictionary<string, object> customInfo);
public abstract object DeserializeJsonToExistingData(Type classType, JObject jObject, object data, object transaction, OperationType operationType, ConflictType conflictType, string synchronizationId, Dictionary<string, object> customInfo);
public abstract void PersistData(Type classType, object data, bool isNew, object transaction, OperationType operationType, string synchronizationId, Dictionary<string, object> customInfo);
public virtual object TransformIdType(Type classType, JValue id, object transaction, OperationType operationType, string synchronizationId, Dictionary<string, object> customInfo)
{
return id;
}
public virtual void PostEventDelete(Type classType, object id, string synchronizationId, Dictionary<string, object> customInfo)
{
}
public void HookPreInsertOrUpdateGlobalTimeStamp(object data)
{
if (data == null) throw new NullReferenceException(nameof(data));
SyncConfiguration.SchemaInfo schemaInfo = GetSchemaInfo(SyncConfiguration, data.GetType());
if (SyncConfiguration.TimeStampStrategy == SyncConfiguration.TimeStampStrategyEnum.GlobalTimeStamp)
{
long nowTicks = GetNowTicks();
if (!IsServerEngine())
{
long lastSync = GetClientLastSync();
if (nowTicks <= lastSync && !SyncConfiguration.SyncConfigurationOptions.GlobalTimeStampAllowHooksToUpdateWithOlderSystemDateTime) throw new SyncEngineConstraintException("System Date and Time is older than the lastSync value");
}
data.GetType().GetProperty(schemaInfo.PropertyInfoLastUpdated.Name).SetValue(data, nowTicks);
}
else
{
throw new SyncEngineConstraintException($"Mismatch Hook Method for {SyncConfiguration.TimeStampStrategy}");
}
}
public void HookPreDeleteGlobalTimeStamp(object data)
{
if (data == null) throw new NullReferenceException(nameof(data));
SyncConfiguration.SchemaInfo schemaInfo = GetSchemaInfo(SyncConfiguration, data.GetType());
if (SyncConfiguration.TimeStampStrategy == SyncConfiguration.TimeStampStrategyEnum.GlobalTimeStamp)
{
long nowTicks = GetNowTicks();
if (!IsServerEngine())
{
long lastSync = GetClientLastSync();
if (nowTicks <= lastSync && !SyncConfiguration.SyncConfigurationOptions.GlobalTimeStampAllowHooksToUpdateWithOlderSystemDateTime) throw new SyncEngineConstraintException("System Date and Time is older than the lastSync value");
}
data.GetType().GetProperty(schemaInfo.PropertyInfoDeleted.Name).SetValue(data, nowTicks);
}
else
{
throw new SyncEngineConstraintException($"Mismatch Hook Method for {SyncConfiguration.TimeStampStrategy}");
}
}
public void HookPreInsertOrUpdateDatabaseTimeStamp(object data, object transaction, string synchronizationId, Dictionary<string, object> customInfo)
{
if (data == null) throw new NullReferenceException(nameof(data));
SyncConfiguration.SchemaInfo schemaInfo = GetSchemaInfo(SyncConfiguration, data.GetType());
if (SyncConfiguration.TimeStampStrategy == SyncConfiguration.TimeStampStrategyEnum.DatabaseTimeStamp)
{
long timeStamp = InvokeGetNextTimeStamp();
UpdateLocalKnowledgeTimeStamp(timeStamp, synchronizationId, customInfo, null, transaction);
data.GetType().GetProperty(schemaInfo.PropertyInfoDatabaseInstanceId.Name).SetValue(data, null);
data.GetType().GetProperty(schemaInfo.PropertyInfoLastUpdated.Name).SetValue(data, timeStamp);
}
else
{
throw new SyncEngineConstraintException($"Mismatch Hook Method for {SyncConfiguration.TimeStampStrategy}");
}
}
public void HookPreDeleteDatabaseTimeStamp(object data, object transaction, string synchronizationId, Dictionary<string, object> customInfo)
{
if (data == null) throw new NullReferenceException(nameof(data));
SyncConfiguration.SchemaInfo schemaInfo = GetSchemaInfo(SyncConfiguration, data.GetType());
if (SyncConfiguration.TimeStampStrategy == SyncConfiguration.TimeStampStrategyEnum.DatabaseTimeStamp)
{
long timeStamp = InvokeGetNextTimeStamp();
UpdateLocalKnowledgeTimeStamp(timeStamp, synchronizationId, customInfo, null, transaction);
data.GetType().GetProperty(schemaInfo.PropertyInfoDatabaseInstanceId.Name).SetValue(data, null);
data.GetType().GetProperty(schemaInfo.PropertyInfoLastUpdated.Name).SetValue(data, timeStamp);
data.GetType().GetProperty(schemaInfo.PropertyInfoDeleted.Name).SetValue(data, true);
}
else
{
throw new SyncEngineConstraintException($"Mismatch Hook Method for {SyncConfiguration.TimeStampStrategy}");
}
}
}
}