private void ProcessInsertDesignIdea(int id) { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var designideas = from d in db.Contributions where d.id == id select d; if (designideas.Count() == 1) { Contribution di = designideas.Single <Contribution>(); User u = find_user_of_contribution(di); Activity a = find_activity_of_contribution(di); if (u == null || a == null) { return; } string cid = a.technical_info.Split(new char[] { ';' })[0]; SContext c = server_api.GetContext(cid); if (c == null) { return; } SNote note = server_api.CreateNote(u.name, "DesignIdea", di.note, c.name, di.status.ToString()); if (note == null) { return; } di.technical_info = note.id.ToString(); db.SubmitChanges(); if (RESTService.Last_Exception != null) { this.errors.Add(RESTService.Last_Exception); } } }
private void button_remove_user_Click(object sender, EventArgs e) { if (cached_users.Count == 0) { MessageBox.Show("Please refresh the users list."); return; } if (comboBox_users.SelectedIndex < 0) { MessageBox.Show("Please select a user."); return; } int id = cached_users[comboBox_users.SelectedIndex].id; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var users = from u in db.Users where u.id == id select u; if (users.Count() != 1) { MessageBox.Show("Error. User not found."); return; } User the_user = users.Single <User>(); var collections = from c in db.Collections where c.user_id == the_user.id select c; if (collections.Count() > 0) { MessageBox.Show("This user cannot be deleted, because of his/her collections."); return; } var feedbacks = from f in db.Feedbacks where f.user_id == the_user.id select f; if (feedbacks.Count() > 0) { MessageBox.Show("This user cannot be deleted, because of his/her feedbacks."); return; } db.Users.DeleteOnSubmit(the_user); sync.classes.Action action = new classes.Action(); action.type_id = 3; action.user_id = 0; action.date = DateTime.UtcNow; action.object_type = "nature_net.User"; action.object_id = the_user.id; db.Actions.InsertOnSubmit(action); action.technical_info = the_user.name; try { db.SubmitChanges(); } catch (Exception ex) { MessageBox.Show("Error (" + ex.Message + "):\r\n" + ex.StackTrace); return; } }
private void comboBox_collections_addcontrib_SelectedIndexChanged(object sender, EventArgs e) { int cid = cached_user_collections[comboBox_collections_addcontrib.SelectedIndex].id; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); // Activity activity = (from c in db.Collections where c.id == cid select c).Single <Collection>().Activity; user_collection_activity = activity; comboBox_activities_addcontrib.Text = activity.name; }
public void ProcessInsertInteractions_Forced() { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var interactions = from i in db.Interaction_Logs where i.id > Configurations.last_interaction_id select i; if (interactions.Count() == 0) { return; } CombineAndSendInteractions(interactions.ToList <Interaction_Log>(), db); }
private void button_refresh_activities_Click(object sender, EventArgs e) { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var activities = from u in db.Activities select u; cached_activities.Clear(); cached_activities.AddRange(activities); comboBox_activities.Items.Clear(); for (int counter = 1; counter < cached_activities.Count + 1; counter++) { comboBox_activities.Items.Add(counter.ToString() + " - " + cached_activities[counter - 1].name); } }
private void button_refresh_ideas_Click(object sender, EventArgs e) { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var ideas = from u in db.Design_Ideas select u; cached_ideas.Clear(); cached_ideas.AddRange(ideas); comboBox_ideas.Items.Clear(); for (int counter = 1; counter < cached_ideas.Count + 1; counter++) { comboBox_ideas.Items.Add(counter.ToString() + " - " + cached_ideas[counter - 1].note); } }
public int ProcessInsertInteractions() { int result = 0; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var interactions = from i in db.Interaction_Logs where i.id > Configurations.last_interaction_id select i; if (interactions.Count() > Configurations.max_interaction_size) { int r = CombineAndSendInteractions(interactions.ToList <Interaction_Log>(), db); result = result + r; } return(result); }
public void GetChanges() { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var actions = from a in db.Actions where a.id > Configurations.last_change_tabletop select a; if (actions.Count() > 0) { this.changes = actions.ToList <sync.classes.Action>(); } else { this.changes = new List <sync.classes.Action>(); } }
private User find_user_of_contribution(Contribution c) { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var users = from mappings in db.Collection_Contribution_Mappings where mappings.contribution_id == c.id select mappings.Collection.User; if (users == null) { return(null); } if (users.Count() == 0) { return(null); } return(users.First <User>()); }
//public bool ProcessInteractionLogChanges2() //{ // TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); // DirectoryInfo info = new DirectoryInfo(Configurations.GetAbsoluteInteractionLogFilePath()); // FileInfo[] files = info.GetFiles(); // long max_times = 0; current_interactions.Clear(); // for (int counter = 0; counter < files.Count(); counter++) // { // long time = 0; // try { time = Convert.ToInt64(files[counter].Name.Substring(2)); } // catch (Exception) { continue; } // if (time > Configurations.last_change_interaction_files) // { // try // { // Stream writer = File.OpenWrite(files[counter].FullName); // writer.Close(); // these two lines are to check if it is being used by another process or not // StreamReader reader = new StreamReader(files[counter].FullName); // string[] contents = reader.ReadToEnd().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); // reader.Close(); // int n = contents.Count(); // List<Interaction_Log> new_logs = new List<Interaction_Log>(); // for (int counter_c = 0; counter_c < n; counter_c++) // { // string[] ilog = contents[counter_c].Split(new char[] { '\t' }); // if (ilog.Count() < 6) // { // continue; // } // Interaction_Log log = new Interaction_Log(); // log.date = Convert.ToDateTime(ilog[0]); // log.details = ilog[5]; // log.touch_id = Convert.ToInt32(ilog[2]); // log.touch_x = Convert.ToDouble(ilog[3]); // log.touch_y = Convert.ToDouble(ilog[4]); // int log_type_id = (from t in db.Interaction_Types // where t.type == ilog[1] // select t.id).Single<int>(); // log.type = log_type_id; // //new_logs.Add(log); // db.Interaction_Logs.InsertOnSubmit(log); // if (counter_c == Configurations.max_submit_changes) // db.SubmitChanges(); // } // //db.Interaction_Logs.InsertAllOnSubmit(new_logs); // db.SubmitChanges(); // } // catch (Exception e) { Log.WriteErrorLog(e); continue; } // if (max_times < time) max_times = time; // current_interactions.Add(files[counter].Name); // } // } // if (max_times != 0) Configurations.last_change_interaction_files = max_times; // return true; //} public int ProcessInteractionLogChanges3() { int result = 0; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); DirectoryInfo info = new DirectoryInfo(Configurations.GetAbsoluteInteractionLogFilePath()); FileInfo[] files = info.GetFiles(); DateTime max_times = Configurations.last_change_interaction_files; for (int counter = 0; counter < files.Count(); counter++) { DateTime time; try { time = DateTime.ParseExact(files[counter].Name, "yyMMdd", System.Globalization.CultureInfo.InvariantCulture); } catch (Exception) { continue; } if (time >= Configurations.last_change_interaction_files) { if (time > max_times) { max_times = time; } StreamReader reader = new StreamReader(files[counter].FullName); while (!reader.EndOfStream) { string ilog = reader.ReadLine(); byte[] mem_data = Convert.FromBase64String(ilog); MemoryStream str_mem = new MemoryStream(mem_data); BinaryFormatter bformatter = new BinaryFormatter(); Interaction_Log_Serializable i = (Interaction_Log_Serializable)bformatter.Deserialize(str_mem); if (i.id > Configurations.last_change_interaction_files_id) { Interaction_Log i2 = new Interaction_Log(); i2.date = i.date; i2.details = i.details; i2.technical_info = i.technical_info; i2.touch_id = i.touch_id; i2.touch_x = i.touch_x; i2.touch_y = i.touch_y; i2.type = i.type; db.Interaction_Logs.InsertOnSubmit(i2); try { db.SubmitChanges(); } catch (Exception ex) { Log.WriteErrorLog(ex); continue; } Configurations.last_change_interaction_files_id = i.id; result++; } } reader.Close(); } } Configurations.last_change_interaction_files = max_times; return(result); }
private void button_remove_activity_Click(object sender, EventArgs e) { if (cached_activities.Count == 0) { MessageBox.Show("Please refresh the activities list."); return; } if (comboBox_activities.SelectedIndex < 0) { MessageBox.Show("Please select an activity."); return; } int id = cached_activities[comboBox_activities.SelectedIndex].id; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var activities = from u in db.Activities where u.id == id select u; if (activities.Count() != 1) { MessageBox.Show("Error. Activity not found."); return; } Activity the_activity = activities.Single <Activity>(); var collections = from c in db.Collections where c.activity_id == the_activity.id select c; if (collections.Count() > 0) { MessageBox.Show("This activity cannot be deleted, because of the contributions in the activity."); return; } db.Activities.DeleteOnSubmit(the_activity); sync.classes.Action action = new classes.Action(); action.type_id = 3; action.user_id = 0; action.date = DateTime.UtcNow; action.object_id = the_activity.id; action.object_type = "nature_net.Activity"; action.technical_info = the_activity.technical_info; db.Actions.InsertOnSubmit(action); try { db.SubmitChanges(); } catch (Exception ex) { MessageBox.Show("Error (" + ex.Message + "):\r\n" + ex.StackTrace); return; } MessageBox.Show("Removed from the local database. It will be synced once (Local -> Server) is enabled."); }
private void ProcessInsertUser(int user_id) { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var users = from u in db.Users where u.id == user_id select u; if (users.Count() == 1) { User us = users.Single <User>(); server_api.CreateAccount(us.name, us.name, us.password, us.email, us.technical_info, us.affiliation.ToString(), us.avatar); if (RESTService.Last_Exception != null) { this.errors.Add(RESTService.Last_Exception); } } }
private Activity find_activity_of_contribution(Contribution c) { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var activities = from mappings in db.Collection_Contribution_Mappings where mappings.contribution_id == c.id select mappings.Collection.Activity; if (activities == null) { return(null); } if (activities.Count() == 0) { return(null); } return(activities.First <Activity>()); }
private void ProcessUpdateDesignIdea(int id) { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var designideas = from d in db.Design_Ideas where d.id == id select d; if (designideas.Count() == 1) { Design_Idea di = designideas.Single <Design_Idea>(); SNote note = server_api.UpdateNote(di.technical_info, di.status.ToString()); if (RESTService.Last_Exception != null) { this.errors.Add(RESTService.Last_Exception); } } }
private void button_add_contrib_Click(object sender, EventArgs e) { try { int uid = cached_users[comboBox_users_addcontrib.SelectedIndex].id; int cid = cached_user_collections[comboBox_collections_addcontrib.SelectedIndex].id; int lid = cached_locations[comboBox_locations_addcontrib.SelectedIndex].id; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); Contribution c = new Contribution(); c.date = DateTime.Now; c.modified_date = DateTime.Now; c.location_id = lid; c.media_url = textBox_mediaurl_addcontrib.Text; c.note = textBox_desc_addcontrib.Text; c.tags = textBox_tags_addcontrib.Text; c.status = ""; db.Contributions.InsertOnSubmit(c); db.SubmitChanges(); if (checkBox_new_collection.Checked) { Collection c2 = new Collection(); c2.activity_id = cached_activities_addcontrib[comboBox_activities_addcontrib.SelectedIndex].id; c2.date = DateTime.Now; c2.user_id = uid; c2.name = Configurations.GetDate_Formatted(c2.date); db.Collections.InsertOnSubmit(c2); db.SubmitChanges(); cid = c2.id; } Collection_Contribution_Mapping ccm = new Collection_Contribution_Mapping(); ccm.collection_id = cid; ccm.contribution_id = c.id; ccm.date = DateTime.Now; db.Collection_Contribution_Mappings.InsertOnSubmit(ccm); db.SubmitChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n--\r\n" + ex.StackTrace); } MessageBox.Show("Contribution was added successfully."); }
private void button_update_activity_Click(object sender, EventArgs e) { if (cached_activities.Count == 0) { MessageBox.Show("Please refresh the activities list."); return; } if (comboBox_activities.SelectedIndex < 0) { MessageBox.Show("Please select an activity."); return; } if (textBox_activity_name.Text == "" || textBox_activity_description.Text == "") { MessageBox.Show("Please fill the name and description textboxes."); return; } int id = cached_activities[comboBox_activities.SelectedIndex].id; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var activities = from u in db.Activities where u.id == id select u; if (activities.Count() != 1) { MessageBox.Show("Error. Activity not found."); return; } Activity the_activity = activities.Single <Activity>(); the_activity.name = textBox_activity_name.Text; the_activity.description = textBox_activity_description.Text; the_activity.avatar = textBox_activity_icon_url.Text; sync.classes.Action action = new classes.Action(); action.type_id = 2; action.user_id = 0; action.date = DateTime.UtcNow; action.object_id = the_activity.id; action.object_type = "nature_net.Activity"; action.technical_info = "Updating name/desc."; db.Actions.InsertOnSubmit(action); try { db.SubmitChanges(); } catch (Exception ex) { MessageBox.Show("Error (" + ex.Message + "):\r\n" + ex.StackTrace); return; } MessageBox.Show("Updated in the local database. It will be synced once (Local -> Server) is enabled."); }
private void ProcessUpdateActivity(int id) { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var activities = from d in db.Activities where d.id == id select d; if (activities.Count() == 1) { Activity di = activities.Single <Activity>(); string context_id = di.technical_info.Split(new char[] { ';' })[0]; SContext context = server_api.UpdateContext(context_id, di.name, di.description, di.avatar); if (RESTService.Last_Exception != null) { this.errors.Add(RESTService.Last_Exception); } } }
public void ProcessInsertActivity(int id) { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var activities = from d in db.Activities where d.id == id select d; if (activities.Count() == 1) { Activity di = activities.Single <Activity>(); SContext context = server_api.AddContext(di.name, di.description, di.avatar); di.technical_info = context.id.ToString() + ";" + context.extras; if (RESTService.Last_Exception != null) { this.errors.Add(RESTService.Last_Exception); } } }
private void comboBox_users_addcontrib_SelectedIndexChanged(object sender, EventArgs e) { int uid = cached_users[comboBox_users_addcontrib.SelectedIndex].id; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); // var collections = from c in db.Collections where c.user_id == uid select c; cached_user_collections.Clear(); cached_user_collections.AddRange(collections); comboBox_collections_addcontrib.Items.Clear(); for (int counter = 0; counter < cached_user_collections.Count; counter++) { comboBox_collections_addcontrib.Items.Add(cached_user_collections[counter].name); } comboBox_collections_addcontrib.SelectedIndex = 0; // }
private void button_remove_idea_Click(object sender, EventArgs e) { if (cached_ideas.Count == 0) { MessageBox.Show("Please refresh the ideas list."); return; } if (comboBox_ideas.SelectedIndex < 0) { MessageBox.Show("Please select an idea."); return; } int id = cached_ideas[comboBox_ideas.SelectedIndex].id; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var ideas = from u in db.Contributions where u.id == id select u; if (ideas.Count() != 1) { MessageBox.Show("Error. Design idea not found."); return; } Contribution the_idea = ideas.Single <Contribution>(); var mappings = from m in db.Collection_Contribution_Mappings where m.contribution_id == the_idea.id select m; db.Collection_Contribution_Mappings.DeleteAllOnSubmit(mappings); db.Contributions.DeleteOnSubmit(the_idea); sync.classes.Action action = new classes.Action(); action.type_id = 3; action.user_id = 0; action.date = DateTime.UtcNow; action.object_id = the_idea.id; action.object_type = "nature_net.Contribution"; action.technical_info = the_idea.technical_info; db.Actions.InsertOnSubmit(action); try { db.SubmitChanges(); } catch (Exception ex) { MessageBox.Show("Error (" + ex.Message + "):\r\n" + ex.StackTrace); return; } }
private void button_refresh_users_addcontrib_Click(object sender, EventArgs e) { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var users = from u in db.Users where u.id > 0 select u; cached_users.Clear(); cached_users.AddRange(users); comboBox_users_addcontrib.Items.Clear(); for (int counter = 1; counter < cached_users.Count + 1; counter++) { comboBox_users_addcontrib.Items.Add(counter.ToString() + " - " + cached_users[counter - 1].name); } // var locations = from l in db.Locations select l; cached_locations.Clear(); cached_locations.AddRange(locations); comboBox_locations_addcontrib.Items.Clear(); for (int counter = 0; counter < cached_locations.Count; counter++) { comboBox_locations_addcontrib.Items.Add(cached_locations[counter].name); } comboBox_locations_addcontrib.SelectedIndex = 0; // var activities = from a in db.Activities select a; cached_activities_addcontrib.Clear(); cached_activities_addcontrib.AddRange(activities); comboBox_activities_addcontrib.Items.Clear(); for (int counter = 0; counter < cached_activities_addcontrib.Count; counter++) { comboBox_activities_addcontrib.Items.Add(cached_activities_addcontrib[counter].name); } }
private void button_update_affiliation_Click(object sender, EventArgs e) { if (cached_users.Count == 0) { MessageBox.Show("Please refresh the users list."); return; } if (comboBox_users.SelectedIndex < 0) { MessageBox.Show("Please select a user."); return; } int id = cached_users[comboBox_users.SelectedIndex].id; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var users = from u in db.Users where u.id == id select u; if (users.Count() != 1) { MessageBox.Show("Error. User not found."); return; } User the_user = users.Single <User>(); the_user.affiliation = textBox_affiliation.Text; sync.classes.Action action = new classes.Action(); action.type_id = 2; action.user_id = 0; action.date = DateTime.UtcNow; action.object_id = the_user.id; action.object_type = "nature_net.User"; action.technical_info = "Updating affiliation."; db.Actions.InsertOnSubmit(action); try { db.SubmitChanges(); } catch (Exception ex) { MessageBox.Show("Error (" + ex.Message + "):\r\n" + ex.StackTrace); return; } }
public bool ProcessInteractionLogChanges() { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); List <SInteractionLog> logs; logs = server_api.GetInteractionLogs(Configurations.last_change_server_interactions.Year.ToString(), Configurations.last_change_server_interactions.Month.ToString(), Configurations.last_change_server_interactions.Day.ToString(), Configurations.last_change_server_interactions.Hour.ToString(), Configurations.last_change_server_interactions.Minute.ToString()); if (logs == null) { return(false); } if (logs.Count == 0) { return(false); } for (int counter = 0; counter < logs.Count; counter++) { var existings = from i in db.Interaction_Logs where i.technical_info == logs[counter].id.ToString() select i; if (existings.Count() > 0) { continue; } Interaction_Log log = new Interaction_Log(); log.date = Convert.ToDateTime(logs[counter].date); log.details = logs[counter].details; log.touch_id = logs[counter].touch_id; log.touch_x = logs[counter].touch_x; log.touch_y = logs[counter].touch_y; log.type = logs[counter].type; log.technical_info = logs[counter].id.ToString(); db.Interaction_Logs.InsertOnSubmit(log); db.SubmitChanges(); } Configurations.last_change_server_interactions = DateTime.UtcNow; return(true); }
private void button_add_activity_Click(object sender, EventArgs e) { if (textBox_activity_name.Text == "" || textBox_activity_description.Text == "") { MessageBox.Show("Please fill the name and description textboxes."); return; } Activity a = new Activity(); a.name = textBox_activity_name.Text; a.description = textBox_activity_description.Text; a.avatar = textBox_activity_icon_url.Text; a.avatar = ""; a.creation_date = DateTime.UtcNow; a.location_id = 0; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); db.Activities.InsertOnSubmit(a); sync.classes.Action action = new classes.Action(); action.type_id = 1; action.user_id = 0; action.date = DateTime.UtcNow; action.object_type = "nature_net.Activity"; action.technical_info = "Adding an activity."; try { db.SubmitChanges(); } catch (Exception ex) { MessageBox.Show("Error (" + ex.Message + "):\r\n" + ex.StackTrace); return; } action.object_id = a.id; db.Actions.InsertOnSubmit(action); db.SubmitChanges(); try { db.SubmitChanges(); } catch (Exception ex) { MessageBox.Show("Error (" + ex.Message + "):\r\n" + ex.StackTrace); return; } MessageBox.Show("Added to the local database. It will be synced once (Local -> Server) is enabled."); }
public bool ProcessWebUserChanges() { DateTime save_time = DateTime.UtcNow; List <SWebAccount> accounts = null; accounts = server_api.GetWebAccountsCreatedSince(Configurations.last_change_server_webusers.Year.ToString(), Configurations.last_change_server_webusers.Month.ToString(), Configurations.last_change_server_webusers.Day.ToString(), Configurations.last_change_server_webusers.Hour.ToString(), Configurations.last_change_server_webusers.Minute.ToString()); if (accounts == null) { return(false); } else { this.current_webaccounts = accounts; } TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); for (int counter = 0; counter < accounts.Count; counter++) { if (accounts[counter].username == "default") { continue; } var ru = from u in db.WebUsers where u.username == accounts[counter].username select u; if (ru.Count() == 0) { WebUser u_new = new WebUser(); u_new.username = accounts[counter].username; u_new.password = accounts[counter].password; u_new.email = accounts[counter].email; u_new.fullname = accounts[counter].name; u_new.technical_info = accounts[counter].consent; u_new.affiliation = accounts[counter].affiliation; if (accounts[counter].icon_url.StartsWith("nn_")) { u_new.avatar = accounts[counter].icon_url; } else { u_new.avatar = Configurations.GetRandomAvatar(); } try { SAccount related_account = server_api.GetAccount(accounts[counter].account_id); string uname = "default"; if (related_account != null) { uname = related_account.username; } if (uname == "default") { u_new.user_id = 0; } else { var u = from u0 in db.Users where u0.name == uname select u0; User related_user = u.Single <User>(); u_new.user_id = related_user.id; } } catch (Exception ex) { Log.WriteErrorLog(ex); } db.WebUsers.InsertOnSubmit(u_new); } else { if (ru.Count() == 1) { WebUser u = ru.Single <WebUser>(); u.password = accounts[counter].password; u.affiliation = accounts[counter].affiliation; if (accounts[counter].icon_url.StartsWith("nn_")) { u.avatar = accounts[counter].icon_url; } u.email = accounts[counter].email; u.technical_info = accounts[counter].consent; try { SAccount related_account = server_api.GetAccount(accounts[counter].account_id); string uname = related_account.username; if (uname == "default") { u.user_id = 0; } else { var u1 = from u0 in db.Users where u0.name == uname select u0; User related_user = u1.Single <User>(); u.user_id = related_user.id; } } catch (Exception ex) { Log.WriteErrorLog(ex); } db.SubmitChanges(); } } } if (accounts.Count == 0) { return(true); } if (SubmitChangesToLocalDB(db)) { Configurations.last_change_server_webusers = save_time; Configurations.SaveSettings(); return(true); } return(false); }
public bool UpdateLocalActivitites() { try { List <SContext> contexts = server_api.GetContextsForSite(Configurations.GetSiteNameForServer()); TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); if (contexts != null) { if (contexts.Count > 0) { var activities = from ac in db.Activities select ac; for (int counter = 0; counter < contexts.Count; counter++) { if (contexts[counter].kind != "Landmark" && contexts[counter].title != "Pilot") { Activity a = null; foreach (Activity activity in activities) { if (activity.technical_info.Split(new char[] { ';' })[0] == contexts[counter].id.ToString()) { a = activity; break; } } if (a == null) { //create the activity a = new Activity(); a.name = contexts[counter].title; a.location_id = 0; a.description = contexts[counter].description; a.creation_date = DateTime.UtcNow; Extras ex = JsonConvert.DeserializeObject <Extras>(contexts[counter].extras); a.technical_info = contexts[counter].id.ToString() + ";" + contexts[counter].kind + ";" + contexts[counter].extras; if (ex != null) { a.avatar = ex.icon; if (!ex.active) { a.expire_date = DateTime.Now; } } db.Activities.InsertOnSubmit(a); } else { //modify it a.name = contexts[counter].title; a.description = contexts[counter].description; Extras ex = JsonConvert.DeserializeObject <Extras>(contexts[counter].extras); a.technical_info = contexts[counter].id.ToString() + ";" + contexts[counter].kind + ";" + contexts[counter].extras; a.avatar = ex.icon; if (!ex.active) { a.expire_date = DateTime.Now; } else { a.expire_date = null; } } if (!SubmitChangesToLocalDB(db)) { return(false); } } } } } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); } return(true); }
public bool ProcessFeedbackChanges() { DateTime save_time = DateTime.UtcNow; List <SFeedback> feedbacks = server_api.GetFeedbacksCreatedSince(Configurations.last_change_server_feedbacks.Year.ToString(), Configurations.last_change_server_feedbacks.Month.ToString(), Configurations.last_change_server_feedbacks.Day.ToString(), Configurations.last_change_server_feedbacks.Hour.ToString(), Configurations.last_change_server_feedbacks.Minute.ToString(), true); if (feedbacks == null) { return(false); } else { current_feedbacks = feedbacks; } TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); bool all_fine = true; for (int counter = 0; counter < feedbacks.Count; counter++) { if (feedbacks[counter].kind == "Landmark") { continue; } var fdbacks = from f in db.Feedbacks where f.technical_info == feedbacks[counter].id.ToString() select f; if (fdbacks.Count() > 1) { continue; } if (fdbacks.Count() == 1) { // update feedback Feedback f = fdbacks.Single <Feedback>(); f.note = feedbacks[counter].content; f.web_username = feedbacks[counter].webusername; //f.date = (Configurations.unix_epoch.AddMilliseconds(feedbacks[counter].modified_at)).ToLocalTime(); var usrs = from u in db.Users where u.name == feedbacks[counter].account.username select u; if (usrs.Count() > 0) { f.user_id = usrs.First <User>().id; } else { f.user_id = 0; } db.SubmitChanges(); continue; } Feedback fb = new Feedback(); fb.date = (Configurations.unix_epoch.AddMilliseconds(feedbacks[counter].created_at)).ToLocalTime(); fb.web_username = feedbacks[counter].webusername; if (feedbacks[counter].kind.ToLower() == "comment") { fb.type_id = 1; // comment } if (feedbacks[counter].kind.ToLower() == "like") { fb.type_id = 2; // like } if (feedbacks[counter].kind.ToLower() == "rating") { fb.type_id = 3; // rating } if (feedbacks[counter].kind.ToLower() == "like") { fb.note = "true"; } else { fb.note = feedbacks[counter].content; } // find local user id var users = from u in db.Users where u.name == feedbacks[counter].account.username select u; if (users.Count() > 0) { fb.user_id = users.First <User>().id; } else { fb.user_id = 0; } fb.parent_id = 0; if (feedbacks[counter].parent_id != 0) { // find local parent id var parents = from p in db.Feedbacks where p.technical_info == feedbacks[counter].parent_id.ToString() select p; if (parents.Count() == 1) { fb.parent_id = parents.Single <Feedback>().id; } } if (feedbacks[counter].target.model.ToLower() == "note") { // search for the object_id in local database var cntribs = from c in db.Contributions where c.technical_info == feedbacks[counter].target.id.ToString() select c; if (cntribs.Count() > 0) { fb.object_id = cntribs.First <Contribution>().id; fb.object_type = "nature_net.Contribution"; } } if (feedbacks[counter].target.model.ToLower() == "account") { string username = find_account(feedbacks[counter].target.id); // search for the object_id in local database var cntribs = from c in db.Users where c.name == username select c; if (cntribs.Count() > 0) { fb.object_id = cntribs.First <User>().id; fb.object_type = "nature_net.User"; } } if (feedbacks[counter].target.model.ToLower() == "context") { string context_name = find_context(feedbacks[counter].target.id); // search for the object_id in local database if (context_name.StartsWith(Configurations.GetSiteNameForServer() + "_landmark")) { //search for locations var cntribs = from c in db.Locations where c.id == Convert.ToInt32(context_name.Substring(Configurations.GetSiteNameForServer().Length + "_landmark".Length)) select c; if (cntribs.Count() > 0) { fb.object_id = cntribs.First <Location>().id; fb.object_type = "nature_net.Location"; } } else { //search for activities var cntribs = from c in db.Activities where c.name == context_name select c; if (cntribs.Count() > 0) { fb.object_id = cntribs.First <Activity>().id; fb.object_type = "nature_net.Activity"; } } } fb.technical_info = feedbacks[counter].id.ToString(); //fb.technical_info = ""; db.Feedbacks.InsertOnSubmit(fb); if (!SubmitChangesToLocalDB(db)) { all_fine = false; return(false); } } if (feedbacks.Count == 0) { return(true); } if (all_fine) { Configurations.last_change_server_feedbacks = save_time; Configurations.SaveSettings(); return(true); } return(false); }
public bool ProcessUserChanges() { DateTime save_time = DateTime.UtcNow; List <SAccount> accounts = null; accounts = server_api.GetAccountsCreatedSince(Configurations.last_change_server_users.Year.ToString(), Configurations.last_change_server_users.Month.ToString(), Configurations.last_change_server_users.Day.ToString(), Configurations.last_change_server_users.Hour.ToString(), Configurations.last_change_server_users.Minute.ToString(), true); if (accounts == null) { return(false); } else { this.current_accounts = accounts; } TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); for (int counter = 0; counter < accounts.Count; counter++) { if (accounts[counter].username == "default") { continue; } var ru = from u in db.Users where u.name == accounts[counter].username select u; if (ru.Count() == 0) { User u_new = new User(); u_new.name = accounts[counter].username; u_new.password = accounts[counter].password; u_new.email = accounts[counter].email; u_new.technical_info = accounts[counter].consent; u_new.affiliation = accounts[counter].affiliation; if (accounts[counter].icon_url.StartsWith("nn_")) { u_new.avatar = accounts[counter].icon_url; } else { u_new.avatar = Configurations.GetRandomAvatar(); } db.Users.InsertOnSubmit(u_new); } else { if (ru.Count() == 1) { User u = ru.Single <User>(); u.password = accounts[counter].password; u.affiliation = accounts[counter].affiliation; if (accounts[counter].icon_url.StartsWith("nn_")) { u.avatar = accounts[counter].icon_url; } u.email = accounts[counter].email; u.technical_info = accounts[counter].consent; db.SubmitChanges(); } } } if (accounts.Count == 0) { return(true); } if (SubmitChangesToLocalDB(db)) { Configurations.last_change_server_users = save_time; Configurations.SaveSettings(); return(true); } return(false); }
public bool ProcessContributionChanges() { DateTime save_time = DateTime.UtcNow; List <SNote> notes; notes = server_api.GetNotesCreatedSince(Configurations.last_change_server_contributions.Year.ToString(), Configurations.last_change_server_contributions.Month.ToString(), Configurations.last_change_server_contributions.Day.ToString(), Configurations.last_change_server_contributions.Hour.ToString(), Configurations.last_change_server_contributions.Minute.ToString(), true); if (notes == null) { return(false); } else { this.current_notes = notes; } bool submit_changes = true; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); for (int counter = 0; counter < notes.Count; counter++) { var cntribs = from c in db.Contributions where c.technical_info == notes[counter].id.ToString() select c; if (cntribs.Count() > 0) { // existing contribution -- update it Contribution c1 = cntribs.First <Contribution>(); c1.modified_date = (Configurations.unix_epoch.AddMilliseconds(notes[counter].modified_at)).ToLocalTime(); c1.location_id = find_location_id(notes[counter]); c1.note = notes[counter].content; c1.status = notes[counter].status; c1.web_username = notes[counter].webusername; if (notes[counter].kind == "DesignIdea") { c1.tags = "Design Idea"; } else { if (notes[counter].medias != null && notes[counter].medias.Count > 0) { c1.tags = notes[counter].medias[0].kind; c1.media_url = notes[counter].medias[0].link; } } int a_id = 0; // free observation if (notes[counter].context.name.Substring(Configurations.GetSiteNameForServer().Count() + 1) == "design_idea") { a_id = 1; // design idea } var activities0 = from a in db.Activities where a.name == notes[counter].context.title select a; if (activities0.Count() > 0) { a_id = activities0.First <Activity>().id; } c1.technical_info = notes[counter].id.ToString(); db.SubmitChanges(); //if (a_id != 1) update_or_create_collection(db, c1.id, a_id, c1.date); // updating the user string new_username = notes[counter].account.username; int new_user_id = 0; var usrs = from u in db.Users where u.name == new_username select u; if (usrs.Count() == 1) { new_user_id = usrs.Single <User>().id; } this.update_user_for_contribution(db, c1.id, new_user_id, c1.date); continue; } string username = notes[counter].account.username; DateTime note_date = (Configurations.unix_epoch.AddMilliseconds(notes[counter].created_at)).ToLocalTime(); DateTime note_modified_date = note_date; if (notes[counter].modified_at != null && notes[counter].modified_at > 0) { note_modified_date = (Configurations.unix_epoch.AddMilliseconds(notes[counter].modified_at)).ToLocalTime(); } //DateTime note_date= DateTime.FromOADate(notes[counter].created_at //int activity_id = notes[counter].context.?? int activity_id = 0; // free observation //int activity_id = 1; // design idea //int activity_id = other; // other activities if (notes[counter].context.name.Substring(Configurations.GetSiteNameForServer().Count() + 1) == "design_idea") { activity_id = 1; } var activities = from a in db.Activities where a.name == notes[counter].context.title select a; if (activities.Count() > 0) { activity_id = activities.First <Activity>().id; } int col_id = this.get_or_create_collection(db, username, activity_id, note_date); if (col_id == -1) { continue; } foreach (SMedia media in notes[counter].medias) { bool could_create = create_contribution(notes[counter], media, note_date, note_modified_date, notes[counter].content, notes[counter].id.ToString(), (notes[counter].kind == "DesignIdea"), col_id, notes[counter].status, db); if (!could_create) { submit_changes = false; } } if (notes[counter].medias == null) { notes[counter].medias = new List <SMedia>(); } if (notes[counter].medias.Count == 0) { bool could_create = create_contribution(notes[counter], null, note_date, note_modified_date, notes[counter].content, notes[counter].id.ToString(), (notes[counter].kind == "DesignIdea"), col_id, notes[counter].status, db); if (!could_create) { submit_changes = false; } } } if (notes.Count == 0) { return(true); } if (submit_changes) { Configurations.last_change_server_contributions = save_time; Configurations.SaveSettings(); return(true); } return(false); }
private void ProcessInsertFeedback(int feedback_id) { TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); var feedbacks = from f in db.Feedbacks where f.id == feedback_id select f; if (feedbacks.Count() == 1) { Feedback fb = feedbacks.Single <Feedback>(); SFeedback result = null; if (fb.object_type == "nature_net.Contribution") // feedback on contribution (note) { var contributions = from c in db.Contributions where c.id == fb.object_id select c; if (contributions.Count() == 1) { Contribution cn = contributions.Single <Contribution>(); if (fb.parent_id == 0) { if (fb.type_id == 1) // comment on contribution { result = server_api.CreateFeedback("comment", "note", cn.technical_info, fb.User.name, fb.note, "0"); } if (fb.type_id == 2) // like on contribution (currently design idea) { result = server_api.CreateFeedback("like", "note", cn.technical_info, "default", fb.note, "0"); } } else { var fbs = from f in db.Feedbacks where f.id == fb.parent_id select f; if (fbs.Count() == 1) { Feedback parent = fbs.Single <Feedback>(); if (fb.type_id == 1) { result = server_api.CreateFeedback("comment", "note", cn.technical_info, fb.User.name, fb.note, parent.technical_info); } } } } } if (fb.object_type == "nature_net.User") // feedback on user (account) { var users = from u in db.Users where u.id == fb.object_id select u; if (users.Count() == 1) { User us = users.Single <User>(); int account_id = find_account(us.name); if (account_id != -1) { if (fb.parent_id == 0) { if (fb.type_id == 1) // comment on user { result = server_api.CreateFeedback("comment", "account", account_id.ToString(), fb.User.name, fb.note, "0"); } } else { var fbs = from f in db.Feedbacks where f.id == fb.parent_id select f; if (fbs.Count() == 1) { Feedback parent = fbs.Single <Feedback>(); if (fb.type_id == 1) { result = server_api.CreateFeedback("comment", "account", account_id.ToString(), fb.User.name, fb.note, parent.technical_info); } } } } } } if (fb.object_type == "nature_net.Activity") // feedback on activity (context) { var activities = from a in db.Activities where a.id == fb.object_id select a; if (activities.Count() == 1) { Activity ac = activities.Single <Activity>(); int context_id = find_activity(ac.name); if (context_id != -1) { if (fb.parent_id == 0) { if (fb.type_id == 1) // comment on activity { result = server_api.CreateFeedback("comment", "context", context_id.ToString(), fb.User.name, fb.note, "0"); } } else { var fbs = from f in db.Feedbacks where f.id == fb.parent_id select f; if (fbs.Count() == 1) { Feedback parent = fbs.Single <Feedback>(); if (fb.type_id == 1) { result = server_api.CreateFeedback("comment", "context", context_id.ToString(), fb.User.name, fb.note, parent.technical_info); } } } } } } if (fb.object_type == "nature_net.Location") // feedback on location (context) { var locations = from l in db.Locations where l.id == fb.object_id select l; if (locations.Count() == 1) { Location lc = locations.Single <Location>(); int context_id = find_location(lc.id.ToString()); if (context_id != -1) { if (fb.parent_id == 0) { if (fb.type_id == 1) //comment on location { result = server_api.CreateFeedback("comment", "context", context_id.ToString(), fb.User.name, fb.note, "0"); } } else { var fbs = from f in db.Feedbacks where f.id == fb.parent_id select f; if (fbs.Count() == 1) { Feedback parent = fbs.Single <Feedback>(); if (fb.type_id == 1) { result = server_api.CreateFeedback("comment", "context", context_id.ToString(), fb.User.name, fb.note, parent.technical_info); } } } } } } if (result != null) { fb.technical_info = result.id.ToString(); db.SubmitChanges(); } if (RESTService.Last_Exception != null) { this.errors.Add(RESTService.Last_Exception); } } }