/// <summary> /// Saves Fmotifs to db. /// </summary> /// <param name="fmotif"> /// The Fmotif. /// </param> /// <returns> /// The <see cref="long"/>. /// </returns> public long CreateFmotif(Fmotif fmotif) { var fmotifChain = new BaseChain(fmotif.NoteList.Cast <IBaseObject>().ToList()); long[] notes = new ElementRepository(db).GetOrCreateNotesInDb(fmotifChain.Alphabet); var localFmotifHash = fmotif.GetHashCode().ToString(); var dbFmotifs = db.Fmotif.Where(f => f.Value == localFmotifHash).ToList(); if (dbFmotifs.Count > 0) { foreach (var dbFmotif in dbFmotifs) { long[] dbAlphabet = db.GetFmotifAlphabet(dbFmotif.Id); if (notes.SequenceEqual(dbAlphabet)) { int[] dbBuilding = db.GetFmotifBuilding(dbFmotif.Id); if (fmotifChain.Building.SequenceEqual(dbBuilding)) { if (fmotif.Type != dbFmotif.FmotifType) { throw new Exception("Found in db fmotif is not equal to local fmotif."); } return(dbFmotif.Id); } } } } return(Create(fmotif, notes, fmotifChain.Building)); }
/// <summary> /// The details. /// </summary> /// <param name="id"> /// The music sequence's id. /// </param> /// <returns> /// The <see cref="System.Threading.Tasks.Task"/>. /// </returns> public async Task <ActionResult> Details(long?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } using (var db = new LibiadaWebEntities()) { MusicSequence musicSequence = db.MusicSequence.Include(m => m.Matter).Single(m => m.Id == id); if (musicSequence == null) { return(HttpNotFound()); } var musicChainAlphabet = db.GetAlphabetElementIds(musicSequence.Id) .Select(el => db.Fmotif.Single(f => f.Id == el)) .ToList(); var musicChainBuilding = db.GetSequenceBuilding(musicSequence.Id); var sortedFmotifs = new Dictionary <LibiadaWeb.Fmotif, int>(); for (int i = 0; i < musicChainAlphabet.Count; i++) { sortedFmotifs.Add(musicChainAlphabet[i], musicChainBuilding.Count(el => el == i + 1)); } sortedFmotifs = sortedFmotifs.OrderByDescending(pair => pair.Value) .ToDictionary(pair => pair.Key, pair => pair.Value); var fmotifsChain = new List <Fmotif>(); foreach (var fmotif in sortedFmotifs.Keys) { var newFmotif = new Fmotif(fmotif.FmotifType, musicSequence.PauseTreatment, fmotif.Id); var fmotifAlphabet = db.GetFmotifAlphabet(fmotif.Id); var fmotifBuilding = db.GetFmotifBuilding(fmotif.Id); foreach (var position in fmotifBuilding) { var dbNoteId = fmotifAlphabet.ElementAt(position - 1); var dbNote = db.Note.Single(n => n.Id == dbNoteId); var newPitches = new List <Pitch>(); foreach (var pitch in dbNote.Pitch) { newPitches.Add(new Pitch(pitch.Midinumber)); } var newNote = new ValueNote(newPitches, new Duration(dbNote.Numerator, dbNote.Denominator), dbNote.Triplet, dbNote.Tie); newNote.Id = dbNote.Id; newFmotif.NoteList.Add(newNote); } fmotifsChain.Add(newFmotif); } var result = new Dictionary <string, object> { { "fmotifs", fmotifsChain }, { "sequentialTransfer", musicSequence.SequentialTransfer } }; ViewBag.data = JsonConvert.SerializeObject(new Dictionary <string, object> { { "data", result } }); return(View(musicSequence)); } }