PageRenderTime 462ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Orchard.Web/Modules/Orchard.Projections/Drivers/QueryPartDriver.cs

http://vietnamicare.codeplex.com
C# | 201 lines | 178 code | 20 blank | 3 comment | 6 complexity | 392d278ef568f2b22e9b67ac3e952b26 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, CPL-1.0, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. using System;
  2. using System.Linq;
  3. using System.Xml.Linq;
  4. using Orchard.ContentManagement;
  5. using Orchard.ContentManagement.Drivers;
  6. using Orchard.ContentManagement.Handlers;
  7. using Orchard.Projections.Models;
  8. namespace Orchard.Projections.Drivers {
  9. public class QueryPartDriver : ContentPartDriver<QueryPart> {
  10. protected override DriverResult Editor(QueryPart part, IUpdateModel updater, dynamic shapeHelper) {
  11. if(updater == null) {
  12. return null;
  13. }
  14. return null;
  15. }
  16. protected override void Exporting(QueryPart part, ExportContentContext context) {
  17. var element = context.Element(part.PartDefinition.Name);
  18. element.Add(
  19. new XElement("FilterGroups",
  20. part.FilterGroups.Select( filterGroup =>
  21. new XElement("FilterGroup",
  22. filterGroup.Filters.Select( filter =>
  23. new XElement("Filter",
  24. new XAttribute("Category", filter.Category ?? ""),
  25. new XAttribute("Description", filter.Description ?? ""),
  26. new XAttribute("Position", filter.Position),
  27. new XAttribute("State", filter.State ?? ""),
  28. new XAttribute("Type", filter.Type ?? "")
  29. )
  30. )
  31. )
  32. )
  33. ),
  34. new XElement("SortCriteria",
  35. part.SortCriteria.Select( sortCriterion =>
  36. new XElement("SortCriterion",
  37. new XAttribute("Category", sortCriterion.Category ?? ""),
  38. new XAttribute("Description", sortCriterion.Description ?? ""),
  39. new XAttribute("Position", sortCriterion.Position),
  40. new XAttribute("State", sortCriterion.State ?? ""),
  41. new XAttribute("Type", sortCriterion.Type ?? "")
  42. )
  43. )
  44. ),
  45. new XElement("Layouts",
  46. part.Layouts.Select( layout =>
  47. new XElement("Layout",
  48. // Attributes
  49. new XAttribute("Category", layout.Category ?? ""),
  50. new XAttribute("Description", layout.Description ?? ""),
  51. new XAttribute("State", layout.State),
  52. new XAttribute("Display", layout.Display),
  53. new XAttribute("DisplayType", layout.DisplayType ?? ""),
  54. new XAttribute("Type", layout.Type ?? ""),
  55. // Properties
  56. new XElement("Properties", layout.Properties.Select(GetPropertyXml)),
  57. // Group
  58. new XElement("Group", GetPropertyXml(layout.GroupProperty))
  59. )
  60. )
  61. )
  62. );
  63. }
  64. protected override void Importing(QueryPart part, ImportContentContext context) {
  65. var queryElement = context.Data.Element(part.PartDefinition.Name);
  66. part.Record.FilterGroups.Clear();
  67. foreach(var item in queryElement.Element("FilterGroups").Elements("FilterGroup").Select(filterGroup =>
  68. new FilterGroupRecord {
  69. Filters = filterGroup.Elements("Filter").Select( filter =>
  70. new FilterRecord {
  71. Category = filter.Attribute("Category").Value,
  72. Description = filter.Attribute("Description").Value,
  73. Position = Convert.ToInt32(filter.Attribute("Position").Value),
  74. State = filter.Attribute("State").Value,
  75. Type = filter.Attribute("Type").Value
  76. }).ToList()
  77. })) {
  78. part.Record.FilterGroups.Add(item);
  79. }
  80. part.Record.SortCriteria.Clear();
  81. foreach(var item in queryElement.Element("SortCriteria").Elements("SortCriterion").Select(sortCriterion =>
  82. new SortCriterionRecord {
  83. Category = sortCriterion.Attribute("Category").Value,
  84. Description = sortCriterion.Attribute("Description").Value,
  85. Position = Convert.ToInt32(sortCriterion.Attribute("Position").Value),
  86. State = sortCriterion.Attribute("State").Value,
  87. Type = sortCriterion.Attribute("Type").Value
  88. })) {
  89. part.Record.SortCriteria.Add(item);
  90. }
  91. part.Record.Layouts.Clear();
  92. foreach(var item in queryElement.Element("Layouts").Elements("Layout").Select(layout =>
  93. new LayoutRecord {
  94. Category = layout.Attribute("Category").Value,
  95. Description = layout.Attribute("Description").Value,
  96. Display = int.Parse(layout.Attribute("Display").Value),
  97. DisplayType = layout.Attribute("DisplayType").Value,
  98. State = layout.Attribute("State").Value,
  99. Type = layout.Attribute("Type").Value,
  100. Properties = layout.Element("Properties").Elements("Property").Select(GetProperty).ToList(),
  101. GroupProperty = GetProperty(layout.Element("Group").Element("Property"))
  102. })) {
  103. part.Record.Layouts.Add(item);
  104. }
  105. }
  106. private XElement GetPropertyXml(PropertyRecord property) {
  107. if(property == null) {
  108. return null;
  109. }
  110. return new XElement("Property",
  111. new XAttribute("Category", property.Category ?? ""),
  112. new XAttribute("Description", property.Description ?? ""),
  113. new XAttribute("Position", property.Position),
  114. new XAttribute("State", property.State ?? ""),
  115. new XAttribute("Type", property.Type ?? ""),
  116. new XAttribute("AddEllipsis", property.AddEllipsis),
  117. new XAttribute("CreateLabel", property.CreateLabel),
  118. new XAttribute("CustomLabelCss", property.CustomLabelCss ?? ""),
  119. new XAttribute("CustomLabelTag", property.CustomLabelTag ?? ""),
  120. new XAttribute("CustomPropertyCss", property.CustomPropertyCss ?? ""),
  121. new XAttribute("CustomPropertyTag", property.CustomPropertyTag ?? ""),
  122. new XAttribute("CustomWrapperCss", property.CustomWrapperCss ?? ""),
  123. new XAttribute("CustomWrapperTag", property.CustomWrapperTag ?? ""),
  124. new XAttribute("CustomizeLabelHtml", property.CustomizeLabelHtml),
  125. new XAttribute("CustomizePropertyHtml", property.CustomizePropertyHtml),
  126. new XAttribute("CustomizeWrapperHtml", property.CustomizeWrapperHtml),
  127. new XAttribute("ExcludeFromDisplay", property.ExcludeFromDisplay),
  128. new XAttribute("HideEmpty", property.HideEmpty),
  129. new XAttribute("Label", property.Label ?? ""),
  130. new XAttribute("LinkToContent", property.LinkToContent),
  131. new XAttribute("MaxLength", property.MaxLength),
  132. new XAttribute("NoResultText", property.NoResultText ?? ""),
  133. new XAttribute("PreserveLines", property.PreserveLines),
  134. new XAttribute("RewriteOutput", property.RewriteOutput),
  135. new XAttribute("RewriteText", property.RewriteText ?? ""),
  136. new XAttribute("StripHtmlTags", property.StripHtmlTags),
  137. new XAttribute("TrimLength", property.TrimLength),
  138. new XAttribute("TrimOnWordBoundary", property.TrimOnWordBoundary),
  139. new XAttribute("TrimWhiteSpace", property.TrimWhiteSpace),
  140. new XAttribute("ZeroIsEmpty", property.ZeroIsEmpty)
  141. );
  142. }
  143. private PropertyRecord GetProperty(XElement property) {
  144. if(property == null) {
  145. return null;
  146. }
  147. return new PropertyRecord {
  148. AddEllipsis = Convert.ToBoolean(property.Attribute("AddEllipsis").Value),
  149. Category = property.Attribute("Category").Value,
  150. CreateLabel = Convert.ToBoolean(property.Attribute("CreateLabel").Value),
  151. CustomLabelCss = property.Attribute("CustomLabelCss").Value,
  152. Description = property.Attribute("Description").Value,
  153. Type = property.Attribute("Type").Value,
  154. CustomLabelTag = property.Attribute("CustomLabelTag").Value,
  155. CustomPropertyCss = property.Attribute("CustomPropertyCss").Value,
  156. CustomPropertyTag = property.Attribute("CustomPropertyTag").Value,
  157. CustomWrapperCss = property.Attribute("CustomWrapperCss").Value,
  158. CustomWrapperTag = property.Attribute("CustomWrapperTag").Value,
  159. CustomizeLabelHtml = Convert.ToBoolean(property.Attribute("CustomizeLabelHtml").Value),
  160. CustomizePropertyHtml = Convert.ToBoolean(property.Attribute("CustomizePropertyHtml").Value),
  161. CustomizeWrapperHtml = Convert.ToBoolean(property.Attribute("CustomizeWrapperHtml").Value),
  162. ExcludeFromDisplay = Convert.ToBoolean(property.Attribute("ExcludeFromDisplay").Value),
  163. HideEmpty = Convert.ToBoolean(property.Attribute("HideEmpty").Value),
  164. Label = property.Attribute("Label").Value,
  165. LinkToContent = Convert.ToBoolean(property.Attribute("LinkToContent").Value),
  166. MaxLength = Convert.ToInt32(property.Attribute("MaxLength").Value),
  167. NoResultText = property.Attribute("NoResultText").Value,
  168. Position = Convert.ToInt32(property.Attribute("Position").Value),
  169. PreserveLines = Convert.ToBoolean(property.Attribute("PreserveLines").Value),
  170. RewriteOutput = Convert.ToBoolean(property.Attribute("RewriteOutput").Value),
  171. RewriteText = property.Attribute("RewriteText").Value,
  172. State = property.Attribute("State").Value,
  173. StripHtmlTags = Convert.ToBoolean(property.Attribute("StripHtmlTags").Value),
  174. TrimLength = Convert.ToBoolean(property.Attribute("TrimLength").Value),
  175. TrimOnWordBoundary = Convert.ToBoolean(property.Attribute("TrimOnWordBoundary").Value),
  176. TrimWhiteSpace = Convert.ToBoolean(property.Attribute("TrimWhiteSpace").Value),
  177. ZeroIsEmpty = Convert.ToBoolean(property.Attribute("ZeroIsEmpty").Value),
  178. };
  179. }
  180. }
  181. }