/// <summary>
        /// Turn the unprocessed data into keyed name-value pairs
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> UnpackData(Activity activity)
        {
            if (activity == null)
            {
                throw new ApplicationException("Attempting to unpack activity data when no database record.");
            }

            var keyValuePairs = new Dictionary<string, string>();

            // Form of data is "name=value,name=value" etc
            var keyValuePairsRaw = activity.Data.Split(new[] { ',' });

            var pattern = new Regex(RegexNameValue, RegexOptions.None);

            foreach (var keyValuePairRaw in keyValuePairsRaw)
            {
                var match = pattern.Match(keyValuePairRaw);

                if (match.Success)
                {
                    keyValuePairs.Add(match.Groups[1].Value, match.Groups[2].Value);
                }
            }

            return keyValuePairs;
        }
 /// <summary>
 /// Constructor - useful when constructing a badge activity after reading database
 /// </summary>
 public MemberJoinedActivity(Activity activity, Member user)
 {
     ActivityMapped = activity;
     User = user;
 }
        /// <summary>
        /// Make a badge activity object from the more generic database activity object
        /// </summary>
        /// <param name="activity"></param>
        /// <returns></returns>
        private BadgeActivity GenerateBadgeActivity(Activity activity)
        {
            // Get the corresponding badge
            var dataPairs = ActivityBase.UnpackData(activity);

            if (!dataPairs.ContainsKey(AppConstants.KeyBadgeId))
            {
                // Log the problem then skip
                AppHelpers.LogError(string.Format("A badge activity record with id '{0}' has no badge id in its data.", activity.Id));
                return null;
            }

            var badgeId = dataPairs[AppConstants.KeyBadgeId];
            var badge = ServiceFactory.BadgeService.Get(new Guid(badgeId));

            if (badge == null)
            {
                // Log the problem then skip
                AppHelpers.LogError(string.Format("A badge activity record with id '{0}' has a badge id '{1}' that is not found in the badge table.",
                    activity.Id, badgeId));
                return null;
            }

            var userId = dataPairs[AppConstants.KeyUserId];
            var user = ServiceFactory.MemberService.Get(Convert.ToInt32(userId));

            if (user == null)
            {
                // Log the problem then skip
                AppHelpers.LogError(string.Format("A badge activity record with id '{0}' has a user id '{1}' that is not found in the user table.",
                    activity.Id, userId));
                return null;
            }

            return new BadgeActivity(activity, badge, user);
        }
 public void Delete(Activity item)
 {
     ContextPerRequest.Db.Activity.Remove(item);
 }
 /// <summary>
 /// Add a new activity (expected id already assigned)
 /// </summary>
 /// <param name="newActivity"></param>
 public Activity Add(Activity newActivity)
 {
     return ContextPerRequest.Db.Activity.Add(newActivity);
 }
        /// <summary>
        /// Make a member joined activity object from the more generic database activity object
        /// </summary>
        /// <param name="activity"></param>
        /// <returns></returns>
        private MemberJoinedActivity GenerateMemberJoinedActivity(Activity activity)
        {
            var dataPairs = ActivityBase.UnpackData(activity);

            if (!dataPairs.ContainsKey(AppConstants.KeyUserId))
            {
                // Log the problem then skip
                AppHelpers.LogError(string.Format("A member joined activity record with id '{0}' has no user id in its data.", activity.Id));
                return null;
            }

            var userId = dataPairs[AppConstants.KeyUserId];
            var user = ServiceFactory.MemberService.Get(Convert.ToInt32(userId));

            if (user == null)
            {
                // Log the problem then skip
                AppHelpers.LogError(string.Format("A member joined activity record with id '{0}' has a user id '{1}' that is not found in the user table.",
                    activity.Id, userId));
                return null;
            }

            return new MemberJoinedActivity(activity, user);
        }
 /// <summary>
 /// Constructor - useful when constructing a badge activity after reading database
 /// </summary>
 public ProfileUpdatedActivity(Activity activity, Member user)
 {
     ActivityMapped = activity;
     User = user;
 }
 /// <summary>
 /// Constructor - useful when constructing a badge activity after reading database
 /// </summary>
 public BadgeActivity(Activity activity, Badge badge, Member user)
 {
     ActivityMapped = activity;
     Badge = badge;
     User = user;
 }