PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/Src/uSiteBuilder.Admin/ExportCode.ascx.cs

https://bitbucket.org/StephenWRogers/kelvindigital.usitebuilder.package
C# | 381 lines | 324 code | 55 blank | 2 comment | 26 complexity | 9f94fb000e76899a7f09b673bf996eea MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.UI.WebControls;
  8. using ICSharpCode.SharpZipLib.Core;
  9. using ICSharpCode.SharpZipLib.Zip;
  10. using Vega.USiteBuilder;
  11. using umbraco.cms.businesslogic.datatype;
  12. using umbraco.cms.businesslogic.template;
  13. using umbraco.cms.businesslogic.web;
  14. namespace kelvinDigital.USiteBuilderAdmin
  15. {
  16. public partial class usercontrols_ExportCode : System.Web.UI.UserControl
  17. {
  18. private const string code = @"using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Web;
  22. using Vega.USiteBuilder;
  23. {7}
  24. {{
  25. [DocumentType(Name=""{0}"", IconUrl=""{1}"",
  26. Alias=""{8}"",
  27. AllowedTemplates= new string[] {{{2}}},
  28. AllowedChildNodeTypes = new Type[] {{{3}}})]
  29. public partial class {4} : {5}
  30. {{
  31. public {4} () {{}}
  32. public {4} (int nodeId) : base(nodeId){{}}
  33. {6}
  34. }}
  35. }}";
  36. private string property = @"
  37. [DocumentTypeProperty(UmbracoPropertyType.{5}, {6} Name = ""{0}"", Description = ""{1}"", Tab = ""{2}"", Mandatory = {3} )]
  38. public {7} {4} {{ get; set; }}";
  39. private const string dataTypeCode = @"using System;
  40. using System.Collections.Generic;
  41. using System.Linq;
  42. using System.Web;
  43. using umbraco.cms.businesslogic.datatype;
  44. using Vega.USiteBuilder;
  45. {6}
  46. {{
  47. [DataType(Name = ""{0}"", UniqueId = ""{1}"", RenderControlGuid = ""{2}"", DatabaseDataType = {3})]
  48. public partial class {4} : DataTypeBase
  49. {{
  50. public override DataTypePrevalue[] Prevalues
  51. {{
  52. get
  53. {{
  54. return new DataTypePrevalue[]
  55. {{
  56. {5}
  57. }};
  58. }}
  59. }}
  60. }}
  61. }}";
  62. private const string templateCode = @"using System.Web.UI;
  63. using Vega.USiteBuilder;
  64. {0}
  65. {{
  66. public partial class {1} : TemplateBase{2}
  67. {{
  68. }}
  69. }}";
  70. protected override void OnLoad(EventArgs e)
  71. {
  72. base.OnLoad(e);
  73. txtDataTypesNamespace.Enabled = cbxExportDataTypes.Checked;
  74. txtDocTypesNamespace.Enabled = cbxExportDocTypes.Checked;
  75. txtTemplatesNamespace.Enabled = cbxExportTemplates.Checked;
  76. }
  77. protected void preview_Click(object sender, EventArgs e)
  78. {
  79. StringBuilder sb = new StringBuilder();
  80. if (cbxExportTemplates.Checked)
  81. {
  82. Template.GetAllAsList().ForEach(template => sb.AppendLine(ExportTemplate(template)));
  83. }
  84. if (cbxExportDataTypes.Checked)
  85. {
  86. DataTypeDefinition.GetAll().ToList().ForEach(dataType => sb.AppendLine(ExportDataType(dataType)));
  87. }
  88. if (cbxExportDocTypes.Checked)
  89. {
  90. DocumentType.GetAllAsList().ForEach(docType => sb.AppendLine(ExportDocType(docType.Id)));
  91. }
  92. litCode.Text = Server.HtmlEncode(sb.ToString());
  93. activateTab.Visible = true;
  94. }
  95. protected void save_Click(object sender, EventArgs e)
  96. {
  97. Response.ContentType = "application/octet-stream";
  98. Response.AppendHeader("content-disposition", "attachment; filename=\"Exported uSiteBuilder Classes.zip\"");
  99. Response.CacheControl = "Private";
  100. Response.Cache.SetExpires(DateTime.Now.AddMinutes(3));
  101. var zipStream = new ZipOutputStream(Response.OutputStream);
  102. zipStream.SetLevel(3);
  103. if (cbxExportDocTypes.Checked)
  104. {
  105. DocumentType.GetAllAsList().ForEach(docType =>
  106. {
  107. string docTypeCode = ExportDocType(docType.Id);
  108. var memoryStream = new MemoryStream();
  109. using (TextWriter tw = new StreamWriter(memoryStream))
  110. {
  111. tw.Write(docTypeCode);
  112. tw.Flush();
  113. ZipEntry newEntry =
  114. new ZipEntry("DocumentTypes/" + CleanName(docType.Alias) +
  115. ".cs");
  116. newEntry.DateTime = DateTime.Now;
  117. zipStream.PutNextEntry(newEntry);
  118. memoryStream.Position = 0;
  119. StreamUtils.Copy(memoryStream, zipStream, new byte[4096]);
  120. zipStream.CloseEntry();
  121. }
  122. });
  123. }
  124. if (cbxExportDataTypes.Checked)
  125. {
  126. DataTypeDefinition.GetAll().ToList().ForEach(dataType =>
  127. {
  128. string dataTypeCode = ExportDataType(dataType);
  129. var memoryStream = new MemoryStream();
  130. using (
  131. TextWriter tw = new StreamWriter(memoryStream))
  132. {
  133. tw.Write(dataTypeCode);
  134. tw.Flush();
  135. ZipEntry newEntry =
  136. new ZipEntry("DataTypes/" +
  137. CleanName(dataType.Text) +
  138. ".cs");
  139. newEntry.DateTime = DateTime.Now;
  140. zipStream.PutNextEntry(newEntry);
  141. memoryStream.Position = 0;
  142. StreamUtils.Copy(memoryStream, zipStream,
  143. new byte[4096]);
  144. zipStream.CloseEntry();
  145. }
  146. });
  147. }
  148. if (cbxExportTemplates.Checked)
  149. {
  150. Template.GetAllAsList().ForEach(template =>
  151. {
  152. string exportedTemplate = ExportTemplate(template);
  153. var memoryStream = new MemoryStream();
  154. using (TextWriter tw = new StreamWriter(memoryStream))
  155. {
  156. tw.Write(exportedTemplate);
  157. tw.Flush();
  158. ZipEntry newEntry =
  159. new ZipEntry("Templates/" + CleanName(template.Alias) +
  160. ".master.cs");
  161. newEntry.DateTime = DateTime.Now;
  162. zipStream.PutNextEntry(newEntry);
  163. memoryStream.Position = 0;
  164. StreamUtils.Copy(memoryStream, zipStream, new byte[4096]);
  165. zipStream.CloseEntry();
  166. }
  167. var memoryStream2 = new MemoryStream();
  168. using (TextWriter tw = new StreamWriter(memoryStream2))
  169. {
  170. tw.Write("<%@ Master Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"" + CleanName(template.Alias) + ".master.cs\" Inherits=\"" + txtTemplatesNamespace.Text +"." + CleanName(template.Alias) + "\" %>");
  171. string design = template.Design;
  172. int startPos = design.IndexOf(">");
  173. tw.Write(design.Substring(startPos + 1));
  174. tw.Flush();
  175. ZipEntry newEntry =
  176. new ZipEntry("Templates/" + CleanName(template.Alias) +
  177. ".master");
  178. newEntry.DateTime = DateTime.Now;
  179. zipStream.PutNextEntry(newEntry);
  180. memoryStream2.Position = 0;
  181. StreamUtils.Copy(memoryStream2, zipStream, new byte[4096]);
  182. zipStream.CloseEntry();
  183. }
  184. });
  185. }
  186. zipStream.IsStreamOwner = false;
  187. zipStream.Close();
  188. Response.End();
  189. }
  190. // these are private readonlys as const can't be Guids
  191. private readonly Guid DATATYPE_YESNO_GUID = new Guid("38b352c1-e9f8-4fd8-9324-9a2eab06d97a");
  192. private readonly Guid DATATYPE_TINYMCE_GUID = new Guid("5e9b75ae-face-41c8-b47e-5f4b0fd82f83");
  193. private readonly Guid DATATYPE_DATETIMEPICKER_GUID = new Guid("b6fb1622-afa5-4bbf-a3cc-d9672a442222");
  194. private readonly Guid DATATYPE_DATEPICKER_GUID = new Guid("23e93522-3200-44e2-9f29-e61a6fcbb79a");
  195. private string GetPropertyType (Guid datatype)
  196. {
  197. if(datatype == DATATYPE_YESNO_GUID)
  198. {
  199. return "bool";
  200. }
  201. if(datatype == DATATYPE_TINYMCE_GUID)
  202. {
  203. return "HtmlString";
  204. }
  205. if(datatype == DATATYPE_DATETIMEPICKER_GUID)
  206. {
  207. return "DateTime";
  208. }
  209. if(datatype == DATATYPE_DATEPICKER_GUID)
  210. {
  211. return "DateTime";
  212. }
  213. return "string";
  214. }
  215. private string CleanName(string name)
  216. {
  217. //Compliant with item 2.4.2 of the C# specification
  218. System.Text.RegularExpressions.Regex regex =
  219. new System.Text.RegularExpressions.Regex(
  220. @"[^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Nl}\p{Mn}\p{Mc}\p{Cf}\p{Pc}\p{Lm}]");
  221. string ret = regex.Replace(name, ""); //The identifier must start with a character
  222. ret = ret.Substring(0, 1).ToUpper() + ret.Substring(1, ret.Length - 1);
  223. if (!char.IsLetter(ret, 0))
  224. {
  225. ret = string.Concat("_", ret);
  226. }
  227. return ret;
  228. }
  229. private string ExportTemplate(Template template)
  230. {
  231. string docTypeClass = "";
  232. List<DocumentType> documentTypes = DocumentType.GetAllAsList();
  233. if (documentTypes.Count(docType => docType.allowedTemplates.Any(tmpl => tmpl.Alias == template.Alias)) == 1)
  234. {
  235. var defaultDocumentType =
  236. documentTypes.Single(docType => docType.allowedTemplates.Any(tmpl => tmpl.Alias == template.Alias));
  237. docTypeClass = string.IsNullOrEmpty(txtDocTypesNamespace.Text)
  238. ? string.Format("<{0}>", CleanName(defaultDocumentType.Alias))
  239. : string.Format("<{0}.{1}>", txtDocTypesNamespace.Text, CleanName(defaultDocumentType.Alias));
  240. }
  241. return string.Format(templateCode,
  242. !string.IsNullOrEmpty(txtTemplatesNamespace.Text)
  243. ? "namespace " + txtTemplatesNamespace.Text
  244. : "",
  245. CleanName(template.Alias.Replace(" ", "")),
  246. docTypeClass);
  247. }
  248. private string ExportDataType(DataTypeDefinition dataType)
  249. {
  250. if (dataType.DataType == null)
  251. {
  252. return "";
  253. }
  254. var defaultData = dataType.DataType.Data as DefaultData;
  255. string dbType = "DBTypes.Nvarchar";
  256. if (defaultData != null)
  257. {
  258. dbType = "DBTypes." + defaultData.DatabaseType;
  259. }
  260. string preValues = "";
  261. var settingsStorage = new DataEditorSettingsStorage();
  262. var existingSettings = settingsStorage.GetSettings(dataType.Id);
  263. if (existingSettings.Any())
  264. {
  265. preValues = string.Join("," + Environment.NewLine,
  266. existingSettings.Select(
  267. setting =>
  268. string.Format("new DataTypePrevalue(\"{0}\", \"{1}\")", setting.Key,
  269. setting.Value.Replace("\"", "\\\""))));
  270. }
  271. return string.Format(dataTypeCode, dataType.Text.Replace("\"", "\\\""), dataType.UniqueId.ToString(),
  272. dataType.DataType.Id.ToString(), dbType, CleanName(dataType.Text), preValues,
  273. !string.IsNullOrEmpty(txtDataTypesNamespace.Text)
  274. ? "namespace " + txtDataTypesNamespace.Text
  275. : "");
  276. }
  277. private string ExportDocType(int id)
  278. {
  279. DocumentType docType = new DocumentType(id);
  280. string templates = String.Join(", ",
  281. docType.allowedTemplates.Select(template => "\"" + template.Alias + "\""));
  282. string childNodes = String.Join(", ",
  283. docType.AllowedChildContentTypeIDs.Select(
  284. contentTypeId => "typeof(" + CleanName(new DocumentType(contentTypeId).Alias) + ")"));
  285. string properties = String.Join(string.Empty,
  286. docType.getVirtualTabs.SelectMany(
  287. tab =>
  288. tab.GetPropertyTypes(id, false).Select(
  289. prop =>
  290. String.Format(property, prop.Name.Replace("\"", "\\\""), prop.Description.Replace(Environment.NewLine, "").Replace("\"", "\\\""),
  291. (prop.TabId == 0
  292. ? "Properties"
  293. : umbraco.cms.businesslogic.ContentType.Tab.
  294. GetTab(prop.TabId).Caption),
  295. prop.Mandatory.ToString().ToLower(), CleanName(prop.Alias),
  296. Enum.IsDefined(typeof(UmbracoPropertyType),
  297. prop.DataTypeDefinition.Id)
  298. ? ((UmbracoPropertyType)
  299. prop.DataTypeDefinition.Id).ToString()
  300. : "Other",
  301. Enum.IsDefined(typeof(UmbracoPropertyType),
  302. prop.DataTypeDefinition.Id)
  303. ? ""
  304. : "OtherTypeName=\"" +
  305. prop.DataTypeDefinition.Text.Replace("\"", "\\\"") + "\", ",
  306. GetPropertyType(umbraco.cms.businesslogic.ContentType.GetDataType(docType.Alias, prop.Alias))))));
  307. return string.Format(code,
  308. docType.Text.Replace("\"", "\\\""),
  309. docType.IconUrl,
  310. templates,
  311. childNodes,
  312. CleanName(docType.Alias),
  313. (docType.MasterContentType == 0
  314. ? "DocumentTypeBase"
  315. : CleanName(new DocumentType(docType.MasterContentType).Alias)),
  316. properties,
  317. !string.IsNullOrEmpty(txtDocTypesNamespace.Text)
  318. ? "namespace " + txtDocTypesNamespace.Text
  319. : "",
  320. docType.Alias.Replace("\"", "\\\""));
  321. }
  322. }
  323. }