/// <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;
     }
 }
 public bool InsertSynonym(CompositeSymbol word, string name, List<Symbol> components)
 {
     var GlobalSynonymDAL = new GlobalSynonymDAL(testing);
     return GlobalSynonymDAL.InsertSynonym(word, name, components);
 }
 public List<GlobalSynonym> GetSynonymForWord(CompositeSymbol word)
 {
     var GlobalSynonymDAL = new GlobalSynonymDAL(testing);
     return GlobalSynonymDAL.GetSynonymForWord(word);
 }
        public ActionResult ConvertToComposite(Int32[] isPartOf, int rawId)
        {
            if (Session["logged_in"] == null)
                return RedirectToAction("Index", "Index");

            RawSymbolImportBLL raw = new RawSymbolImportBLL();
            SymbolBLL symbols = new SymbolBLL();
            CompositeSymbolBLL comp = new CompositeSymbolBLL();

            RawSymbolImport rawBecomingComp = raw.GetExact(rawId);
            List<Symbol> partOf = new List<Symbol>();

            foreach (Int32 part in isPartOf)
            {
                var temp = symbols.GetExactByID(part);
                if (temp != null)
                {
                    partOf.Add(temp);
                }
            }

            CompositeSymbol compOfRaw = new CompositeSymbol()
            {
                compId = rawBecomingComp.rawId,
                compName = rawBecomingComp.rawName,
                compJpeg = rawBecomingComp.rawJpeg
            };

            var ok = comp.Insert(compOfRaw, partOf);
            if (ok != -1)
            {
                TempData["msg"] = "Raw symbol '" + rawBecomingComp.rawName + "' is now a composite symbol";
                var successfullyDeleted = raw.DeleteExact(rawId);
                if(!successfullyDeleted)
                {
                    TempData["msg"] += ", but was not deleted from raw list.";
                }
            }
            return RedirectToAction("Index", "CompositeSymbol");
        }
        /// <summary>
        /// Deletes a composite symbol and it's rows in
        /// CompositeOf table. Takes a CompositeSymbol 
        /// model as inn parameter
        /// </summary>
        /// <param name="compModel"></param>
        /// <returns>true or false depending on success</returns>
        public bool DeleteCompositeSymbol(CompositeSymbol compModel)
        {
            if (compModel == null)
                return false;

            CompositeOfDAL compOf = new CompositeOfDAL();
            var ok = compOf.DeleteByCompositeSymbol(compModel);
            if (ok)
            {
                CompositeSymbols compSymbol = GetExactCompositeRowByID(compModel.compId);
                return DeleteCompositeSymbolByRow(compSymbol);
            }
            return false;
        }
        /// <summary>
        /// Takes a list of Symbol Models and a name.
        /// Creates a CompositeSymbol by merging components JPEG's
        /// and adding name. Returns null if merging jpegs failed, or
        /// if identical CompositeSymbol exists. The returned CompositeSymbol
        /// should be passed to CompositeSymbolDAL.Insert if the sybol is intended 
        /// to be saved in the database table
        /// </summary>
        /// <param name="CompositeName"></param>
        /// <param name="components"></param>
        /// <returns>BlissBase.Model.CompositeSymbol or null</returns>
        public CompositeSymbol CreateCompositeByComponents(String compositeName,
            List<Symbol> components)
        {
            List<Symbol> sortedList = components.OrderBy(s => s.symId).ToList();

            CompositeSymbol nameExists = GetExactCompositeSymbolByName(compositeName);
            if (nameExists != null)
            {
                CompositeOfDAL composites = new CompositeOfDAL();
                List<Symbol> hasComponents = composites.GetComponentsOf(nameExists);
                if (Enumerable.SequenceEqual(sortedList, hasComponents))
                    return null;
            }
            byte[] newCompJpeg = null;
            newCompJpeg = CombineJpegFromComponents(sortedList);

            CompositeSymbol newCompSymbol = null;
            if (newCompJpeg != null)
            {
                newCompSymbol = new CompositeSymbol()
                {
                    compName = compositeName,
                    compJpeg = newCompJpeg
                };
            }

            return newCompSymbol;
        }
        /// <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;
            }
        }
 public List<Symbol> GetComponentsOf(CompositeSymbol innCompSymbol)
 {
     var CompositeOfDAL = new CompositeOfDAL(testing);
     return CompositeOfDAL.GetComponentsOf(innCompSymbol);
 }
 public bool DeleteCompositeSymbol(CompositeSymbol compSymbol)
 {
     var CompositeSymbolDAL = new CompositeSymbolDAL(testing);
     return CompositeSymbolDAL.DeleteCompositeSymbol(compSymbol);
 }
        /// <summary>
        /// Returns all Global synonyms for a word
        /// </summary>
        /// <param name="word"></param>
        /// <returns>List of BlissBase.Model.GlobalSynonym or null</returns>
        public List<GlobalSynonym> GetSynonymForWord(CompositeSymbol word)
        {
            List<GlobalSynonym> allSynonymsForWord = new List<GlobalSynonym>();

            using (var db = new BlissBaseContext(testing))
            {
                allSynonymsForWord = db.GlobalSynonyms.Where(gS => gS.GSynWord == word.compId).Select(
                    gS => new GlobalSynonym
                    {
                        gSynId = gS.GSynID,
                        gSynWord = gS.GSynWord,
                        gSynSynonym = gS.GSynSynonym
                    }).ToList();
            }
            return allSynonymsForWord.Count == 0 ? null : allSynonymsForWord;
        }
 public List<UserSynonym> GetSynonymForWord(CompositeSymbol word)
 {
     var UserSynonymDAL = new UserSynonymDAL(testing);
     return UserSynonymDAL.GetSynonymForWord(word);
 }
        /// <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>
        /// Returns all base symbols the innCompSymbol is composed of
        /// </summary>
        /// <param name="innCompSymbol"></param>
        /// <returns>List of BlissBase.Model.Symbol or null</returns>
        public List<Symbol> GetComponentsOf(CompositeSymbol innCompSymbol)
        {
            using (var db = new BlissBaseContext(testing))
            {
                List<CompositesOf> rows = db.CompositesOf.Where(e => e.CompID == innCompSymbol.compId).ToList();
                List<Symbols> symbols = new List<Symbols>();
                foreach(CompositesOf co in rows)
                {
                    Symbols temp = db.Symbols.Where(s => s.SymID == co.SymID).First();
                    symbols.Add(temp);
                }
                //List<Symbols> symbolRows = db.CompositesOf.Where(e => e.CompID == innCompSymbol.compId).Select(e => e.Symbols).ToList();
                List<Symbol> components = new List<Symbol>();

                foreach (Symbols t in symbols)
                {
                    components.Add(
                    new Symbol()
                    {
                        symId = t.SymID,
                        symName = t.SymName,
                        symJpeg = t.SymJPEG
                    });
                }
                return components;

            }
        }
 public bool SetCompositeOfSymbol(CompositeSymbol compositeModel, List<Symbol> composedOf)
 {
     var CompositeOfDAL = new CompositeOfDAL(testing);
     return CompositeOfDAL.SetCompositeOfSymbol(compositeModel, composedOf);
 }
        /// <summary>
        /// Returns all UserSynonyms for a CompositeSymbol "word"
        /// </summary>
        /// <param name="word"></param>
        /// <returns>List of BlissBase.Model.UserSynonym or null</returns>
        public List<UserSynonym> GetSynonymForWord(CompositeSymbol word)
        {
            List<UserSynonym> allUserSynonymsForWord = new List<UserSynonym>();

            using (var db = new BlissBaseContext(testing))
            {
                allUserSynonymsForWord = db.UserSynonyms.Where(uS => uS.USynWord == word.compId).Select(
                    uS => new UserSynonym
                    {
                        uSynId = uS.USynID,
                        uSynWord = uS.USynWord,
                        uSynSynonym = uS.USynSynonym,
                        uListUserId = uS.UserID,
                        uSynApproved = uS.USynApproved
                    }).ToList();
            }
            return allUserSynonymsForWord.Count == 0 ? null : allUserSynonymsForWord;
        }
 public int Insert(CompositeSymbol innCompSymbol, List<Symbol> components)
 {
     var CompositeSymbolDAL = new CompositeSymbolDAL(testing);
     return CompositeSymbolDAL.Insert(innCompSymbol, components);
 }
        /// <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;
                }
            }
        }
 public bool DeleteByCompositeSymbol(CompositeSymbol composit)
 {
     var CompositeOfDAL = new CompositeOfDAL(testing);
     return CompositeOfDAL.DeleteByCompositeSymbol(composit);
 }