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;
2using System.Collections.Generic;
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
46 public Task<bool> StartLedger()
47 {
48 return this.output.StartLedger();
49 }
50
55 public Task<bool> EndLedger()
56 {
57 return this.output.EndLedger();
58 }
59
65 public virtual Task<bool> StartCollection(string CollectionName)
66 {
67 this.startedCollection = CollectionName;
68 this.startedCollectionExported = false;
69
70 return Task.FromResult(true);
71 }
72
77 public async Task<bool> EndCollection()
78 {
79 if (this.startedCollectionExported)
80 {
81 if (!await this.output.EndCollection())
82 return false;
83
84 this.startedCollectionExported = false;
85 }
86
87 this.startedCollection = null;
88
89 return true;
90 }
91
97 public virtual Task<bool> StartBlock(string BlockID)
98 {
99 this.startedBlock = BlockID;
100 this.startedBlockExported = false;
101 this.hasBlockMetaData = false;
102
103 return Task.FromResult(true);
104 }
105
112 public virtual Task<bool> BlockMetaData(string Key, object Value)
113 {
114 this.blockMetaData[Key] = Value;
115 this.hasBlockMetaData = true;
116
117 return Task.FromResult(true);
118 }
119
124 public async Task<bool> EndBlock()
125 {
126 if (this.startedBlockExported)
127 {
128 if (!await this.output.EndBlock())
129 return false;
130
131 this.startedBlockExported = false;
132 }
133
134 if (this.hasBlockMetaData)
135 {
136 this.blockMetaData.Clear();
137 this.hasBlockMetaData = false;
138 }
139
140 this.startedBlock = null;
141
142 return true;
143 }
144
153 public abstract bool IncludeEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp);
154
159 public abstract bool IncludeNonEntryEvent();
160
165 public virtual bool ContinueExport() => true;
166
175 public virtual async Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
176 {
177 if (this.IncludeEntry(ObjectId, TypeName, EntryType, EntryTimestamp))
178 {
179 if (!await this.OutputPendingInfo())
180 return false;
181
182 this.includeCurrentEntry = true;
183
184 return await this.output.StartEntry(ObjectId, TypeName, EntryType, EntryTimestamp);
185 }
186 else
187 {
188 this.includeCurrentEntry = false;
189 return true;
190 }
191 }
192
193 private async Task<bool> OutputPendingInfo()
194 {
195 if (!this.startedCollectionExported && !(this.startedCollection is null))
196 {
197 if (!await this.output.StartCollection(this.startedCollection))
198 return false;
199
200 this.startedCollectionExported = true;
201 }
202
203 if (!this.startedBlockExported && !(this.startedBlock is null))
204 {
205 if (!await this.output.StartBlock(this.startedBlock))
206 return false;
207
208 this.startedBlockExported = true;
209
210 if (this.hasBlockMetaData)
211 {
212 foreach (KeyValuePair<string, object> P in this.blockMetaData)
213 {
214 if (!await this.output.BlockMetaData(P.Key, P.Value))
215 return false;
216 }
217
218 this.blockMetaData.Clear();
219 this.hasBlockMetaData = false;
220 }
221 }
222
223 return true;
224 }
225
232 public virtual Task<bool> ReportProperty(string PropertyName, object PropertyValue)
233 {
234 if (this.includeCurrentEntry)
235 return this.output.ReportProperty(PropertyName, PropertyValue);
236 else
237 return Task.FromResult(true);
238 }
239
244 public virtual async Task<bool> EndEntry()
245 {
246 if (this.includeCurrentEntry)
247 {
248 this.includeCurrentEntry = false;
249 if (!await this.output.EndEntry())
250 return false;
251 }
252
253 return this.ContinueExport();
254 }
255
261 public async Task<bool> CollectionCleared(DateTimeOffset EntryTimestamp)
262 {
263 if (this.IncludeNonEntryEvent())
264 {
265 if (!await this.OutputPendingInfo())
266 return false;
267
268 if (!await this.output.CollectionCleared(EntryTimestamp))
269 return false;
270 }
271
272 return true;
273 }
274
280 public Task<bool> ReportError(string Message)
281 {
282 if (this.IncludeNonEntryEvent())
283 return this.output.ReportError(Message);
284 else
285 return Task.FromResult(true);
286 }
287
293 public Task<bool> ReportException(Exception Exception)
294 {
295 if (this.IncludeNonEntryEvent())
296 return this.output.ReportException(Exception);
297 else
298 return Task.FromResult(true);
299 }
300 }
301}
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.
Task< bool > StartLedger()
Is called when export of ledger is started.
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 > 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.
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9