/SqlMetalIncludeGUI/Config.cs

# · C# · 425 lines · 373 code · 52 blank · 0 comment · 22 complexity · cb2b6e6ef2cfcbb84fbbc1a4bbf3a362 MD5 · raw file

  1. using System;
  2. using System.ComponentModel;
  3. using System.Data.SqlClient;
  4. using System.Windows;
  5. using System.Xml.Linq;
  6. using System.Collections.ObjectModel;
  7. namespace SqlMetalIncludeGUI
  8. {
  9. internal class Config : INotifyPropertyChanged
  10. {
  11. private static readonly Config Instance = new Config();
  12. public static Config Default
  13. {
  14. get { return Instance; }
  15. }
  16. private string _username = string.Empty;
  17. private string _password = string.Empty;
  18. private bool _windowsAuth = true;
  19. private bool _sqlAuth;
  20. private string _sqlServer = string.Empty;
  21. private string _sqlDatabase = string.Empty;
  22. private string _configFilename = string.Empty;
  23. private string _projectName = string.Empty;
  24. private bool _excludeMode;
  25. private bool _sqlMetalIncludeViews;
  26. private bool _sqlMetalIncludeSprocs;
  27. private string _sqlMetalOutput = "FullDbMap.dbml";
  28. private bool _sqlMetalPluralise;
  29. private string _outputCodeFile = string.Empty;
  30. private string _outputCodeNamespace = string.Empty;
  31. private string _outputCodeLanguage = "Default";
  32. private string _outputCodeEntityBase = string.Empty;
  33. private string _outputCodeContextName = string.Empty;
  34. private bool _outputCodeSerializable;
  35. private bool _outputCodeUseDesignerFile = true;
  36. private readonly ObservableCollection<DbObject> _dbObjects = new ObservableCollection<DbObject>();
  37. public ObservableCollection<DbObject> DbObjects
  38. {
  39. get { return _dbObjects; }
  40. }
  41. public void WriteConfig()
  42. {
  43. var config = new XDocument();
  44. var dbObjectsElement = new XElement("DbObjects");
  45. foreach (DbObject dbo in _dbObjects)
  46. {
  47. dbObjectsElement.Add(new XElement("DbObject",
  48. new XAttribute("Name", dbo.Name),
  49. new XAttribute("ListName", dbo.ListName),
  50. new XAttribute("ClassName", dbo.ClassName),
  51. new XAttribute("Selected", dbo.Selected)));
  52. }
  53. config.Add(new XElement("SqlMetalIncludeConfig",
  54. new XAttribute("ProjectName", _projectName),
  55. new XAttribute("ExcludeMode", _excludeMode),
  56. new XElement("SqlConfiguration",
  57. new XAttribute("SqlServer", _sqlServer),
  58. new XAttribute("SqlDatabase", _sqlDatabase),
  59. new XAttribute("WindowsAuth", _windowsAuth),
  60. new XAttribute("SqlAuth", _sqlAuth),
  61. new XAttribute("Username", _username),
  62. new XAttribute("Password", _password)),
  63. new XElement("SqlMetalConfiguration",
  64. new XAttribute("IncludeViews", _sqlMetalIncludeViews),
  65. new XAttribute("IncludeSprocs", _sqlMetalIncludeSprocs),
  66. new XAttribute("OutputFilename", _sqlMetalOutput),
  67. new XAttribute("Pluralise", _sqlMetalPluralise)),
  68. new XElement("OutputConfiguration",
  69. new XAttribute("ContextName", _outputCodeContextName),
  70. new XAttribute("Namespace", _outputCodeNamespace),
  71. new XAttribute("Language", _outputCodeLanguage),
  72. new XAttribute("EntityBase", _outputCodeEntityBase),
  73. new XAttribute("Filename", _outputCodeFile),
  74. new XAttribute("Serialisable", _outputCodeSerializable),
  75. new XAttribute("UseDesignerFile", _outputCodeUseDesignerFile)),
  76. dbObjectsElement
  77. ));
  78. if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(_configFilename)))
  79. System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(_configFilename));
  80. config.Save(_configFilename);
  81. }
  82. public void LoadConfig()
  83. {
  84. XDocument config = XDocument.Load(_configFilename);
  85. if (config == null)
  86. return;
  87. XElement rootConfig = config.Element("SqlMetalIncludeConfig");
  88. if (rootConfig == null)
  89. return;
  90. _projectName = rootConfig.Attribute("ProjectName").Value;
  91. _excludeMode = Convert.ToBoolean(rootConfig.Attribute("ExcludeMode").Value);
  92. XElement sqlConfig = rootConfig.Element("SqlConfiguration");
  93. if (sqlConfig != null)
  94. {
  95. _sqlServer = sqlConfig.Attribute("SqlServer").Value;
  96. _sqlDatabase = sqlConfig.Attribute("SqlDatabase").Value;
  97. _windowsAuth = Convert.ToBoolean(sqlConfig.Attribute("WindowsAuth").Value);
  98. _sqlAuth = Convert.ToBoolean(sqlConfig.Attribute("SqlAuth").Value);
  99. _username = sqlConfig.Attribute("Username").Value;
  100. _password = sqlConfig.Attribute("Password").Value;
  101. }
  102. XElement sqlMetalConfig = rootConfig.Element("SqlMetalConfiguration");
  103. if (sqlMetalConfig != null)
  104. {
  105. _sqlMetalIncludeViews = Convert.ToBoolean(sqlMetalConfig.Attribute("IncludeViews").Value);
  106. _sqlMetalIncludeSprocs = Convert.ToBoolean(sqlMetalConfig.Attribute("IncludeSprocs").Value);
  107. _sqlMetalPluralise = Convert.ToBoolean(sqlMetalConfig.Attribute("Pluralise").Value);
  108. _sqlMetalOutput = sqlMetalConfig.Attribute("OutputFilename").Value;
  109. }
  110. XElement outputConfig = rootConfig.Element("OutputConfiguration");
  111. if (outputConfig != null)
  112. {
  113. _outputCodeContextName = outputConfig.Attribute("ContextName").Value;
  114. _outputCodeNamespace = outputConfig.Attribute("Namespace").Value;
  115. _outputCodeLanguage = outputConfig.Attribute("Language").Value;
  116. _outputCodeEntityBase = outputConfig.Attribute("EntityBase").Value;
  117. _outputCodeFile = outputConfig.Attribute("Filename").Value;
  118. _outputCodeSerializable = Convert.ToBoolean(outputConfig.Attribute("Serialisable").Value);
  119. _outputCodeUseDesignerFile = Convert.ToBoolean(outputConfig.Attribute("UseDesignerFile").Value);
  120. }
  121. XElement dbObjectsElement = rootConfig.Element("DbObjects");
  122. _dbObjects.Clear();
  123. if (dbObjectsElement != null)
  124. {
  125. foreach (XElement dbo in dbObjectsElement.Elements("DbObject"))
  126. {
  127. _dbObjects.Add(new DbObject
  128. {
  129. Name = dbo.Attribute("Name").Value,
  130. ListName = dbo.Attribute("ListName").Value,
  131. ClassName = dbo.Attribute("ClassName").Value,
  132. Selected = Convert.ToBoolean(dbo.Attribute("Selected").Value)
  133. });
  134. }
  135. }
  136. }
  137. public bool OutputCodeUseDesignerFile
  138. {
  139. get { return _outputCodeUseDesignerFile; }
  140. set
  141. {
  142. _outputCodeUseDesignerFile = value;
  143. NotifyChanged("OutputCodeUseDesignerFile");
  144. }
  145. }
  146. public bool OutputCodeSerializable
  147. {
  148. get { return _outputCodeSerializable; }
  149. set
  150. {
  151. _outputCodeSerializable = value;
  152. NotifyChanged("OutputCodeSerializable");
  153. }
  154. }
  155. public string OutputCodeContextName
  156. {
  157. get { return _outputCodeContextName; }
  158. set
  159. {
  160. _outputCodeContextName = value;
  161. NotifyChanged("OutputCodeContextName");
  162. }
  163. }
  164. public string ProjectName
  165. {
  166. get { return _projectName; }
  167. set
  168. {
  169. _projectName = value;
  170. NotifyChanged("ProjectName");
  171. }
  172. }
  173. public bool ExcludeMode
  174. {
  175. get { return _excludeMode; }
  176. set
  177. {
  178. _excludeMode = value;
  179. NotifyChanged("ExcludeMode");
  180. }
  181. }
  182. public string ConfigFilename
  183. {
  184. get { return _configFilename; }
  185. set
  186. {
  187. _configFilename = value;
  188. NotifyChanged("ConfigFilename");
  189. }
  190. }
  191. public string OutputCodeEntityBase
  192. {
  193. get { return _outputCodeEntityBase; }
  194. set
  195. {
  196. _outputCodeEntityBase = value;
  197. NotifyChanged("OutputCodeEntityBase");
  198. }
  199. }
  200. public string OutputCodeLanguage
  201. {
  202. get { return _outputCodeLanguage; }
  203. set
  204. {
  205. _outputCodeLanguage = value;
  206. NotifyChanged("OutputCodeLanguage");
  207. }
  208. }
  209. public string OutputCodeNamespace
  210. {
  211. get { return _outputCodeNamespace; }
  212. set
  213. {
  214. _outputCodeNamespace = value;
  215. NotifyChanged("OutputCodeNamespace");
  216. }
  217. }
  218. public string OutputCodeFile
  219. {
  220. get { return _outputCodeFile; }
  221. set
  222. {
  223. _outputCodeFile = value;
  224. NotifyChanged("OutputCodeFile");
  225. }
  226. }
  227. public bool SqlMetalPluralise
  228. {
  229. get { return _sqlMetalPluralise; }
  230. set
  231. {
  232. _sqlMetalPluralise = value;
  233. NotifyChanged("SqlMetalPluralise");
  234. }
  235. }
  236. public string SqlMetalOutput
  237. {
  238. get { return _sqlMetalOutput; }
  239. set
  240. {
  241. _sqlMetalOutput = value;
  242. NotifyChanged("SqlMetalOutput");
  243. }
  244. }
  245. public bool SqlMetalIncludeSprocs
  246. {
  247. get { return _sqlMetalIncludeSprocs; }
  248. set
  249. {
  250. _sqlMetalIncludeSprocs = value;
  251. NotifyChanged("SqlMetalIncludeSprocs");
  252. }
  253. }
  254. public bool SqlMetalIncludeViews
  255. {
  256. get { return _sqlMetalIncludeViews; }
  257. set
  258. {
  259. _sqlMetalIncludeViews = value;
  260. NotifyChanged("SqlMetalIncludeViews");
  261. }
  262. }
  263. public string ConnectionString
  264. {
  265. get
  266. {
  267. var connString = new SqlConnectionStringBuilder
  268. {
  269. DataSource = _sqlServer,
  270. InitialCatalog = _sqlDatabase,
  271. IntegratedSecurity = _windowsAuth
  272. };
  273. if (_sqlAuth)
  274. {
  275. connString.UserID = _username;
  276. connString.Password = _password;
  277. }
  278. return connString.ToString();
  279. }
  280. }
  281. public bool TestConnection(bool showSuccessMessage, bool showFailureMessge)
  282. {
  283. var conn = new SqlConnection(ConnectionString);
  284. try
  285. {
  286. conn.Open();
  287. }
  288. catch (SqlException ex)
  289. {
  290. if (showFailureMessge)
  291. MessageBox.Show(ex.Message, "Connection failed!", MessageBoxButton.OK, MessageBoxImage.Error);
  292. return false;
  293. }
  294. if (showSuccessMessage)
  295. MessageBox.Show("Connection successful", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);
  296. return true;
  297. }
  298. public bool SqlConfigValid
  299. {
  300. get
  301. {
  302. return (!string.IsNullOrEmpty(_sqlServer) && !string.IsNullOrEmpty(_sqlDatabase) &&
  303. (_windowsAuth ||
  304. (_sqlAuth && !string.IsNullOrEmpty(_username) && !string.IsNullOrEmpty(_password))));
  305. }
  306. }
  307. public string SqlDatabase
  308. {
  309. get { return _sqlDatabase; }
  310. set
  311. {
  312. _sqlDatabase = value;
  313. NotifyChanged("SqlDatabase", "SqlConfigValid");
  314. }
  315. }
  316. public string SqlServer
  317. {
  318. get { return _sqlServer; }
  319. set
  320. {
  321. _sqlServer = value;
  322. NotifyChanged("SqlServer", "SqlConfigValid");
  323. }
  324. }
  325. public bool WindowsAuth
  326. {
  327. get { return _windowsAuth; }
  328. set
  329. {
  330. _windowsAuth = value;
  331. _sqlAuth = !value;
  332. NotifyChanged("WindowsAuth", "SqlAuth", "SqlConfigValid");
  333. }
  334. }
  335. public bool SqlAuth
  336. {
  337. get { return _sqlAuth; }
  338. set
  339. {
  340. _sqlAuth = value;
  341. _windowsAuth = !value;
  342. NotifyChanged("WindowsAuth", "SqlAuth", "SqlConfigValid");
  343. }
  344. }
  345. public string Password
  346. {
  347. get { return _password; }
  348. set
  349. {
  350. _password = value;
  351. NotifyChanged("Password", "SqlConfigValid");
  352. }
  353. }
  354. public string Username
  355. {
  356. get { return _username; }
  357. set
  358. {
  359. _username = value;
  360. NotifyChanged("Username", "SqlConfigValid");
  361. }
  362. }
  363. private void NotifyChanged(params string[] propertyNames)
  364. {
  365. if (PropertyChanged != null)
  366. {
  367. foreach (string propertyName in propertyNames)
  368. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  369. }
  370. }
  371. public event PropertyChangedEventHandler PropertyChanged;
  372. }
  373. }