PageRenderTime 132ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Tum.CollabXT.TFS/Processors/WSS.cs

#
C# | 260 lines | 183 code | 41 blank | 36 comment | 20 complexity | 42ab17462dca1094c10aaa39f9a1b72c MD5 | raw file
  1. #region Copyright ©2008-2011, Technische Universitaet Muenchen
  2. // ====================================================================================================
  3. //
  4. // Last Changed by $Author: then $
  5. // Last Changed Date $LastChangedDate: 2009-06-09 09:28:29 +0200 (Tue, 09 Jun 2009) $
  6. // Last Changed Revision $Rev: 185 $
  7. //
  8. // ====================================================================================================
  9. #endregion
  10. using System.IO;
  11. using System.Xml;
  12. using System.Collections.Generic;
  13. using Tum.CollabXT.TFS.Resources;
  14. using System;
  15. namespace Tum.CollabXT.TFS
  16. {
  17. class WSS : IProcessor
  18. {
  19. string LibraryOutputFolder;
  20. XmlNode DocumentLibrariesNode;
  21. XmlNode FoldersNode;
  22. XmlNode FilesNode;
  23. public string TaskFileFolder
  24. {
  25. get;
  26. set;
  27. }
  28. public string TaskFileName
  29. {
  30. get;
  31. set;
  32. }
  33. public bool Process(TemplateProcessor processor)
  34. {
  35. processor.ToolProvider.ProcessLog.AddEntry(Language.Processing_WSS);
  36. string wssTemplateFileName = processor.ToolProvider.OutputPath + "/" + TaskFileFolder + "/" + TaskFileName;
  37. #region Load template
  38. XmlDocument wssDoc = new XmlDocument();
  39. processor.ToolProvider.ProcessLog.AddEntry(Language.WSS_Loading);
  40. try
  41. {
  42. wssDoc.Load(wssTemplateFileName);
  43. }
  44. catch
  45. {
  46. processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_LoadingFailed, wssTemplateFileName), LogEntryType.Error);
  47. return false;
  48. }
  49. #endregion
  50. //Compose folder path
  51. LibraryOutputFolder = processor.ToolProvider.OutputPath + "/" + TaskFileFolder;
  52. #region Prepare needed template nodes
  53. const string portalNodePath = "/tasks/task[@plugin='Microsoft.ProjectCreationWizard.Portal']/taskXml/Portal";
  54. //Select needed nodes from template
  55. processor.ToolProvider.ProcessLog.AddEntry(Language.WSS_SelectingLibrariesNode);
  56. DocumentLibrariesNode = wssDoc.SelectSingleNode(portalNodePath + "/documentLibraries");
  57. if (DocumentLibrariesNode == null)
  58. {
  59. processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_NotFound_InvalidTemplate, wssTemplateFileName + ": " + portalNodePath + "/documentLibraries"));
  60. return false;
  61. }
  62. processor.ToolProvider.ProcessLog.AddEntry(Language.WSS_SelectingFoldersNode);
  63. FoldersNode = wssDoc.SelectSingleNode(portalNodePath + "/folders");
  64. if (FoldersNode == null)
  65. {
  66. processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_NotFound_InvalidTemplate, wssTemplateFileName + ": " + portalNodePath + "/folders"), LogEntryType.Error);
  67. return false;
  68. }
  69. processor.ToolProvider.ProcessLog.AddEntry(Language.WSS_SelectingFilesNode);
  70. FilesNode = wssDoc.SelectSingleNode(portalNodePath + "/files");
  71. if (FilesNode == null)
  72. {
  73. processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_NotFound_InvalidTemplate, wssTemplateFileName + ": " + portalNodePath + "/files"), LogEntryType.Error);
  74. return false;
  75. }
  76. #endregion
  77. #region Write document libraries and folders
  78. List<string> writtenLibraries = new List<string>();
  79. //Get process guidance name from template configuration
  80. var processGuidanceNode = processor.TemplateConfigDocument.SelectSingleNode("//Tasks.Portal/ProcessGuidanceName");
  81. if (processGuidanceNode == null || processGuidanceNode.InnerText.Trim().Length == 0)
  82. {
  83. processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_NotFound_InvalidTemplate, "Template config: //Tasks.Portal/ProcessGuidanceName"), LogEntryType.Error);
  84. return false;
  85. }
  86. string processGuidanceName = processGuidanceNode.InnerText;
  87. //Write process guidance
  88. if (!string.IsNullOrEmpty(processor.ToolProvider.ProcessGuidancePath) && Directory.Exists(processor.ToolProvider.ProcessGuidancePath))
  89. {
  90. //Write default PG page document if necessary
  91. string pgFolder = LibraryOutputFolder + "/" + processGuidanceName;
  92. string defPgFilePath = pgFolder + "/ProcessGuidance.html";
  93. if (!string.IsNullOrEmpty(processor.ToolProvider.ProcessGuidanceDefaultPage) && !File.Exists(defPgFilePath))
  94. {
  95. Directory.CreateDirectory(pgFolder);
  96. using (StreamWriter defPgFileWriter = new StreamWriter(defPgFilePath))
  97. {
  98. defPgFileWriter.WriteLine("<meta http-equiv=\"refresh\" content=\"0;URL=" + processor.ToolProvider.ProcessGuidanceDefaultPage + "\" >");
  99. defPgFileWriter.Close();
  100. }
  101. }
  102. //Write PG document library
  103. WriteDocumentLibrary(processGuidanceName, processor.ToolProvider.ProcessGuidancePath, processor.ToolProvider.OutputPath, true);
  104. writtenLibraries.Add(processGuidanceName);
  105. }
  106. //Write additional libraries
  107. foreach (KeyValuePair<string, string> curLibrary in processor.ToolProvider.DocumentLibraries)
  108. {
  109. //Do not write two libraries with same name
  110. foreach (var existingLibraryName in writtenLibraries)
  111. {
  112. if (existingLibraryName.Equals(curLibrary.Key, StringComparison.InvariantCultureIgnoreCase))
  113. {
  114. processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_DuplicateLibraryName, existingLibraryName), LogEntryType.Error);
  115. return false;
  116. }
  117. }
  118. //Write library
  119. WriteDocumentLibrary(curLibrary.Key, curLibrary.Value, processor.ToolProvider.OutputPath, false);
  120. }
  121. #endregion
  122. #region Save WSS file
  123. processor.ToolProvider.ProcessLog.AddEntry(Language.WSS_Saving);
  124. try
  125. {
  126. wssDoc.Save(wssTemplateFileName);
  127. }
  128. catch
  129. {
  130. processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_SavingFailed, wssTemplateFileName), LogEntryType.Error);
  131. return false;
  132. }
  133. #endregion
  134. return true;
  135. }
  136. /// <summary>
  137. /// Creates a new document library in the template
  138. /// </summary>
  139. private void WriteDocumentLibrary(string libraryName, string sourceFolder, string outputFolder, bool isProcessGuidance)
  140. {
  141. string libraryFolder = LibraryOutputFolder + "/" + libraryName;
  142. //Create library folder and copy files to it
  143. Directory.CreateDirectory(libraryFolder);
  144. Helper.CopyDirectory(sourceFolder, libraryFolder);
  145. //Normalize folder path
  146. libraryFolder = new DirectoryInfo(libraryFolder).FullName;
  147. #region Add document library node
  148. XmlNode docLibNode = DocumentLibrariesNode.OwnerDocument.CreateElement("documentLibrary");
  149. //Name
  150. XmlAttribute nameAttribute = DocumentLibrariesNode.OwnerDocument.CreateAttribute("name");
  151. nameAttribute.Value = libraryName;
  152. docLibNode.Attributes.Append(nameAttribute);
  153. //Description
  154. XmlAttribute descriptionAttribute = DocumentLibrariesNode.OwnerDocument.CreateAttribute("description");
  155. descriptionAttribute.Value = libraryName;
  156. docLibNode.Attributes.Append(descriptionAttribute);
  157. //Process Guidance
  158. if (isProcessGuidance)
  159. {
  160. XmlAttribute pgAttribute = DocumentLibrariesNode.OwnerDocument.CreateAttribute("isProcessGuidance");
  161. pgAttribute.Value = "true";
  162. docLibNode.Attributes.Append(pgAttribute);
  163. }
  164. //Add node and save library name
  165. DocumentLibrariesNode.AppendChild(docLibNode);
  166. #endregion
  167. WriteChildFolders(libraryName, libraryFolder, libraryFolder, outputFolder);
  168. }
  169. /// <summary>
  170. /// Write folders section for a specific folder of a document library and its subfolders.
  171. /// </summary>
  172. /// <param name="curFolderPath">Folder for which the current node is to be written.</param>
  173. private void WriteChildFolders(string libraryName, string libraryFolderPath, string curFolderPath, string outputFolder)
  174. {
  175. //Write folder
  176. foreach (string curChildFolder in Directory.GetDirectories(curFolderPath))
  177. {
  178. //Create path format required for process template
  179. string normalizedFolderPath = curChildFolder.Substring(libraryFolderPath.Length + 1).Replace('\\', '/');
  180. //Check if folder node already exists
  181. if (FoldersNode.SelectNodes("folder[@documentLibrary='" + libraryName + "'][@name='" + normalizedFolderPath + "']").Count == 0)
  182. {
  183. //Does not exist yet, write it
  184. XmlNode newFolderNode = FoldersNode.OwnerDocument.CreateElement("folder");
  185. XmlAttribute docLibraryAttribute = FoldersNode.OwnerDocument.CreateAttribute("documentLibrary");
  186. docLibraryAttribute.Value = libraryName;
  187. newFolderNode.Attributes.Append(docLibraryAttribute);
  188. XmlAttribute nameAttribute = FoldersNode.OwnerDocument.CreateAttribute("name");
  189. nameAttribute.Value = normalizedFolderPath;
  190. newFolderNode.Attributes.Append(nameAttribute);
  191. FoldersNode.AppendChild(newFolderNode);
  192. }
  193. //Continue recursively
  194. WriteChildFolders(libraryName, libraryFolderPath, curChildFolder, outputFolder);
  195. }
  196. //Write files
  197. string normalizedOutputFolderPath = new DirectoryInfo(outputFolder).FullName;
  198. foreach (string curFilePath in Directory.GetFiles(curFolderPath))
  199. {
  200. string localFilePath = curFilePath.Substring(libraryFolderPath.Length + 1).Replace('\\', '/');
  201. //Check if file node already exists
  202. if (FilesNode.SelectNodes("file[@documentLibrary='" + libraryName + "'][@target='" + localFilePath + "']").Count == 0)
  203. {
  204. XmlNode newFileNode = FilesNode.OwnerDocument.CreateNode("element", "file", string.Empty);
  205. XmlAttribute targetAttribute = FilesNode.OwnerDocument.CreateAttribute("target");
  206. targetAttribute.Value = localFilePath;
  207. newFileNode.Attributes.Append(targetAttribute);
  208. XmlAttribute docLibraryAttribute = FilesNode.OwnerDocument.CreateAttribute("documentLibrary");
  209. docLibraryAttribute.Value = libraryName;
  210. newFileNode.Attributes.Append(docLibraryAttribute);
  211. XmlAttribute sourceAttribute = FilesNode.OwnerDocument.CreateAttribute("source");
  212. sourceAttribute.Value = curFilePath.Substring(normalizedOutputFolderPath.Length).Replace('\\', '/');
  213. newFileNode.Attributes.Append(sourceAttribute);
  214. FilesNode.AppendChild(newFileNode);
  215. }
  216. }
  217. }
  218. }
  219. }