/// <summary> /// Deletes exacly one Symbol by id in Symbols table, /// deletes all the entries of the symbol in CompositesOf table /// </summary> /// <param name="id"></param> /// <returns>true false depending on success</returns> public bool DeleteExact(int id) { using (var db = new BlissBaseContext(testing)) { Symbols sym = db.Symbols.Find(id); try { // Deletes the relations where the symbol is a component of // a composite symbol var compOf = new CompositeOfDAL(); compOf.DeleteBySymbolID(id); // Sets the symbol as a standard type (Deletes it from // SymbolTypes table) var type = new SymbolTypeDAL(); type.SetStandardForSymID(id); // Deletes the symbol from Symbols table db.Symbols.Remove(sym); db.SaveChanges(); } catch (Exception e) { Debug.WriteLine("Exception when remowing symbol: " + id); Debug.WriteLine(e.StackTrace); return false; } } return true; }
/// <summary> /// Deletes all of the synonyms for a user by id /// </summary> /// <returns>true or false depending on success</returns> public bool DeleteAllSynonymsForUser(int userId) { List<UserSynonym> uSynModels = GetSynonymForUser(userId); using (var db = new BlissBaseContext(testing)) { foreach (UserSynonym model in uSynModels) { db.UserSynonyms.Remove(new UserSynonyms { USynID = model.uSynId, USynApproved = model.uSynApproved, USynSynonym = model.uSynSynonym, USynWord = model.uSynWord }); } try { db.SaveChanges(); return true; } catch (Exception e) { Debug.WriteLine("Exception when deleting all synonyms for user: " + userId); Debug.WriteLine(e.StackTrace); return false; } } }
/// <summary> /// Deletes all the symbols in the RawSymbolsImport table /// </summary> /// <returns>true or false depending on success</returns> public bool DeleteAll() { using (var db = new BlissBaseContext(testing)) { try { db.Database.ExecuteSqlCommand("TRUNCATE TABLE [RawSymbolsImport]"); db.SaveChanges(); } catch (Exception e) { Debug.WriteLine("Exception trying to TRUNCATE table RawSymbolsImport"); Debug.WriteLine(e.StackTrace); return false; } } return true; }
/// <summary> /// Deletes row in UserSynonyms where UsynSynonym == id /// For the synonym found, the CompositeSymbol with CompId == id /// will be deleted /// </summary> /// <param name="id"></param> /// <returns>true or false</returns> public bool DeleteSynonym(int id) { using (var db = new BlissBaseContext(testing)) { try { UserSynonyms toRemove = db.UserSynonyms.Find(id); db.UserSynonyms.Remove(toRemove); db.SaveChanges(); return true; } catch (Exception e) { Debug.WriteLine("Exception when deleting user synonym: " + id); Debug.WriteLine(e.StackTrace); return false; } } }
/// <summary> /// Deletes all rows in CompositesOf table by CompID /// </summary> /// <param name="composit"></param> /// <returns>true or false depending on success</returns> public bool DeleteByCompositeSymbol(CompositeSymbol composit) { using (var db = new BlissBaseContext(testing)) { var composedOf = db.CompositesOf.Where(c => c.CompID == composit.compId); foreach (CompositesOf comp in composedOf) { db.CompositesOf.Remove(comp); } try { db.SaveChanges(); } catch (Exception e) { Debug.WriteLine("Exception occured when deleting composed of composite relation"); Debug.WriteLine(e.StackTrace); return false; } return true; } }
/// <summary> /// Removes a users admin privileges /// </summary> /// <param name="id"></param> /// <returns>true or false depending on success, /// if user is currently not an admin true is returned</returns> public bool DisableUserAdmin(int id) { Admins toRemove = GetExcactByUserId(id); if (toRemove != null) { try { using (BlissBaseContext db = new BlissBaseContext(testing)) { db.Admins.Remove(toRemove); db.SaveChanges(); } } catch (Exception e) { Debug.WriteLine("A exception was thrown when remowing admin permissions for user: " + id); Debug.WriteLine(e.StackTrace); return false; } } return true; }
/// <summary> /// Takes a users id and bool status as parameters. Updates the given user's /// UserApproved status and sets it to status /// </summary> /// <param name="id"></param> /// <param name="status"></param> /// <returns>true or false depending on success</returns> public bool UpdateUserApproved(int id, bool status) { try { using (var db = new BlissBaseContext(testing)) { Users toApprove = db.Users.Find(id); toApprove.UserApproved = status; db.SaveChanges(); return true; } } catch (Exception e) { Debug.WriteLine("An exception was thrown when changing userId: " + id + " to approved status = " + status); Debug.WriteLine(e.StackTrace); return false; } }
/// <summary> /// Creates and inserts a synonym to the CompositeSymblos, /// and defines it as a UserSynonym /// </summary> /// <param name="word"></param> /// <param name="name"></param> /// <param name="components"></param> /// <returns>true or false depending on success</returns> public bool InsertSynonym(CompositeSymbol word, string name, List<Symbol> components) { var composite = new CompositeSymbolDAL(); CompositeSymbol synonym = composite.CreateCompositeByComponents(name, components); int synonymId = composite.Insert(synonym, components); using (var db = new BlissBaseContext(testing)) { try { db.UserSynonyms.Add(new UserSynonyms { USynWord = word.compId, USynSynonym = synonymId }); db.SaveChanges(); return true; } catch (Exception e) { Debug.WriteLine("Exception when trying to link new user synonym: " + synonymId + " with word: " + word.compId); Debug.WriteLine(e.StackTrace); return false; } } }
/// <summary> /// Updates a CompID's language code. Does not set language for a CompID /// only updates. /// </summary> /// <param name="compId"></param> /// <param name="code"></param> /// <returns>true or false depending on success</returns> public bool UpdateLanguageForCompID(int compId, LanguageCodes code) { var toUpdate = GetExactRowByCompID(compId); try { using (var db = new BlissBaseContext(testing)) { toUpdate.LangCode = code; db.SaveChanges(); return true; } } catch (Exception e) { Debug.WriteLine("A Exception was thrown when trying to update language code for " + "CompId: " + compId + ". Might be that composite sybol is defined as international"); Debug.WriteLine(e.StackTrace); return false; } }
/// <summary> /// Delete user from UsersSynonymsList table by users Id /// </summary> /// <param name="userId"></param> /// <returns>true or false depending on success</returns> public bool DeleteUser(int userId) { using (var db = new BlissBaseContext(testing)) { try { UserSynonymList toRemove = FindUser(userId); if (toRemove != null) { db.UsersSynonymsList.Remove(new UsersSynonymsList { UserID = toRemove.uListUserId }); } db.SaveChanges(); return true; } catch (Exception e) { Debug.WriteLine("Exception removing user: "******" synonym list"); Debug.WriteLine(e.StackTrace); return false; } } }
/// <summary> /// Inserts a new user and returns a UserSynonymsList model /// for the new user /// </summary> /// <param name="id"></param> /// <returns>BlissBase.Model.UserSynonymList or null</returns> public UserSynonymList InsertUser(int userId) { using (var db = new BlissBaseContext(testing)) { try { UsersSynonymsList toInsert = new UsersSynonymsList { UserID = userId }; db.UsersSynonymsList.Add(toInsert); db.SaveChanges(); return new UserSynonymList { uListUserId = toInsert.UserID }; } catch (Exception e) { Debug.WriteLine("Exception Adding user id: " + userId + " to UserSynonymsList"); Debug.WriteLine(e.StackTrace); return null; } } }
/// <summary> /// Sets a non standard type: "TypeCodes code" for a SymID symId /// </summary> /// <param name="symId"></param> /// <param name="code"></param> /// <returns>true or false depending on success</returns> public bool SetLanguageForSymID(int symId, TypeCodes code) { var toAdd = new SymbolTypes { SymID = symId, TypeIndicator = code }; try { using (var db = new BlissBaseContext(testing)) { db.SymbolTypes.Add(toAdd); db.SaveChanges(); return true; } } catch (Exception e) { Debug.WriteLine("An exception was thrown when trying to set TypeCode: " + code + " for symId: " + symId); Debug.WriteLine(e.StackTrace); return false; } }
/// <summary> /// When a sybol is moved to the Symbols or CompositeSymbols /// table, the raw version should be deleted. This method /// deletes RawSymbol by id /// </summary> /// <param name="id"></param> /// <returns>true or false depending on success</returns> public bool DeleteExact(int id) { using (var db = new BlissBaseContext(testing)) { try { RawSymbolsImport symbolToDelete = db.RawSymbolsImport.First(e => e.RawID == id); if (symbolToDelete != null) { db.RawSymbolsImport.Remove(symbolToDelete); db.SaveChanges(); return true; } } catch (Exception e) { Debug.WriteLine("Could not find and delete RawSymbol with id: " + id); Debug.WriteLine(e.StackTrace); } } return false; }
/// <summary> /// Accepts a path variable and import all jpeg files in the directory. /// rawName == the filename of the image rawJpeg == imagedata as byte[] /// Successfully imported images is moved to Imported folder, with /// Day, Month, Year appended to the path. /// </summary> /// <param name="path"></param> /// <returns>true or false depending on success</returns> public bool ImportFromPath(String path) { using (var db = new BlissBaseContext(testing)) { try { string[] directory = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + path); foreach (string fullPath in directory) { string fileName = Path.GetFileNameWithoutExtension(fullPath); byte[] fullSizeRawImage = File.ReadAllBytes(fullPath); byte[] imageData = ResizeImageAsBytes(fullSizeRawImage, dbImageHeight); if (imageData != null) { db.RawSymbolsImport.Add(new RawSymbolsImport { RawJPEG = imageData, RawName = fileName }); db.SaveChanges(); } // Move file to imported + /yyyy/mm/dd/ and create directory if not exists string targetPath = AppDomain.CurrentDomain.BaseDirectory + path + "/Imported/" + DateTime.Now.ToString("yyyy\\/MM\\/dd"); String targetFile = Path.Combine(targetPath, Path.GetFileNameWithoutExtension(fullPath)); if (!Directory.Exists(targetPath)) Directory.CreateDirectory(targetPath); File.Copy(fullPath, targetFile, true); File.Delete(fullPath); } } catch (DirectoryNotFoundException dnfe) { Debug.WriteLine("Directory not found when importing imagefiles to db"); Debug.WriteLine(dnfe.ToString()); return false; } catch (Exception e) { Debug.WriteLine("There was an exception when trying to import imagefiles to db"); Debug.WriteLine(e.ToString()); return false; } } return true; }
/// <summary> /// Deletes all rows in CompositesOf table by component id /// </summary> /// <param name="symId"></param> /// <returns>true or false, depending on success</returns> internal bool DeleteBySymbolID(int symId) { using (var db = new BlissBaseContext(testing)) { var components = db.CompositesOf.Where(c => c.SymID == symId); foreach (CompositesOf comp in components) { db.CompositesOf.Remove(comp); } try { db.SaveChanges(); } catch (Exception e) { Debug.WriteLine("Exception occured when deleting componet: " + symId + " from CompositesOf table"); Debug.WriteLine(e.StackTrace); return false; } return true; } }
/// <summary> /// Method accepts a Model of CompositeSymbols table, and a /// list of Symbol Models. The ComposedOf table is updated /// to keep track of the relation between the composit symbol /// and it's base symbols /// </summary> /// <param name="compositeModel"></param> /// <param name="composedOf"></param> /// <returns>true or false depending on success</returns> public bool SetCompositeOfSymbol(CompositeSymbol compositeModel, List<Symbol> composedOf) { // Reference used: http://stackoverflow.com/a/3309230 List<Symbol> sortedList = composedOf.OrderBy(s => s.symId).ToList(); using (var db = new BlissBaseContext(testing)) { foreach (Symbol sym in composedOf) try { db.CompositesOf.Add(new CompositesOf() { CompID = compositeModel.compId, SymID = sym.symId }); db.SaveChanges(); } catch (Exception e) { Debug.WriteLine("Error setting CompositedOf: ", e.ToString()); Debug.WriteLine(e.StackTrace); return false; } return true; } }
/// <summary> /// Updates score for given id and returns new score. /// Sets difference as score if no score is found for the id, /// and returns difference /// </summary> /// <param name="id"></param> /// <param name="difference"></param> /// <returns>Score after update as int</returns> public int UpdateScoreFor(int id, int difference) { using (var db = new BlissBaseContext(testing)) { try { CompositeScores toEdit = db.CompositeScores.Find(id); if (toEdit == null) { toEdit = new CompositeScores { CompID = id, CompScore = 0 }; toEdit.CompScore += difference; db.CompositeScores.Add(toEdit); } else { toEdit.CompScore += difference; } db.SaveChanges(); return toEdit.CompScore; } catch (Exception e) { Debug.WriteLine("Ecxeption when updating score for id: " + id); Debug.WriteLine(e.StackTrace); return 0; } } }
/// <summary> /// Deletes a SymID from SymbolTypes table and effectively /// sets it as standard /// </summary> /// <param name="symId"></param> /// <returns>true or false depending on success</returns> public bool SetStandardForSymID(int symId) { var toRemove = GetExactRowBySymID(symId); try { using (var db = new BlissBaseContext(testing)) { db.SymbolTypes.Attach(toRemove); db.Entry(toRemove).State = EntityState.Deleted; db.SaveChanges(); return true; } } catch (Exception e) { Debug.WriteLine("Exception thrown when trying to set symId: " + symId + " as standard SymbolType"); Debug.WriteLine(e.StackTrace); return false; } }
/// <summary> /// Updates a SymID's TypeIndicator code. Does not set type for a SymID /// only updates. /// </summary> /// <param name="symId"></param> /// <param name="code"></param> /// <returns>true or false depending on success</returns> public bool UpdateTypeForSymID(int symId, TypeCodes code) { var toUpdate = GetExactRowBySymID(symId); try { using (var db = new BlissBaseContext(testing)) { toUpdate.TypeIndicator = code; db.Entry(toUpdate).State = EntityState.Modified; db.SaveChanges(); return true; } } catch (Exception e) { Debug.WriteLine("An exception was thrown when trying to update TypeIndicator code for " + "symId: " + symId); Debug.WriteLine(e.StackTrace); return false; } }
/// <summary> /// Takes a CompositeSymbol Model and a list of Symbol Models. /// Inserts composite symbol and links it to the list of Symbols /// using CompositesOf /// </summary> /// <param name="innCompSymbol"></param> /// <param name="components"></param> /// <returns>Id of new BlissBase.DAL.CompositeSymbols as int, or -1 if insert failed</returns> public int Insert(CompositeSymbol innCompSymbol, List<Symbol> components) { var newCompSymbol = new CompositeSymbols() { CompName = innCompSymbol.compName, CompJPEG = innCompSymbol.compJpeg }; using (var db = new BlissBaseContext(testing)) { try { db.CompositeSymbols.Add(newCompSymbol); db.SaveChanges(); CompositeOfDAL compOf = new CompositeOfDAL(); compOf.SetCompositeOfRow(newCompSymbol, components); } catch (Exception e) { Debug.WriteLine("Error Inserting Composite symbol: " + innCompSymbol.compId); Debug.WriteLine(e.StackTrace); return -1; } return newCompSymbol.CompID; } }
/// <summary> /// Sets a non international language for a CompID /// </summary> /// <param name="compId"></param> /// <param name="code"></param> /// <returns>true or false depending on success</returns> public bool SetLanguageForCompID(int compId, LanguageCodes code) { var toAdd = new Languages { CompID = compId, LangCode = code }; try { using (var db = new BlissBaseContext(testing)) { db.Languages.Add(toAdd); db.SaveChanges(); return true; } } catch (Exception e) { Debug.WriteLine("An exception was thrown when defining composite symbol: " + compId + " as non international, with LanguageCodes: " + code); Debug.WriteLine(e.StackTrace); return false; } }
/// <summary> /// Updates a composite symbol's jpeg data by id /// </summary> /// <param name="id"></param> /// <param name="jpeg"></param> /// <returns>true or false depending on success</returns> public bool UpdateCompositeJPEG(int id, byte[] jpeg) { if (jpeg == null) { return false; } else { using (var db = new BlissBaseContext(testing)) { try { CompositeSymbols toEdit = db.CompositeSymbols.Find(id); toEdit.CompJPEG = jpeg; db.SaveChanges(); return true; } catch (Exception e) { Debug.WriteLine("An exception was thrown when updating JPEG for CompID: " + id); Debug.WriteLine(e.StackTrace); return false; } } } }
/// <summary> /// Deletes a CompID from Languages table and effectively /// sets it as international /// </summary> /// <param name="compId"></param> /// <returns></returns> public bool SetInternationalForCompID(int compId) { var toRemove = GetExactRowByCompID(compId); try { using (var db = new BlissBaseContext(testing)) { db.Languages.Remove(toRemove); db.SaveChanges(); return true; } } catch (Exception e) { Debug.WriteLine("An exception was thrown when defining composite symbol: " + compId + " as international"); Debug.WriteLine(e.StackTrace); return false; } }
/// <summary> /// Updates a composite symbol's name by id. /// </summary> /// <param name="id"></param> /// <param name="name"></param> /// <returns>true, false depending on success</returns> public bool UpdateCompositeName(int id, String name) { if (String.IsNullOrEmpty(name)) { return false; } else { using (var db = new BlissBaseContext(testing)) { try { CompositeSymbols toEdit = db.CompositeSymbols.Find(id); toEdit.CompName = name; db.SaveChanges(); return true; } catch (Exception e) { Debug.WriteLine("An exception was thrown when updating name for CompID: " + id); Debug.WriteLine(e.StackTrace); return false; } } } }
/// <summary> /// Sets a user synonym to approved or not /// </summary> /// <param name="bit"></param> /// <returns>true or false depending on successfulness</returns> public bool SetUserSynonymApproved(int uSynId, bool bit) { using (var db = new BlissBaseContext(testing)) { UserSynonyms toEdit = db.UserSynonyms.Find(uSynId); if (toEdit != null) { try { toEdit.USynApproved = bit; db.SaveChanges(); return true; } catch (Exception e) { Debug.WriteLine("Exception when approving user synonym: " + uSynId); Debug.WriteLine(e.StackTrace); } } } return false; }
/// <summary> /// Deletes a composite symbol and it's rows in /// CompositeOf table. Takes a CompositeSymbols /// row as inn parameter /// </summary> /// <param name="compSymbol"></param> /// <returns>true or false depending on success</returns> internal bool DeleteCompositeSymbolByRow(CompositeSymbols compSymbol) { if (compSymbol == null) { return false; } else { using (var db = new BlissBaseContext(testing)) { try { db.CompositeSymbols.Attach(compSymbol); db.Entry(compSymbol).State = EntityState.Deleted; db.SaveChanges(); return true; } catch (Exception e) { Debug.WriteLine("Exception when removing symbol: " + compSymbol.CompID); Debug.WriteLine(e.StackTrace); return false; } } } }
/// <summary> /// Takes a BlissBase.Model.User and inserts it /// to the BlissBase.DAL.Users table. /// The user is not approved and is awaiting approval by admin /// </summary> /// <param name="user"></param> /// <returns>true if successfull, else throws Exception</returns> public bool InsertUser(User user) { using (var db = new BlissBaseContext(testing)) { string salt = generateSalt(); Users toAdd = new Users { UserID = user.userId, UserName = user.username, UserFirstName = user.userFirstName, UserLastName = user.userLastName, UserSalt = salt, UserPasswd = hashString(user.userPasswd + salt), UserApproved = false }; try { db.Users.Add(toAdd); db.SaveChanges(); return true; } catch { throw; } } }
/// <summary> /// Sets a user as a administrator by UserID /// </summary> /// <param name="id"></param> /// <returns>true or false depending on success, /// if user allready is an admin true is returned</returns> public bool SetUserAdmin(int id) { if (!CheckIfUserAdmin(id)) { Admins toAdd = new Admins { UserID = id }; try { using (BlissBaseContext db = new BlissBaseContext(testing)) { db.Admins.Add(toAdd); db.SaveChanges(); } } catch (Exception e) { Debug.WriteLine("A exception was thrown when setting user: "******" as admin"); Debug.WriteLine(e.StackTrace); return false; } } return true; }
/// <summary> /// Takes an BlissBase.Model.User and delete it if /// it excists /// </summary> /// <param name="user"></param> /// <returns>return true or false depending on success, /// or throws exception</returns> public bool DeleteUser(User user) { using (var db = new BlissBaseContext(testing)) { Users toRemove = db.Users.Find(user.userId); if (toRemove != null) try { db.Users.Remove(toRemove); db.SaveChanges(); return true; } catch { throw; } return false; } }
/// <summary> /// Updates a base symbol's name by id. /// </summary> /// <param name="id"></param> /// <param name="name"></param> /// <returns>true or false depending on success</returns> public bool UpdateName(int id, String name) { using (var db = new BlissBaseContext(testing)) { Symbols sym = db.Symbols.Find(id); sym.SymName = name; try { db.SaveChanges(); } catch (Exception e) { Debug.WriteLine("Exception when updating symbol name of symbol: " + id); Debug.WriteLine(e.StackTrace); return false; } } return true; }