/// <summary> /// Gets the Gender, if no gender could be determined "keine Angabe" will be returned /// </summary> /// <param name="salutation">The entered saluatation</param> /// <returns>The gender as <see cref="string"/></returns> private string GetGender(string salutation) { // Initializations TitleDTO titles = JsonSerializer.Deserialize <TitleDTO>(JsonContent); string gender; int position; // Return correct gender for men foreach (string x in titles.SalutationsMale) { if (x == salutation) { position = titles.SalutationsMale.IndexOf(x); gender = titles.GenderMale[position]; return(gender); } } // Return correct gender for women foreach (string x in titles.SalutationsFemale) { if (x == salutation) { position = titles.SalutationsFemale.IndexOf(x); gender = titles.GenderFemale[position]; return(gender); } } gender = "keine Angabe"; return(gender); }
public ActionResult AddTitle() { TitleDTO Td = new TitleDTO(); return(PartialView("_AddTitle", Td)); }
public ActionResult GetTitle(int id) { try { TitleDTO Title = new TitleDTO(); using (MABRUKLISTEntities dbcontext = new MABRUKLISTEntities()) { var title = dbcontext.mblist_title.Find(id); if (title != null) { Title.id = title.title_key; Title.Title = title.title_name; return(PartialView("_AddTitle", Title)); } else { return(Json(new { key = false, value = "Title not Found its Deleted from data base!!" }, JsonRequestBehavior.AllowGet)); } } } catch (Exception) { throw; } }
public void WriteNewTitleFromFile() { // Declarations string expectedContent, actualContent; // Get ressources of the assembly Assembly assembly = Assembly.GetAssembly(typeof(NameParser)); Stream resourceStream = assembly.GetManifestResourceStream("ContactParser.App.Data.Data.json"); // Get file path of the titles.json file and delete if it exists Uri location = new Uri(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)); string filePath = Path.Combine(location.AbsolutePath, "titles.json"); using (var StreamReader = new StreamReader(resourceStream)) { expectedContent = StreamReader.ReadToEnd(); } // Build expected object for comparison TitleDTO expectedTitles = JsonSerializer.Deserialize <TitleDTO>(expectedContent); expectedTitles.Title.Add("Sc."); expectedContent = JsonSerializer.Serialize(expectedTitles); using (var titleManager = new TitleManager()) { titleManager.WriteNewTitle("Sc."); } // Read the content from the file actualContent = File.ReadAllText(filePath); Assert.AreEqual(expectedContent, actualContent); }
/// <summary> /// Extract the noble names /// </summary> /// <param name="adresselement">The parsed elements from the name</param> /// <returns>The lastName as <see cref="string"/></returns> private string GetNobleName(List <string> adresselement) { // Initializations string lastName = string.Empty; TitleDTO nameElements = new TitleDTO(); TitleDTO titles = JsonSerializer.Deserialize <TitleDTO>(JsonContent); int pos = -1; // Check for noble names foreach (string x in titles.NobleIndicator) { foreach (string y in adresselement) { if (x == y) { pos = adresselement.IndexOf(x); break; } } } // If no noble name take last elements surname if (pos > -1) { for (int i = pos; i < adresselement.Count; i++) { lastName = lastName + adresselement[i] + " "; if (adresselement[i].EndsWith(",")) { break; } } for (int i = pos; i < adresselement.Count; i++) { if (adresselement[i].EndsWith(",")) { adresselement.RemoveAt(i); break; } else { adresselement.RemoveAt(i); i -= 1; } } lastName = lastName.Remove(lastName.Length - 1, 1); nameElements.Elements = adresselement; return(lastName); } else { lastName = adresselement[adresselement.Count - 1]; adresselement.RemoveAt(adresselement.Count - 1); nameElements.Elements = adresselement; } return(lastName); }
public ActionResult AddOrUpdateTitle(TitleDTO dto) { try { if (ModelState.IsValid) { using (MABRUKLISTEntities dbcontext = new MABRUKLISTEntities()) { if (dto.id == 0) { var data = dbcontext.mblist_title.Where(x => x.title_name == dto.Title).FirstOrDefault(); if (data != null) { return(Json(new { key = false, value = "Title already exist" }, JsonRequestBehavior.AllowGet)); } else { mblist_title title = new mblist_title() { title_name = dto.Title }; dbcontext.mblist_title.Add(title); dbcontext.SaveChanges(); return(Json(new { key = true, value = "Title added successfully" }, JsonRequestBehavior.AllowGet)); } } else { var data = dbcontext.mblist_title.Find(dto.id); if (data != null) { data.title_name = dto.Title; dbcontext.SaveChanges(); return(Json(new { key = true, value = "Title Updated Successfully" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { key = false, value = "Title is not found" }, JsonRequestBehavior.AllowGet)); } } }; } else { return(Json(new { key = false, value = "Please enter correct data" }, JsonRequestBehavior.AllowGet)); } } catch (Exception) { return(Json(new { key = false, value = "Unable to save the Title" }, JsonRequestBehavior.AllowGet));; } }
/// <summary> /// Extract the title /// </summary> /// <param name="adresselement">The parsed elements from the name</param> /// <returns>The title as <see cref="string"/></returns> private string GetTitle(List <string> adresselement) { // Initializations string title = string.Empty; TitleDTO titles = JsonSerializer.Deserialize <TitleDTO>(JsonContent); TitleDTO nameElements = new TitleDTO(); int pos = -1; // Search for recognized title in the titles list until no titles are found anymore foreach (string x in adresselement) { foreach (string y in titles.Title) { if (x == y) { pos = adresselement.IndexOf(x); title = title + adresselement[pos] + " "; } } } // Delete title from address title for (int i = pos; i >= 0; i--) { adresselement.RemoveAt(i); } nameElements.Elements = adresselement; if (title == string.Empty) { title = "keine Angabe"; return(title); } // Delete last whitespace title = title.Remove(title.Length - 1, 1); return(title); }
public void WriteNewTitleFromAssembly() { // Declarations string expectedContent, actualContent; // Get file path of the titles.json file and delete if it exists Uri location = new Uri(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)); string filePath = Path.Combine(location.AbsolutePath, "titles.json"); // Delete the file if its existing if (File.Exists(filePath)) { File.Delete(filePath); } // Get ressources of the assembly Assembly assembly = Assembly.GetAssembly(typeof(NameParser)); Stream resourceStream = assembly.GetManifestResourceStream("ContactParser.App.Data.Data.json"); using (var StreamReader = new StreamReader(resourceStream)) { expectedContent = StreamReader.ReadToEnd(); } // Prepare expected object TitleDTO titles = JsonSerializer.Deserialize <TitleDTO>(expectedContent); titles.Title.Add("Eng."); expectedContent = JsonSerializer.Serialize(titles); // Get the actual content after adding "nat." using (var titleManager = new TitleManager()) { titleManager.WriteNewTitle("Eng."); actualContent = File.ReadAllText(filePath); } Assert.AreEqual(expectedContent, actualContent); }
/// <summary> /// Extracts the salutation /// </summary> /// <param name="adresselement">The parsed elements from the name</param> /// <returns>The salutation as <see cref="string"/></returns> private string GetSalutation(List <string> adresselement) { string salutation; TitleDTO titles = JsonSerializer.Deserialize <TitleDTO>(JsonContent); int position = 0; foreach (string x in titles.SalutationIndicator) { if (x == adresselement[0]) { position = titles.SalutationIndicator.IndexOf(x); adresselement.RemoveAt(0); break; } } // Add salutation according to the position salutation = titles.SalutationIndicator[position]; TitleDTO nameElements = new TitleDTO(); nameElements.Elements = adresselement; return(salutation); }
/// <summary> /// Parse the input contact string /// </summary> /// <param name="input"></param> /// <returns>An instance of type <see cref="Name"/> containing all the information about the contact</returns> /// <exception cref="FormatException">Thrown, when no firstname was entered</exception> /// <exception cref="ArgumentException">Thrown, when the input contains invalid characters</exception> public Name ParseName(string input) { try { // Validate input input = ValidateAndPrepareInput(input); } catch (FormatException e) { throw e; } catch (ArgumentException e) { throw e; } // Splitting the input string to List based on empty characters List <string> nameElements = input.Split(' ').ToList(); TitleDTO titles = new TitleDTO(); titles.Elements = nameElements; // Determines the gender from the salutaion string gender = GetGender(nameElements[0]); // Extract the Salutation string salutation = GetSalutation(nameElements); // Extract the Lastname string lastName = GetNobleName(titles.Elements); // Extract the Title string title = GetTitle(titles.Elements); string firstName; try { // Extract the Firstname firstName = GetFirstName(titles.Elements); } catch (FormatException e) { throw e; } // Extract the Middlename string middleName = GetMiddleName(titles.Elements); string firstNameTemp, lastNameTemp, salutationTemp, titleTemp; // If last char of lastName = "," then store lastname in firstname, and firstname in lastName and remove "," List <string> names = ChangeFirstAndLastName(firstName, lastName); firstNameTemp = names[0]; lastNameTemp = names[1]; // If salutation = "keine Angabe" replace with "" for the Greeting if (salutation.Equals("keine Angabe")) { salutationTemp = ""; } else { salutationTemp = salutation; } // If title = "keine Angabe" replace with "" for the Greeting if (title == "keine Angabe") { titleTemp = ""; } else { titleTemp = title; } if (middleName == "keine Angabe") { middleName = ""; } // Build the full Greeting string greeting = GetGreeting(lastNameTemp, firstNameTemp, salutationTemp, titleTemp); // If greeting contains no titel and salutation denn Replace greeting = greeting.Replace(" ", " "); // If greeting contains no titel or salutation denn Replace greeting = greeting.Replace(" ", " "); // Fill the Name object to be returned Name nameData = new Name { Gender = gender, LastName = lastNameTemp, FirstName = firstNameTemp + " " + middleName, MiddleName = middleName, Salutation = salutation, Title = title, Greeting = greeting, }; return(nameData); }
/// <summary> /// Writes a new title to the json file on the desktop /// </summary> /// <param name="title">The title to write</param> /// <exception cref="IOException">Thrown, when reading or writing to the file failed</exception> /// <exception cref="ArgumentException">Thrown, when an argument was wrong</exception> /// <exception cref="JsonException">Thrown, when the JSON could not be serialized</exception> /// <exception cref="Exception">Thrown, when another fatal error occured</exception> public void WriteNewTitle(string title) { // Declarations string filePath, content; // Split input and get file var titleElements = title.Split(" "); try { // Get or create the file and return the path filePath = GetOrCreateFile(); // Read all from file content = File.ReadAllText(filePath); } catch (Exception) { throw new IOException("Error while reading from titles.json"); } // Check the content whether its empty or filled if (string.IsNullOrEmpty(content)) { try { // Read from the assembly string ressourceContent = GetTextFromAssembly(); TitleDTO titles = JsonSerializer.Deserialize <TitleDTO>(ressourceContent); foreach (var item in titleElements) { if (!titles.Title.Contains(item)) { titles.Title.Add(item); } } File.WriteAllText(filePath, JsonSerializer.Serialize(titles)); } catch (ArgumentException) { throw new ArgumentException("An argument was wrong"); } catch (JsonException) { throw new JsonException("JSON could not be serialized"); } catch (Exception) { throw new Exception("An fatal error occured"); } } else { try { // Add new title when there is no duplicate TitleDTO titles = JsonSerializer.Deserialize <TitleDTO>(content); foreach (var item in titleElements) { if (!titles.Title.Contains(item)) { titles.Title.Add(item); } } File.WriteAllText(filePath, JsonSerializer.Serialize(titles)); } catch (ArgumentException) { throw new ArgumentException("An argument was wrong"); } catch (JsonException) { throw new JsonException("JSON could not be serialized"); } catch (Exception) { throw new Exception("An fatal error occured"); } } }