public ActionResult DeleteConfirmed(string id) { GEDCOM_Data gedcom_data = db.GEDCOM_Data.Find(id); db.GEDCOM_Data.Remove(gedcom_data); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit(GEDCOM_Data gedcom_data) { if (ModelState.IsValid) { db.Entry(gedcom_data).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(gedcom_data)); }
// // GET: /GEDCOMData/Edit/5 public ActionResult Edit(string id = null) { GEDCOM_Data gedcom_data = db.GEDCOM_Data.Find(id); if (gedcom_data == null) { return(HttpNotFound()); } return(View(gedcom_data)); }
public ActionResult Create(GEDCOM_Data gedcom_data) { if (ModelState.IsValid) { db.GEDCOM_Data.Add(gedcom_data); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(gedcom_data)); }
// Read the .ged file and store its contents in the database public Boolean GED_to_DB(HttpPostedFileBase file) { // Initialise all flags, variables, counters and input streams int state = 1; int ID_counter = 0; Boolean zero_flag = true; Boolean space_flag = false; string ID_string = ""; string tag_string = ""; string data_string = ""; int file_len = file.ContentLength; byte[] ged_data = new byte[file_len]; file.InputStream.Read(ged_data, 0, file_len); var data_temp = System.Text.Encoding.ASCII.GetString(ged_data); // Gain access to our database and clear all of its contents before we begin GEDCOM_Data gedcom_DB = db.GEDCOM_Data.Create(); db.Database.ExecuteSqlCommand("DELETE FROM GEDCOM_Data"); foreach (char current_char in data_temp) { // Create the state machine switch (state) { // State 1: Obtain the GEDCOM entry's level case 1: if (!(current_char.Equals('\n') || current_char.Equals('\r'))) // Ignore any empty lines { gedcom_DB.Level = (int)current_char - (int)'0'; // Obtain the level if (gedcom_DB.Level > 10 && gedcom_DB.Level <= 0) // Perform some sanity checks on the level { return(false); } if (gedcom_DB.Level == 0) // We will handle level 0 data differently in succeeding states { zero_flag = true; ID_string = ""; } else { zero_flag = false; } space_flag = false; // We haven't had to handle any spaces yet, set the flag up for this state = 2; // Proceed to the next state } break; // State 2: Handle any unwanted white spaces between the level and the tag/ID case 2: if (!current_char.Equals(' ')) // Get rid of all the white space { if (!space_flag) { return(false); } if (!zero_flag) // If we aren't handeling a 0 level entry... { tag_string += current_char; // ... we build up the first piece of the tag } else // If we are handeling a 0 level entry... { ID_string += current_char; // ... we build up the first piece of the ID } state = 3; // Proceed to the next state } space_flag = true; break; // State 3: Build up the ID (for level 0 entries) or the tag (for other level entries) case 3: if (!current_char.Equals(' ') && !(current_char.Equals('\n') || current_char.Equals('\r'))) { if (!zero_flag) // If we aren't handeling a 0 level entry... { tag_string += current_char; // ... we build up the rest of the tag } else // If we are handeling a 0 level entry... { ID_string += current_char; // ... we build up the rest of the ID } } else // Once we've finished building up the tag/ID { if (zero_flag) // For level zero entries, format and save the ID data { ID_string = ID_string.Replace("@", ""); gedcom_DB.GEDCOM_ID = ID_string; gedcom_DB.Tag = ID_string; } else // For all other level entries, save the tag data { gedcom_DB.Tag = tag_string; tag_string = ""; } if (!current_char.Equals(' ')) { // If there is no GEDCOM DATA (text) in the entry, set everything up to save the data into the database gedcom_DB.GEDCOM_ID = ID_string; gedcom_DB.Data = ""; state = 1; tag_string = ""; ID_counter++; gedcom_DB.ID = ID_counter; db.GEDCOM_Data.Add(gedcom_DB); if (db.Tag_Conv.Find(gedcom_DB.Tag) == null) // Check if the tag of the entry that is going to be saved is valid { return(false); } try // Save the data we have just formulated into the database { db.SaveChanges(); } catch (Exception e) { return(false); } gedcom_DB = db.GEDCOM_Data.Create(); // Reset break; } state = 4; // Proceed to the next state } break; // State 4: Once again, handle unneccessary white space case 4: if (!current_char.Equals(' ')) { if (zero_flag) // For level zero entries... { tag_string += current_char; // ... we build up the first piece of the tag } else // For other level entries... { data_string += current_char; // ... we build up the first piece of the data } state = 5; // Proceed to the next state } break; // State 5: Finish building the tag/data (dependant on level) case 5: if (!(current_char.Equals('\n') || current_char.Equals('\r'))) // Check if we haven't reached the end of the line entry yet { if (zero_flag) { if (current_char.Equals(' ')) // In the rare case that an entry has a level, ID, tag AND data, we handle this correctly { state = 4; zero_flag = false; gedcom_DB.Tag = tag_string; } else // We build up the rest of the tag (level 0 entries) { tag_string += current_char; } } else // We build up the rest of the data (other level entries) { data_string += current_char; } } else // Once we've reached the end of the line entry... { if (zero_flag) // ... save the tag for level 0 entries { gedcom_DB.Tag = tag_string; gedcom_DB.Data = ""; } else // ... save the data for level 0 entries { gedcom_DB.Data = data_string; gedcom_DB.GEDCOM_ID = ID_string; } // Set everything up to save the entry into the database data_string = ""; tag_string = ""; state = 1; ID_counter++; gedcom_DB.ID = ID_counter; db.GEDCOM_Data.Add(gedcom_DB); if (db.Tag_Conv.Find(gedcom_DB.Tag) == null) // Check if the tag of the entry that is going to be saved is valid { return(false); } try // Save the data we have just formulated into the database { db.SaveChanges(); } catch (Exception e) { return(false); } gedcom_DB = db.GEDCOM_Data.Create(); // Reset } break; default: break; } } return(true); // We managed to read the entire file successfully! }