using System;
using System.Collections.Generic;
using Navitaire.NewSkies.UI.Common.Services;
using Navitaire.NewSkies.UI.Common.Rez.Controllers;
using Navitaire.NewSkies.UI.Common.Rez.Notifications.BookingModel;
using Navitaire.NewSkies.Messages.Booking;
using Navitaire.NewSkies.UI.Common;
using Navitaire.NewSkies.UI.Common.Rez.Rules;
using Navitaire.NewSkies.Messages.Session.Request;
using Navitaire.NewSkies.Messages.Session.Response;
using Navitaire.NewSkies.ClientServices.Common;
using Navitaire.NewSkies.Messages.Common;
using Navitaire.Common.UI.Shell.Interfaces.Services;
using Navitaire.Common.UI.Shell;
using Navitaire.Common.UI.Shell.Interfaces;
using Navitaire.NewSkies.UI.Common.Rez.Notifications.UserSessionModel;
namespace Web.SkySales.Rules
{
///
/// This rule was created to provide a better foundation for commonly used functions in Spirit rules
/// This should eliminate the need to reinvent the wheel each time a decendant rule is written
///
public abstract class CustomBaseRule : BaseRule
{
#region Constants & Private Variables
// These are hardcoded ID's cannot be changed. They are the keys to retrieving
// these objects from the cache.
private const string BOOKING_CONTROLLER_ID = "BookingController";
private const string USER_SESSION_CONTROLLER_ID = "UserSessionController";
// This is an attribute key you can use in your SkySales.xml file to cancel
// the setting of the bad session variable the page checks and the "kicking back"
// of the user to a default state in the custom virtual page view
private const string CANCEL_SESSION_KICKBACK = "nosessionkickback";
private BookingController _bookingController;
private ObjectManager _objectManager;
private Dictionary _attributes;
private Booking _booking;
private ResourcesCache _resourcesCache;
private UserSessionController _userSessionController;
private UserSession _userSession;
private ICacheService _cacheService;
private bool _sessionStable = true;
#endregion
#region Properties (most with lazy invocations)
///
/// This entire rule depends on this object manager, which is populated on Initialize
/// users of this object should not be able to set it, just this class (thus, the private set)
///
protected ObjectManager ObjectManager
{
get { return _objectManager; }
private set { _objectManager = value; }
}
protected Boolean SessionStable
{
get { return _sessionStable; }
set { _sessionStable = value; }
}
///
/// This represents the attributes passed into the rule as a name/value pair
/// It is accessed here so child classes aren't required to override Initialize to get this
///
protected Dictionary Attributes
{
get { return _attributes; }
private set { _attributes = value; }
}
protected BookingController BookingController
{
get
{
if (_bookingController == null)
{
if (ObjectManager != null)
{
_bookingController = ObjectManager.GetObjectReference(BOOKING_CONTROLLER_ID, true) as BookingController;
}
}
return _bookingController;
}
}
protected UserSessionController UserSessionController
{
get
{
if (_userSessionController == null)
{
_userSessionController = ObjectManager.GetObjectReference(USER_SESSION_CONTROLLER_ID, true) as UserSessionController;
}
return _userSessionController;
}
}
protected Booking Booking
{
get
{
if (_booking == null && BookingController != null)
{
SyncNotificationConsumer sync = new SyncNotificationConsumer();
BookingController.GetBooking(sync);
RetrievedBookingNotification bookingNotification = sync.WaitForResult();
if (bookingNotification != null)
{
_booking = bookingNotification.Booking;
}
}
return _booking;
}
}
protected UserSession UserSession
{
get
{
if (_userSession == null)
{
SyncNotificationConsumer syncAdapter = new SyncNotificationConsumer();
UserSessionController.RetrieveUserSession(syncAdapter);
RetrievedUserSessionNotification userSessionNotification = syncAdapter.WaitForResult();
if (userSessionNotification != null)
_userSession = userSessionNotification.UserSession;
}
return _userSession;
}
}
protected ResourcesCache ResourcesCache
{
get
{
if (_resourcesCache == null)
{
foreach (IApplicationService service in ObjectManager.RuntimeHost.ServiceContainer.Services)
{
if (service.GetType() == typeof(ResourcesCacheService))
{
_resourcesCache = service as ResourcesCacheService;
break;
}
}
}
return _resourcesCache;
}
}
protected ICacheService CacheService
{
get
{
if (_cacheService == null)
{
_cacheService = ObjectManager.RuntimeHost.ServiceContainer.GetService(typeof(ICacheService)) as ICacheService;
}
return _cacheService;
}
}
#endregion
public override void Initialize(ObjectManager objectManager, Dictionary attributes)
{
base.Initialize(objectManager, attributes);
ObjectManager = objectManager;
Attributes = attributes;
CheckSession();
}
public override bool Validate(WorkflowState currentWorkflowState)
{
// If something bad has happened, we'll return here and not process
// the child rule at all because we will be redirecting in the virtual page
// anyway!
if (SessionStable == false)
return true;
return OnValidate(currentWorkflowState);
}
///
/// Child rules must override this method - this is where they do their work.
///
///
///
public abstract bool OnValidate(WorkflowState workflowState);
private void CheckSession()
{
// If we don't have a UserSession here, our session just got dumped.
// If the state we are trying to load has the cancel session kickback attribute set
// in the rule, that is telling us not to set the sessionStable variable here.
if (UserSession == null && !Attributes.ContainsKey(CANCEL_SESSION_KICKBACK))
{
SessionStable = false; // Flag the session here
CacheService.SetObject("InvalidSession", false);
}
}
}
}