Skip to content

Commit

Permalink
Land Subscriptions Application Services (Use cases)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcota committed Feb 21, 2019
1 parent ea72e86 commit 6f08b50
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 4 deletions.
35 changes: 35 additions & 0 deletions Land.Registration/Data/MessagingData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Empiria Land **********************************************************************************************
* *
* Module : Land Messaging services Component : Subscription services *
* Assembly : Empiria.Land.Registration.dll Pattern : Data Services *
* Type : MessagingData License : Please read LICENSE.txt file *
* *
* Summary : Read and write methods for land system messaging components. *
* *
************************* Copyright(c) La Vía Óntica SC, Ontica LLC and contributors. All rights reserved. **/
using System;

using Empiria.Data;

namespace Empiria.Land.Messaging {

/// <summary>Read and write methods for land system messaging components.</summary>
static internal class MessagingData {

#region Internal methods

static internal void WriteSubscription(Subscription o) {
var op = DataOperation.Parse("writeLRSPosting", o.Id, o.UID,
o.SubscriptionType.ToString(), o.SubscribedObjectUID,
o.SendTo.ToString(), String.Empty,
o.PostingTime, -1, (char) o.Status);

DataWriter.Execute(op);
}


#endregion Internal methods

} // class MessagingData

} // namespace Empiria.Land.Data
30 changes: 26 additions & 4 deletions Land.Registration/Messaging/LandMessenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
using System.Threading;

using Empiria.Json;
using Empiria.Land.Registration;
using Empiria.Land.Registration.Transactions;
using Empiria.Messaging;
using Empiria.StateEnums;

using Empiria.Land.Registration;
using Empiria.Land.Registration.Transactions;

namespace Empiria.Land.Messaging {

public class LandMessenger {
Expand Down Expand Up @@ -44,6 +45,14 @@ static internal void Notify(LRSTransaction transaction,
}


/// <summary>Notifies messenger about a workflow status change of a land transaction.</summary>
static internal void Notify(SubscriptionEventType eventType, Subscription subscription) {
Assertion.AssertObject(subscription, "subscription");
NotificationType notificationType = ConvertToNotificationType(eventType);

EnqueueNotification(notificationType, subscription);
}

#endregion Notification and subscription methods


Expand Down Expand Up @@ -234,6 +243,19 @@ static private void EnqueueNotification(SendTo sendTo, LRSTransaction transactio
}


static private void EnqueueNotification(NotificationType notificationType,
Subscription subscription) {
var data = new JsonObject();

data.Add("NotificationType", notificationType.ToString());
data.Add("SendTo", subscription.SendTo.ToJson());

var newMessage = new Message(data);

MESSAGE_QUEUE.AddMessage(newMessage, subscription.UID);
}


static private void NotifyAgency(LRSTransaction transaction, TransactionEventType eventType) {
if (eventType == TransactionEventType.TransactionReceived) {
return;
Expand Down Expand Up @@ -266,14 +288,14 @@ static private void NotifyInterested(LRSTransaction transaction, TransactionEven
#region Utility methods


static private NotificationType ConvertToNotificationType(TransactionEventType eventType) {
static private NotificationType ConvertToNotificationType(Enum eventType) {
NotificationType result;

if (Enum.TryParse<NotificationType>(eventType.ToString(), out result)) {
return result;
}

throw Assertion.AssertNoReachThisCode($"Can't convert to NotificationType from TransactionEventType value {eventType}.");
throw Assertion.AssertNoReachThisCode($"Can't convert to NotificationType from {eventType.GetType().Name} value {eventType}.");
}


Expand Down
106 changes: 106 additions & 0 deletions Land.Registration/Messaging/SubscriptionRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* Empiria Land **********************************************************************************************
* *
* Module : Land Messaging services Component : Application Services *
* Assembly : Empiria.Land.Registration.dll Pattern : Interface adapter *
* Type : SubscriptionRequest License : Please read LICENSE.txt file *
* *
* Summary : Describes a subscription request used to subscribe, unsubscribe or confirm subscriptions. *
* *
************************* Copyright(c) La Vía Óntica SC, Ontica LLC and contributors. All rights reserved. **/
using System;

using Empiria.Json;
using Empiria.Messaging;

namespace Empiria.Land.Messaging {

/// <summary>Describes a subscription request used to subscribe,
/// unsubscribe or confirm subscriptions.</summary>
public class SubscriptionRequest {

#region Constructors and parsers

private SubscriptionRequest(JsonObject json) {
this.LoadData(json);
}


static public SubscriptionRequest Parse(JsonObject json) {
EnsureIsValid(json);

return new SubscriptionRequest(json);
}


static public void EnsureIsValid(JsonObject json) {
Assertion.AssertObject(json, "json");

Assertion.Assert(json.HasValue("command"),
"Subscription request must have a 'command' value.");

Assertion.Assert(json.HasValue("subscriptionType"),
"Subscription request must have a 'subscriptionType' value.");

Assertion.Assert(json.HasValue("subscribedObjectUID"),
"Subscription request must have a 'subscribedObjectUID' that refers to a resource, " +
"certificate or recording document.");

Assertion.Assert(json.HasValue("sendTo"),
"Subscription request must have a 'sendTo' value.");

}

#endregion Constructors and parsers


#region Properties


public SubscriptionRequestCommand Command {
get;
private set;
}



public SubscriptionType SubscriptionType {
get;
private set;
}


public string SubscribedObjectUID {
get;
private set;
} = String.Empty;


public SendTo SendTo {
get;
private set;
}


public string HashCode {
get;
private set;
} = String.Empty;


#endregion Properties

#region Methods

private void LoadData(JsonObject json) {
this.Command = json.Get<SubscriptionRequestCommand>("command");
this.SubscriptionType = json.Get<SubscriptionType>("subscriptionType");
this.SubscribedObjectUID = json.Get<string>("subscribedObjectUID");
this.SendTo = SendTo.Parse(json.Slice("sendTo"));
this.HashCode = json.Get<string>("hashCode", String.Empty);
}

#endregion Methods

} // class SubscriptionRequest

} // namespace Empiria.Land.Messaging
25 changes: 25 additions & 0 deletions Land.Registration/Messaging/SubscriptionRequestCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Empiria Land **********************************************************************************************
* *
* Module : Land Messaging services Component : Application Services *
* Assembly : Empiria.Land.Registration.dll Pattern : Enumeration type *
* Type : SubscriptionRequestCommand License : Please read LICENSE.txt file *
* *
* Summary : Describes a SubscriptionRequest command. *
* *
************************* Copyright(c) La Vía Óntica SC, Ontica LLC and contributors. All rights reserved. **/
using System;

namespace Empiria.Land.Messaging {

/// <summary>Describes a SubscriptionRequest command.</summary>
public enum SubscriptionRequestCommand {

Subscribe,

ConfirmSubscription,

Unsubscribe

} // SubscriptionRequestCommand

} // namespace Empiria.Land.Messaging
139 changes: 139 additions & 0 deletions Land.Registration/Messaging/SubscriptionServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* Empiria Land **********************************************************************************************
* *
* Module : Land Messaging services Component : Application Services *
* Assembly : Empiria.Land.Registration.dll Pattern : Service *
* Type : SubscriptionServices License : Please read LICENSE.txt file *
* *
* Summary : Application services to subscribe and unsubscribe for resource and document changes. *
* *
************************* Copyright(c) La Vía Óntica SC, Ontica LLC and contributors. All rights reserved. **/
using System;

namespace Empiria.Land.Messaging {

/// <summary>Application services to subscribe and unsubscribe for resource and document changes.</summary>
static public class SubscriptionServices {

#region Services


static public Subscription ConfirmSubscription(SubscriptionRequest request) {
EnsureIsValid(request);

throw new NotImplementedException();
}


static public Subscription Subscribe(SubscriptionRequest request) {
EnsureIsValid(request);

if (ExistsSubscription(request)) {
throw new ResourceConflictException("Land.Subscription.AlreadyExists",
$"El correo electrónico {0} ya está registrado para recibir avisos registrales " +
"sobre este recurso.");
}

var subscription = new Subscription(request.SubscriptionType, request.SubscribedObjectUID, request.SendTo);

subscription.Save();

LandMessenger.Notify(GetSubscriptionEventType(request), subscription);

return subscription;
}


static public Subscription Unsubscribe(string subscriptionUID, SubscriptionRequest request) {
EnsureIsValid(request);

var subscription = Subscription.TryParse(subscriptionUID);

if (subscription == null) {
throw new ResourceConflictException("Land.Subscription.NotFound",
$"No tenemos registrada una subscripción a los servicios de alerta registral " +
$"con identificador {subscriptionUID}.");
}

subscription.Unsubscribe();

subscription.Save();

LandMessenger.Notify(GetSubscriptionEventType(request), subscription);

return subscription;
}


#endregion Services

#region Private methods


static private void EnsureIsValid(SubscriptionRequest request) {

}


static private void EnsureNoRegistered() {

}


static private bool ExistsSubscription(SubscriptionRequest subscriptionRequest) {
return false;
}


private static SubscriptionEventType GetSubscriptionEventType(SubscriptionRequest request) {
SubscriptionRequestCommand command = request.Command;
SubscriptionType type = request.SubscriptionType;

if (command == SubscriptionRequestCommand.Subscribe) {

switch (type) {
case SubscriptionType.CertificateChangesSubscription:
return SubscriptionEventType.SubscribedForCertificateChanges;

case SubscriptionType.RecordingDocumentChangesSubscription:
return SubscriptionEventType.SubscribedForRecordingDocumentChanges;

case SubscriptionType.ResourceChangesSubscription:
return SubscriptionEventType.SubscribedForResourceChanges;
}

} else if (command == SubscriptionRequestCommand.ConfirmSubscription) {

switch (type) {
case SubscriptionType.CertificateChangesSubscription:
return SubscriptionEventType.ConfirmedForCertificateChanges;

case SubscriptionType.RecordingDocumentChangesSubscription:
return SubscriptionEventType.ConfirmedForRecordingDocumentChanges;

case SubscriptionType.ResourceChangesSubscription:
return SubscriptionEventType.ConfirmedForResourceChanges;
}

} else if (command == SubscriptionRequestCommand.Unsubscribe) {

switch (type) {
case SubscriptionType.CertificateChangesSubscription:
return SubscriptionEventType.UnsubscribedForCertificateChanges;

case SubscriptionType.RecordingDocumentChangesSubscription:
return SubscriptionEventType.UnsubscribedForRecordingDocumentChanges;

case SubscriptionType.ResourceChangesSubscription:
return SubscriptionEventType.UnsubscribedForResourceChanges;
}

} // else if

throw Assertion.AssertNoReachThisCode();
}

#endregion Private methods

} // class SubscriptionServices

} // namespace Empiria.Land.Messaging

0 comments on commit 6f08b50

Please sign in to comment.