using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Xml.Serialization; using Activizr.Basic.Types; using Activizr.Database; namespace Activizr.Logic.Pirates { [Serializable] public class Volunteers : List { public static Volunteers GetOpen() { return FromArray(PirateDb.GetDatabase().GetOpenVolunteers()); } public static Volunteers FromArray (BasicVolunteer[] basicArray) { var result = new Volunteers(); result.Capacity = basicArray.Length*11/10; foreach (BasicVolunteer basic in basicArray) { result.Add(Volunteer.FromBasic(basic)); } return result; } public static Volunteers FromSingle (Volunteer volunteer) { var volunteers = new Volunteers(); volunteers.Add(volunteer); return volunteers; } public void Remove (Volunteers volunteersToRemove) { var identityLookup = new Dictionary(); foreach (Volunteer volunteer in volunteersToRemove) { identityLookup[volunteer.Identity] = true; } int index = 0; while (index < Count) { if (identityLookup.ContainsKey(this[index].Identity)) { // This volunteer should be removed from the list. RemoveAt(index); } else { // If nothing was removed, increment index. index++; } } } public new void Remove (Volunteer volunteerToRemove) { Remove(FromSingle(volunteerToRemove)); } public DataSet ToDataSet() { // This function takes a detour around in-memory XML for maintainability of this code. // It's possible to write it more optimized, and build the DataSet by hand, but that takes // around 100 LoC. var serializer = new XmlSerializer(typeof (Volunteers)); var sw = new StringWriter(); serializer.Serialize(sw, this); var ds = new DataSet(); ds.ReadXml(sw.ToString()); return ds; } } }