package com.d.menu.model
{
import flash.net.SharedObject;
import flash.net.registerClassAlias;
import mx.collections.ArrayCollection;
/**
* Manager for accessing the SharedObjects for the menu. Helps
* with the loading and saving of state variables so uses can navigate to
* and from the menu without losing cart information
*/
public class SOManager
{
public static const SO_APP_STATE:String = "d_SO_App_State";
public static const SO_CATEGORY_ID:String = "d_SO_Category_ID";
public static const SO_STORE_ID:String = "d_SO_Store_ID";
public static const SO_SELECTED_MENU_ITEM_ID:String = "d_SO_Selected_Menu_Item_ID";
public static const SO_SELECTED_COUPON_CODE:String = "d_SO_SELECTED_COUPON_CODE";
public static const SO_ORDER:String = "d_SO_ORDER";
public static const SO_CUSTOMER:String = "d_SO_CUSTOMER";
public static const SO_PAYMENT:String = "d_SO_PAYMENT";
public static const SO_PAYMENT_TYPE:String = "d_SO_PAYMENT_TYPE";
public static const SO_GIFTCARDS:String = "d_SO_GIFTCARD";
public static const SO_APPlIED:Array = new Array();
public var giftCards:Array = new Array();
public static const SO_ORDER_ID_TO_LOAD:String = "d_SO_ORDER_TO_LOAD";
public static const SO_TIMESTAMP:String = "d_SO_TIMESTAMP";
public static const SO_CUSTOMER_ID:String = "d_SO_CUSTOMER_ID";
public var giftCardCount:int = 0;
private var _isReady:Boolean = false;
public function get isReady():Boolean
{
return _isReady;
}
// main shared object variable that is used for storage of menu variables
private var _so:SharedObject;
public function get SO():SharedObject
{
return _so;
}
public function set SO(value:SharedObject):void
{
_so = value;
}
/**
* Constructor
*/
public function SOManager():void
{
}
/**
* Creates a reference to the shared object based on the user's customer ID
*/
public function createSharedObject(loadedCustomerID:int):void
{
// grab anon profile
var anonSO:SharedObject = getAnonSO();
// get current time
var now:Date = new Date();
// if user is anonymous, check for timestamp expiration to reset
if (loadedCustomerID == -1)
{
// assign the _so the anonymous value
_so = anonSO;
// grab current timestamp for this user
var timestamp:Date = getValue(SOManager.SO_TIMESTAMP);
if (timestamp && isTimestampExpired(timestamp))
{
// the timestamp is too old. clear cookie
//Debug.log( "Deleting SO for anon user b/c timestamp has expired");
deleteSharedObject();
// session is expired. update timestamp and continue
//Debug.log( "Setting new timestamp for anon user");
saveValue(SOManager.SO_TIMESTAMP, now);
}
}
else
{
// we have a non-anon customer. this customer could have been
// anonymous last load....so we need to check and assign this
// user the anon profile if necessary.
// check for user-specific cookie
_so = SharedObject.getLocal(formatName(loadedCustomerID), "/");
if (anonSO.data.timestamp && !isTimestampExpired(anonSO.data.timestamp))
{
// anon SO was not expired, so can't we assume that
// the incoming user was anonymous but now is not??
transferAnonToCustomer(anonSO);
}
//Debug.log( "Setting new timestamp for current Customer (" + loadedCustomerID + ")");
saveValue(SOManager.SO_TIMESTAMP, now);
}
// check to see if there is a store to load
var storeSO:SharedObject = SharedObject.getLocal("dStoreToLoad", "/");
if (storeSO.data.storeID && storeSO.data.storeID != -1)
{
saveValue(SO_STORE_ID, storeSO.data.storeID);
storeSO.data.storeID = -1;
storeSO.flush();
}
// check to see if there is an order to load
var orderSO:SharedObject = SharedObject.getLocal("dOrderToLoad", "/");
if (orderSO.data.orderHeaderID && orderSO.data.orderHeaderID != -1)
{
saveValue(SO_ORDER_ID_TO_LOAD, orderSO.data.orderHeaderID);
orderSO.data.orderHeaderID = -1;
orderSO.flush();
}
// set soManager to READY!
_isReady = true;
}
public function getAnonSO():SharedObject
{
// grab anon profile
return SharedObject.getLocal(formatName(-1), "/");
}
/**
* Compares cookie timestamp to current time
*/
public function isTimestampExpired(timestamp:Date):Boolean
{
// grab session timeout variable
var timeout:Number = Model.getInstance().assetProxy.config..system.sessiontimeout;
// create expiration date by adding config.xml minutes to timestamp
var expirationTime:Number = timestamp.setMinutes(timestamp.minutes + timeout);
//Debug.log( "Expires at " + expirationTime + "; currently is " + new Date().time); // expired if the expiration time is less than now
return expirationTime < new Date().time;
}
/**
* Deletes the active shared object from storage
*/
public function deleteSharedObject():void
{
_so.clear();
}
/**
* Deletes the gift card shared object from storage
*/
public function deleteGiftCards():void
{
_so.data.giftcards = null;
giftCards = new Array();
giftCards.length = 0;
}
/**
* Returns the specified property of the data object from the
* customer's SharedObject
*/
public function getValue(soName:String):*
{
var data:*;
// return data
switch (soName)
{
case SO_SELECTED_MENU_ITEM_ID:
data = _so.data.selectedMenuItemID as String;
if (!data)
data = "-1";
break;
case SO_SELECTED_COUPON_CODE:
data = _so.data.selectedCouponCode as String;
if (!data)
data = "-1";
break;
case SO_GIFTCARDS:
data = _so.data.giftcards as Array;
if (!data)
data = null;
break;
case SO_CATEGORY_ID:
data = _so.data.categoryID as int;
if (!data)
data = "-1";
break;
case SO_CUSTOMER_ID:
data = _so.data.customerID as int;
if (!data)
data = -1;
break;
case SO_STORE_ID:
data = _so.data.storeID as int;
if (!data)
data = -1;
break;
case SO_APP_STATE:
data = _so.data.appState as int;
if (!data)
data = ApplicationStateType.MENU;
break;
case SO_ORDER:
data = _so.data.order as Object;
break;
case SO_PAYMENT:
data = _so.data.payment+giftCardCount as Object;
if (!data)
data = "-1";
break;
case SO_PAYMENT_TYPE:
data = _so.data.selectedPaymentType as String;
if (!data)
data = "-1";
break;
case SO_CUSTOMER:
data = _so.data.customer as Object;
break;
case SO_ORDER_ID_TO_LOAD:
data = _so.data.orderHeaderID as int;
if (!data)
data = -1;
break;
case SO_TIMESTAMP:
data = _so.data.timestamp as Date;
break;
}
return data;
}
/**
* Saves a value to the specified property of the active shared object
*/
public function saveValue(soName:String, value:Object):void
{
Debug.log(value.toString());
switch (soName)
{
case SO_SELECTED_MENU_ITEM_ID:
_so.data.selectedMenuItemID = value;
break;
case SO_SELECTED_COUPON_CODE:
_so.data.selectedCouponCode = value;
break;
case SO_CATEGORY_ID:
_so.data.categoryID = value;
break;
case SO_CUSTOMER_ID:
_so.data.customerID = value;
break;
case SO_STORE_ID:
_so.data.storeID = value;
break;
case SO_APP_STATE:
_so.data.appState = value;
break;
case SO_ORDER:
_so.data.order = Order.compress(value);
break;
case SO_CUSTOMER:
_so.data.customer = Customer.compress(value);
break;
case SO_PAYMENT:
_so.data.payment = Payment.compress(value);
break;
case SO_PAYMENT_TYPE:
_so.data.selectedPaymentType = value;
break;
case SO_TIMESTAMP:
_so.data.timestamp = value;
break;
}
// save object immediately
_so.flush();
}
/**
* Creates a standardized naming convention based on customer ID and name of the
* SharedObject
*/
private const NAME_TOKEN:String = "dOMS%CID%";
private function formatName(cid:int):String
{
return NAME_TOKEN.replace("%CID%", cid.toString());
}
/**
* Transfers data from the anon profile to the user profile.
*/
private function transferAnonToCustomer(anonSO:SharedObject):void
{
if (anonSO.data.storeID)
saveValue(SO_STORE_ID, anonSO.data.storeID);
if (anonSO.data.order)
{
var orderObj:Object = anonSO.data.order;
var order:Order = new Order();
if (orderObj)
{
order = Order.decompress(orderObj);
}
else
{
order = new Order();
}
saveValue(SO_ORDER, order);
}
if (anonSO.data.categoryID)
{
saveValue(SO_CATEGORY_ID, anonSO.data.categoryID);
}
}
}
}
{
import flash.net.SharedObject;
import flash.net.registerClassAlias;
import mx.collections.ArrayCollection;
/**
* Manager for accessing the SharedObjects for the menu. Helps
* with the loading and saving of state variables so uses can navigate to
* and from the menu without losing cart information
*/
public class SOManager
{
public static const SO_APP_STATE:String = "d_SO_App_State";
public static const SO_CATEGORY_ID:String = "d_SO_Category_ID";
public static const SO_STORE_ID:String = "d_SO_Store_ID";
public static const SO_SELECTED_MENU_ITEM_ID:String = "d_SO_Selected_Menu_Item_ID";
public static const SO_SELECTED_COUPON_CODE:String = "d_SO_SELECTED_COUPON_CODE";
public static const SO_ORDER:String = "d_SO_ORDER";
public static const SO_CUSTOMER:String = "d_SO_CUSTOMER";
public static const SO_PAYMENT:String = "d_SO_PAYMENT";
public static const SO_PAYMENT_TYPE:String = "d_SO_PAYMENT_TYPE";
public static const SO_GIFTCARDS:String = "d_SO_GIFTCARD";
public static const SO_APPlIED:Array = new Array();
public var giftCards:Array = new Array();
public static const SO_ORDER_ID_TO_LOAD:String = "d_SO_ORDER_TO_LOAD";
public static const SO_TIMESTAMP:String = "d_SO_TIMESTAMP";
public static const SO_CUSTOMER_ID:String = "d_SO_CUSTOMER_ID";
public var giftCardCount:int = 0;
private var _isReady:Boolean = false;
public function get isReady():Boolean
{
return _isReady;
}
// main shared object variable that is used for storage of menu variables
private var _so:SharedObject;
public function get SO():SharedObject
{
return _so;
}
public function set SO(value:SharedObject):void
{
_so = value;
}
/**
* Constructor
*/
public function SOManager():void
{
}
/**
* Creates a reference to the shared object based on the user's customer ID
*/
public function createSharedObject(loadedCustomerID:int):void
{
// grab anon profile
var anonSO:SharedObject = getAnonSO();
// get current time
var now:Date = new Date();
// if user is anonymous, check for timestamp expiration to reset
if (loadedCustomerID == -1)
{
// assign the _so the anonymous value
_so = anonSO;
// grab current timestamp for this user
var timestamp:Date = getValue(SOManager.SO_TIMESTAMP);
if (timestamp && isTimestampExpired(timestamp))
{
// the timestamp is too old. clear cookie
//Debug.log( "Deleting SO for anon user b/c timestamp has expired");
deleteSharedObject();
// session is expired. update timestamp and continue
//Debug.log( "Setting new timestamp for anon user");
saveValue(SOManager.SO_TIMESTAMP, now);
}
}
else
{
// we have a non-anon customer. this customer could have been
// anonymous last load....so we need to check and assign this
// user the anon profile if necessary.
// check for user-specific cookie
_so = SharedObject.getLocal(formatName(loadedCustomerID), "/");
if (anonSO.data.timestamp && !isTimestampExpired(anonSO.data.timestamp))
{
// anon SO was not expired, so can't we assume that
// the incoming user was anonymous but now is not??
transferAnonToCustomer(anonSO);
}
//Debug.log( "Setting new timestamp for current Customer (" + loadedCustomerID + ")");
saveValue(SOManager.SO_TIMESTAMP, now);
}
// check to see if there is a store to load
var storeSO:SharedObject = SharedObject.getLocal("dStoreToLoad", "/");
if (storeSO.data.storeID && storeSO.data.storeID != -1)
{
saveValue(SO_STORE_ID, storeSO.data.storeID);
storeSO.data.storeID = -1;
storeSO.flush();
}
// check to see if there is an order to load
var orderSO:SharedObject = SharedObject.getLocal("dOrderToLoad", "/");
if (orderSO.data.orderHeaderID && orderSO.data.orderHeaderID != -1)
{
saveValue(SO_ORDER_ID_TO_LOAD, orderSO.data.orderHeaderID);
orderSO.data.orderHeaderID = -1;
orderSO.flush();
}
// set soManager to READY!
_isReady = true;
}
public function getAnonSO():SharedObject
{
// grab anon profile
return SharedObject.getLocal(formatName(-1), "/");
}
/**
* Compares cookie timestamp to current time
*/
public function isTimestampExpired(timestamp:Date):Boolean
{
// grab session timeout variable
var timeout:Number = Model.getInstance().assetProxy.config..system.sessiontimeout;
// create expiration date by adding config.xml minutes to timestamp
var expirationTime:Number = timestamp.setMinutes(timestamp.minutes + timeout);
//Debug.log( "Expires at " + expirationTime + "; currently is " + new Date().time); // expired if the expiration time is less than now
return expirationTime < new Date().time;
}
/**
* Deletes the active shared object from storage
*/
public function deleteSharedObject():void
{
_so.clear();
}
/**
* Deletes the gift card shared object from storage
*/
public function deleteGiftCards():void
{
_so.data.giftcards = null;
giftCards = new Array();
giftCards.length = 0;
}
/**
* Returns the specified property of the data object from the
* customer's SharedObject
*/
public function getValue(soName:String):*
{
var data:*;
// return data
switch (soName)
{
case SO_SELECTED_MENU_ITEM_ID:
data = _so.data.selectedMenuItemID as String;
if (!data)
data = "-1";
break;
case SO_SELECTED_COUPON_CODE:
data = _so.data.selectedCouponCode as String;
if (!data)
data = "-1";
break;
case SO_GIFTCARDS:
data = _so.data.giftcards as Array;
if (!data)
data = null;
break;
case SO_CATEGORY_ID:
data = _so.data.categoryID as int;
if (!data)
data = "-1";
break;
case SO_CUSTOMER_ID:
data = _so.data.customerID as int;
if (!data)
data = -1;
break;
case SO_STORE_ID:
data = _so.data.storeID as int;
if (!data)
data = -1;
break;
case SO_APP_STATE:
data = _so.data.appState as int;
if (!data)
data = ApplicationStateType.MENU;
break;
case SO_ORDER:
data = _so.data.order as Object;
break;
case SO_PAYMENT:
data = _so.data.payment+giftCardCount as Object;
if (!data)
data = "-1";
break;
case SO_PAYMENT_TYPE:
data = _so.data.selectedPaymentType as String;
if (!data)
data = "-1";
break;
case SO_CUSTOMER:
data = _so.data.customer as Object;
break;
case SO_ORDER_ID_TO_LOAD:
data = _so.data.orderHeaderID as int;
if (!data)
data = -1;
break;
case SO_TIMESTAMP:
data = _so.data.timestamp as Date;
break;
}
return data;
}
/**
* Saves a value to the specified property of the active shared object
*/
public function saveValue(soName:String, value:Object):void
{
Debug.log(value.toString());
switch (soName)
{
case SO_SELECTED_MENU_ITEM_ID:
_so.data.selectedMenuItemID = value;
break;
case SO_SELECTED_COUPON_CODE:
_so.data.selectedCouponCode = value;
break;
case SO_CATEGORY_ID:
_so.data.categoryID = value;
break;
case SO_CUSTOMER_ID:
_so.data.customerID = value;
break;
case SO_STORE_ID:
_so.data.storeID = value;
break;
case SO_APP_STATE:
_so.data.appState = value;
break;
case SO_ORDER:
_so.data.order = Order.compress(value);
break;
case SO_CUSTOMER:
_so.data.customer = Customer.compress(value);
break;
case SO_PAYMENT:
_so.data.payment = Payment.compress(value);
break;
case SO_PAYMENT_TYPE:
_so.data.selectedPaymentType = value;
break;
case SO_TIMESTAMP:
_so.data.timestamp = value;
break;
}
// save object immediately
_so.flush();
}
/**
* Creates a standardized naming convention based on customer ID and name of the
* SharedObject
*/
private const NAME_TOKEN:String = "dOMS%CID%";
private function formatName(cid:int):String
{
return NAME_TOKEN.replace("%CID%", cid.toString());
}
/**
* Transfers data from the anon profile to the user profile.
*/
private function transferAnonToCustomer(anonSO:SharedObject):void
{
if (anonSO.data.storeID)
saveValue(SO_STORE_ID, anonSO.data.storeID);
if (anonSO.data.order)
{
var orderObj:Object = anonSO.data.order;
var order:Order = new Order();
if (orderObj)
{
order = Order.decompress(orderObj);
}
else
{
order = new Order();
}
saveValue(SO_ORDER, order);
}
if (anonSO.data.categoryID)
{
saveValue(SO_CATEGORY_ID, anonSO.data.categoryID);
}
}
}
}