00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using System.IO;
00005 using System.Runtime.Serialization.Formatters.Binary;
00006 using System.Data;
00007 using Shared;
00008
00009 namespace Server.Data
00010 {
00014 public class BinaryData : IPersistentData
00015 {
00016 private string directory = "";
00021 public BinaryData(string directory)
00022 {
00023 this.directory = directory;
00024 if (!Directory.Exists(directory))
00025 {
00026 Directory.CreateDirectory(directory);
00027 }
00028 }
00033 public DataContainer LoadData()
00034 {
00035 BinaryFormatter bf = new BinaryFormatter();
00036
00037 Dictionary<string, Person> people = new Dictionary<string, Person>();
00038 using (FileStream fs = File.Open(Path.Combine(directory, "people.bin"), FileMode.OpenOrCreate))
00039 {
00040 while (fs.Length != 0 && fs.Position < fs.Length)
00041 {
00042 PersonInfo pi = (PersonInfo)bf.Deserialize(fs);
00043 people.Add(pi.username, Person.FromInfo(pi));
00044 }
00045 }
00046
00047 Dictionary<int, Project> projects = new Dictionary<int, Project>();
00048 using (FileStream fs = File.Open(Path.Combine(directory, "projects.bin"), FileMode.OpenOrCreate))
00049 {
00050 while (fs.Length != 0 && fs.Position < fs.Length)
00051 {
00052 ProjectInfo pi = (ProjectInfo)bf.Deserialize(fs);
00053 projects.Add(pi.id, Project.FromInfo(pi));
00054 }
00055 }
00056
00057 Dictionary<int, Literature> literature = new Dictionary<int, Literature>();
00058 using (FileStream fs = File.Open(Path.Combine(directory, "literature.bin"), FileMode.OpenOrCreate))
00059 {
00060 while (fs.Length != 0 && fs.Position < fs.Length)
00061 {
00062 LiteratureInfo pi = (LiteratureInfo)bf.Deserialize(fs);
00063 literature.Add(pi.id, Literature.FromInfo(pi, people[pi.creator]));
00064 }
00065 }
00066
00067
00068
00069 using (FileStream fs = File.Open(Path.Combine(directory, "roles.bin"), FileMode.OpenOrCreate))
00070 {
00071 while (fs.Length != 0 && fs.Position < fs.Length)
00072 {
00073 RoleInfo ri = (RoleInfo)bf.Deserialize(fs);
00074 if (projects.ContainsKey(ri.Project) && people.ContainsKey(ri.UserName))
00075 {
00076 projects[ri.Project].AddPerson(people[ri.UserName], ri.Type);
00077 }
00078 }
00079 }
00080
00081 using (FileStream fs = File.Open(Path.Combine(directory, "references.bin"), FileMode.OpenOrCreate))
00082 {
00083 while (fs.Length != 0 && fs.Position < fs.Length)
00084 {
00085 object obj = bf.Deserialize(fs);
00086 if (obj is ReferenceInfo)
00087 {
00088 ReferenceInfo r = (ReferenceInfo)obj;
00089 if (projects.ContainsKey(r.project) && literature.ContainsKey(r.literature))
00090 {
00091 Reference re = new Reference(projects[r.project], literature[r.literature], r.date, r.used, r.status);
00092 literature[r.literature].References.Add(re);
00093 projects[r.project].References.Add(re);
00094 }
00095 }
00096 if (obj is SuggestionInfo)
00097 {
00098 SuggestionInfo r = (SuggestionInfo)obj;
00099 if (projects.ContainsKey(r.project) && literature.ContainsKey(r.literature) && people.ContainsKey(r.person))
00100 {
00101 Suggestion s = new Suggestion(projects[r.project], people[r.person], literature[r.literature], r.date, r.used, r.status, r.reason);
00102 literature[r.literature].References.Add(s);
00103 projects[r.project].References.Add(s);
00104 }
00105 }
00106
00107 if (obj is ReviewInfo)
00108 {
00109 ReviewInfo r = (ReviewInfo)obj;
00110 if (projects.ContainsKey(r.project) && literature.ContainsKey(r.literature) && people.ContainsKey(r.person))
00111 {
00112 Review re = new Review(projects[r.project], people[r.person], literature[r.literature], r.date, r.used, r.status, r.title, r.text, r.rating);
00113 literature[r.literature].References.Add(re);
00114 projects[r.project].References.Add(re);
00115 }
00116 }
00117 }
00118 }
00119
00120 Dictionary<int, Comment> comments = new Dictionary<int, Comment>();
00121 using (FileStream fs = File.Open(Path.Combine(directory, "comments.bin"), FileMode.OpenOrCreate))
00122 {
00123 while (fs.Length != 0 && fs.Position < fs.Length)
00124 {
00125 CommentInfo comment = (CommentInfo)bf.Deserialize(fs);
00126 if (people.ContainsKey(comment.poster))
00127 {
00128 switch (comment.type)
00129 {
00130 case CommentType.Reference:
00131 if (!projects.ContainsKey(comment.project) || !literature.ContainsKey(comment.parent))
00132 break;
00133
00134 foreach (Reference r in projects[comment.project].References)
00135 {
00136 if (r.Literature.ID == comment.parent)
00137 {
00138 Comment c = new Comment(comment.id, people[comment.poster], comment.title, comment.text, comment.type, comment.date);
00139 r.Comments.Add(c);
00140 comments.Add(c.ID, c);
00141 break;
00142 }
00143 }
00144 break;
00145 case CommentType.Literature:
00146 if (!literature.ContainsKey(comment.parent))
00147 break;
00148
00149 Comment c2 = new Comment(comment.id, people[comment.poster], comment.title, comment.text, comment.type, comment.date);
00150 literature[comment.parent].Comments.Add(c2);
00151 comments.Add(c2.ID, c2);
00152 break;
00153 case CommentType.Comment:
00154 if (!comments.ContainsKey(comment.parent))
00155 break;
00156
00157 Comment c3 = new Comment(comment.id, people[comment.poster], comment.title, comment.text, comment.type, comment.date);
00158 comments[comment.parent].Comments.Add(c3);
00159 comments.Add(c3.ID, c3);
00160 break;
00161 }
00162 }
00163 }
00164 }
00165
00166
00167
00168 DataTable tags = DataContainer.CreateEmptyTagTable();
00169
00170 using (FileStream fs = File.Open(Path.Combine(directory, "tags.bin"), FileMode.OpenOrCreate))
00171 {
00172 while (fs.Length != 0 && fs.Position < fs.Length)
00173 {
00174 TagInfo ri = (TagInfo)bf.Deserialize(fs);
00175 if ((projects.ContainsKey(ri.ID) || literature.ContainsKey(ri.ID)) && people.ContainsKey(ri.UserName))
00176 {
00177 tags.Rows.Add(ri.Name, ri.ID, ri.UserName);
00178 }
00179 }
00180 }
00181 tags.AcceptChanges();
00182
00183 return new DataContainer(projects, literature, comments, people, tags);
00184
00185 }
00186
00194 private void SerializeComment(Comment c, Comment parent, BinaryFormatter bf, Stream stream)
00195 {
00196 bf.Serialize(stream, new CommentInfo(c.ID, parent.ID, -1, c.Poster.UserName, c.Title, c.Text, c.Date, CommentType.Comment));
00197 foreach (Comment c2 in c.Comments)
00198 SerializeComment(c2, c, bf, stream);
00199 }
00204 public void SaveData(DataContainer data)
00205 {
00206 BinaryFormatter bf = new BinaryFormatter();
00207 using (FileStream commentFs = File.Open(Path.Combine(directory, "comments.bin"), FileMode.Create))
00208 {
00209
00210
00211 using (FileStream refs = File.Open(Path.Combine(directory, "references.bin"), FileMode.Create))
00212 using (FileStream rfs = File.Open(Path.Combine(directory, "roles.bin"), FileMode.Create))
00213 using (FileStream fs = File.Open(Path.Combine(directory, "projects.bin"), FileMode.Create))
00214 {
00215 foreach (Project p in data.ProjectTable.Values)
00216 {
00217 bf.Serialize(fs, p.Info);
00218 foreach (Role r in p.Roles)
00219 bf.Serialize(rfs, r.Info);
00220
00221 foreach (Reference r in p.References)
00222 {
00223 if (r is Suggestion)
00224 {
00225 bf.Serialize(refs, ((Suggestion)r).Info);
00226 }
00227 else if (r is Review)
00228 {
00229 bf.Serialize(refs, ((Review)r).Info);
00230 }
00231 else
00232 bf.Serialize(refs, r.Info);
00233
00234 foreach (Comment c in r.Comments)
00235 {
00236 bf.Serialize(commentFs, new CommentInfo(c.ID, r.Literature.ID, p.ID, c.Poster.UserName, c.Title, c.Text, c.Date, CommentType.Reference));
00237 foreach (Comment cc in c.Comments)
00238 SerializeComment(cc, c, bf, commentFs);
00239 }
00240 }
00241 }
00242 }
00243
00244 using (FileStream fs = File.Open(Path.Combine(directory, "people.bin"), FileMode.Create))
00245 {
00246 foreach (Person p in data.PersonTable.Values)
00247 {
00248 PersonInfo pi = p.Info;
00249 pi.password = p.Password;
00250 bf.Serialize(fs, pi);
00251 }
00252 }
00253
00254 using (FileStream fs = File.Open(Path.Combine(directory, "literature.bin"), FileMode.Create))
00255 {
00256 foreach (Literature p in data.LiteratureTable.Values)
00257 {
00258 bf.Serialize(fs, p.Info);
00259
00260 foreach (Comment c in p.Comments)
00261 {
00262 bf.Serialize(commentFs, new CommentInfo(c.ID, p.ID, -1, c.Poster.UserName, c.Title, c.Text, c.Date, CommentType.Literature));
00263 foreach (Comment cc in c.Comments)
00264 SerializeComment(cc, c, bf, commentFs);
00265 }
00266
00267 }
00268 }
00269
00270 using (FileStream fs = File.Open(Path.Combine(directory, "tags.bin"), FileMode.Create))
00271 {
00272 foreach (DataRow row in data.Tags.Rows)
00273 {
00274 bf.Serialize(fs, new TagInfo((string)row[0], (int)row[1], (string)row[2]));
00275 }
00276 }
00277 }
00278
00279
00280 }
00281 }
00282 }