PageRenderTime 132ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Programming/Labs/4sem/ISP/MusicSheetEditor/Benko.MusicSheetEditor.BLL/MusicSheetsManager.cs

http://bsuir-informatics-labs.googlecode.com/
C# | 372 lines | 298 code | 52 blank | 22 comment | 18 complexity | 60a648022e82817528f1113d5d7d1a8b MD5 | raw file
Possible License(s): AGPL-1.0, CC0-1.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Configuration;
  5. using System.Linq;
  6. using Benko.MusicSheetEditor.DAL.Interfaces;
  7. using Benko.MusicSheetEditor.DAL.Entities;
  8. using Benko.MusicSheetEditor.DAL.DatabaseBridge;
  9. namespace Benko.MusicSheetEditor.BLL
  10. {
  11. public class MusicSheetsManager
  12. {
  13. private string[] c_asAccidentalNotes = { "A", "B", "C", "D", "E", "F", "G" };
  14. private static MusicSheetsManager g_oInstance = null;
  15. private int m_iSheetsCount;
  16. private List<Sheet> m_loSheets;
  17. private List<SheetEntity> m_loSheetEntities;
  18. private bool m_bIsInitialized = false;
  19. private Dictionary<Sheet, bool> m_oUsedSheets;
  20. internal IDataAccessBridge m_oDataAccessBridge;
  21. public static MusicSheetsManager Instance
  22. {
  23. get
  24. {
  25. if (g_oInstance == null)
  26. {
  27. g_oInstance = new MusicSheetsManager();
  28. }
  29. if (!g_oInstance.Initialized)
  30. g_oInstance.Initialize();
  31. return g_oInstance;
  32. }
  33. }
  34. public bool Initialized
  35. {
  36. get
  37. {
  38. return m_bIsInitialized;
  39. }
  40. }
  41. protected MusicSheetsManager()
  42. {
  43. m_bIsInitialized = false;
  44. Initialize();
  45. }
  46. protected void loadSheets()
  47. {
  48. m_loSheetEntities = m_oDataAccessBridge.ReadAllSheets().ToList();
  49. m_iSheetsCount = m_loSheetEntities.Count;
  50. }
  51. public void Initialize()
  52. {
  53. m_bIsInitialized = false;
  54. var sDatabaseType = ConfigurationManager.AppSettings["DatabaseType"];
  55. var sParam = ConfigurationManager.AppSettings["DataAccessBridgeStringParam"];
  56. var oFactory = new DatabaseBridgeFactory();
  57. switch (sDatabaseType)
  58. {
  59. case "xml":
  60. m_oDataAccessBridge = oFactory.CreateDataAccessBridge(StandartDatabaseBridges.XmlFileDatabase, sParam);
  61. break;
  62. case "bin":
  63. m_oDataAccessBridge = oFactory.CreateDataAccessBridge(StandartDatabaseBridges.BinFileDatabase, sParam);
  64. break;
  65. case "other":
  66. var sAssemblyName = ConfigurationManager.AppSettings["DataAccessBridgeAssemblyName"];
  67. var sTypeName = ConfigurationManager.AppSettings["DataAccessBridgeTypeName"];
  68. m_oDataAccessBridge = oFactory.CreateDataAccessBridge(sAssemblyName, sTypeName, sParam);
  69. break;
  70. default:
  71. throw new ArgumentException("Argument value is unknow", "DatabaseType");
  72. }
  73. if(m_oDataAccessBridge != null )
  74. m_bIsInitialized = true;
  75. m_oUsedSheets = new Dictionary<Sheet, bool>();
  76. m_loSheets = new List<Sheet>();
  77. m_iSheetsCount = 0;
  78. loadSheets();
  79. }
  80. public List<Sheet> GetAllSheets()
  81. {
  82. m_loSheets = m_oDataAccessBridge.ReadAllSheets().Select(x => new Sheet(x)).ToList();
  83. useSheets(m_loSheets);
  84. return m_loSheets;
  85. }
  86. protected void useSheets(List<Sheet> v_loSheets)
  87. {
  88. foreach (var oSheet in v_loSheets)
  89. {
  90. m_oUsedSheets[oSheet] = true;
  91. }
  92. }
  93. public List<String> GetSheetNames()
  94. {
  95. return m_loSheetEntities.Select(x => x.Description).ToList();
  96. }
  97. public Sheet GetSheetByName(string v_sName)
  98. {
  99. try
  100. {
  101. var oSheets = new Sheet(m_oDataAccessBridge.ReadSheetByName(v_sName));
  102. if (!m_oUsedSheets.ContainsKey(oSheets))
  103. {
  104. m_oUsedSheets.Add(oSheets, true);
  105. }
  106. //m_oUsedSheets[oSheets] = true;
  107. return oSheets;
  108. }
  109. catch (Exception ex)
  110. {
  111. MusicSheetBLLLogger.WriteExceptionLog(ex);
  112. //throw;
  113. return null;
  114. }
  115. }
  116. public List<Sheet> GetSheetsFromTimeInterval(DateTime v_oBeginTime, DateTime v_oEndTime)
  117. {
  118. try
  119. {
  120. m_loSheets = m_oDataAccessBridge.ReadSheetsFromTimeInterval(v_oBeginTime, v_oEndTime).Select(x => new Sheet(x)).ToList();
  121. useSheets(m_loSheets);
  122. return m_loSheets;
  123. }
  124. catch (Exception ex)
  125. {
  126. MusicSheetBLLLogger.WriteExceptionLog(ex);
  127. return null;
  128. }
  129. }
  130. public List<Sheet> GetSheetsByAuthor(string v_sAuthorName)
  131. {
  132. try
  133. {
  134. m_loSheets = m_oDataAccessBridge.ReadSheetsByAuthor(v_sAuthorName).Select(x => new Sheet(x)).ToList();
  135. useSheets(m_loSheets);
  136. return m_loSheets;
  137. }
  138. catch (Exception ex)
  139. {
  140. return null;
  141. }
  142. }
  143. public void AddSheet(Sheet v_oSheet)
  144. {
  145. //if (!m_oUsedSheets.ContainsKey(v_oSheet))
  146. //{
  147. // m_iSheetsCount++;
  148. // m_oUsedSheets[v_oSheet] = true;
  149. //}
  150. m_iSheetsCount++;
  151. v_oSheet.SaveAllChanges();
  152. m_loSheetEntities = m_oDataAccessBridge.ReadAllSheets().ToList();
  153. }
  154. public int GetSheetCount()
  155. {
  156. return m_iSheetsCount;
  157. }
  158. public void SaveAllChanges()
  159. {
  160. lock (new object())
  161. {
  162. foreach (var x in m_oUsedSheets)
  163. {
  164. x.Key.SaveAllChanges();
  165. }
  166. m_oUsedSheets.Clear();
  167. }
  168. }
  169. public void DeleteSheet(string v_sName)
  170. {
  171. //var oSheet = m_loSheetEntities.Find(x => x.Description == v_sName);
  172. //if (oSheet != null)
  173. //{
  174. // oSheet.ID = -Math.Abs(oSheet.ID);
  175. // m_oUsedSheets[new Sheet(oSheet)] = true;
  176. //}
  177. //else
  178. //{
  179. // foreach (var x in m_oUsedSheets)
  180. // {
  181. // if (x.Key.Name == v_sName)
  182. // x.Key.ID = -Math.Abs(x.Key.ID);
  183. // }
  184. //}
  185. var oSheet = GetSheetByName(v_sName);
  186. DeleteSheet(oSheet);
  187. }
  188. public void DeleteSheet(Sheet v_oSheet)
  189. {
  190. v_oSheet.m_bIsDeleted = true;
  191. v_oSheet.SaveAllChanges();
  192. if (m_oUsedSheets.ContainsKey(v_oSheet))
  193. {
  194. m_oUsedSheets.Remove(v_oSheet);
  195. }
  196. m_loSheetEntities = m_oDataAccessBridge.ReadAllSheets().ToList();
  197. }
  198. internal NoteValueEntity getNoteValueEntityByMidiNumber(int v_iMidiNumber)
  199. {
  200. if (!m_bIsInitialized)
  201. throw new NotInitializedException();
  202. return m_oDataAccessBridge.ReadNoteValue(v_iMidiNumber);
  203. }
  204. public string GetRelativeLoudnessTypeName(RelativeLoudnessType v_iRelativeLoudnessType)
  205. {
  206. return m_oDataAccessBridge.ReadRelativeLoudnessType((int)v_iRelativeLoudnessType).Name;
  207. }
  208. public int GetRelativeLoudnessTypeValue(RelativeLoudnessType v_iRelativeLoudnessType)
  209. {
  210. return m_oDataAccessBridge.ReadRelativeLoudnessType((int)v_iRelativeLoudnessType).Value;
  211. }
  212. public string GetTimeValueName(TimeValue v_iTimeValue)
  213. {
  214. return m_oDataAccessBridge.ReadTimeValue((int)v_iTimeValue).Name;
  215. }
  216. public int GetTimeValueValue(TimeValue v_iTimeValue)
  217. {
  218. return m_oDataAccessBridge.ReadTimeValue((int)v_iTimeValue).Value;
  219. }
  220. public string GetGradualChangeTypeName(GradualChangeType v_iGradualChangeType)
  221. {
  222. return m_oDataAccessBridge.ReadGradualChangeType((int)v_iGradualChangeType).Name;
  223. }
  224. public int GetGradualChangeTypeValue(GradualChangeType v_iGradualChangeType)
  225. {
  226. return m_oDataAccessBridge.ReadGradualChangeType((int)v_iGradualChangeType).Value;
  227. }
  228. public string GetArticulationName(Articulation v_iArticulation)
  229. {
  230. return m_oDataAccessBridge.ReadArticulation((int)v_iArticulation).Name;
  231. }
  232. public string GetAccidentalName(Accidental v_iAccidental)
  233. {
  234. return m_oDataAccessBridge.ReadAccidental((int)v_iAccidental).Name;
  235. }
  236. public int GetAccidentalValue(Accidental v_iAccidental)
  237. {
  238. return m_oDataAccessBridge.ReadAccidental((int)v_iAccidental).Value;
  239. }
  240. public string GetAccidentalNoteName(AccidentalNote v_iAccidentalNote)
  241. {
  242. return c_asAccidentalNotes[(int)v_iAccidentalNote];
  243. }
  244. public double GetNoteValueFrequency(int v_iMidiNumber)
  245. {
  246. return getNoteValueEntityByMidiNumber(v_iMidiNumber).Frequency;
  247. }
  248. public string GetNoteValueLongName(int v_iMidiNumber)
  249. {
  250. return getNoteValueEntityByMidiNumber(v_iMidiNumber).LongStringNumber;
  251. }
  252. public string GetNoteValueShortName(int v_iMidiNumber)
  253. {
  254. return getNoteValueEntityByMidiNumber(v_iMidiNumber).ShortStringNumber;
  255. }
  256. public void Clear()
  257. {
  258. m_bIsInitialized = false;
  259. m_iSheetsCount = 0;
  260. m_loSheetEntities = null;
  261. m_loSheets = null;
  262. m_oDataAccessBridge.Dispose();
  263. m_oDataAccessBridge = null;
  264. m_oUsedSheets = null;
  265. GC.Collect(2, GCCollectionMode.Forced);
  266. }
  267. public bool IsChanged()
  268. {
  269. if (m_oUsedSheets == null)
  270. return false;
  271. return m_oUsedSheets.Count == 0;
  272. }
  273. public IEnumerable<Accidental> GetAllAccidentals()
  274. {
  275. for (int i = 0; i < (int)Accidental.EndElement; i++)
  276. {
  277. yield return (Accidental)i;
  278. }
  279. }
  280. public IEnumerable<AccidentalNote> GetAllAccidentalValues()
  281. {
  282. for (int i = 0; i < (int)AccidentalNote.EndElement; i++)
  283. {
  284. yield return (AccidentalNote)i;
  285. }
  286. }
  287. public IEnumerable<Articulation> GetAllArticulations()
  288. {
  289. for (int i = 0; i < (int)Articulation.EndElement; i++)
  290. {
  291. yield return (Articulation)i;
  292. }
  293. }
  294. public IEnumerable<GradualChangeType> GetAllGradualChangeTypes()
  295. {
  296. for (int i = 0; i < (int)GradualChangeType.EndElement; i++)
  297. {
  298. yield return (GradualChangeType)i;
  299. }
  300. }
  301. public IEnumerable<RelativeLoudnessType> GetAllRelativeLoudnessTypes()
  302. {
  303. for (int i = 0; i < (int)RelativeLoudnessType.EndElement; i++)
  304. {
  305. yield return (RelativeLoudnessType)i;
  306. }
  307. }
  308. public IEnumerable<TimeValue> GetAllTimeValues()
  309. {
  310. for (int i = 0; i < (int)TimeValue.EndElement; i++)
  311. {
  312. yield return (TimeValue)i;
  313. }
  314. }
  315. ~MusicSheetsManager()
  316. {
  317. //SaveAllChanges();
  318. }
  319. }
  320. }