Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
EDalerPrimaryTransaction.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Events;
11
13{
19 {
20 private Transaction transaction;
21
28 : base(Uri)
29 {
30 }
31
36 protected override async Task<bool> DoPrepare()
37 {
38 this.transaction = await Database.TryLoadObject<Transaction>(this.Uri.Id);
39 if (!(this.transaction is null))
40 {
41 this.Uri.State.Error(EDalerUriErrorType.ResourceConstraint, "eDaler transaction already processed.", false);
42 return false;
43 }
44
45 return true;
46 }
47
52 protected override async Task<bool> DoExecute()
53 {
55 {
56 ObjectId = this.Uri.Id,
57 Created = this.Uri.Created,
58 Processed = DateTime.UtcNow,
59 From = this.Uri.From.Address,
60 FromType = this.Uri.FromType,
61 To = this.Uri.To.IsEmpty ? CaseInsensitiveString.Empty : this.Uri.To.Address,
62 ToType = this.Uri.To.IsEmpty ? EntityType.NetworkJid : this.Uri.ToType,
63 Sender = this.Uri.State.OriginalSender,
64 Amount = this.Uri.TotalAmount,
65 Currency = this.Uri.Currency,
66 Uri = this.Uri.UriString
67 };
68
69 try
70 {
72 }
74 {
75 this.Uri.State.Error(EDalerUriErrorType.ResourceConstraint, "eDaler transaction already processed.", false);
76 return false;
77 }
78 catch (Exception ex)
79 {
80 Log.Exception(ex);
81
82 this.Uri.State.Error(EDalerUriErrorType.ServiceUnavailable, "eDaler transaction could not be processed due to internal failure.", false);
83 return false;
84 }
85
86 this.transaction = Transaction;
87
88 return true;
89 }
90
95 protected override async Task<bool> DoCommit()
96 {
97 this.Uri.State.Result(this.transaction.ToXml());
98
99 KeyValuePair<string, object>[] Tags = await LoginAuditor.Annotate(
100 new XmppAddress(this.transaction.Sender).BareJid,
101 new KeyValuePair<string, object>("Amount", this.transaction.Amount),
102 new KeyValuePair<string, object>("Currency", this.transaction.Currency.Value),
103 new KeyValuePair<string, object>("RefId", this.transaction.ObjectId.ToString()),
104 new KeyValuePair<string, object>("Sender", this.transaction.Sender.Value),
105 new KeyValuePair<string, object>("From", this.transaction.From.Value),
106 new KeyValuePair<string, object>("To", this.transaction.To.Value),
107 new KeyValuePair<string, object>("Uri", this.transaction.Uri));
108
109 this.LogEvent(this.transaction.To, this.transaction.From,
110 this.transaction.Amount, this.transaction.Currency, Tags);
111
112 return true;
113 }
114
123 protected abstract void LogEvent(string To, string From, decimal Amount, string Currency,
124 KeyValuePair<string, object>[] Tags);
125
130 protected override async Task<bool> DoRollback()
131 {
132 if (!(this.transaction is null))
133 {
134 await Database.Delete(this.transaction);
135 this.transaction = null;
136 }
137
138 return true;
139 }
140 }
141}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
CaseInsensitiveString Address
XMPP Address
Definition: XmppAddress.cs:37
CaseInsensitiveString BareJid
Bare JID
Definition: XmppAddress.cs:45
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static async Task Delete(object Object)
Deletes an object in the database.
Definition: Database.cs:717
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:95
static Task< object > TryLoadObject(string CollectionName, object ObjectId)
Tries to load an object given its Object ID ObjectId and its collection name CollectionName .
Definition: Database.cs:1079
An attempt to insert a key was done, but the key was already there.
Transaction()
Abstract base class for transactions.
Definition: Transaction.cs:26
Class that monitors login events, and help applications determine malicious intent....
Definition: LoginAuditor.cs:26
static async Task< KeyValuePair< string, object >[]> Annotate(string RemoteEndpoint, params KeyValuePair< string, object >[] Tags)
Annotates a remote endpoint.
Abstract base class for primary transaction objects (i.e. objects that control the transaction).
override async Task< bool > DoRollback()
Performs actual rollback.
override async Task< bool > DoCommit()
Performs actual commit.
abstract void LogEvent(string To, string From, decimal Amount, string Currency, KeyValuePair< string, object >[] Tags)
Logs an event corresponding to the transaction.
EDalerPrimaryTransaction(EDalerUri Uri)
Abstract base class for primary transaction objects (i.e. objects that control the transaction).
override async Task< bool > DoExecute()
Performs actual execution.
override async Task< bool > DoPrepare()
Performs actual preparation.
Abstract base class for eDaler transactions.
Abstract base class for eDaler URIs
Definition: EDalerUri.cs:20
EntityType ToType
Type of recipient.
Definition: EDalerUri.cs:191
EDalerUriState State
URI State object.
Definition: EDalerUri.cs:173
DateTime Created
When URI was created
Definition: EDalerUri.cs:136
decimal TotalAmount
Total amount: Amount+AmountExtra
Definition: EDalerUri.cs:121
virtual void Error(EDalerUriErrorType ErrorType, string ErrorMessage, bool LogAsNotice)
Reports an error with the URI
virtual CaseInsensitiveString OriginalSender
Who the original sender of the URI is.
virtual void Result(string Xml)
Reports a positive result.