public ConfigurationReader(String fileName) { List<List<String>> datas = new List<List<string>>(); using (FileStream stream = new FileStream(fileName, FileMode.Open)) { using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.GetEncoding(1252))) { String content = reader.ReadToEnd(); String[] items = content.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (String item in items) { string data = item; if (item.StartsWith("\"")) {data = data.Substring(1); } if (item.EndsWith("\"")) { data = data.Substring(0,data.Length -1); } datas.Add(data.Split(";".ToCharArray(), StringSplitOptions.None).ToList ()); } } } DataContextPrf dataContextPrf = ServiceLocator.Current.GetInstance<DataContextPrf>(); DbSet<PrfBmUser> setUsers = dataContextPrf.Set<PrfBmUser>(); foreach (PrfBmUser user in setUsers) { setUsers.Remove(user); } dataContextPrf.SaveChanges(); List<List<String>> userItems = (from d in datas where d[0].Equals("USER") select d).ToList(); foreach (List<String> userItem in userItems) { PrfBmUser user = new PrfBmUser(); user.Login = userItem[1]; user.Nom = userItem[2]; user.Prenom = userItem[3]; user.FamDecInf = userItem[4]; user.CdDecInf = userItem[5]; if (userItems.Count > 5) {user.Passwords = userItem[6];} setUsers.Add(user); } dataContextPrf.SaveChanges(); List<PrfBmUser> users = setUsers.AsQueryable<PrfBmUser>().ToList(); DbSet<PrfBmSchema> setSchemas = dataContextPrf.Set<PrfBmSchema>(); foreach (PrfBmSchema schema in setSchemas) { setSchemas.Remove(schema); } dataContextPrf.SaveChanges(); List<List<String> >schemaItems = (from d in datas where d[0].Equals("SCHEMA") select d).ToList(); foreach (List<String> schemaItem in schemaItems) { PrfBmSchema schema = new PrfBmSchema(); schema.Schema = schemaItem[1]; schema.Libelle = schemaItem[2]; setSchemas.Add(schema); } dataContextPrf.SaveChanges(); List<PrfBmSchema> schemas = setSchemas.AsQueryable<PrfBmSchema>().ToList (); DbSet<PrfBmTable> setTables = dataContextPrf.Set<PrfBmTable>(); foreach (PrfBmTable table in setTables) { setTables.Remove(table); } dataContextPrf.SaveChanges(); List<List<String>> tableItems = (from d in datas where d[0].Equals("TABLE") select d).ToList(); foreach (List<String> tableItem in tableItems) { PrfBmTable table = new PrfBmTable(); foreach (PrfBmSchema s in schemas) { if (s.Schema.Equals(tableItem[1])) {table.PrfBmSchema = s;} } table.Nom = tableItem[2]; table.Libelle = tableItem[3]; setTables.Add(table); } dataContextPrf.SaveChanges(); List<PrfBmTable> tables = setTables.AsQueryable<PrfBmTable>().ToList (); DbSet<PrfBmProfil> setProfils = dataContextPrf.Set<PrfBmProfil>(); foreach (PrfBmProfil profil in setProfils) { setProfils.Remove(profil); } dataContextPrf.SaveChanges(); List<List<String>> profilItems = (from d in datas where d[0].Equals("PROFIL") select d).ToList(); foreach (List<String> profilItem in profilItems) { PrfBmProfil profil = new PrfBmProfil(); foreach (PrfBmSchema s in schemas) { if (s.Schema.Equals(profilItem[1])) { profil.PrfBmSchema = s; } } profil.Profil = profilItem[2]; profil.Libelle = profilItem[3]; setProfils.Add(profil); } dataContextPrf.SaveChanges(); List<PrfBmProfil> profils = setProfils.AsQueryable<PrfBmProfil>().ToList (); //PROFIL_TABLE DbSet<PrfBmProfilTable> setProfilTables = dataContextPrf.Set<PrfBmProfilTable>(); foreach (PrfBmProfilTable profilTable in setProfilTables) { setProfilTables.Remove(profilTable); } dataContextPrf.SaveChanges(); List<List<String>> profilTableItems = (from d in datas where d[0].Equals("PROFIL_TABLE") select d).ToList(); foreach (List<String> profilTableItem in profilTableItems) { PrfBmProfilTable profilTable = new PrfBmProfilTable(); foreach (PrfBmProfil p in profils) { if (p.Profil.Equals(profilTableItem[1])) { profilTable.PrfBmProfil = p; } } foreach (PrfBmTable t in tables) { if (t.Nom.Equals(profilTableItem[2])) { profilTable.PrfBmTable = t; } } profilTable.Ecrire = profilTableItem[3].Replace("-", "").Equals("1"); profilTable.Importer = profilTableItem[1].Replace("-", "").Equals("1"); profilTable.Afficher = true; setProfilTables.Add(profilTable); } dataContextPrf.SaveChanges(); DbSet<PrfBmUserProfil> setUserProfil = dataContextPrf.Set<PrfBmUserProfil>(); foreach (PrfBmUserProfil userProfil in setUserProfil) { setUserProfil.Remove(userProfil); } dataContextPrf.SaveChanges(); List<List<String>> userProfilItems = (from d in datas where d[0].Equals("USER_PROFIL") select d).ToList(); foreach (List<String> userProfilItem in userProfilItems) { PrfBmUserProfil userProfil = new PrfBmUserProfil(); foreach (PrfBmProfil p in profils) { if (p.Profil.Equals(userProfilItem[2])) { userProfil.PrfBmProfil = p; } } foreach (PrfBmUser u in users) { if (u.Login.Equals(userProfilItem[1])) { userProfil.PrfBmUser = u; } } setUserProfil.Add(userProfil); } dataContextPrf.SaveChanges(); }
private void AddUser() { UserView view = new UserView(); PrfBmUser user = new PrfBmUser(); DbSet<PrfBmSchema> setSchema = this.DataContextPrf.Set<PrfBmSchema>(); DbSet<PrfBmProfil> setProfils = this.DataContextPrf.Set<PrfBmProfil>(); DbSet<PrfBmUserProfil> setUserProfils = this.DataContextPrf.Set<PrfBmUserProfil>(); UserViewModel vm = new UserViewModel(user); view.DataContext = vm; view.Title = "Ajout d'un utilisateur"; view.Owner = Application.Current.GetActiveWindow(); Nullable<Boolean> result = view.ShowDialog(); if (result.HasValue && result.Value == true) { vm.Write(); if (vm.FamDec != null && vm.CdDec != null) { vm.Model.FamDecInf = vm.FamDec.FamDec; vm.Model.CdDecInf = vm.CdDec.CdDec; } else { vm.Model.FamDecInf = null; vm.Model.CdDecInf = null; } this.DataContextPrf.Set<PrfBmUser>().Add(vm.Model); this.DataContextPrf.SaveChanges(); this._users.Add(vm); foreach (UserProfilViewModel profilVm in vm.Profils) { PrfBmUserProfil userProfil = new PrfBmUserProfil(); userProfil.BmUserLogin = vm.Model.Login; switch (profilVm.PrivilegeLevel) { case Layers.Shared.Enums.PrivilegeLevel.Administrateur : userProfil.BmProfilProfil = "ADMIN_"+profilVm.Schema.Schema ; break; case Layers.Shared.Enums.PrivilegeLevel.Consultant: userProfil.BmProfilProfil ="CONSULT_"+ profilVm.Schema.Schema ; break; case Layers.Shared.Enums.PrivilegeLevel.Valideur: userProfil.BmProfilProfil = "VALID_"+profilVm.Schema.Schema; break; } userProfil.PrfBmUserIdPk = vm.Model.IdPk; userProfil.PrfBmProfilIdPk = (from p in setProfils where p.Profil.Equals (userProfil.BmProfilProfil) select p.IdPk).FirstOrDefault(); setUserProfils.Add(userProfil); } this.DataContextPrf.SaveChanges(); vm.RaiseChanges(); } }