/Programming/Labs/4sem/ISP/MusicSheetEditor/Benko.MusicSheetEditor.BLL/MusicSheetsManager.cs
C# | 372 lines | 298 code | 52 blank | 22 comment | 18 complexity | 60a648022e82817528f1113d5d7d1a8b MD5 | raw file
Possible License(s): AGPL-1.0, CC0-1.0
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Configuration;
- using System.Linq;
- using Benko.MusicSheetEditor.DAL.Interfaces;
- using Benko.MusicSheetEditor.DAL.Entities;
- using Benko.MusicSheetEditor.DAL.DatabaseBridge;
-
- namespace Benko.MusicSheetEditor.BLL
- {
- public class MusicSheetsManager
- {
- private string[] c_asAccidentalNotes = { "A", "B", "C", "D", "E", "F", "G" };
-
- private static MusicSheetsManager g_oInstance = null;
-
- private int m_iSheetsCount;
- private List<Sheet> m_loSheets;
- private List<SheetEntity> m_loSheetEntities;
- private bool m_bIsInitialized = false;
-
- private Dictionary<Sheet, bool> m_oUsedSheets;
-
- internal IDataAccessBridge m_oDataAccessBridge;
-
- public static MusicSheetsManager Instance
- {
- get
- {
- if (g_oInstance == null)
- {
- g_oInstance = new MusicSheetsManager();
- }
- if (!g_oInstance.Initialized)
- g_oInstance.Initialize();
- return g_oInstance;
- }
- }
-
- public bool Initialized
- {
- get
- {
- return m_bIsInitialized;
- }
- }
-
- protected MusicSheetsManager()
- {
- m_bIsInitialized = false;
- Initialize();
- }
-
- protected void loadSheets()
- {
- m_loSheetEntities = m_oDataAccessBridge.ReadAllSheets().ToList();
- m_iSheetsCount = m_loSheetEntities.Count;
- }
-
- public void Initialize()
- {
- m_bIsInitialized = false;
-
- var sDatabaseType = ConfigurationManager.AppSettings["DatabaseType"];
- var sParam = ConfigurationManager.AppSettings["DataAccessBridgeStringParam"];
- var oFactory = new DatabaseBridgeFactory();
-
- switch (sDatabaseType)
- {
- case "xml":
- m_oDataAccessBridge = oFactory.CreateDataAccessBridge(StandartDatabaseBridges.XmlFileDatabase, sParam);
- break;
-
- case "bin":
- m_oDataAccessBridge = oFactory.CreateDataAccessBridge(StandartDatabaseBridges.BinFileDatabase, sParam);
- break;
-
- case "other":
- var sAssemblyName = ConfigurationManager.AppSettings["DataAccessBridgeAssemblyName"];
- var sTypeName = ConfigurationManager.AppSettings["DataAccessBridgeTypeName"];
- m_oDataAccessBridge = oFactory.CreateDataAccessBridge(sAssemblyName, sTypeName, sParam);
- break;
-
- default:
- throw new ArgumentException("Argument value is unknow", "DatabaseType");
- }
- if(m_oDataAccessBridge != null )
- m_bIsInitialized = true;
-
- m_oUsedSheets = new Dictionary<Sheet, bool>();
- m_loSheets = new List<Sheet>();
- m_iSheetsCount = 0;
- loadSheets();
- }
-
-
- public List<Sheet> GetAllSheets()
- {
- m_loSheets = m_oDataAccessBridge.ReadAllSheets().Select(x => new Sheet(x)).ToList();
- useSheets(m_loSheets);
- return m_loSheets;
- }
-
- protected void useSheets(List<Sheet> v_loSheets)
- {
- foreach (var oSheet in v_loSheets)
- {
- m_oUsedSheets[oSheet] = true;
- }
- }
-
- public List<String> GetSheetNames()
- {
- return m_loSheetEntities.Select(x => x.Description).ToList();
- }
-
- public Sheet GetSheetByName(string v_sName)
- {
- try
- {
- var oSheets = new Sheet(m_oDataAccessBridge.ReadSheetByName(v_sName));
- if (!m_oUsedSheets.ContainsKey(oSheets))
- {
- m_oUsedSheets.Add(oSheets, true);
- }
- //m_oUsedSheets[oSheets] = true;
- return oSheets;
- }
- catch (Exception ex)
- {
- MusicSheetBLLLogger.WriteExceptionLog(ex);
- //throw;
- return null;
- }
- }
-
- public List<Sheet> GetSheetsFromTimeInterval(DateTime v_oBeginTime, DateTime v_oEndTime)
- {
- try
- {
- m_loSheets = m_oDataAccessBridge.ReadSheetsFromTimeInterval(v_oBeginTime, v_oEndTime).Select(x => new Sheet(x)).ToList();
- useSheets(m_loSheets);
- return m_loSheets;
- }
- catch (Exception ex)
- {
- MusicSheetBLLLogger.WriteExceptionLog(ex);
- return null;
- }
- }
-
- public List<Sheet> GetSheetsByAuthor(string v_sAuthorName)
- {
- try
- {
- m_loSheets = m_oDataAccessBridge.ReadSheetsByAuthor(v_sAuthorName).Select(x => new Sheet(x)).ToList();
- useSheets(m_loSheets);
- return m_loSheets;
- }
- catch (Exception ex)
- {
- return null;
- }
- }
-
- public void AddSheet(Sheet v_oSheet)
- {
- //if (!m_oUsedSheets.ContainsKey(v_oSheet))
- //{
- // m_iSheetsCount++;
- // m_oUsedSheets[v_oSheet] = true;
- //}
- m_iSheetsCount++;
- v_oSheet.SaveAllChanges();
- m_loSheetEntities = m_oDataAccessBridge.ReadAllSheets().ToList();
- }
-
- public int GetSheetCount()
- {
- return m_iSheetsCount;
- }
-
- public void SaveAllChanges()
- {
- lock (new object())
- {
- foreach (var x in m_oUsedSheets)
- {
- x.Key.SaveAllChanges();
- }
- m_oUsedSheets.Clear();
- }
- }
-
- public void DeleteSheet(string v_sName)
- {
- //var oSheet = m_loSheetEntities.Find(x => x.Description == v_sName);
- //if (oSheet != null)
- //{
- // oSheet.ID = -Math.Abs(oSheet.ID);
- // m_oUsedSheets[new Sheet(oSheet)] = true;
- //}
- //else
- //{
- // foreach (var x in m_oUsedSheets)
- // {
- // if (x.Key.Name == v_sName)
- // x.Key.ID = -Math.Abs(x.Key.ID);
- // }
- //}
- var oSheet = GetSheetByName(v_sName);
- DeleteSheet(oSheet);
- }
-
- public void DeleteSheet(Sheet v_oSheet)
- {
- v_oSheet.m_bIsDeleted = true;
- v_oSheet.SaveAllChanges();
- if (m_oUsedSheets.ContainsKey(v_oSheet))
- {
- m_oUsedSheets.Remove(v_oSheet);
- }
- m_loSheetEntities = m_oDataAccessBridge.ReadAllSheets().ToList();
- }
-
- internal NoteValueEntity getNoteValueEntityByMidiNumber(int v_iMidiNumber)
- {
- if (!m_bIsInitialized)
- throw new NotInitializedException();
- return m_oDataAccessBridge.ReadNoteValue(v_iMidiNumber);
- }
-
- public string GetRelativeLoudnessTypeName(RelativeLoudnessType v_iRelativeLoudnessType)
- {
- return m_oDataAccessBridge.ReadRelativeLoudnessType((int)v_iRelativeLoudnessType).Name;
- }
-
- public int GetRelativeLoudnessTypeValue(RelativeLoudnessType v_iRelativeLoudnessType)
- {
- return m_oDataAccessBridge.ReadRelativeLoudnessType((int)v_iRelativeLoudnessType).Value;
- }
-
- public string GetTimeValueName(TimeValue v_iTimeValue)
- {
- return m_oDataAccessBridge.ReadTimeValue((int)v_iTimeValue).Name;
- }
-
- public int GetTimeValueValue(TimeValue v_iTimeValue)
- {
- return m_oDataAccessBridge.ReadTimeValue((int)v_iTimeValue).Value;
- }
-
- public string GetGradualChangeTypeName(GradualChangeType v_iGradualChangeType)
- {
- return m_oDataAccessBridge.ReadGradualChangeType((int)v_iGradualChangeType).Name;
- }
-
- public int GetGradualChangeTypeValue(GradualChangeType v_iGradualChangeType)
- {
- return m_oDataAccessBridge.ReadGradualChangeType((int)v_iGradualChangeType).Value;
- }
-
- public string GetArticulationName(Articulation v_iArticulation)
- {
- return m_oDataAccessBridge.ReadArticulation((int)v_iArticulation).Name;
- }
-
- public string GetAccidentalName(Accidental v_iAccidental)
- {
- return m_oDataAccessBridge.ReadAccidental((int)v_iAccidental).Name;
- }
-
- public int GetAccidentalValue(Accidental v_iAccidental)
- {
- return m_oDataAccessBridge.ReadAccidental((int)v_iAccidental).Value;
- }
-
- public string GetAccidentalNoteName(AccidentalNote v_iAccidentalNote)
- {
- return c_asAccidentalNotes[(int)v_iAccidentalNote];
- }
-
- public double GetNoteValueFrequency(int v_iMidiNumber)
- {
- return getNoteValueEntityByMidiNumber(v_iMidiNumber).Frequency;
- }
-
- public string GetNoteValueLongName(int v_iMidiNumber)
- {
- return getNoteValueEntityByMidiNumber(v_iMidiNumber).LongStringNumber;
- }
-
- public string GetNoteValueShortName(int v_iMidiNumber)
- {
- return getNoteValueEntityByMidiNumber(v_iMidiNumber).ShortStringNumber;
- }
-
-
- public void Clear()
- {
- m_bIsInitialized = false;
- m_iSheetsCount = 0;
- m_loSheetEntities = null;
- m_loSheets = null;
- m_oDataAccessBridge.Dispose();
- m_oDataAccessBridge = null;
- m_oUsedSheets = null;
- GC.Collect(2, GCCollectionMode.Forced);
- }
-
- public bool IsChanged()
- {
- if (m_oUsedSheets == null)
- return false;
- return m_oUsedSheets.Count == 0;
- }
-
- public IEnumerable<Accidental> GetAllAccidentals()
- {
- for (int i = 0; i < (int)Accidental.EndElement; i++)
- {
- yield return (Accidental)i;
- }
- }
- public IEnumerable<AccidentalNote> GetAllAccidentalValues()
- {
- for (int i = 0; i < (int)AccidentalNote.EndElement; i++)
- {
- yield return (AccidentalNote)i;
- }
- }
-
- public IEnumerable<Articulation> GetAllArticulations()
- {
- for (int i = 0; i < (int)Articulation.EndElement; i++)
- {
- yield return (Articulation)i;
- }
- }
-
- public IEnumerable<GradualChangeType> GetAllGradualChangeTypes()
- {
- for (int i = 0; i < (int)GradualChangeType.EndElement; i++)
- {
- yield return (GradualChangeType)i;
- }
- }
-
- public IEnumerable<RelativeLoudnessType> GetAllRelativeLoudnessTypes()
- {
- for (int i = 0; i < (int)RelativeLoudnessType.EndElement; i++)
- {
- yield return (RelativeLoudnessType)i;
- }
- }
-
- public IEnumerable<TimeValue> GetAllTimeValues()
- {
- for (int i = 0; i < (int)TimeValue.EndElement; i++)
- {
- yield return (TimeValue)i;
- }
- }
-
- ~MusicSheetsManager()
- {
- //SaveAllChanges();
- }
-
- }
- }