/// <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 fill parameters. /// </summary> /// <param name="fmotif"> /// The Fmotif. /// </param> /// <param name="alphabet"> /// The alphabet. /// </param> /// <param name="building"> /// The building. /// </param> /// <returns> /// The <see cref="List{Object}"/>. /// </returns> protected List <NpgsqlParameter> FillParams(Fmotif fmotif, long[] alphabet, int[] building) { fmotif.Id = db.GetNewElementId(); var fmotivValue = fmotif.GetHashCode().ToString(); var parameters = new List <NpgsqlParameter> { new NpgsqlParameter <long>("id", NpgsqlDbType.Bigint) { TypedValue = fmotif.Id }, new NpgsqlParameter <string>("value", NpgsqlDbType.Varchar) { TypedValue = fmotivValue }, new NpgsqlParameter <byte>("notation", NpgsqlDbType.Smallint) { TypedValue = (byte)Notation.FormalMotifs }, new NpgsqlParameter <long[]>("alphabet", NpgsqlDbType.Array | NpgsqlDbType.Bigint) { TypedValue = alphabet }, new NpgsqlParameter <int[]>("building", NpgsqlDbType.Array | NpgsqlDbType.Integer) { TypedValue = building }, new NpgsqlParameter <byte>("fmotif_type", NpgsqlDbType.Smallint) { TypedValue = (byte)fmotif.Type } }; return(parameters); }
/// <summary> /// The insert. /// </summary> /// <param name="fmotif"> /// The Fmotif. /// </param> /// <param name="alphabet"> /// The alphabet. /// </param> /// <param name="building"> /// The building. /// </param> /// <returns> /// The <see cref="long"/>. /// </returns> public long Create(Fmotif fmotif, long[] alphabet, int[] building) { List <NpgsqlParameter> parameters = FillParams(fmotif, alphabet, building); const string Query = @"INSERT INTO fmotif ( id, value, notation, alphabet, building, fmotif_type ) VALUES ( @id, @value, @notation, @alphabet, @building, @fmotif_type );"; db.ExecuteCommand(Query, parameters.ToArray()); return(fmotif.Id); }
/// <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)); } }