public bool Add(ChallengeStatusVote vote)
        {
            bool success = false;

            ChallengeStatusVoteDb d = VoteToDbVote(vote);
            context.MergeOption = MergeOption.AppendOnly;
            context.AttachTo(TableName, d, null);
            context.UpdateObject(d);

            try
            {
                context.SaveChangesWithRetries();
                success=true;
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.WriteLine("Status repo vote exception: Couldn't add vote " + e.ToString());
                success=false;
            }
            finally
            {
                context.Detach(d);
                context.MergeOption = MergeOption.NoTracking;
            }

            return success;
        }
        public void AcceptClaim(ChallengeStatus status)
        {
            ChallengeStatus s = StatusRepo.Get(status.CustomerID, status.ChallengeID);

            ChallengeBid bid = BidRepo.CustomerDidBidOnChallenge(((DareyaIdentity)HttpContext.Current.User.Identity).CustomerID, s.ChallengeID);
            if (bid == null)
                throw new HttpResponseException(System.Net.HttpStatusCode.Forbidden);

            try
            {
                if (VoteRepo.BidderDidVote(s.ChallengeID, s.CustomerID, bid.CustomerID) != null)
                    return; // you already voted, dawg.
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.WriteLine("Vote Repo Exception: " + e.ToString());
                throw new HttpResponseException(System.Net.HttpStatusCode.InternalServerError);
            }

            ChallengeStatusVote vote = new ChallengeStatusVote();
            vote.ChallengeID = s.ChallengeID;
            vote.CustomerID = s.CustomerID;
            vote.BidderCustomerID = ((DareyaIdentity)HttpContext.Current.User.Identity).CustomerID;
            vote.Accepted = true;

            try
            {
                if (!VoteRepo.Add(vote))
                {
                    // if this returns false, they probably already voted.
                    return; // HACK.
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.WriteLine("Vote Repo Exception: " + e.ToString());
                throw new HttpResponseException(System.Net.HttpStatusCode.InternalServerError);
            }

            try
            {
                Activity activity = new Activity(s.ChallengeID, DateTime.UtcNow) { Type = (int)ActivityType.ActivityVoteYes, TakerCustomerID = vote.CustomerID, CustomerID = vote.BidderCustomerID };
                RepoFactory.GetActivityRepo().Add(activity);
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.WriteLine("Activity Exception: " + e.ToString());
            }

            int yesVotes = VoteRepo.GetYesVotes(s);
            if (yesVotes > (BidRepo.GetBidCountForChallenge(status.ChallengeID) * 0.33))
            {
                Challenge chal = ChalRepo.Get(s.ChallengeID);
                chal.State = (int)Challenge.ChallengeState.Completed;
                chal.TargetCustomerID = status.CustomerID;

                try
                {
                    ChalRepo.Update(chal);

                    BidRepo.UpdateStatusForBidsOnChallenge(s.ChallengeID, ChallengeBid.BidStatusCodes.Accepted);

                    s.Status = (int)ChallengeStatus.StatusCodes.Completed;
                    StatusRepo.Update(s);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.WriteLine("Challenge Status Exception: " + e.ToString());
                    throw new HttpResponseException(System.Net.HttpStatusCode.InternalServerError);
                }

                // challenge completed! award the money! DO IT DO IT!
                try
                {
                    Activity activity = new Activity(s.ChallengeID, DateTime.UtcNow) { Type = (int)ActivityType.ActivityComplete, CustomerID = status.CustomerID };
                    RepoFactory.GetActivityRepo().Add(activity);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.WriteLine("Activity Exception: " + e.ToString());
                }

                // queue the billing system to process this challenge status
                Dictionary<string, long> billingQueueItemData=new Dictionary<string,long>();
                billingQueueItemData.Add("ChalID", s.ChallengeID);
                billingQueueItemData.Add("CustID", s.CustomerID);
                RepoFactory.GetProcessingQueue().PutQueueMessage(ProcessingQueue.MessageType.Billing, billingQueueItemData);

                // notify the customer
                CustomerNotifier.NotifyChallengeAwardedToYou(s.CustomerID, s.ChallengeID);
            }
            else
            {
                // keep waitin'.

            }
        }
        public void RejectClaim(ChallengeStatus status)
        {
            ChallengeStatus s = StatusRepo.Get(status.CustomerID, status.ChallengeID);

            ChallengeBid bid=BidRepo.CustomerDidBidOnChallenge(((DareyaIdentity)HttpContext.Current.User.Identity).CustomerID, s.ChallengeID);
            if (bid==null)
                throw new HttpResponseException(System.Net.HttpStatusCode.Forbidden);

            if (VoteRepo.BidderDidVote(s.ChallengeID, s.CustomerID, bid.CustomerID)!=null)
                return; // you already voted, dawg.

            // add the "no" vote
            ChallengeStatusVote vote = new ChallengeStatusVote();
            vote.ChallengeID = s.ChallengeID;
            vote.CustomerID = s.CustomerID;
            vote.BidderCustomerID = ((DareyaIdentity)HttpContext.Current.User.Identity).CustomerID;
            vote.Accepted = false;

            if (!VoteRepo.Add(vote))
                return; // HACK.

            try
            {
                Activity activity = new Activity(s.ChallengeID, DateTime.UtcNow) { Type = (int)ActivityType.ActivityVoteNo, TakerCustomerID=vote.CustomerID, CustomerID = vote.BidderCustomerID };
                RepoFactory.GetActivityRepo().Add(activity);
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.WriteLine("Activity Exception: " + e.ToString());
            }

            int noVotes=VoteRepo.GetNoVotes(s);
            if(noVotes > (BidRepo.GetBidCountForChallenge(status.ChallengeID)*0.66))
            {
                s.Status = (int)ChallengeStatus.StatusCodes.SourceRejected;
                StatusRepo.Update(s);

                // get the next homey who's working on this, if any.
                ChallengeStatus nextStatus = StatusRepo.GetNextVotePendingStatusForChallenge(s.ChallengeID);
                if (nextStatus != null)
                {
                    BidRepo.UpdateVotePendingCustomerIDForChallenge(s.ChallengeID, nextStatus.CustomerID);
                }
                else
                {
                    // reopen the bidding! NO WINNER NO WINNER

                }

                // you've failed this challenge my friend.
                //CustomerNotifier.NotifyChallengeRejected(s.CustomerID, s.ChallengeID);
            }
            else
            {

            }
        }
        private ChallengeStatusVoteDb VoteToDbVote(ChallengeStatusVote v)
        {
            if (v == null) return null;

            ChallengeStatusVoteDb d = new ChallengeStatusVoteDb();

            d.Accepted = v.Accepted;
            d.ChallengeID = v.ChallengeID;
            d.CustomerID = v.CustomerID;
            d.BidderCustomerID = v.BidderCustomerID;

            d.PartitionKey = DbPartitionForTaker(d.ChallengeID, d.CustomerID);
            d.RowKey = DbRowForBidder(d.BidderCustomerID);

            return d;
        }
        private ChallengeStatusVote DbVoteToVote(ChallengeStatusVoteDb d)
        {
            if (d == null) return null;

            ChallengeStatusVote v = new ChallengeStatusVote();

            v.Accepted = d.Accepted;
            v.ChallengeID = d.ChallengeID;
            v.CustomerID = d.CustomerID;
            v.BidderCustomerID = d.BidderCustomerID;

            return v;
        }