//implement a values based equality
 public bool Equals(WorkItem w)
 {
     return
             (this.WorkItemID == w.WorkItemID) &&
             (this.Name == w.Name) &&
             (this.Description == w.Description);
 }
        /*public void WorkItemAWSPost()
        {
            var wif = new dalewilbanks.net.factories.WorkItemFactoryAWS(
            System.Configuration.ConfigurationManager.AppSettings["AWSAccessKey"],
            System.Configuration.ConfigurationManager.AppSettings["AWSSecretKey"]);

            int workItemID = 0;
            Int32.TryParse(Request["WorkItemID"], out workItemID);

            if (workItemID != 0)
            {
                var workItem = new dalewilbanks.net.entities.WorkItem() { WorkItemID = workItemID, Name = Request["Name"], Type = Request["Type"], Description = Request["Description"], Attachments = Request["Attachments"], EnteredBy = Request["EnteredBy"], EntryDate = Request["EntryDate"] };
                wif.Save(workItem);
            }
        }*/

        public void WorkItemEFPost()
        {
            var workItemsContext = new WorkItemContext();

            var workItem = new dalewilbanks.net.entities.WorkItem() { Name = Request["Name"], Type = Request["Type"], Description = Request["Description"], Attachments = String.Empty, EnteredBy = Request["EnteredBy"], EntryDate = Request["EntryDate"] };
            workItemsContext.WorkItems.Add(workItem);
            workItemsContext.SaveChanges();
        }
        public List<WorkItem> GetWorkItems()
        {
            String selectExpression = "Select * From WorkItems";
            SelectRequest selectRequestAction = new SelectRequest().WithSelectExpression(selectExpression);
            SelectResponse selectResponse = sdb.Select(selectRequestAction);

            if (!selectResponse.IsSetSelectResult()) { throw new ApplicationException("No Select Result"); }

            SelectResult selectResult = selectResponse.SelectResult;

            var list = new List<WorkItem>();

            foreach(Item item in selectResult.Item)
            {
                int id = 0;   
                Int32.TryParse(item.Name, out id);

                var workItem = new WorkItem() { WorkItemID = id };

                foreach (Amazon.SimpleDB.Model.Attribute attribute in item.Attribute)
                {
                        //TODO: Break this out into a method that accepts an attribute and a workItem
                       switch (attribute.Name)
                       {
                           case "Name":
                               workItem.Name = attribute.Value;
                               break;

                           case "Type":
                               workItem.Type = attribute.Value;
                               break;

                           case "Description":
                               workItem.Description = attribute.Value;
                               break;

                           case "Attachments":
                               workItem.Attachments = attribute.Value;
                               break;

                           case "EntryDate":
                               workItem.EntryDate = attribute.Value;
                               break;

                           case "EnteredBy":
                               workItem.EnteredBy = attribute.Value;
                               break;
                       }
                   }//end foreach attribute

                list.Add(workItem);
            }//end foreach item

            return list;

        }//end method
 public ActionResult Edit(WorkItem workitem)
 {
     if (ModelState.IsValid)
     {
         db.Entry(workitem).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(workitem);
 }
        private WorkItem HydrateWorkItem(DataRow dataRow)
        {
            var wi = new WorkItem();

            wi.WorkItemID = (int)dataRow["WorkItemId"];
            wi.Name = (string)dataRow["Name"];
            wi.Description = (string)dataRow["Description"];

            return wi;
        }
        public ActionResult Create(WorkItem workitem)
        {
            if (ModelState.IsValid)
            {
                db.WorkItems.Add(workitem);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(workitem);
        }
        public ActionResult Create(WorkItem workitem)
        {
            if (ModelState.IsValid)
            {
                db.WorkItems.Add(workitem);
                db.SaveChanges();
                return PartialView("GridData", new WorkItem[] { workitem });
            }

            return PartialView("Edit", workitem);
        }
 public void Save(WorkItem workItem)
 {
     throw new NotImplementedException();
 }
 public WorkItem Update(string id, WorkItem instance)
 {
     // TODO: Update the given instance of WorkItem in the collection
     throw new NotImplementedException();
 }
 public WorkItem Create(WorkItem instance)
 {
     // TODO: Add the new instance of WorkItem to the collection
     throw new NotImplementedException();
 }
        }//end method


        //Need to break out the deserialization into it's own method to make this DRY
        public WorkItem GetWorkItem(int id)
        {
            GetAttributesRequest gar = new GetAttributesRequest();
            gar.DomainName = "WorkItems";
            gar.ItemName = id.ToString(); 
            GetAttributesResponse response = sdb.GetAttributes(gar);

            var workItem = new WorkItem() { WorkItemID = id };

            foreach (Amazon.SimpleDB.Model.Attribute attribute in response.GetAttributesResult.Attribute)
            {
                switch (attribute.Name)
                {
                    case "Name":
                        workItem.Name = attribute.Value;
                        break;

                    case "Type":
                        workItem.Type = attribute.Value;
                        break;

                    case "Description":
                        workItem.Description = attribute.Value;
                        break;

                    case "Attachments":
                        workItem.Attachments = attribute.Value;
                        break;

                    case "EntryDate":
                        workItem.EntryDate = attribute.Value;
                        break;

                    case "EnteredBy":
                        workItem.EnteredBy = attribute.Value;
                        break;
                }
            }

            return workItem;
        }
        //TODO: Add Exception Handling and Logging
        public void Save(WorkItem workItem)
        {
            String itemName = workItem.WorkItemID.ToString();

            PutAttributesRequest putAttributesAction = new PutAttributesRequest().WithDomainName("WorkItems").WithItemName(itemName);
            List<ReplaceableAttribute> attributesOne = putAttributesAction.Attribute;
            attributesOne.Add(new ReplaceableAttribute().WithName("Name").WithValue(workItem.Name));
            attributesOne.Add(new ReplaceableAttribute().WithName("Type").WithValue(workItem.Type));
            attributesOne.Add(new ReplaceableAttribute().WithName("Description").WithValue(workItem.Description));
            attributesOne.Add(new ReplaceableAttribute().WithName("Attachments").WithValue(workItem.Attachments));
            attributesOne.Add(new ReplaceableAttribute().WithName("EntryDate").WithValue(workItem.EntryDate));
            attributesOne.Add(new ReplaceableAttribute().WithName("EnteredBy").WithValue(workItem.EnteredBy));

            sdb.PutAttributes(putAttributesAction);

        }
        public ActionResult Edit(WorkItem workitem)
        {
            if (ModelState.IsValid)
            {
                db.Entry(workitem).State = EntityState.Modified;
                db.SaveChanges();
                return PartialView("GridData", new WorkItem[] { workitem });
            }

            return PartialView(workitem);
        }