PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/AUSRIS/Integration/Utilities/Deployment/IncrementalPublisher.cs

#
C# | 479 lines | 437 code | 35 blank | 7 comment | 38 complexity | 56b6d24a114dc652d4db8975a527c64b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using AUSRIS.Reports.Utilities.Deployment.Entities;
  5. using AUSRIS.Reports.Utilities.Deployment.FileSystem;
  6. using AUSRIS.Reports.Utilities.Deployment.Logging;
  7. using AUSRIS.Reports.Utilities.Deployment.Target;
  8. using AUSRIS.Reports.Utilities.Deployment.Configuration;
  9. namespace AUSRIS.Reports.Utilities.Deployment
  10. {
  11. /// <summary>
  12. /// This implementation of <see cref="IPublisher"/>
  13. /// reads through each file in the <see cref="DeploymentSet"/>,
  14. /// checks it out from the repository, compares it's revision
  15. /// to what was previosuly published, and publishes the
  16. /// file if necessary.
  17. /// </summary>
  18. public class IncrementalPublisher : IPublisher
  19. {
  20. #region Private Fields
  21. private bool error;
  22. private IReportServer reportServer;
  23. private IRepository repository;
  24. private ILogger logger;
  25. private IConfigurationDatabase configurationDatabase;
  26. private DeploymentSet currentDeploymentSet;
  27. private IList<Report> publishedReportLog;
  28. private IList<Report> skippedReportLog;
  29. #endregion
  30. #region Public Properties
  31. public ILogger Logger
  32. {
  33. get
  34. {
  35. return logger;
  36. }
  37. set
  38. {
  39. logger = value;
  40. }
  41. }
  42. public IRepository Repository
  43. {
  44. get
  45. {
  46. return repository;
  47. }
  48. set
  49. {
  50. repository = value;
  51. }
  52. }
  53. public IReportServer ReportServer
  54. {
  55. get
  56. {
  57. return reportServer;
  58. }
  59. set
  60. {
  61. reportServer = value;
  62. }
  63. }
  64. public IConfigurationDatabase ConfigurationDatabase
  65. {
  66. get
  67. {
  68. return configurationDatabase;
  69. }
  70. set
  71. {
  72. configurationDatabase = value;
  73. }
  74. }
  75. #endregion
  76. #region Public Methods
  77. public void LoadDeploymentSet(string name)
  78. {
  79. this.skippedReportLog = new List<Report>();
  80. this.publishedReportLog = new List<Report>();
  81. this.error = false;
  82. this.Logger.LogMessage("Loading Deployment Set '{0}'", name);
  83. this.currentDeploymentSet = this.ConfigurationDatabase.GetDeploymentSet(name);
  84. if ( this.currentDeploymentSet == null )
  85. {
  86. this.error = true;
  87. this.Logger.LogException(new Exception("Could not load Deployment Set: " + name));
  88. }
  89. }
  90. public void Publish()
  91. {
  92. if ( this.currentDeploymentSet == null )
  93. {
  94. throw new InvalidOperationException("No Deployment Set Loaded");
  95. }
  96. this.CheckReportServer();
  97. this.BeginTransaction();
  98. this.CleanupMissingItems();
  99. this.PublishFolders();
  100. this.PublishDatasources();
  101. this.PublishFiles();
  102. this.CommitTransaction();
  103. this.UpdateReportParameters();
  104. this.LogCompletionMessages();
  105. this.Logger.Flush();
  106. }
  107. #endregion
  108. #region Private Methods
  109. private void LogCompletionMessages()
  110. {
  111. if ( !error )
  112. {
  113. this.Logger.LogHeader("Publish Executed Successfully");
  114. this.Logger.LogMessage("Processed {0} report(s)", this.skippedReportLog.Count + this.publishedReportLog.Count);
  115. this.Logger.LogMessage("Published {0} report(s)", this.publishedReportLog.Count);
  116. this.Logger.LogMessage("Skipped {0} report(s)", this.skippedReportLog.Count);
  117. if ( this.publishedReportLog.Count > 0 )
  118. {
  119. this.Logger.LogNewLine();
  120. this.Logger.LogMessage("The following reports were published:");
  121. foreach ( Report report in this.publishedReportLog )
  122. {
  123. this.Logger.LogMessage(report.Path);
  124. }
  125. }
  126. }
  127. }
  128. private void CheckReportServer()
  129. {
  130. this.Logger.LogMessage("Checking report server {0}", this.ReportServer.ToString());
  131. try
  132. {
  133. if ( !this.ReportServer.CanConnect() )
  134. {
  135. this.Logger.LogMessage("Could not connect to report server");
  136. this.error = true;
  137. }
  138. }
  139. catch ( Exception ex )
  140. {
  141. this.Logger.LogException(ex);
  142. this.error = true;
  143. }
  144. }
  145. private void BeginTransaction()
  146. {
  147. if ( !error )
  148. {
  149. try
  150. {
  151. this.Logger.LogNewLine();
  152. this.Logger.LogMessage("Beginning Transaction");
  153. this.ReportServer.BeginTransaction();
  154. }
  155. catch ( Exception ex )
  156. {
  157. this.Logger.LogException(ex);
  158. }
  159. }
  160. }
  161. private void CommitTransaction()
  162. {
  163. if ( !error )
  164. {
  165. try
  166. {
  167. this.Logger.LogNewLine();
  168. this.Logger.LogMessage("Committing Transaction");
  169. this.ReportServer.CommitTransaction();
  170. }
  171. catch ( Exception ex )
  172. {
  173. this.Logger.LogException(ex);
  174. this.Cancel();
  175. }
  176. }
  177. }
  178. private void Cancel()
  179. {
  180. this.error = true;
  181. this.Logger.LogNewLine();
  182. this.Logger.LogMessage("Rolling Back Transaction");
  183. this.ReportServer.RollbackTransaction();
  184. this.Logger.LogNewLine();
  185. this.Logger.LogMessage("Publish Failed");
  186. }
  187. private void CleanupMissingItems()
  188. {
  189. if ( !this.error )
  190. {
  191. try
  192. {
  193. this.Logger.LogNewLine();
  194. this.Logger.LogMessage("Cleaning up items on server");
  195. IList<Folder> topFolders = this.currentDeploymentSet.Folders.GetTopFolders();
  196. foreach ( Folder topFolder in topFolders )
  197. {
  198. this.CleanFolder(topFolder);
  199. }
  200. }
  201. catch ( Exception ex )
  202. {
  203. this.Logger.LogException(ex);
  204. this.Cancel();
  205. }
  206. }
  207. }
  208. private void CleanFolder(Folder folder)
  209. {
  210. Folder deploymentFolder = null;
  211. if(this.currentDeploymentSet.Folders.ContainsKey(folder.Path))
  212. {
  213. deploymentFolder = this.currentDeploymentSet.Folders[folder.Path];
  214. }
  215. if ( deploymentFolder == null )
  216. {
  217. this.Logger.LogMessage("Removing Folder '{0}'", folder.Path);
  218. this.ReportServer.DeleteItem(folder);
  219. }
  220. else
  221. {
  222. foreach ( CatalogFile file in this.ReportServer.GetFiles(folder) )
  223. {
  224. if ( !deploymentFolder.ContainsFile(file) )
  225. {
  226. this.Logger.LogMessage("Removing File '{0}'", file.Path);
  227. this.ReportServer.DeleteItem(file);
  228. }
  229. }
  230. foreach ( Folder subFolder in this.ReportServer.GetFolders(folder) )
  231. {
  232. this.CleanFolder(subFolder);
  233. }
  234. }
  235. }
  236. private void PublishFolders()
  237. {
  238. if ( !error )
  239. {
  240. this.Logger.LogNewLine();
  241. this.Logger.LogMessage("Publishing Folders");
  242. foreach ( Folder folder in this.currentDeploymentSet.Folders.GetFlatList() )
  243. {
  244. try
  245. {
  246. if ( !this.ReportServer.ItemExists(folder) )
  247. {
  248. this.ReportServer.PublishFolder(folder);
  249. this.Logger.LogMessage("Folder created '{0}' at '{1}'", folder.Name, folder.ParentPath);
  250. this.PublishPolicies(folder);
  251. }
  252. }
  253. catch ( Exception ex )
  254. {
  255. this.Logger.LogException( ex);
  256. this.Cancel();
  257. break;
  258. }
  259. }
  260. }
  261. }
  262. private void PublishDatasources()
  263. {
  264. if ( !error )
  265. {
  266. this.Logger.LogNewLine();
  267. foreach ( Folder folder in this.currentDeploymentSet.Folders.GetFlatList() )
  268. {
  269. if ( folder.Datasources.Count > 0 )
  270. {
  271. this.Logger.LogMessage("Publishing DataSources in '{0}'", folder.Path);
  272. foreach ( DataSource dataSource in folder.Datasources )
  273. {
  274. try
  275. {
  276. if ( !dataSource.Overwrite && this.ReportServer.ItemExists(dataSource) )
  277. {
  278. this.Logger.LogMessage("DataSouce '{0}' already published, not overwriting", dataSource.Name);
  279. }
  280. else
  281. {
  282. this.Logger.LogMessage("Publishing '{0}'", dataSource.Name);
  283. this.ReportServer.PublishDataSource(dataSource);
  284. this.Logger.LogMessage("DataSource published '{0}'", dataSource.Name);
  285. }
  286. }
  287. catch ( Exception ex )
  288. {
  289. this.Logger.LogException(ex);
  290. this.Cancel();
  291. break;
  292. }
  293. }
  294. }
  295. }
  296. }
  297. }
  298. private void PublishFiles()
  299. {
  300. if ( !error )
  301. {
  302. foreach ( Folder folder in this.currentDeploymentSet.Folders.GetFlatList() )
  303. {
  304. if ( !error )
  305. {
  306. if ( folder.Files.Count > 0 )
  307. {
  308. this.Logger.LogHeader("Publishing Files in '{0}'", folder.Path);
  309. foreach ( CatalogFile file in folder.Files )
  310. {
  311. this.Logger.LogNewLine();
  312. this.Logger.LogMessage("Checking out file '{0}'", file.Name);
  313. try
  314. {
  315. this.Repository.Checkout(file, this.currentDeploymentSet.VersionTag);
  316. }
  317. catch ( Exception ex )
  318. {
  319. this.Logger.LogException(ex);
  320. this.Cancel();
  321. break;
  322. }
  323. this.Logger.LogMessage("Getting revision for '{0}'", file.Name);
  324. try
  325. {
  326. this.Repository.SetRevision(file);
  327. }
  328. catch ( Exception ex )
  329. {
  330. this.Logger.LogException(ex);
  331. this.Cancel();
  332. break;
  333. }
  334. try
  335. {
  336. if ( this.FileIsPublished(folder, file) )
  337. {
  338. this.Logger.LogMessage("File '{0}', Revision '{1}' is already published", file.Name, file.Revision);
  339. this.LogItem(file, false);
  340. }
  341. else
  342. {
  343. this.Logger.LogMessage("Publishing '{0}'", file.Name);
  344. this.PublishFile(folder, file);
  345. this.PublishPolicies(file);
  346. this.Logger.LogMessage("Published File '{0}'", file.Name);
  347. this.LogItem(file, true);
  348. }
  349. }
  350. catch ( Exception ex )
  351. {
  352. this.Logger.LogException(ex);
  353. this.Cancel();
  354. break;
  355. }
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }
  362. private void PublishPolicies(DeploymentItem item)
  363. {
  364. if ( !error )
  365. {
  366. if ( item.Policies.Count > 0 )
  367. {
  368. this.Logger.LogMessage("Publishing Policies for '{0}'", item.Name);
  369. this.ReportServer.PublishPolicies(item);
  370. this.Logger.LogMessage("Policies set for '{0}'", item.Name);
  371. }
  372. }
  373. }
  374. private bool FileIsPublished(Folder folder, CatalogFile file)
  375. {
  376. Report report = file as Report;
  377. if ( report != null )
  378. {
  379. string lastRevisionPublished = this.ReportServer.GetReportRevision(report);
  380. return lastRevisionPublished == report.Revision;
  381. }
  382. else
  383. {
  384. return this.ReportServer.ItemExists(file);
  385. }
  386. }
  387. private void UpdateReportParameters()
  388. {
  389. if ( !error )
  390. {
  391. this.Logger.LogNewLine();
  392. this.Logger.LogMessage("Updating report parameters on server");
  393. foreach ( Folder folder in this.currentDeploymentSet.Folders.GetFlatList() )
  394. {
  395. foreach ( CatalogFile file in folder.Files )
  396. {
  397. Report report = file as Report;
  398. if ( report != null )
  399. {
  400. if ( this.publishedReportLog.Contains(report) ) //this.reportLog.ContainsKey(report) && this.reportLog[report] == true )
  401. {
  402. try
  403. {
  404. this.ReportServer.UpdateReportParameters(report);
  405. }
  406. catch ( Exception ex )
  407. {
  408. this.Logger.LogException(ex);
  409. }
  410. this.Logger.LogMessage("Updated parameters for '{0}'", report.Name);
  411. }
  412. }
  413. }
  414. }
  415. }
  416. }
  417. private void PublishFile(Folder folder, CatalogFile file)
  418. {
  419. if ( file.GetType().Equals(typeof(Report)) )
  420. {
  421. this.ReportServer.PublishReport((Report)file);
  422. }
  423. else if ( file.GetType().Equals(typeof(Resource)) )
  424. {
  425. this.ReportServer.PublishResource((Resource) file);
  426. }
  427. }
  428. private void LogItem(DeploymentItem item, bool wasPublished)
  429. {
  430. if ( item.GetType().Equals(typeof(Report)) )
  431. {
  432. if ( wasPublished )
  433. {
  434. this.publishedReportLog.Add((Report) item);
  435. }
  436. else
  437. {
  438. this.skippedReportLog.Add((Report) item);
  439. }
  440. }
  441. }
  442. #endregion
  443. }
  444. }