Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CustomEntryExport.cs
1using System;
3using System.Threading.Tasks;
6
8{
12 public abstract class CustomEntryExport : ILedgerExport
13 {
14 private readonly Dictionary<string, object> blockMetaData = new Dictionary<string, object>();
15 private readonly ILedgerExport output;
16 private string startedCollection = null;
17 private string startedBlock = null;
18 private bool startedCollectionExported = false;
19 private bool startedBlockExported = false;
20 private bool hasBlockMetaData = false;
21 private bool includeCurrentEntry = false;
22
28 {
29 this.output = Output;
30 }
31
35 protected string StartedCollection => this.startedCollection;
36
40 protected string StartedBlock => this.startedBlock;
41
47 public Task<bool> StartLedger(ILedgerProvider Provider)
48 {
49 return this.output.StartLedger(Provider);
50 }
51
56 public Task<bool> EndLedger()
57 {
58 return this.output.EndLedger();
59 }
60
66 public virtual Task<bool> StartCollection(string CollectionName)
67 {
68 this.startedCollection = CollectionName;
69 this.startedCollectionExported = false;
70
71 return Task.FromResult(true);
72 }
73
78 public async Task<bool> EndCollection()
79 {
80 if (this.startedCollectionExported)
81 {
82 if (!await this.output.EndCollection())
83 return false;
84
85 this.startedCollectionExported = false;
86 }
87
88 this.startedCollection = null;
89
90 return true;
91 }
92
98 public virtual Task<bool> StartBlock(string BlockID)
99 {
100 this.startedBlock = BlockID;
101 this.startedBlockExported = false;
102 this.hasBlockMetaData = false;
103
104 return Task.FromResult(true);
105 }
106
113 public virtual Task<bool> BlockMetaData(string Key, object Value)
114 {
115 this.blockMetaData[Key] = Value;
116 this.hasBlockMetaData = true;
117
118 return Task.FromResult(true);
119 }
120
125 public async Task<bool> EndBlock()
126 {
127 if (this.startedBlockExported)
128 {
129 if (!await this.output.EndBlock())
130 return false;
131
132 this.startedBlockExported = false;
133 }
134
135 if (this.hasBlockMetaData)
136 {
137 this.blockMetaData.Clear();
138 this.hasBlockMetaData = false;
139 }
140
141 this.startedBlock = null;
142
143 return true;
144 }
145
154 public abstract bool IncludeEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp);
155
160 public abstract bool IncludeNonEntryEvent();
161
166 public virtual bool ContinueExport() => true;
167
176 public virtual async Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
177 {
178 if (this.IncludeEntry(ObjectId, TypeName, EntryType, EntryTimestamp))
179 {
180 if (!await this.OutputPendingInfo())
181 return false;
182
183 this.includeCurrentEntry = true;
184
185 return await this.output.StartEntry(ObjectId, TypeName, EntryType, EntryTimestamp);
186 }
187 else
188 {
189 this.includeCurrentEntry = false;
190 return true;
191 }
192 }
193
194 private async Task<bool> OutputPendingInfo()
195 {
196 if (!this.startedCollectionExported && !(this.startedCollection is null))
197 {
198 if (!await this.output.StartCollection(this.startedCollection))
199 return false;
200
201 this.startedCollectionExported = true;
202 }
203
204 if (!this.startedBlockExported && !(this.startedBlock is null))
205 {
206 if (!await this.output.StartBlock(this.startedBlock))
207 return false;
208
209 this.startedBlockExported = true;
210
211 if (this.hasBlockMetaData)
212 {
213 foreach (KeyValuePair<string, object> P in this.blockMetaData)
214 {
215 if (!await this.output.BlockMetaData(P.Key, P.Value))
216 return false;
217 }
218
219 this.blockMetaData.Clear();
220 this.hasBlockMetaData = false;
221 }
222 }
223
224 return true;
225 }
226
233 public virtual Task<bool> ReportProperty(string PropertyName, object PropertyValue)
234 {
235 if (this.includeCurrentEntry)
236 return this.output.ReportProperty(PropertyName, PropertyValue);
237 else
238 return Task.FromResult(true);
239 }
240
245 public virtual async Task<bool> EndEntry()
246 {
247 if (this.includeCurrentEntry)
248 {
249 this.includeCurrentEntry = false;
250 if (!await this.output.EndEntry())
251 return false;
252 }
253
254 return this.ContinueExport();
255 }
256
262 public async Task<bool> CollectionCleared(DateTimeOffset EntryTimestamp)
263 {
264 if (this.IncludeNonEntryEvent())
265 {
266 if (!await this.OutputPendingInfo())
267 return false;
268
269 if (!await this.output.CollectionCleared(EntryTimestamp))
270 return false;
271 }
272
273 return true;
274 }
275
281 public Task<bool> ReportError(string Message)
282 {
283 if (this.IncludeNonEntryEvent())
284 return this.output.ReportError(Message);
285 else
286 return Task.FromResult(true);
287 }
288
294 public Task<bool> ReportException(Exception Exception)
295 {
296 if (this.IncludeNonEntryEvent())
297 return this.output.ReportException(Exception);
298 else
299 return Task.FromResult(true);
300 }
301 }
302}
Abstract base class for ledger exports with custom entry filter rules.
virtual Task< bool > ReportProperty(string PropertyName, object PropertyValue)
Is called when a property is reported.
virtual Task< bool > StartCollection(string CollectionName)
Is called when a collection is started.
async Task< bool > EndCollection()
Is called when a collection is finished.
string StartedBlock
Block that is currently being processed.
CustomEntryExport(ILedgerExport Output)
Abstract base class for ledger exports with custom entry filter rules.
async Task< bool > CollectionCleared(DateTimeOffset EntryTimestamp)
Is called when the collection has been cleared.
Task< bool > ReportException(Exception Exception)
Is called when an exception has occurred.
Task< bool > ReportError(string Message)
Is called when an error is reported.
abstract bool IncludeNonEntryEvent()
If a non-entry event should be included.
virtual async Task< bool > StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
Is called when an entry is started.
virtual Task< bool > StartBlock(string BlockID)
Is called when a block in a collection is started.
virtual Task< bool > BlockMetaData(string Key, object Value)
Reports block meta-data.
virtual bool ContinueExport()
If export should be continued or not.
string StartedCollection
Collection that is currently being processed.
async Task< bool > EndBlock()
Is called when a block in a collection is finished.
Task< bool > StartLedger(ILedgerProvider Provider)
Is called when export of ledger is started.
Task< bool > EndLedger()
Is called when export of ledger is finished.
abstract bool IncludeEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
If an entry should be included.
virtual async Task< bool > EndEntry()
Is called when an entry is finished.
Interface for ledger providers that can be plugged into the static Ledger class.
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9